home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 58 / pcpp58a.iso / extras / quake 3 source / Q3A_ToolSource.exe / Main / misc_model.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-02  |  11.6 KB  |  452 lines

  1.  
  2. #include "qbsp.h"
  3. #include "aselib.h"
  4. #ifdef _WIN32
  5. #ifdef _TTIMOBUILD
  6. #include "pakstuff.h"
  7. #else
  8. #include "../libs/pakstuff.h"
  9. #endif
  10. #endif
  11.  
  12.  
  13. typedef struct {
  14.     char        modelName[1024];
  15.     md3Header_t    *header;
  16. } loadedModel_t;
  17.  
  18. int        c_triangleModels;
  19. int        c_triangleSurfaces;
  20. int        c_triangleVertexes;
  21. int        c_triangleIndexes;
  22.  
  23.  
  24. #define    MAX_LOADED_MODELS    1024
  25. loadedModel_t        loadedModels[MAX_LOADED_MODELS];
  26. int                    numLoadedModels;
  27.  
  28. /*
  29. =================
  30. R_LoadMD3
  31. =================
  32. */
  33. #define    LL(x) x=LittleLong(x)
  34. md3Header_t *R_LoadMD3( const char *mod_name ) {
  35.     int                    i, j;
  36.     md3Header_t            *md3;
  37.     md3Frame_t            *frame;
  38.     md3Surface_t        *surf;
  39.     md3Triangle_t        *tri;
  40.     md3St_t                *st;
  41.     md3XyzNormal_t        *xyz;
  42.     int                    version;
  43.     char                filename[1024];
  44.     int                    len;
  45.  
  46.     sprintf( filename, "%s%s", gamedir, mod_name );
  47.     len = TryLoadFile( filename, (void **)&md3 );
  48. #ifdef _WIN32
  49.   if ( len <= 0 ) {
  50.     len = PakLoadAnyFile(filename, (void **)&md3);
  51.   }
  52. #endif
  53.     if ( len <= 0 ) {
  54.       return NULL;
  55.     }
  56.  
  57.   version = LittleLong (md3->version);
  58.     if (version != MD3_VERSION) {
  59.         _printf( "R_LoadMD3: %s has wrong version (%i should be %i)\n",
  60.                  mod_name, version, MD3_VERSION);
  61.         return NULL;
  62.     }
  63.  
  64.     LL(md3->ident);
  65.     LL(md3->version);
  66.     LL(md3->numFrames);
  67.     LL(md3->numTags);
  68.     LL(md3->numSurfaces);
  69.     LL(md3->numSkins);
  70.     LL(md3->ofsFrames);
  71.     LL(md3->ofsTags);
  72.     LL(md3->ofsSurfaces);
  73.     LL(md3->ofsEnd);
  74.  
  75.     if ( md3->numFrames < 1 ) {
  76.         _printf( "R_LoadMD3: %s has no frames\n", mod_name );
  77.         return NULL;
  78.     }
  79.  
  80.     // we don't need to swap tags in the renderer, they aren't used
  81.     
  82.     // swap all the frames
  83.     frame = (md3Frame_t *) ( (byte *)md3 + md3->ofsFrames );
  84.     for ( i = 0 ; i < md3->numFrames ; i++, frame++) {
  85.         frame->radius = LittleFloat( frame->radius );
  86.         for ( j = 0 ; j < 3 ; j++ ) {
  87.             frame->bounds[0][j] = LittleFloat( frame->bounds[0][j] );
  88.             frame->bounds[1][j] = LittleFloat( frame->bounds[1][j] );
  89.             frame->localOrigin[j] = LittleFloat( frame->localOrigin[j] );
  90.         }
  91.     }
  92.  
  93.     // swap all the surfaces
  94.     surf = (md3Surface_t *) ( (byte *)md3 + md3->ofsSurfaces );
  95.     for ( i = 0 ; i < md3->numSurfaces ; i++) {
  96.  
  97.         LL(surf->ident);
  98.         LL(surf->flags);
  99.         LL(surf->numFrames);
  100.         LL(surf->numShaders);
  101.         LL(surf->numTriangles);
  102.         LL(surf->ofsTriangles);
  103.         LL(surf->numVerts);
  104.         LL(surf->ofsShaders);
  105.         LL(surf->ofsSt);
  106.         LL(surf->ofsXyzNormals);
  107.         LL(surf->ofsEnd);
  108.         
  109.         if ( surf->numVerts > SHADER_MAX_VERTEXES ) {
  110.             Error ("R_LoadMD3: %s has more than %i verts on a surface (%i)",
  111.                 mod_name, SHADER_MAX_VERTEXES, surf->numVerts );
  112.         }
  113.         if ( surf->numTriangles*3 > SHADER_MAX_INDEXES ) {
  114.             Error ("R_LoadMD3: %s has more than %i triangles on a surface (%i)",
  115.                 mod_name, SHADER_MAX_INDEXES / 3, surf->numTriangles );
  116.         }
  117.  
  118.         // swap all the triangles
  119.         tri = (md3Triangle_t *) ( (byte *)surf + surf->ofsTriangles );
  120.         for ( j = 0 ; j < surf->numTriangles ; j++, tri++ ) {
  121.             LL(tri->indexes[0]);
  122.             LL(tri->indexes[1]);
  123.             LL(tri->indexes[2]);
  124.         }
  125.  
  126.         // swap all the ST
  127.         st = (md3St_t *) ( (byte *)surf + surf->ofsSt );
  128.         for ( j = 0 ; j < surf->numVerts ; j++, st++ ) {
  129.             st->st[0] = LittleFloat( st->st[0] );
  130.             st->st[1] = LittleFloat( st->st[1] );
  131.         }
  132.  
  133.         // swap all the XyzNormals
  134.         xyz = (md3XyzNormal_t *) ( (byte *)surf + surf->ofsXyzNormals );
  135.         for ( j = 0 ; j < surf->numVerts * surf->numFrames ; j++, xyz++ ) 
  136.         {
  137.             xyz->xyz[0] = LittleShort( xyz->xyz[0] );
  138.             xyz->xyz[1] = LittleShort( xyz->xyz[1] );
  139.             xyz->xyz[2] = LittleShort( xyz->xyz[2] );
  140.  
  141.             xyz->normal = LittleShort( xyz->normal );
  142.         }
  143.  
  144.  
  145.         // find the next surface
  146.         surf = (md3Surface_t *)( (byte *)surf + surf->ofsEnd );
  147.     }
  148.  
  149.     return md3;
  150. }
  151.  
  152.  
  153. /*
  154. ================
  155. LoadModel
  156. ================
  157. */
  158. md3Header_t *LoadModel( const char *modelName ) {
  159.     int                i;
  160.     loadedModel_t    *lm;
  161.  
  162.     // see if we already have it loaded
  163.     for ( i = 0, lm = loadedModels ; i < numLoadedModels ; i++, lm++ ) {
  164.         if ( !strcmp( modelName, lm->modelName ) ) {
  165.             return lm->header;
  166.         }
  167.     }
  168.  
  169.     // load it
  170.     if ( numLoadedModels == MAX_LOADED_MODELS ) {
  171.         Error( "MAX_LOADED_MODELS" );
  172.     }
  173.     numLoadedModels++;
  174.  
  175.     strcpy( lm->modelName, modelName );
  176.  
  177.     lm->header = R_LoadMD3( modelName );
  178.  
  179.     return lm->header;
  180. }
  181.  
  182. /*
  183. ============
  184. InsertMD3Model
  185.  
  186. Convert a model entity to raw geometry surfaces and insert it in the tree
  187. ============
  188. */
  189. void InsertMD3Model( const char *modelName, vec3_t origin, float angle, tree_t *tree ) {
  190.     int                    i, j;
  191.     md3Header_t            *md3;
  192.     md3Surface_t        *surf;
  193.     md3Shader_t            *shader;
  194.     md3Triangle_t        *tri;
  195.     md3St_t                *st;
  196.     md3XyzNormal_t        *xyz;
  197.     drawVert_t            *outv;
  198.     float                lat, lng;
  199.     float                angleCos, angleSin;
  200.     mapDrawSurface_t    *out;
  201.     vec3_t                temp;
  202.  
  203.     angle = angle / 180 * Q_PI;
  204.     angleCos = cos( angle );
  205.     angleSin = sin( angle );
  206.  
  207.     // load the model
  208.     md3 = LoadModel( modelName );
  209.     if ( !md3 ) {
  210.         return;
  211.     }
  212.  
  213.     // each md3 surface will become a new bsp surface
  214.  
  215.     c_triangleModels++;
  216.     c_triangleSurfaces += md3->numSurfaces;
  217.  
  218.     // expand, translate, and rotate the vertexes
  219.     // swap all the surfaces
  220.     surf = (md3Surface_t *) ( (byte *)md3 + md3->ofsSurfaces );
  221.     for ( i = 0 ; i < md3->numSurfaces ; i++) {
  222.         // allocate a surface
  223.         out = AllocDrawSurf();
  224.         out->miscModel = qtrue;
  225.  
  226.         shader = (md3Shader_t *) ( (byte *)surf + surf->ofsShaders );
  227.  
  228.         out->shaderInfo = ShaderInfoForShader( shader->name );
  229.  
  230.         out->numVerts = surf->numVerts;
  231.         out->verts = malloc( out->numVerts * sizeof( out->verts[0] ) );
  232.  
  233.         out->numIndexes = surf->numTriangles * 3;
  234.         out->indexes = malloc( out->numIndexes * sizeof( out->indexes[0] ) );
  235.  
  236.         out->lightmapNum = -1;
  237.         out->fogNum = -1;
  238.  
  239.         // emit the indexes
  240.         c_triangleIndexes += surf->numTriangles * 3;
  241.         tri = (md3Triangle_t *) ( (byte *)surf + surf->ofsTriangles );
  242.         for ( j = 0 ; j < surf->numTriangles ; j++, tri++ ) {
  243.             out->indexes[j*3+0] = tri->indexes[0];
  244.             out->indexes[j*3+1] = tri->indexes[1];
  245.             out->indexes[j*3+2] = tri->indexes[2];
  246.         }
  247.  
  248.         // emit the vertexes
  249.         st = (md3St_t *) ( (byte *)surf + surf->ofsSt );
  250.         xyz = (md3XyzNormal_t *) ( (byte *)surf + surf->ofsXyzNormals );
  251.  
  252.         c_triangleVertexes += surf->numVerts;
  253.         for ( j = 0 ; j < surf->numVerts ; j++, st++, xyz++ ) {
  254.             outv = &out->verts[ j ];
  255.  
  256.             outv->st[0] = st->st[0];
  257.             outv->st[1] = st->st[1];
  258.  
  259.             outv->lightmap[0] = 0;
  260.             outv->lightmap[1] = 0;
  261.  
  262.             // the colors will be set by the lighting pass
  263.             outv->color[0] = 255;
  264.             outv->color[1] = 255;
  265.             outv->color[2] = 255;
  266.             outv->color[3] = 255;
  267.  
  268.             outv->xyz[0] = origin[0] + MD3_XYZ_SCALE * ( xyz->xyz[0] * angleCos - xyz->xyz[1] * angleSin );
  269.             outv->xyz[1] = origin[1] + MD3_XYZ_SCALE * ( xyz->xyz[0] * angleSin +  xyz->xyz[1] * angleCos );
  270.             outv->xyz[2] = origin[2] + MD3_XYZ_SCALE * ( xyz->xyz[2] );
  271.  
  272.             // decode the lat/lng normal to a 3 float normal
  273.             lat = ( xyz->normal >> 8 ) & 0xff;
  274.             lng = ( xyz->normal & 0xff );
  275.             lat *= Q_PI/128;
  276.             lng *= Q_PI/128;
  277.  
  278.             temp[0] = cos(lat) * sin(lng);
  279.             temp[1] = sin(lat) * sin(lng);
  280.             temp[2] = cos(lng);
  281.  
  282.             // rotate the normal
  283.             outv->normal[0] = temp[0] * angleCos - temp[1] * angleSin;
  284.             outv->normal[1] = temp[0] * angleSin +  temp[1] * angleCos;
  285.             outv->normal[2] = temp[2];
  286.         }
  287.  
  288.         // find the next surface
  289.         surf = (md3Surface_t *)( (byte *)surf + surf->ofsEnd );
  290.     }
  291.  
  292. }
  293.  
  294. //==============================================================================
  295.  
  296.  
  297. /*
  298. ============
  299. InsertASEModel
  300.  
  301. Convert a model entity to raw geometry surfaces and insert it in the tree
  302. ============
  303. */
  304. void InsertASEModel( const char *modelName, vec3_t origin, float angle, tree_t *tree ) {
  305.     int                    i, j;
  306.     drawVert_t            *outv;
  307.     float                angleCos, angleSin;
  308.     mapDrawSurface_t    *out;
  309.     int                    numSurfaces;
  310.     const char            *name;
  311.     polyset_t            *pset;
  312.     int                    numFrames;
  313.     char                filename[1024];
  314.  
  315.     sprintf( filename, "%s%s", gamedir, modelName );
  316.  
  317.     angle = angle / 180 * Q_PI;
  318.     angleCos = cos( angle );
  319.     angleSin = sin( angle );
  320.  
  321.     // load the model
  322.     ASE_Load( filename, qfalse, qfalse );
  323.  
  324.     // each ase surface will become a new bsp surface
  325.     numSurfaces = ASE_GetNumSurfaces();
  326.  
  327.     c_triangleModels++;
  328.     c_triangleSurfaces += numSurfaces;
  329.  
  330.     // expand, translate, and rotate the vertexes
  331.     // swap all the surfaces
  332.     for ( i = 0 ; i < numSurfaces ; i++) {
  333.         name = ASE_GetSurfaceName( i );
  334.  
  335.         pset = ASE_GetSurfaceAnimation( i, &numFrames, -1, -1, -1 );
  336.         if ( !name || !pset ) {
  337.             continue;
  338.         }
  339.  
  340.         // allocate a surface
  341.         out = AllocDrawSurf();
  342.         out->miscModel = qtrue;
  343.  
  344.         out->shaderInfo = ShaderInfoForShader( pset->materialname );
  345.  
  346.         out->numVerts = 3 * pset->numtriangles;
  347.         out->verts = malloc( out->numVerts * sizeof( out->verts[0] ) );
  348.  
  349.         out->numIndexes = 3 * pset->numtriangles;
  350.         out->indexes = malloc( out->numIndexes * sizeof( out->indexes[0] ) );
  351.  
  352.         out->lightmapNum = -1;
  353.         out->fogNum = -1;
  354.  
  355.         // emit the indexes
  356.         c_triangleIndexes += out->numIndexes;
  357.         for ( j = 0 ; j < out->numIndexes ; j++ ) {
  358.             out->indexes[j] = j;
  359.         }
  360.  
  361.         // emit the vertexes
  362.         c_triangleVertexes += out->numVerts;
  363.         for ( j = 0 ; j < out->numVerts ; j++ ) {
  364.             int        index;
  365.             triangle_t    *tri;
  366.  
  367.             index = j % 3;
  368.             tri = &pset->triangles[ j / 3 ];
  369.  
  370.             outv = &out->verts[ j ];
  371.  
  372.             outv->st[0] = tri->texcoords[index][0];
  373.             outv->st[1] = tri->texcoords[index][1];
  374.  
  375.             outv->lightmap[0] = 0;
  376.             outv->lightmap[1] = 0;
  377.  
  378.             // the colors will be set by the lighting pass
  379.             outv->color[0] = 255;
  380.             outv->color[1] = 255;
  381.             outv->color[2] = 255;
  382.             outv->color[3] = 255;
  383.  
  384.             outv->xyz[0] = origin[0] + tri->verts[index][0];
  385.             outv->xyz[1] = origin[1] + tri->verts[index][1];
  386.             outv->xyz[2] = origin[2] + tri->verts[index][2];
  387.  
  388.             // rotate the normal
  389.             outv->normal[0] = tri->normals[index][0];
  390.             outv->normal[1] = tri->normals[index][1];
  391.             outv->normal[2] = tri->normals[index][2];
  392.         }
  393.     }
  394.  
  395. }
  396.  
  397.  
  398. //==============================================================================
  399.  
  400.  
  401.  
  402. /*
  403. =====================
  404. AddTriangleModels
  405. =====================
  406. */
  407. void AddTriangleModels( tree_t *tree ) {
  408.     int            entity_num;
  409.     entity_t    *entity;
  410.  
  411.     qprintf("----- AddTriangleModels -----\n");
  412.  
  413.     for ( entity_num=1 ; entity_num< num_entities ; entity_num++ ) {
  414.         entity = &entities[entity_num];
  415.     
  416.         // convert misc_models into raw geometry
  417.         if ( !Q_stricmp( "misc_model", ValueForKey( entity, "classname" ) ) ) {
  418.             const char    *model;
  419.             vec3_t    origin;
  420.             float    angle;
  421.  
  422.             // get the angle for rotation  FIXME: support full matrix positioning
  423.             angle = FloatForKey( entity, "angle" );
  424.  
  425.             GetVectorForKey( entity, "origin", origin );
  426.  
  427.             model = ValueForKey( entity, "model" );
  428.             if ( !model[0] ) {
  429.                 _printf("WARNING: misc_model at %i %i %i without a model key\n", (int)origin[0],
  430.                     (int)origin[1], (int)origin[2] );
  431.                 continue;
  432.             }
  433.             if ( strstr( model, ".md3" ) || strstr( model, ".MD3" ) ) {
  434.                 InsertMD3Model( model, origin, angle, tree );
  435.                 continue;
  436.             }
  437.             if ( strstr( model, ".ase" ) || strstr( model, ".ASE" ) ) {
  438.                 InsertASEModel( model, origin, angle, tree );
  439.                 continue;
  440.             }
  441.             _printf( "Unknown misc_model type: %s\n", model );
  442.             continue;
  443.         }
  444.     }
  445.  
  446.     qprintf( "%5i triangle models\n", c_triangleModels );
  447.     qprintf( "%5i triangle surfaces\n", c_triangleSurfaces );
  448.     qprintf( "%5i triangle vertexes\n", c_triangleVertexes );
  449.     qprintf( "%5i triangle indexes\n", c_triangleIndexes );
  450. }
  451.  
  452.