00001 #ifndef MODEL_H
00002
00003 #include <stdio.h>
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
00028
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
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
00055
00056
00057
00058 #define MODEL_SCALE 1.0
00059
00060
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
00082 unsigned int temp = ((long*) f)[0];
00083
00084
00085 __stwbrx( temp, &transfer.i, 0);
00086
00087
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
00099 struct JOEHeader
00100 {
00101 int magic;
00102 int version;
00103 int num_faces;
00104 int num_frames;
00105 };
00106
00107
00108 struct JOEVertex
00109 {
00110 float vertex[3];
00111 };
00112
00113
00114 struct JOEFace
00115 {
00116 short vertexIndex[3];
00117 short normalIndex[3];
00118
00119 short textureIndex[JOE_MAX_TEXTURES*3];
00120 };
00121
00122
00123 struct JOETexCoord
00124 {
00125 float u, v;
00126 };
00127
00128
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
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 struct JOEObject
00158 {
00159 JOEHeader info;
00160 JOEFrame * frames;
00161 };
00162
00163
00164 class JOEMODEL
00165 {
00166
00167 public:
00168 JOEMODEL();
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();
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
00186
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;
00225 float radiusxz;
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
00239 void ReadData(FILE *m_FilePointer, JOEPACK * pack);
00240
00241
00242 void CleanUp(FILE *m_FilePointer, JOEPACK * pack);
00243
00244
00245
00246
00247
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
00254
00255 string modelpath;
00256
00257
00258
00259
00260
00261
00262 JOEObject * pObject;
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 };
00280
00281 #define MODEL_H
00282 #endif