static void LoadHTR(char *fileName, triangle_t **triList, int *triangleCount) { int i, j; int vertexCount; int vertexNum; struct vList_s { float v[3]; } *vList; int triCount; float origin[3]; triangle_t *tList; float x, y, z; float x2, y2, z2; float rx, ry, rz; TK_Init(); TK_OpenSource(fileName); TK_Beyond(TK_C_HEXEN); TK_Beyond(TK_C_TRIANGLES); TK_BeyondRequire(TK_C_VERSION, TK_INTNUMBER); if (tk_IntNumber != 1) { COM_Error("Unsupported version (%d) in file %s\n", tk_IntNumber, InputFileName); } // Get vertex count TK_BeyondRequire(TK_VERTICES, TK_INTNUMBER); vertexCount = tk_IntNumber; vList = (struct vList_s *) SafeMalloc(vertexCount * sizeof(vList[0])); // Get triangle count TK_BeyondRequire(TK_FACES, TK_INTNUMBER); triCount = tk_IntNumber; if (triCount >= MAXTRIANGLES) { COM_Error("Too many triangles in file %s\n", InputFileName); } *triangleCount = triCount; tList = (triangle_t *) SafeMalloc(MAXTRIANGLES * sizeof(triangle_t)); *triList = tList; // Get origin TK_Beyond(TK_ORIGIN); TK_Require(TK_FLOATNUMBER); origin[0] = tk_FloatNumber; TK_FetchRequire(TK_FLOATNUMBER); origin[1] = tk_FloatNumber; TK_FetchRequire(TK_FLOATNUMBER); origin[2] = tk_FloatNumber; //rx = 90.0/360.0*2.0*MY_PI; rx =(float)(FixHTRRotateX/360.0*2.0*MY_PI); ry =(float)(FixHTRRotateY/360.0*2.0*MY_PI); rz =(float)(FixHTRRotateZ/360.0*2.0*MY_PI); // Get vertex list for (i = 0; i < vertexCount; i++) { TK_FetchRequire(TK_VERTEX); TK_FetchRequire(TK_FLOATNUMBER); x = tk_FloatNumber-origin[0]; TK_FetchRequire(TK_FLOATNUMBER); y = tk_FloatNumber-origin[1]; TK_FetchRequire(TK_FLOATNUMBER); z = tk_FloatNumber-origin[2]; x += FixHTRTranslateX; y += FixHTRTranslateY; z += FixHTRTranslateZ; y2 = (float)(y*cos(rx)-z*sin(rx)); z2 = (float)(y*sin(rx)+z*cos(rx)); y = y2; z = z2; x2 = (float)(x*cos(ry)+z*sin(ry)); z2 = (float)(-x*sin(ry)+z*cos(ry)); x = x2; z = z2; x2 = (float)(x*cos(rz)-y*sin(rz)); y2 = (float)(x*sin(rz)+y*cos(rz)); x = x2; y = y2; vList[i].v[0] = x; vList[i].v[1] = y; vList[i].v[2] = z; } // Get face list for (i = 0; i < triCount; i++) { TK_FetchRequire(TK_FACE); TK_FetchRequire(TK_LPAREN); for (j = 0; j < 3; j++) { TK_FetchRequire(TK_INTNUMBER); vertexNum = tk_IntNumber-1; if (vertexNum >= vertexCount) { COM_Error("File '%s', line %d:\nVertex number" " >= vertexCount: %d\n", tk_SourceName, tk_Line, tk_IntNumber); } tList[i].verts[2-j][0] = vList[vertexNum].v[0]; tList[i].verts[2-j][1] = vList[vertexNum].v[1]; tList[i].verts[2-j][2] = vList[vertexNum].v[2]; } TK_FetchRequire(TK_RPAREN); /* printf("Face %i:\n v0: %f, %f, %f\n v1: %f, %f, %f\n" " v2: %f, %f, %f\n", i, tList[i].verts[0][0], tList[i].verts[0][1], tList[i].verts[0][2], tList[i].verts[1][0], tList[i].verts[1][1], tList[i].verts[1][2], tList[i].verts[2][0], tList[i].verts[2][1], tList[i].verts[2][2]); */ } }
void HandleHRCModel( triangle_t **triList, int *triangleCount, mesh_node_t **nodesList, int *num_mesh_nodes, int ActiveNode, int Depth, int numVerts ){ void ReadHRCClusterList( mesh_node_t *meshNode, int baseIndex ); int i, j; int vertexCount; int triCount; triangle_t *tList; mesh_node_t *meshNode; float x, y, z; float x2, y2, z2; float rx, ry, rz; tokenType_t nextToken; float orig_scaling[3]; float orig_rotation[3]; float orig_translation[3]; int start_tri; int pos,bit; int vertIndexBase; // Update Node Info if ( nodesList ) { TK_BeyondRequire( TK_NAME, TK_STRING ); if ( Depth == 0 || tk_String[0] == '_' ) { // Root ActiveNode = *num_mesh_nodes; ( *num_mesh_nodes )++; if ( ( *num_mesh_nodes ) > MAX_FM_MESH_NODES ) { Error( "Too many mesh nodes in file %s\n", hrc_name ); } meshNode = &( *nodesList )[ActiveNode]; // memset(meshNode, 0, sizeof(mesh_node_t)); strcpy( meshNode->name, tk_String ); memset( meshNode->tris, 0, sizeof( meshNode->tris ) ); memset( meshNode->verts, 0, sizeof( meshNode->verts ) ); meshNode->start_glcmds = 0; meshNode->num_glcmds = 0; vertIndexBase = 0; } else { // Childs under the children meshNode = &( *nodesList )[ActiveNode]; vertIndexBase = numVerts; } } else { meshNode = NULL; } // Get the scaling, rotation, and translation values TK_Beyond( TK_SCALING ); for ( i = 0; i < 3; i++ ) { orig_scaling[i] = scaling[i]; TK_Require( TK_FLOATNUMBER ); scaling[i] *= tk_FloatNumber; TK_Fetch(); } TK_Beyond( TK_ROTATION ); for ( i = 0; i < 3; i++ ) { orig_rotation[i] = rotation[i]; TK_Require( TK_FLOATNUMBER ); rotation[i] = tk_FloatNumber; TK_Fetch(); } TK_Beyond( TK_TRANSLATION ); for ( i = 0; i < 3; i++ ) { orig_translation[i] = translation[i]; TK_Require( TK_FLOATNUMBER ); translation[i] += tk_FloatNumber; TK_Fetch(); } rx = ( ( rotation[0] - 90.0 ) / 360.0 ) * 2.0 * M_PI; ry = ( rotation[2] / 360.0 ) * 2.0 * M_PI; rz = ( rotation[1] / 360.0 ) * 2.0 * M_PI; // rjr - might not work if there an item doesn't have a mesh nextToken = tk_Token; if ( nextToken == TK_ACTOR_DATA ) { while ( nextToken != TK_MODEL && nextToken != TK_RBRACE ) { nextToken = TK_Fetch(); } } while ( nextToken == TK_SPLINE ) { // spline node has two right braces nextToken = TK_Beyond( TK_RBRACE ); nextToken = TK_Beyond( TK_RBRACE ); } while ( nextToken == TK_MATERIAL ) { nextToken = TK_Beyond( TK_RBRACE ); } while ( nextToken == TK_MODEL ) { HandleHRCModel( triList,triangleCount,nodesList,num_mesh_nodes,ActiveNode, Depth + 1, 0 ); nextToken = TK_Fetch(); } if ( nextToken == TK_MESH ) { // Get all the tri and vertex info TK_BeyondRequire( TK_VERTICES, TK_INTNUMBER ); vertexCount = tk_IntNumber; for ( i = 0; i < vertexCount; i++ ) { TK_BeyondRequire( TK_LBRACKET, TK_INTNUMBER ); if ( tk_IntNumber != i ) { Error( "File '%s', line %d:\nVertex index mismatch.\n", tk_SourceName, tk_Line ); } TK_Beyond( TK_POSITION ); // Apply the scaling, rotation, and translation in the order // specified in the HRC file. This could be wrong. TK_Require( TK_FLOATNUMBER ); x = tk_FloatNumber * scaling[0]; TK_FetchRequire( TK_FLOATNUMBER ); y = tk_FloatNumber * scaling[1]; TK_FetchRequire( TK_FLOATNUMBER ); z = tk_FloatNumber * scaling[2]; y2 = y * cos( rx ) + z*sin( rx ); z2 = -y*sin( rx ) + z*cos( rx ); y = y2; z = z2; x2 = x * cos( ry ) - z*sin( ry ); z2 = x * sin( ry ) + z*cos( ry ); x = x2; z = z2; x2 = x * cos( rz ) + y*sin( rz ); y2 = -x*sin( rz ) + y*cos( rz ); x = x2; y = y2; vList[i].v[0] = x + translation[0]; vList[i].v[1] = y - translation[2]; vList[i].v[2] = z + translation[1]; } TK_BeyondRequire( TK_POLYGONS, TK_INTNUMBER ); triCount = tk_IntNumber; if ( triCount >= MAXTRIANGLES ) { Error( "Too many triangles in file %s\n", hrc_name ); } start_tri = *triangleCount; *triangleCount += triCount; tList = *triList; for ( i = 0; i < triCount; i++ ) { if ( meshNode ) { // Update the node pos = ( i + start_tri ) >> 3; bit = 1 << ( ( i + start_tri ) & 7 ); meshNode->tris[pos] |= bit; } TK_BeyondRequire( TK_LBRACKET, TK_INTNUMBER ); if ( tk_IntNumber != i ) { Error( "File '%s', line %d:\nTriangle index mismatch.\n", tk_SourceName, tk_Line ); } TK_BeyondRequire( TK_NODES, TK_INTNUMBER ); if ( tk_IntNumber != 3 ) { Error( "File '%s', line %d:\nBad polygon vertex count: %d.", tk_SourceName, tk_Line, tk_IntNumber ); } tList[i + start_tri].HasUV = true; for ( j = 0; j < 3; j++ ) { TK_BeyondRequire( TK_LBRACKET, TK_INTNUMBER ); if ( tk_IntNumber != j ) { Error( "File '%s', line %d:\nTriangle vertex index" " mismatch. %d should be %d\n", tk_SourceName, tk_Line, tk_IntNumber, j ); } TK_BeyondRequire( TK_VERTEX, TK_INTNUMBER ); tList[i + start_tri].verts[2 - j][0] = vList[tk_IntNumber].v[0]; tList[i + start_tri].verts[2 - j][1] = vList[tk_IntNumber].v[1]; tList[i + start_tri].verts[2 - j][2] = vList[tk_IntNumber].v[2]; #if 1 tList[i + start_tri].indicies[2 - j] = tk_IntNumber + vertIndexBase; #endif TK_BeyondRequire( TK_UVTEXTURE, TK_FLOATNUMBER ); tList[i + start_tri].uv[2 - j][0] = tk_FloatNumber; TK_Fetch(); TK_Require( TK_FLOATNUMBER ); tList[i + start_tri].uv[2 - j][1] = tk_FloatNumber; } /* printf("Face %i:\n v0: %f, %f, %f\n v1: %f, %f, %f\n" " v2: %f, %f, %f\n", i, tList[i].verts[0][0], tList[i].verts[0][1], tList[i].verts[0][2], tList[i].verts[1][0], tList[i].verts[1][1], tList[i].verts[1][2], tList[i].verts[2][0], tList[i].verts[2][1], tList[i].verts[2][2]); */ } TK_Beyond( TK_RBRACE ); TK_Beyond( TK_RBRACE ); if ( tk_Token == TK_EDGES ) { // TK_Beyond(TK_EDGES); TK_Beyond( TK_RBRACE ); } scaling[0] = scaling[1] = scaling[2] = 1.0; // rotation[0] = rotation[1] = rotation[2] = 0.0; // translation[0] = translation[1] = translation[2] = 0.0; // See if there are any other models belonging to this node #if 1 TK_Fetch(); nextToken = tk_Token; if ( nextToken == TK_CLUSTERS ) { if ( g_skelModel.clustered == -1 ) { ReadHRCClusterList( meshNode, vertIndexBase ); } else { nextToken = TK_Get( TK_CLUSTER_NAME ); while ( nextToken == TK_CLUSTER_NAME ) { TK_BeyondRequire( TK_CLUSTER_STATE, TK_INTNUMBER ); nextToken = TK_Fetch(); } } // one right brace follow the list of clusters nextToken = TK_Beyond( TK_RBRACE ); } else { if ( g_skelModel.clustered == -1 && !vertIndexBase ) { meshNode->clustered = false; } } #endif nextToken = tk_Token; if ( nextToken == TK_SPLINE ) { while ( nextToken == TK_SPLINE ) { // spline node has two right braces nextToken = TK_Beyond( TK_RBRACE ); nextToken = TK_Beyond( TK_RBRACE ); } nextToken = TK_Beyond( TK_RBRACE ); } while ( nextToken == TK_MATERIAL ) { nextToken = TK_Beyond( TK_RBRACE ); } while ( nextToken == TK_MODEL ) { HandleHRCModel( triList,triangleCount,nodesList, num_mesh_nodes, ActiveNode, Depth + 1, vertexCount + vertIndexBase ); nextToken = TK_Fetch(); } }
static void LoadHRC(char *fileName, triangle_t **triList, int *triangleCount) { int i, j; int vertexCount; struct vList_s { float v[3]; } *vList; int triCount; triangle_t *tList; float scaling[3]; float rotation[3]; float translation[3]; float x, y, z; float x2, y2, z2; float rx, ry, rz; TK_Init(); TK_OpenSource(fileName); TK_FetchRequire(TK_HRCH); TK_FetchRequire(TK_COLON); TK_FetchRequire(TK_SOFTIMAGE); TK_Beyond(TK_MODEL); TK_Beyond(TK_SCALING); for (i = 0; i < 3; i++) { TK_Require(TK_FLOATNUMBER); scaling[i] = tk_FloatNumber; TK_Fetch(); } TK_Beyond(TK_ROTATION); for (i = 0; i < 3; i++) { TK_Require(TK_FLOATNUMBER); rotation[i] = tk_FloatNumber; TK_Fetch(); } TK_Beyond(TK_TRANSLATION); for (i = 0; i < 3; i++) { TK_Require(TK_FLOATNUMBER); translation[i] = tk_FloatNumber; TK_Fetch(); } rx = (float)(((rotation[0]-90.0)/360.0)*2.0*MY_PI); ry = (float)((rotation[1]/360.0)*2.0*MY_PI); rz = (float)((rotation[2]/360.0)*2.0*MY_PI); TK_Beyond(TK_MESH); TK_BeyondRequire(TK_VERTICES, TK_INTNUMBER); vertexCount = tk_IntNumber; vList = (struct vList_s *) SafeMalloc(vertexCount * sizeof(vList[0])); for (i = 0; i < vertexCount; i++) { TK_BeyondRequire(TK_LBRACKET, TK_INTNUMBER); if (tk_IntNumber != i) { COM_Error("File '%s', line %d:\nVertex index mismatch.\n", tk_SourceName, tk_Line); } TK_Beyond(TK_POSITION); // Apply the scaling, rotation, and translation in the order // specified in the HRC file. This could be wrong. TK_Require(TK_FLOATNUMBER); x = tk_FloatNumber*scaling[0]; TK_FetchRequire(TK_FLOATNUMBER); y = tk_FloatNumber*scaling[1]; TK_FetchRequire(TK_FLOATNUMBER); z = tk_FloatNumber*scaling[2]; y2 = (float)(y*cos(rx)+z*sin(rx)); z2 = (float)(-y*sin(rx)+z*cos(rx)); y = y2; z = z2; x2 = (float)(x*cos(ry)-z*sin(ry)); z2 = (float)(x*sin(ry)+z*cos(ry)); x = x2; z = z2; x2 = (float)(x*cos(rz)+y*sin(rz)); y2 = (float)(-x*sin(rz)+y*cos(rz)); x = x2; y = y2; vList[i].v[0] = x+translation[0]; vList[i].v[1] = y+translation[1]; vList[i].v[2] = z+translation[2]; } TK_BeyondRequire(TK_POLYGONS, TK_INTNUMBER); triCount = tk_IntNumber; if (triCount >= MAXTRIANGLES) { COM_Error("Too many triangles in file %s\n", InputFileName); } *triangleCount = triCount; tList = (triangle_t *) SafeMalloc(MAXTRIANGLES * sizeof(triangle_t)); *triList = tList; for (i = 0; i < triCount; i++) { TK_BeyondRequire(TK_LBRACKET, TK_INTNUMBER); if (tk_IntNumber != i) { COM_Error("File '%s', line %d:\nTriangle index mismatch.\n", tk_SourceName, tk_Line); } TK_BeyondRequire(TK_NODES, TK_INTNUMBER); if (tk_IntNumber != 3) { COM_Error("File '%s', line %d:\nBad polygon vertex count: %d.", tk_SourceName, tk_Line, tk_IntNumber); } for (j = 0; j < 3; j++) { TK_BeyondRequire(TK_LBRACKET, TK_INTNUMBER); if (tk_IntNumber != j) { COM_Error("File '%s', line %d:\nTriangle vertex index" " mismatch. %d should be %d\n", tk_SourceName, tk_Line, tk_IntNumber, j); } TK_BeyondRequire(TK_VERTEX, TK_INTNUMBER); tList[i].verts[2-j][0] = vList[tk_IntNumber].v[0]; tList[i].verts[2-j][1] = vList[tk_IntNumber].v[1]; tList[i].verts[2-j][2] = vList[tk_IntNumber].v[2]; } /* printf("Face %i:\n v0: %f, %f, %f\n v1: %f, %f, %f\n" " v2: %f, %f, %f\n", i, tList[i].verts[0][0], tList[i].verts[0][1], tList[i].verts[0][2], tList[i].verts[1][0], tList[i].verts[1][1], tList[i].verts[1][2], tList[i].verts[2][0], tList[i].verts[2][1], tList[i].verts[2][2]); */ } }