1 /***************************************************************************
2 * Copyright (C) 2005 by Dimitris Saougos & Filippos Papadopoulos *
3 * <psybases@gmail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Library General Public License as *
7 * published by the Free Software Foundation; either version 2 of the *
8 * License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU Library General Public *
16 * License along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
28 // extern FILE *errorfp;
35 struct vec3 normalVectorArray[] =
37 { -0.525731f, 0.000000f, 0.850651f },
38 { -0.442863f, 0.238856f, 0.864188f },
39 { -0.295242f, 0.000000f, 0.955423f },
40 { -0.309017f, 0.500000f, 0.809017f },
41 { -0.162460f, 0.262866f, 0.951056f },
42 { 0.000000f, 0.000000f, 1.000000f },
43 { 0.000000f, 0.850651f, 0.525731f },
44 { -0.147621f, 0.716567f, 0.681718f },
45 { 0.147621f, 0.716567f, 0.681718f },
46 { 0.000000f, 0.525731f, 0.850651f },
47 { 0.309017f, 0.500000f, 0.809017f },
48 { 0.525731f, 0.000000f, 0.850651f },
49 { 0.295242f, 0.000000f, 0.955423f },
50 { 0.442863f, 0.238856f, 0.864188f },
51 { 0.162460f, 0.262866f, 0.951056f },
52 { -0.681718f, 0.147621f, 0.716567f },
53 { -0.809017f, 0.309017f, 0.500000f },
54 { -0.587785f, 0.425325f, 0.688191f },
55 { -0.850651f, 0.525731f, 0.000000f },
56 { -0.864188f, 0.442863f, 0.238856f },
57 { -0.716567f, 0.681718f, 0.147621f },
58 { -0.688191f, 0.587785f, 0.425325f },
59 { -0.500000f, 0.809017f, 0.309017f },
60 { -0.238856f, 0.864188f, 0.442863f },
61 { -0.425325f, 0.688191f, 0.587785f },
62 { -0.716567f, 0.681718f, -0.147621f },
63 { -0.500000f, 0.809017f, -0.309017f },
64 { -0.525731f, 0.850651f, 0.000000f },
65 { 0.000000f, 0.850651f, -0.525731f },
66 { -0.238856f, 0.864188f, -0.442863f },
67 { 0.000000f, 0.955423f, -0.295242f },
68 { -0.262866f, 0.951056f, -0.162460f },
69 { 0.000000f, 1.000000f, 0.000000f },
70 { 0.000000f, 0.955423f, 0.295242f },
71 { -0.262866f, 0.951056f, 0.162460f },
72 { 0.238856f, 0.864188f, 0.442863f },
73 { 0.262866f, 0.951056f, 0.162460f },
74 { 0.500000f, 0.809017f, 0.309017f },
75 { 0.238856f, 0.864188f, -0.442863f },
76 { 0.262866f, 0.951056f, -0.162460f },
77 { 0.500000f, 0.809017f, -0.309017f },
78 { 0.850651f, 0.525731f, 0.000000f },
79 { 0.716567f, 0.681718f, 0.147621f },
80 { 0.716567f, 0.681718f, -0.147621f },
81 { 0.525731f, 0.850651f, 0.000000f },
82 { 0.425325f, 0.688191f, 0.587785f },
83 { 0.864188f, 0.442863f, 0.238856f },
84 { 0.688191f, 0.587785f, 0.425325f },
85 { 0.809017f, 0.309017f, 0.500000f },
86 { 0.681718f, 0.147621f, 0.716567f },
87 { 0.587785f, 0.425325f, 0.688191f },
88 { 0.955423f, 0.295242f, 0.000000f },
89 { 1.000000f, 0.000000f, 0.000000f },
90 { 0.951056f, 0.162460f, 0.262866f },
91 { 0.850651f, -0.525731f, 0.000000f },
92 { 0.955423f, -0.295242f, 0.000000f },
93 { 0.864188f, -0.442863f, 0.238856f },
94 { 0.951056f, -0.162460f, 0.262866f },
95 { 0.809017f, -0.309017f, 0.500000f },
96 { 0.681718f, -0.147621f, 0.716567f },
97 { 0.850651f, 0.000000f, 0.525731f },
98 { 0.864188f, 0.442863f, -0.238856f },
99 { 0.809017f, 0.309017f, -0.500000f },
100 { 0.951056f, 0.162460f, -0.262866f },
101 { 0.525731f, 0.000000f, -0.850651f },
102 { 0.681718f, 0.147621f, -0.716567f },
103 { 0.681718f, -0.147621f, -0.716567f },
104 { 0.850651f, 0.000000f, -0.525731f },
105 { 0.809017f, -0.309017f, -0.500000f },
106 { 0.864188f, -0.442863f, -0.238856f },
107 { 0.951056f, -0.162460f, -0.262866f },
108 { 0.147621f, 0.716567f, -0.681718f },
109 { 0.309017f, 0.500000f, -0.809017f },
110 { 0.425325f, 0.688191f, -0.587785f },
111 { 0.442863f, 0.238856f, -0.864188f },
112 { 0.587785f, 0.425325f, -0.688191f },
113 { 0.688191f, 0.587785f, -0.425325f },
114 { -0.147621f, 0.716567f, -0.681718f },
115 { -0.309017f, 0.500000f, -0.809017f },
116 { 0.000000f, 0.525731f, -0.850651f },
117 { -0.525731f, 0.000000f, -0.850651f },
118 { -0.442863f, 0.238856f, -0.864188f },
119 { -0.295242f, 0.000000f, -0.955423f },
120 { -0.162460f, 0.262866f, -0.951056f },
121 { 0.000000f, 0.000000f, -1.000000f },
122 { 0.295242f, 0.000000f, -0.955423f },
123 { 0.162460f, 0.262866f, -0.951056f },
124 { -0.442863f, -0.238856f, -0.864188f },
125 { -0.309017f, -0.500000f, -0.809017f },
126 { -0.162460f, -0.262866f, -0.951056f },
127 { 0.000000f, -0.850651f, -0.525731f },
128 { -0.147621f, -0.716567f, -0.681718f },
129 { 0.147621f, -0.716567f, -0.681718f },
130 { 0.000000f, -0.525731f, -0.850651f },
131 { 0.309017f, -0.500000f, -0.809017f },
132 { 0.442863f, -0.238856f, -0.864188f },
133 { 0.162460f, -0.262866f, -0.951056f },
134 { 0.238856f, -0.864188f, -0.442863f },
135 { 0.500000f, -0.809017f, -0.309017f },
136 { 0.425325f, -0.688191f, -0.587785f },
137 { 0.716567f, -0.681718f, -0.147621f },
138 { 0.688191f, -0.587785f, -0.425325f },
139 { 0.587785f, -0.425325f, -0.688191f },
140 { 0.000000f, -0.955423f, -0.295242f },
141 { 0.000000f, -1.000000f, 0.000000f },
142 { 0.262866f, -0.951056f, -0.162460f },
143 { 0.000000f, -0.850651f, 0.525731f },
144 { 0.000000f, -0.955423f, 0.295242f },
145 { 0.238856f, -0.864188f, 0.442863f },
146 { 0.262866f, -0.951056f, 0.162460f },
147 { 0.500000f, -0.809017f, 0.309017f },
148 { 0.716567f, -0.681718f, 0.147621f },
149 { 0.525731f, -0.850651f, 0.000000f },
150 { -0.238856f, -0.864188f, -0.442863f },
151 { -0.500000f, -0.809017f, -0.309017f },
152 { -0.262866f, -0.951056f, -0.162460f },
153 { -0.850651f, -0.525731f, 0.000000f },
154 { -0.716567f, -0.681718f, -0.147621f },
155 { -0.716567f, -0.681718f, 0.147621f },
156 { -0.525731f, -0.850651f, 0.000000f },
157 { -0.500000f, -0.809017f, 0.309017f },
158 { -0.238856f, -0.864188f, 0.442863f },
159 { -0.262866f, -0.951056f, 0.162460f },
160 { -0.864188f, -0.442863f, 0.238856f },
161 { -0.809017f, -0.309017f, 0.500000f },
162 { -0.688191f, -0.587785f, 0.425325f },
163 { -0.681718f, -0.147621f, 0.716567f },
164 { -0.442863f, -0.238856f, 0.864188f },
165 { -0.587785f, -0.425325f, 0.688191f },
166 { -0.309017f, -0.500000f, 0.809017f },
167 { -0.147621f, -0.716567f, 0.681718f },
168 { -0.425325f, -0.688191f, 0.587785f },
169 { -0.162460f, -0.262866f, 0.951056f },
170 { 0.442863f, -0.238856f, 0.864188f },
171 { 0.162460f, -0.262866f, 0.951056f },
172 { 0.309017f, -0.500000f, 0.809017f },
173 { 0.147621f, -0.716567f, 0.681718f },
174 { 0.000000f, -0.525731f, 0.850651f },
175 { 0.425325f, -0.688191f, 0.587785f },
176 { 0.587785f, -0.425325f, 0.688191f },
177 { 0.688191f, -0.587785f, 0.425325f },
178 { -0.955423f, 0.295242f, 0.000000f },
179 { -0.951056f, 0.162460f, 0.262866f },
180 { -1.000000f, 0.000000f, 0.000000f },
181 { -0.850651f, 0.000000f, 0.525731f },
182 { -0.955423f, -0.295242f, 0.000000f },
183 { -0.951056f, -0.162460f, 0.262866f },
184 { -0.864188f, 0.442863f, -0.238856f },
185 { -0.951056f, 0.162460f, -0.262866f },
186 { -0.809017f, 0.309017f, -0.500000f },
187 { -0.864188f, -0.442863f, -0.238856f },
188 { -0.951056f, -0.162460f, -0.262866f },
189 { -0.809017f, -0.309017f, -0.500000f },
190 { -0.681718f, 0.147621f, -0.716567f },
191 { -0.681718f, -0.147621f, -0.716567f },
192 { -0.850651f, 0.000000f, -0.525731f },
193 { -0.688191f, 0.587785f, -0.425325f },
194 { -0.587785f, 0.425325f, -0.688191f },
195 { -0.425325f, 0.688191f, -0.587785f },
196 { -0.425325f, -0.688191f, -0.587785f },
197 { -0.587785f, -0.425325f, -0.688191f },
198 { -0.688191f, -0.587785f, -0.425325f }
203 model::model( char * md2_filename, char *bfilename, Graphics *g )
207 modelTexture = new Texture( bfilename, g);
211 FILE *fp = fopen( md2_filename, "rb" );
217 fread( ( void * ) & md2h, 1, sizeof( md2h ), fp );
219 tex1 = ( struct tex * ) malloc( sizeof( struct tex ) * md2h.numTexCoords );
221 fseek( fp, md2h.offsetTexCoords, SEEK_SET );
222 fread( ( void * ) tex1, sizeof( struct tex ), md2h.numTexCoords, fp );
224 tc = ( struct texturecoord * ) malloc( sizeof( struct texturecoord ) * md2h.numTexCoords );
226 for ( i = 0;i < md2h.numTexCoords;i++ )
228 tc[ i ].u = ( float ) ( tex1[ i ].u ) / ( float ) ( md2h.skinWidth );
229 tc[ i ].v = ( float ) ( tex1[ i ].v ) / ( float ) ( md2h.skinHeight );
233 fc = ( struct face * ) malloc( sizeof( struct face ) * md2h.numTriangles );
235 fseek( fp, md2h.offsetTriangles, 0 );
236 fread ( ( void * ) fc, sizeof( struct face ), md2h.numTriangles, fp );
238 fseek( fp, md2h.offsetFrames, 0 );
240 fr = ( struct frame ** ) malloc( sizeof( struct frame * ) * md2h.numFrames );
242 for ( i = 0;i < md2h.numFrames;i++ )
244 fr[ i ] = ( struct frame * ) malloc( sizeof( struct frame ) );
245 fread( ( void * ) ( &( fr[ i ] ->scale[ 0 ] ) ), 3, sizeof( float ), fp );
246 fread( ( void * ) ( &( fr[ i ] ->translate[ 0 ] ) ), 3, sizeof( float ), fp );
247 fread( ( void * ) & ( fr[ i ] ->name[ 0 ] ), 1, sizeof( fr[ i ] ->name ), fp );
248 fr[ i ] ->tr = ( struct triangle ** ) malloc( sizeof( struct triangle* ) * md2h.numVertices );
250 for ( j = 0;j < md2h.numVertices;j++ )
252 fr[ i ] ->tr[ j ] = ( struct triangle * ) malloc( sizeof( struct triangle ) );
253 fread( ( void * ) & ( fr[ i ] ->tr[ j ] ->vertex[ 0 ] ), 3, sizeof( BYTE ), fp );
254 fread( ( void * ) & ( fr[ i ] ->tr[ j ] ->lightNormalIndex ), 1, sizeof( BYTE ), fp );
269 // v = ( struct vertex * ) malloc( sizeof( struct vertex ) * ( md2h.numTriangles + 5 ) * 3 );
270 /* 12 einai to plh8os twn floats (vertices, normals, colour ktl*/
271 v = new GLfloat[12 * ( md2h.numTriangles + 5 ) * 3];
277 model::~model( void )
283 for ( i = 0;i < md2h.numFrames;i++ )
285 for ( j = 0;j < md2h.numVertices;j++ )
286 free ( fr[ i ] ->tr[ j ] );
287 free( fr[ i ] ->tr );
303 void model::render( float t )
311 if (mark!=NULL && strcmp(current_frame_sequence,"run")==0 &&
312 (cpointer->fnumber-current_offset==3 ||
313 cpointer->fnumber-current_offset==0))
317 if (oldt>t) cpointer=cpointer->next;
320 graph->setTexture(modelTexture);
322 for ( i = 0;i < md2h.numTriangles;i++ )
326 for ( k = 0;k < 3;k++ )
328 int p = fc[ i ].vindex[ k ];
333 framecounter = cpointer->fnumber;
335 x = fr[ framecounter ] ->tr[ p ] ->vertex[ 0 ];
336 y = fr[ framecounter ] ->tr[ p ] ->vertex[ 1 ];
337 z = fr[ framecounter ] ->tr[ p ] ->vertex[ 2 ];
339 xx = ( float ) x * fr[ framecounter ] ->scale[ 0 ];
340 yy = ( float ) y * fr[ framecounter ] ->scale[ 1 ];
341 zz = ( float ) z * fr[ framecounter ] ->scale[ 2 ];
343 xx = xx + fr[ framecounter ] ->translate[ 0 ];
344 yy = yy + fr[ framecounter ] ->translate[ 1 ];
345 zz = zz + fr[ framecounter ] ->translate[ 2 ];
348 nx1 = normalVectorArray[ fr[ framecounter ] ->tr[ p ] ->lightNormalIndex ].nx;
349 ny1 = normalVectorArray[ fr[ framecounter ] ->tr[ p ] ->lightNormalIndex ].ny;
350 nz1 = normalVectorArray[ fr[ framecounter ] ->tr[ p ] ->lightNormalIndex ].nz;
356 framecounter = cpointer->next->fnumber;
358 x = fr[ framecounter ] ->tr[ p ] ->vertex[ 0 ];
359 y = fr[ framecounter ] ->tr[ p ] ->vertex[ 1 ];
360 z = fr[ framecounter ] ->tr[ p ] ->vertex[ 2 ];
362 xx1 = ( float ) x * fr[ framecounter ] ->scale[ 0 ];
363 yy1 = ( float ) y * fr[ framecounter ] ->scale[ 1 ];
364 zz1 = ( float ) z * fr[ framecounter ] ->scale[ 2 ];
366 xx1 = xx1 + fr[ framecounter ] ->translate[ 0 ];
367 yy1 = yy1 + fr[ framecounter ] ->translate[ 1 ];
368 zz1 = zz1 + fr[ framecounter ] ->translate[ 2 ];
370 nx2 = normalVectorArray[ fr[ framecounter ] ->tr[ p ] ->lightNormalIndex ].nx;
371 ny2 = normalVectorArray[ fr[ framecounter ] ->tr[ p ] ->lightNormalIndex ].ny;
372 nz2 = normalVectorArray[ fr[ framecounter ] ->tr[ p ] ->lightNormalIndex ].nz;
375 p = fc[ i ].tindex[ k ];
377 v[ 12 * j] = tc[ p ].u;
378 v[ 12 * j + 1] = 0.0 - tc[ p ].v;
380 v[ 12 * j + 2] = 1.0;
381 v[ 12 * j + 3] = 1.0;
382 v[ 12 * j + 4] = 1.0;
383 v[ 12 * j + 5] = 1.0;
385 v[ 12 * j + 6] = nx1 + t * ( nx2 - nx1 );
386 v[ 12 * j + 7] = nz1 + t * ( nz2 - nz1 );
387 v[ 12 * j + 8] = ny1 + t * ( ny2 - ny1 );
389 v[ 12 * j + 9] = xx + t * ( xx1 - xx );
390 v[ 12 * j + 10] = zz + t * ( zz1 - zz );
391 v[ 12 * j + 11] = yy + t * ( yy1 - yy );
399 // graph->renderMd2(v, (md2h.numTriangles+5)*3);
400 graph->setTexture( modelTexture );
401 unsigned int numOfTris = ( md2h.numTriangles + 5 ) * 3;
402 // if ( graph->getAlpha() )
403 // glDisable( GL_DEPTH_TEST );
405 //graph->setAlpha(true);
408 // graph->renderTriangles(AM_TRIANGLES, v, numOfTris);
409 graph->renderVertexArray((vertex *)v, numOfTris, AM_TRIANGLES);
412 //if ( graph->getAlpha() )
413 // glEnable( GL_DEPTH_TEST );
414 // glDisable(GL_DEPTH_TEST);
425 void model::delframelist(void)
436 if (fhead->next==fhead)
446 if (tmp==fhead) break;
463 void model::setframesequence(char *name)
473 for (i=0;i<md2h.numFrames;i++)
475 if (strstr(fr[i]->name,name)!=NULL)
478 for (j=0; j < md2h.numTriangles; j++)
484 int p=fc[j].vindex[k];
485 x=fr[i]->tr[p]->vertex[0]*fr[i]->scale[0]+fr[i]->translate[0];
486 y=fr[i]->tr[p]->vertex[1]*fr[i]->scale[1]+fr[i]->translate[1];
487 z=fr[i]->tr[p]->vertex[2]*fr[i]->scale[2]+fr[i]->translate[2];
497 tmp=(fnode *)malloc(sizeof(fnode));
504 current_offset=tmp->fnumber;
522 current_frame_sequence=name;
531 int model::getframecounter( void )
538 c_model::c_model( model *m )
549 void c_model::setFrameSequence( char *name )
556 xmin = 0;ymin = 0;zmin = 0;
557 xmax = 0;ymax = 0;zmax = 0;
560 for ( i = 0;i < mod->md2h.numFrames;i++ )
562 if ( strstr( mod->fr[ i ] ->name, name ) != NULL )
564 tmp = ( fnode * ) malloc( sizeof( fnode ) );
578 for ( int j = 0;j < mod->md2h.numTriangles;j++ )
581 for ( int k = 0;k < 3;k++ )
583 int p = mod->fc[ j ].vindex[ k ];
586 x = mod->fr[ i ] ->tr[ p ] ->vertex[ 0 ] * mod->fr[ i ] ->scale[ 0 ] + mod->fr[ i ] ->translate[ 0 ];
589 y = mod->fr[ i ] ->tr[ p ] ->vertex[ 2 ] * mod->fr[ i ] ->scale[ 2 ] + mod->fr[ i ] ->translate[ 2 ];
592 z = mod->fr[ i ] ->tr[ p ] ->vertex[ 1 ] * mod->fr[ i ] ->scale[ 1 ] + mod->fr[ i ] ->translate[ 1 ];
593 if ( x < xmin ) xmin = x;
594 if ( x > xmax ) xmax = x;
595 if ( y < ymin ) ymin = y;
596 if ( y > ymax ) ymax = y;
597 if ( z < zmin ) zmin = z;
598 if ( z > zmax ) zmax = z;
610 current_frame_sequence = name;
617 void c_model::delFrameSequence( void )
628 if ( fhead->next == fhead )
638 if ( tmp == fhead ) break;
652 void c_model::render()
656 if ( oldt > t ) cpointer = cpointer->next;
659 mod->cpointer = cpointer;
666 float c_model::gettime( void )
668 int k = SDL_GetTicks();
669 k = k % ( ( int ) ( FPS + 1.0f ) );
670 return ( float ) k / ( float ) FPS;
674 c_model::~c_model( void )