include/model.h

Go to the documentation of this file.
00001 #ifndef MODEL_H
00002 
00003 #include <stdio.h>                      // Header File For Standard Input/Output
00004 #include <stdlib.h>
00005 #include <GL/gl.h>
00006 #include <GL/glu.h>
00007 #include <string>
00008 #include <iostream>
00009 #include <fstream>
00010 #include <cmath>
00011 #include <cassert>
00012 #include <stdint.h>
00013 
00014 #include <string>
00015 #include <vector>
00016 
00017 #ifdef __APPLE__
00018 #include <machine/endian.h>
00019 #endif
00020 
00021 using namespace std;
00022 
00023 #include "quat.h"
00024 #include "utility.h"
00025 #include "textures.h"
00026 #include "joepack.h"
00027 //#include "settings.h"
00028 //#include "terrain.h"
00029 #include "camera.h"
00030 #include "globals.h"
00031 
00032 #define NUM_COLLIDE_CACHE 10
00033 
00034 #define INTERSECT_FUNCTION utility.IntersectTriangleF
00035 
00036 #define JOE_MAX_TRIANGLES       4096
00037 #define JOE_MAX_VERTICES        2048
00038 #define JOE_MAX_TEXCOORDS       2048
00039 #define JOE_MAX_FRAMES          512
00040 
00041 #define JOE_VERSION 3
00042 
00043 //uv coords
00044 #define JOE_MAX_TEXTURES 1
00045 #define MAX_TEXTURE_UNITS 4
00046 
00047 #define MAX_FILENAME_LEN 1024
00048 
00049 #define TEXTUREMODE_NOTEX 0
00050 #define TEXTUREMODE_TEX 1
00051 #define TEXTUREMODE_REFLECTION 2
00052 #define TEXTUREMODE_ADD 3
00053 
00054 //texture files
00055 //#define NUM_TEXTURES 2
00056 
00057 //#define MODEL_SCALE 0.63
00058 #define MODEL_SCALE 1.0
00059 //#define MODEL_SCALE 0.1
00060 //#define MODEL_SCALE 0.7
00061 
00062 #ifdef __BIG_ENDIAN__
00063         #define ENDIAN_SWAP_16(A)  ((((uint16_t)(A) & 0xff00) >> 8) | \
00064                    (((uint16_t)(A) & 0x00ff) << 8))
00065         #define ENDIAN_SWAP_32(A)  ((((uint32_t)(A) & 0xff000000) >> 24) | \
00066                    (((uint32_t)(A) & 0x00ff0000) >> 8)  | \
00067                    (((uint32_t)(A) & 0x0000ff00) << 8)  | \
00068                    (((uint32_t)(A) & 0x000000ff) << 24))
00069         #define ENDIAN_SWAP_FLOAT(A)  LoadLEFloat(&(A))
00070         inline float LoadLEFloat( float *f )
00071         {
00072                    #define __stwbrx( value, base, index ) \
00073                                    __asm__ ( "stwbrx %0, %1, %2" :  : "r" (value), "b%" (index), "r" (base) : "memory" )
00074 
00075                    union
00076                    {
00077                                    long            i;
00078                                    float           f;
00079                    } transfer;
00080 
00081                    //load the float into the integer unit
00082                    unsigned int    temp = ((long*) f)[0];
00083 
00084                    //store it to the transfer union, with byteswapping
00085                    __stwbrx( temp,  &transfer.i, 0);       
00086 
00087                    //load it into the FPU and return it
00088                    return transfer.f;
00089         }
00090 #else
00091         #define ENDIAN_SWAP_16(A)  (A)
00092         #define ENDIAN_SWAP_32(A)  (A)
00093         #define ENDIAN_SWAP_FLOAT(A)  (A)
00094 #endif
00095 
00096 typedef unsigned char byte;
00097 
00098 // This holds the header information that is read in at the beginning of the file
00099 struct JOEHeader
00100 { 
00101         int magic;                   // This is used to identify the file
00102         int version;                 // The version number of the file
00103         int num_faces;              // The number of faces (polygons)
00104         int num_frames;               // The number of animation frames
00105 };
00106 
00107 // This is used to store the vertices that are read in for the current frame
00108 struct JOEVertex
00109 {
00110         float vertex[3];
00111 };
00112 
00113 // This stores the indices into the vertex and texture coordinate arrays
00114 struct JOEFace
00115 {
00116         short vertexIndex[3];
00117         short normalIndex[3];
00118         //short textureIndex[JOE_MAX_TEXTURES*3];
00119         short textureIndex[JOE_MAX_TEXTURES*3];
00120 };
00121 
00122 // This stores UV coordinates
00123 struct JOETexCoord
00124 {
00125         float u, v;
00126 };
00127 
00128 // This stores the frames vertices after they have been transformed
00129 struct JOEFrame
00130 {
00131         int num_verts;
00132         int num_texcoords;
00133         int num_normals;
00134         
00135         JOEFace * faces;
00136         JOEVertex * verts;
00137         JOEVertex * normals;
00138         JOETexCoord * texcoords;
00139 };
00140 
00141 /*// This holds the information for a material.  It may be a texture map of a color.
00142 // Some of these are not used, but I left them because you will want to eventually
00143 // read in the UV tile ratio and the UV tile offset for some models.
00144 struct tMaterialInfo
00145 {
00146     char  strName[255];         // The texture name
00147     char  strFile[255];         // The texture file name (If this is set it's a texture map)
00148     byte  color[3];             // The color of the object (R, G, B)
00149     int   texureId;             // the texture ID
00150     float uTile;                // u tiling of texture  (Currently not used)
00151     float vTile;                // v tiling of texture  (Currently not used)
00152     float uOffset;              // u offset of texture  (Currently not used)
00153     float vOffset;              // v offset of texture  (Currently not used)
00154 } ;*/
00155 
00156 // This holds all the information for our model/scene. 
00157 struct JOEObject 
00158 {
00159         JOEHeader info;
00160         JOEFrame * frames;
00161 };
00162 
00163 // This class handles all of the loading code
00164 class JOEMODEL
00165 {
00166 
00167 public:
00168     JOEMODEL();                             // This inits the data members
00169         ~JOEMODEL();
00170 
00171     bool Load(string strFileName) {return Load(strFileName, true);}
00172         bool Load(string strFileName, bool autoloadtexture);
00173         bool Load(string strFileName, bool autoloadtexture, JOEPACK * pack);
00174         bool LoadFromHandle(FILE * f, JOEPACK * pack);
00175         
00176         int BinaryRead(void * buffer, unsigned int size, unsigned int count, FILE * f, JOEPACK * pack);
00177         
00178         void Draw(int frame, float t);
00179         void Draw(int frame, int nextframe, float t);
00180         void DrawStatic(); //draw frame 0, optimized for speed
00181 
00182         void NewDraw(int frame, float t, VERTEX lightdir, QUATERNION rotation, int pass);
00183         void NewDraw(int frame, int nextframe, float t, VERTEX lightdir, QUATERNION rotation, int pass);
00184 
00185         //GLuint GetTex(int tnum);
00186         //void SetBaseTexture(int tid);
00187 
00188         void NoTexture(int tid) {if (tid < MAX_TEXTURE_UNITS) texturemode[tid] = TEXTUREMODE_NOTEX;}
00189         void Texture(string texturename, int tid);
00190         void ReflectionTexture(string texturename, int tid);
00191         void AdditiveTexture(string texturename, int tid);
00192         
00193         void TextureID(TEXTURE_HANDLE * texid, int tid) {if (tid < MAX_TEXTURE_UNITS) {texturemode[tid] = TEXTUREMODE_TEX; textureid[tid] = *texid; autounload[tid] = false;}}
00194         void ReflectionTextureID(TEXTURE_HANDLE * texid, int tid) {if (tid < MAX_TEXTURE_UNITS) {texturemode[tid] = TEXTUREMODE_REFLECTION; textureid[tid] = *texid; autounload[tid] = false;}}
00195         void AdditiveTextureID(TEXTURE_HANDLE * texid, int tid) {if (tid < MAX_TEXTURE_UNITS) {texturemode[tid] = TEXTUREMODE_ADD; textureid[tid] = *texid; autounload[tid] = false;}}
00196         
00197         void SetTU(int tuid, bool tue) {tuenable[tuid] = tue;}
00198 
00199         float GetRadius() {return radius + 0.5f;}
00200         float GetRadiusXZ() {return radiusxz;}
00201         
00202         unsigned int GetFaces() {if (!loadedfile) return 0; else return pObject->info.num_faces;}
00203         
00204         bool Collide(VERTEX origin, VERTEX direction, VERTEX &outtri, bool closest);
00205         
00206         inline float * GetVert(short index) {return pObject->frames[0].verts[index].vertex;}
00207         short * GetFace(short index) {return pObject->frames[0].faces[index].vertexIndex;}
00208         
00209         inline float * GetNorm(short index) {return pObject->frames[0].normals[index].vertex;}
00210         short * GetNormIdx(short index) {return pObject->frames[0].faces[index].normalIndex;}
00211         
00212         AABB & GetBBOX() {return bbox;}
00213 
00214 private:
00215         
00216         int collidecache[NUM_COLLIDE_CACHE];
00217         int collidecachepos;
00218 
00219         AABB bbox;
00220         
00221         string FileToPNG(string filename);
00222         string FileToTexturesizePNG(string filename);
00223         
00224         float radius; //used for frustum culling
00225         float radiusxz; //used to get an object's vertical profile
00226         
00227         GLuint static_list;
00228 
00229         bool loadedfile;
00230         
00231         inline void ColorFromNormal(VERTEX &norm, VERTEX &ldir);
00232         inline void TexCoordFromNormal(VERTEX &norm, VERTEX &ldir);
00233 
00234         void CorrectEndian(struct JOEFace * p, int num);
00235         void CorrectEndian(struct JOEVertex *p, int num);
00236         void CorrectEndian(struct JOETexCoord *p, int num);
00237 
00238         // This reads in the data from the MD2 file and stores it in the member variables
00239         void ReadData(FILE *m_FilePointer, JOEPACK * pack);
00240 
00241         // This frees memory and closes the file
00242         void CleanUp(FILE *m_FilePointer, JOEPACK * pack);
00243         
00244         // The file pointer
00245         //FILE *m_FilePointer;
00246 
00247         //GLuint texid[NUM_TEXTURES];
00248         TEXTURE_HANDLE textureid[MAX_TEXTURE_UNITS];
00249         int texturemode[MAX_TEXTURE_UNITS];
00250         bool tuenable[MAX_TEXTURE_UNITS];
00251         bool autounload[MAX_TEXTURE_UNITS];
00252 
00253         //bool has_paint;
00254         //int numtex;
00255         string modelpath;
00256         //int active_base;
00257         //bool has_illum;
00258         //GLuint illum_tex;
00259         
00260         // Member variables
00261 
00262         JOEObject * pObject;
00263         
00264         /*tMd2Header              m_Header;           // The header data
00265         tMd2Skin                *m_pSkins;          // The skin data
00266         tMd2TexCoord            *m_pTexCoords;      // The texture coordinates
00267         tMd2Face                *m_pTriangles;      // Face index information
00268         tMd2Frame               *m_pFrames;         // The frames of animation (vertices)
00269         
00270         
00271         int numOfObjects;                   // The number of objects in the model
00272         int numOfMaterials;                 // The number of materials for the model
00273         int numOfAnimations;                // The number of animations in this model (NEW)
00274         int currentAnim;                    // The current index into pAnimations list (NEW)
00275         //int currentFrame;                   // The current frame of the current animation (NEW)
00276         vector<tAnimationInfo> pAnimations; // The list of animations (NEW)
00277         //vector<tMaterialInfo> pMaterials;   // The list of material information (Textures and colors)
00278         vector<t3DObject> pObject;          // The object list for our model*/
00279 };
00280 
00281 #define MODEL_H
00282 #endif

Generated on Thu Oct 19 04:05:47 2006 by  doxygen 1.4.6