Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

ModelBIN.cpp

Go to the documentation of this file.
00001 /*
00002  * ModelBIN.cpp
00003  *
00004  * Part of Fly! Legacy project
00005  *
00006  * Copyright 2003 Chris Wallace
00007  *
00008  * Fly! Legacy is free software; you can redistribute it and/or modify
00009  *   it under the terms of the GNU General Public License as published by
00010  *   the Free Software Foundation; either version 2 of the License, or
00011  *   (at your option) any later version.
00012  *
00013  * Fly! Legacy is distributed in the hope that it will be useful,
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *   GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  *   along with Fly! Legacy; if not, write to the Free Software
00020  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00027 #include "../Include/Globals.h"
00028 #include "../Include/FlyLegacy.h"
00029 #include "../Include/Utility.h"
00030 
00031 
00032 typedef struct {
00033   unsigned long scale;
00034   unsigned long unknown1;
00035   unsigned long unknown2;
00036   unsigned long nVertices;
00037 } SBINHeader;
00038 
00039 typedef struct {
00040   long x, y, z;      // Vertex coordinates
00041 } SBINVertex;
00042 
00043 #define BIN_DATA_BLOCK_EOF      0x00000000
00044 
00045 
00046 CModelBIN::CModelBIN (const char* binFilename)
00047 {
00048   top = new ssgBranch;
00049   top->setName (binFilename);
00050 
00051   rawtexture = NULL;
00052 
00053   // Open POD file
00054   PODFILE *p = popen (&globals->pfs, binFilename);
00055   if (p == NULL) {
00056     gtfo ("CModelBIN : Could not open BIN model %s", binFilename);
00057   }
00058 
00059   // Read file signature
00060   unsigned long signature;
00061   pread (&signature, sizeof(unsigned long), 1, p);
00062   if (signature != 0x14) {
00063     gtfo ("CModelBIN : Invalid file signature (0x%08X) in %s", signature, binFilename);
00064   }
00065 
00066   // Read BIN header
00067   SBINHeader header;
00068   pread (&header, sizeof(SBINHeader), 1, p);
00069 
00070   // Read vertex table
00071   SBINVertex *v = new SBINVertex[header.nVertices];
00072   pread (v, sizeof(SBINVertex), header.nVertices, p);
00073 
00074   // Read data blocks until EOF block
00075   bool eof = false;
00076   while (!eof) {
00077     // Read block type
00078     unsigned long type;
00079     pread (&type, sizeof(unsigned long), 1, p);
00080 
00081     switch (type) {
00082     case BIN_DATA_BLOCK_EOF:
00083       // EOF block
00084       eof = true;
00085       break;
00086 
00087     default:
00088       pclose (p);
00089       delete v;
00090       gtfo ("CModelBIN : Unsupported data block 0x%08X in %s", type, binFilename);
00091     }
00092   }
00093 /*
00094   // Create state information
00095   ssgSimpleState *state = new ssgSimpleState ();
00096 
00097   sgVec4 spec = { 0.0f, 0.0f, 0.0f, 1.0f };
00098   sgVec4 emis = { 0.0f, 0.0f, 0.0f, 1.0f } ;
00099   float  shi  = { 0.0f } ;
00100 
00101   state->setMaterial ( GL_SPECULAR, spec ) ;
00102   state->setMaterial ( GL_EMISSION, emis ) ;
00103   state->setShininess ( shi ) ;
00104   state->enable ( GL_COLOR_MATERIAL ) ;
00105   state->setColourMaterial ( GL_AMBIENT_AND_DIFFUSE ) ;
00106   state->enable  ( GL_LIGHTING ) ;
00107   state->setShadeModel ( GL_SMOOTH ) ;
00108 
00109   if (texid != 0) {
00110     // Texture was loaded, attach it to the part
00111     state->enable (GL_TEXTURE_2D);
00112     state->setTexture (texid);
00113 
00114     // Set rendering states for transparent/opaque parts
00115     if (transparent) {
00116       state->enable ( GL_BLEND ) ;
00117       state->enable (GL_ALPHA_TEST);
00118       state->setTranslucent();
00119     } else {
00120       state->disable ( GL_BLEND ) ;
00121       state->disable (GL_ALPHA_TEST);
00122       state->setOpaque();
00123     }
00124   } else {
00125     state->disable (GL_TEXTURE_2D);
00126     state->disable ( GL_BLEND ) ;
00127   }
00128 
00129     // Before reading vertex list, allocate table of (temporary) structs
00130     //   to hold the vertex table until the face data is read
00131     SVertex *vt = new SVertex[nVerts];
00132 
00133     // Vertex list
00134     for (int j=0; j<nVerts; j++) {
00135       // x, y, z, nx, ny, nz, u, v
00136       float x, y, z, nx, ny, nz, u, v;
00137       pgets (s, LINE_LENGTH, p);
00138       if (sscanf (s, "%f,%f,%f,%f,%f,%f,%f,%f",
00139         &x, &y, &z, &nx, &ny, &nz, &u, &v) != 8) {
00140         // Error
00141         gtfo ("CModelSMF : Error parsing vertex %d for part %d in %s",
00142           j, i, smfFilename) ;
00143       }
00144 
00145       // Assign values to (temporary) vertex table
00146       SVertex *pNext = &vt[j];
00147       pNext->x = x;
00148       pNext->y = z;
00149       pNext->z = y;
00150       pNext->nx = nx;
00151       pNext->ny = nz;
00152       pNext->nz = ny;
00153       pNext->u = u;
00154       pNext->v = v;
00155     }
00156 
00157     // Before beginning to parse the vertex and face lists, create arrays
00158     //   for storage of the vertices, normals, texcoords and face indices
00159     ssgVertexArray *va = new ssgVertexArray(nFaces*3);
00160     ssgNormalArray *na = new ssgNormalArray(nFaces*3);
00161     ssgTexCoordArray *tca = new ssgTexCoordArray(nFaces*3);
00162 
00163     // Face list
00164     for (int k=0; k<nFaces; k++) {
00165       // i1, i2, i3 (vertex indices, clockwise)
00166       int i1, i2, i3;
00167       pgets (s, LINE_LENGTH, p);
00168       if (sscanf (s, "%d,%d,%d",
00169         &i1, &i2, &i3) != 3)
00170       {
00171         // Error
00172         gtfo ("CModelSMF : Error parsing face %d for part %d in %s",
00173           k, i, smfFilename);
00174       }
00175 
00176       // NOTE: ACM meshes are use left-hand coordinate system.
00177       //  In order to convert to OpenGL's right-hand system:
00178       //    - all triangle vertices are reversed order (3, 2, 1)
00179 
00180       // Add vertex coordinates to vertex array
00181       sgVec3 v1, v2, v3;
00182       sgSetVec3 (v1, vt[i1].x, vt[i1].y, vt[i1].z);
00183       sgSetVec3 (v2, vt[i2].x, vt[i2].y, vt[i2].z);
00184       sgSetVec3 (v3, vt[i3].x, vt[i3].y, vt[i3].z);
00185       va->add (v3);
00186       va->add (v2);
00187       va->add (v1);
00188 
00189       // Add normal coordinates to normal array
00190       sgVec3 n1, n2, n3;
00191       sgSetVec3 (n1, vt[i1].nx, vt[i1].ny, vt[i1].nz);
00192       sgSetVec3 (n2, vt[i2].nx, vt[i2].ny, vt[i2].nz);
00193       sgSetVec3 (n3, vt[i3].nx, vt[i3].ny, vt[i3].nz);
00194       na->add (n3);
00195       na->add (n2);
00196       na->add (n1);
00197 
00198       // Add texture coordinates to texcoord array
00199       sgVec2 tc1, tc2, tc3;
00200       sgSetVec2 (tc1, vt[i1].u, vt[i1].v);
00201       sgSetVec2 (tc2, vt[i2].u, vt[i2].v);
00202       sgSetVec2 (tc3, vt[i3].u, vt[i3].v);
00203       tca->add (tc3);
00204       tca->add (tc2);
00205       tca->add (tc1);
00206     }
00207 
00208     // Clean up SVertex list
00209     delete vt;
00210     // Create vertex table leaf for this part
00211     ssgVtxTable *vtab = new ssgVtxTable (GL_TRIANGLES, va, na, tca, NULL);
00212     vtab->setName (name);
00213     vtab->setState (state);
00214     top->addKid (vtab);
00215 */
00216 }
00217 
00218 CModelBIN::~CModelBIN (void)
00219 {
00220   if (rawtexture != NULL) delete rawtexture;
00221   delete top;
00222 }
00223 
00224 ssgEntity *CModelBIN::GetSSGEntity()
00225 {
00226   return top;
00227 }
00228 
SourceForge.net Logo Documentation generated by doxygen