summaryrefslogtreecommitdiffstats
path: root/src/video/model.c
blob: 675820e2f6c6c3ad67e2ff24f14d9f88d1fccc7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// include a few datastructures & constants

#include "video/model.h"
#include "video/video.h"

#include "math.h"
#include "stdio.h"
#include "stdlib.h"
#include "zlib.h"

extern void readMaterialLibrary(char *buf, Mesh *pMesh);
extern void setMaterial(char *buf, Mesh *pMesh, int *iGroup);

void readVector(char *buf, vec3 *pVertex ) {
  if( sscanf(buf, " %f %f %f ", 
	     pVertex->v, pVertex->v + 1, pVertex->v + 2) != 3) {
    fprintf(stderr, "*** failed reading Vector from %s\n", buf);
  }
}

void readTriFace(char *buf, face *pFace, int *iFace, int iGroup) {
  // assumes model in "f %d//%d %d//%d %d//%d\n" format
  if(
     sscanf(buf, "f %d//%d %d//%d %d//%d ",
	    pFace[ *iFace ].vertex + 0, pFace[ *iFace ].normal + 0,
	    pFace[ *iFace ].vertex + 1, pFace[ *iFace ].normal + 1,
	    pFace[ *iFace ].vertex + 2, pFace[ *iFace ].normal + 2) == 6) {
    pFace[ *iFace ].material = iGroup;
    (*iFace)++;
  } else {
    fprintf(stderr, "*** failed parsing face %s\n", buf);
  }
}

void readQuadFace(char *buf, quadFace *pFace, int *iFace, int iGroup) {
// assumes model in "f %d//%d %d//%d %d//%d %d//%d\n" format
  if(
     sscanf(buf, "f %d//%d %d//%d %d//%d %d//%d",
            pFace[ *iFace ].vertex + 0, pFace[ *iFace ].normal + 0,
            pFace[ *iFace ].vertex + 1, pFace[ *iFace ].normal + 1,
            pFace[ *iFace ].vertex + 2, pFace[ *iFace ].normal + 2,
            pFace[ *iFace ].vertex + 3, pFace[ *iFace ].normal + 3) == 8) {
    pFace[ *iFace ].material = iGroup;
    (*iFace)++;
  } else {
    fprintf(stderr, "*** failed parsing face %s\n", buf);
  }
}

Mesh* readMeshFromFile(const char *filename, MeshType iType) {
  // allocate some buffers
  // vertices, normals

  vec3 *pVertices = malloc( sizeof(vec3) * MAX_VERTICES );
  vec3 *pNormals = malloc( sizeof(vec3) * MAX_NORMALS );

  quadFace *pqFaces = NULL;
  face *pFaces = NULL;
  int iFaceSize = 0;

  Mesh *pMesh = malloc( sizeof(Mesh) );
  int iGroup = 0;
  int iVertex = 0, iNormal = 0, iFace = 0;

  gzFile f;
  char buf[BUF_SIZE];

  int i, j, k;


  switch(iType) {
  case TRI_MESH:
    pFaces = malloc( sizeof(face) * MAX_FACES );
    iFaceSize = 3;
    break;
  case QUAD_MESH:
    pqFaces = malloc( sizeof(quadFace) * MAX_FACES );
    iFaceSize = 4;
    break;
  default:
    fprintf(stderr, "[fatal]: illegal mesh type\n");
    exit(1);
  }

  if((f = gzopen(filename, "r")) == 0) {
    fprintf(stderr, "*** could not open file '%s'\n", filename);
    return NULL;
  }

  while( gzgets(f, buf, sizeof(buf)) ) {
    switch(buf[0]) {
    case 'm': // material library
      readMaterialLibrary(buf, pMesh);
      break;
    case 'u': // material  name
      setMaterial(buf, pMesh, &iGroup);
      break;
    case 'v': // vertex, normal, texture coordinate
      switch(buf[1]) {
      case ' ':
	readVector(buf + 1, pVertices + iVertex);
	iVertex++;
	break;
      case 'n': // normal
	readVector(buf + 2, pNormals + iNormal);
	iNormal++;
	break;
      case 't': // texture
	break; // ignore textures;
      }
      break;
    case 'f': // face (can produce multiple faces)
      switch(iType) {
      case TRI_MESH:
	readTriFace(buf, pFaces, &iFace, iGroup);
	break;
      case QUAD_MESH:
	readQuadFace(buf, pqFaces, &iFace, iGroup);
	break;
      }
      break;
    }
  }
  
  gzclose(f);

  // printf("vertices: %d, normals: %d, faces: %d\n", iVertex, iNormal, iFace);

  // count each material
  pMesh->pnFaces = malloc( sizeof(int) * pMesh->nMaterials );
  for(i = 0; i < pMesh->nMaterials; i++) {
    pMesh->pnFaces[ i ] = 0;
  }

  switch(iType) {
  case TRI_MESH:
    for(i = 0; i < iFace; i++) {
      pMesh->pnFaces[ pFaces[i].material ] += 1;
    }
    break;
  case QUAD_MESH:
    for(i = 0; i < iFace; i++) {
      pMesh->pnFaces[ pqFaces[i].material ] += 1;
    }
    break;
  }


  // combine vectors & normals for each vertex, doubling where necessary

  // initialize lookup[ vertex ][ normal ] table
  {
    int nVertices = 0;
    int **lookup = malloc( sizeof(int*) * iVertex );

    for(i = 0; i < iVertex; i++) {
      lookup[i] = malloc( sizeof(int) * iNormal );
      for(j = 0; j < iNormal; j++) {
	lookup[i][j] = -1;
      }
    }
  
    switch(iType) {
    case TRI_MESH:
      for(i = 0; i < iFace; i++) {
	for(j = 0; j < iFaceSize; j++) {
	  int vertex = pFaces[i].vertex[j] - 1;
	  int normal = pFaces[i].normal[j] - 1;
	  if( lookup[ vertex ][ normal ] == -1 ) {
	    lookup[ vertex ][ normal ] = nVertices;
	    nVertices++;
	  }
	}
      }
      break;
    case QUAD_MESH:
      for(i = 0; i < iFace; i++) {
	for(j = 0; j < iFaceSize; j++) {
	  int vertex = pqFaces[i].vertex[j] - 1;
	  int normal = pqFaces[i].normal[j] - 1;
	  if( lookup[ vertex ][ normal ] == -1 ) {
	    lookup[ vertex ][ normal ] = nVertices;
	    nVertices++;
	  }
	}
      }
      break;
    }
  
    // now that we know everything, build vertexarray based mesh
    // copy normals & vertices indexed by lookup-table
    pMesh->nVertices = nVertices;
    pMesh->pVertices = malloc( sizeof(GLfloat) * 3 * nVertices );
    pMesh->pNormals = malloc( sizeof(GLfloat) * 3 * nVertices );
    for(i = 0; i < iVertex; i++) {
      for(j = 0; j < iNormal; j++) {
	int vertex = lookup[ i ][ j ];
	if(vertex != -1 ) {
	  for(k = 0; k < 3; k++) {
	    *(pMesh->pVertices + 3 * vertex + k) = pVertices[ i ].v[k];
	    *(pMesh->pNormals + 3 * vertex + k) = pNormals[ j ].v[k];
	  }
	}
      }
    }

    // build indices (per Material)
    {
      int *face;
      face = malloc( sizeof(int) * pMesh->nMaterials );
      pMesh->ppIndices = malloc( sizeof(GLshort*) * pMesh->nMaterials );
      for(i = 0; i < pMesh->nMaterials; i++) {
	pMesh->ppIndices[i] = 
	  malloc( sizeof(GLshort) * iFaceSize * pMesh->pnFaces[i] );
	face[i] = 0;
      }
      switch(iType) {
      case TRI_MESH:
	for(i = 0; i < iFace; i++) {
	  int material = pFaces[i].material;
	  for(j = 0; j < iFaceSize; j++) {
	    int vertex = pFaces[i].vertex[j] - 1;
	    int normal = pFaces[i].normal[j] - 1;
	    pMesh->ppIndices[ material ][ iFaceSize * face[ material ] + j ] = 
	      lookup[ vertex ][ normal ];
	  }
	  face[ material ] = face[ material] + 1;
	}
	break;
      case QUAD_MESH:
	for(i = 0; i < iFace; i++) {
	  int material = pqFaces[i].material;
	  for(j = 0; j < iFaceSize; j++) {
	    int vertex = pqFaces[i].vertex[j] - 1;
	    int normal = pqFaces[i].normal[j] - 1;
	    pMesh->ppIndices[ material ][ iFaceSize * face[ material ] + j ] = 
	      lookup[ vertex ][ normal ];
	  }
	  face[ material ] = face[ material] + 1;
	}
	break;
      }
      free(face);
    }
    // printf("[scenegraph] vertices: %d, faces: %d\n", nVertices, iFace);

    free(lookup);
    free(pVertices);
    free(pNormals);
    switch(iType) {
    case TRI_MESH:
      free(pFaces);
      break;
    case QUAD_MESH:
      free(pqFaces);
      break;
    }
  }
  computeBBox(pMesh);

  return pMesh;
}

void drawModel(Mesh *pMesh, MeshType iType) {
  int i;
  int iFaceSize = 0;
  GLenum primitive = GL_TRIANGLES;

  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_NORMAL_ARRAY);
  glVertexPointer(3, GL_FLOAT, 0, pMesh->pVertices);
  glNormalPointer(GL_FLOAT, 0, pMesh->pNormals);

  switch(iType) {
  case TRI_MESH:
    primitive = GL_TRIANGLES;
    iFaceSize = 3;
    break;
  case QUAD_MESH:
    primitive = GL_QUADS;
    iFaceSize = 4;
    break;
  default:
    fprintf(stderr, "[fatal]: illegal mesh type\n");
    exit(1);
  }

  for(i = 0; i < pMesh->nMaterials; i++) {
    glMaterialfv(GL_FRONT, GL_AMBIENT,
		 pMesh->pMaterials[i].ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,
		 pMesh->pMaterials[i].diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,
		 pMesh->pMaterials[i].specular);
    glMaterialf(GL_FRONT, GL_SHININESS,
		pMesh->pMaterials[i].shininess);

    glDrawElements(primitive, iFaceSize * pMesh->pnFaces[i],
		   GL_UNSIGNED_SHORT, pMesh->ppIndices[i]);

    polycount += pMesh->pnFaces[i];
  }

}

void drawModelExplosion(Mesh *pMesh, float fRadius) {
  int i, j, k;
#define EXP_VECTORS 10
  float vectors[][3] = {
    { 0.03f, -0.06f, -0.07f }, 
    { 0.04f, 0.08f, -0.03f }, 
    { 0.10f, -0.04f, -0.07f }, 
    { 0.06f, -0.09f, -0.10f }, 
    { -0.03f, -0.05f, 0.02f }, 
    { 0.07f, 0.08f, -0.00f }, 
    { 0.01f, -0.04f, 0.10f }, 
    { -0.01f, -0.07f, 0.09f }, 
    { 0.01f, -0.01f, -0.09f }, 
    { -0.04f, 0.04f, 0.02f }
  };

  for(i = 0; i < pMesh->nMaterials; i++) {
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT,
        pMesh->pMaterials[i].ambient);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,
        pMesh->pMaterials[i].diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,
        pMesh->pMaterials[i].specular);
    glMaterialf(GL_FRONT, GL_SHININESS,
        pMesh->pMaterials[i].shininess);

    for(j = 0; j < pMesh->pnFaces[i]; j++) {

      float *normal, *vertex;

      normal = pMesh->pNormals + 3 * pMesh->ppIndices[i][3 * j];

      glPushMatrix();
      glTranslatef(fRadius * (*(normal + 0) + vectors[j % EXP_VECTORS][0]),
          fRadius * (*(normal + 1) + vectors[j % EXP_VECTORS][1]),
          fabsf(fRadius * (*(normal + 2) + vectors[j % EXP_VECTORS][2]) ));
      glBegin(GL_TRIANGLES);
      for(k = 0; k < 3; k++) {
        normal = pMesh->pNormals + 3 * pMesh->ppIndices[i][3 * j + k];
        vertex = pMesh->pVertices + 3 * pMesh->ppIndices[i][3 * j + k];

        glNormal3fv(normal);
        glVertex3fv(vertex);
      }
      glEnd();
      glPopMatrix();
    }
    polycount += pMesh->pnFaces[i];
  }
}

void computeBBox(Mesh *pMesh) {
  int i, j;
  vec3 vMin, vMax, vSize;

  vcopy(pMesh->pVertices, vMin.v);
  vcopy(pMesh->pVertices, vMax.v);

  for(i = 0; i < pMesh->nVertices; i++) {
    for(j = 0; j < 3; j++) {
      if(vMin.v[j] > pMesh->pVertices[3 * i + j])
	vMin.v[j] = pMesh->pVertices[3 * i + j];
      if(vMax.v[j] < pMesh->pVertices[3 * i + j])
	vMax.v[j] = pMesh->pVertices[3 * i + j];
    }
    /*
    if(
       vMin.v[0] <= pMesh->pVertices[3 * i + 0] &&
       vMin.v[1] <= pMesh->pVertices[3 * i + 1] &&
       vMin.v[2] <= pMesh->pVertices[3 * i + 2]
       )
      vcopy(pMesh->pVertices + 3 * i, vMin.v);
    if(
       vMax.v[0] >= pMesh->pVertices[3 * i + 0] &&
       vMax.v[1] >= pMesh->pVertices[3 * i + 1] &&
       vMax.v[2] >= pMesh->pVertices[3 * i + 2]
       )
      vcopy(pMesh->pVertices + 3 * i, vMax.v);
    */
  }
  
  vsub(vMax.v, vMin.v, vSize.v);
  vcopy(vMin.v, pMesh->BBox.vMin.v);
  vcopy(vSize.v, pMesh->BBox.vSize.v);
  pMesh->BBox.fRadius=length(pMesh->BBox.vSize.v)/10;
}