md2.cpp
Upload User: xuczgm
Upload Date: 2022-04-25
Package Size: 8601k
Code Size: 24k
Category:

Special Effects

Development Platform:

Visual C++

  1. /*/
  2. //                 MD2 Viewer (c) 1999 by Mete Ciragan
  3. //
  4. // file:           md2.c
  5. // last modified:  Apr 28 1999, Mete Ciragan
  6. // copyright:      The programs and associated files contained in this
  7. //                 distribution were developed by Mete Ciragan. The programs
  8. //                 are not in the public domain, but they are freely
  9. //                 distributable without licensing fees. These programs are
  10. //                 provided without guarantee or warrantee expressed or
  11. //                 implied.
  12. //
  13. // version:        1.4
  14. //
  15. // email:          mete@swissquake.ch
  16. // web:            http://www.swissquake.ch/chumbalum-soft/
  17. /*/
  18. #include "stdafx.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h> /* memset */
  22. #include <math.h> /* sqrt */
  23. //#include <mx/gl.h>
  24. #include <windows.h>
  25. #include <gl/gl.h>
  26. #include "md2.h"
  27. #define NUMVERTEXNORMALS 162
  28. float avertexnormals[NUMVERTEXNORMALS][3] = {
  29. #include "anorms.h"
  30. };
  31. static int g_glcmds = 1; /* use glcommands */
  32. static int g_interp = 1; /* interpolate frames */
  33. /*
  34.  * load model
  35.  */
  36. md2_model_t*
  37. md2_readModel (const char *filename)
  38. {
  39. FILE *file;
  40. md2_model_t *model;
  41. byte buffer[MD2_MAX_FRAMESIZE];
  42. int i;
  43. model = (md2_model_t *) malloc (sizeof (md2_model_t));
  44. if (!model)
  45. return 0;
  46. file = fopen (filename, "rb");
  47. if (!file)
  48. {
  49. free (model);
  50. return 0;
  51. }
  52. /* initialize model and read header */
  53. memset (model, 0, sizeof (md2_model_t));
  54. fread (&model->header, sizeof (md2_header_t), 1, file);
  55. #if 0
  56. printf ("magic:tt%dn", model->header.magic);
  57. printf ("version:tt%dn", model->header.version);
  58. printf ("skinWidth:tt%dn", model->header.skinWidth);
  59. printf ("skinHeight:tt%dn", model->header.skinHeight);
  60. printf ("frameSize:tt%dn", model->header.frameSize);
  61. printf ("numSkins:tt%dn", model->header.numSkins);
  62. printf ("numVertices:tt%dn", model->header.numVertices);
  63. printf ("numTexCoords:tt%dn", model->header.numTexCoords);
  64. printf ("numTriangles:tt%dn", model->header.numTriangles);
  65. printf ("numGlCommands:tt%dn", model->header.numGlCommands);
  66. printf ("numFrames:tt%dn", model->header.numFrames);
  67. printf ("offsetSkins:tt%dn", model->header.offsetSkins);
  68. printf ("offsetTexCoords:t%dn", model->header.offsetTexCoords);
  69. printf ("offsetTriangles:t%dn", model->header.offsetTriangles);
  70. printf ("offsetFrames:tt%dn", model->header.offsetFrames);
  71. printf ("offsetGlCommands:t%dn", model->header.offsetGlCommands);
  72. printf ("offsetEnd:tt%dn", model->header.offsetEnd);
  73. #endif
  74. if (model->header.magic != (int) (('2' << 24) + ('P' << 16) + ('D' << 8) + 'I'))
  75. {
  76. fclose (file);
  77. free (model);
  78. return 0;
  79. }
  80. /* read skins */
  81. fseek (file, model->header.offsetSkins, SEEK_SET);
  82. if (model->header.numSkins > 0)
  83. {
  84. model->skins = (md2_skin_t *) malloc (sizeof (md2_skin_t) * model->header.numSkins);
  85. if (!model->skins)
  86. {
  87. md2_freeModel (model);
  88. return 0;
  89. }
  90. for (i = 0; i < model->header.numSkins; i++)
  91. fread (&model->skins[i], sizeof (md2_skin_t), 1, file);
  92. }
  93. /* read texture coordinates */
  94. fseek (file, model->header.offsetTexCoords, SEEK_SET);
  95. if (model->header.numTexCoords > 0)
  96. {
  97. model->texCoords = (md2_textureCoordinate_t *) malloc (sizeof (md2_textureCoordinate_t) * model->header.numTexCoords);
  98. if (!model->texCoords)
  99. {
  100. md2_freeModel (model);
  101. return 0;
  102. }
  103. for (i = 0; i < model->header.numTexCoords; i++)
  104. fread (&model->texCoords[i], sizeof (md2_textureCoordinate_t), 1, file);
  105. }
  106. /* read triangles */
  107. fseek (file, model->header.offsetTriangles, SEEK_SET);
  108. if (model->header.numTriangles > 0)
  109. {
  110. model->triangles = (md2_triangle_t *) malloc (sizeof (md2_triangle_t) * model->header.numTriangles);
  111. if (!model->triangles)
  112. {
  113. md2_freeModel (model);
  114. return 0;
  115. }
  116. for (i = 0; i < model->header.numTriangles; i++)
  117. fread (&model->triangles[i], sizeof (md2_triangle_t), 1, file);
  118. }
  119. /* read alias frames */
  120. fseek (file, model->header.offsetFrames, SEEK_SET);
  121. if (model->header.numFrames > 0)
  122. {
  123. model->frames = (md2_frame_t *) malloc (sizeof (md2_frame_t) * model->header.numFrames);
  124. if (!model->frames)
  125. {
  126. md2_freeModel (model);
  127. return 0;
  128. }
  129. for (i = 0; i < model->header.numFrames; i++)
  130. {
  131. md2_alias_frame_t *frame = (md2_alias_frame_t *) buffer;
  132. int j;
  133. model->frames[i].vertices = (md2_triangleVertex_t *) malloc (sizeof (md2_triangleVertex_t) * model->header.numVertices);
  134. if (!model->frames[i].vertices)
  135. {
  136. md2_freeModel (model);
  137. return 0;
  138. }
  139. fread (frame, 1, model->header.frameSize, file);
  140. strcpy (model->frames[i].name, frame->name);
  141. for (j = 0; j < model->header.numVertices; j++)
  142. {
  143. model->frames[i].vertices[j].vertex[0] = (float) ((int) frame->alias_vertices[j].vertex[0]) * frame->scale[0] + frame->translate[0];
  144. model->frames[i].vertices[j].vertex[2] = -1* ((float) ((int) frame->alias_vertices[j].vertex[1]) * frame->scale[1] + frame->translate[1]);
  145. model->frames[i].vertices[j].vertex[1] = (float) ((int) frame->alias_vertices[j].vertex[2]) * frame->scale[2] + frame->translate[2];
  146. model->frames[i].vertices[j].normal[0] = avertexnormals[frame->alias_vertices[j].lightNormalIndex][0];
  147. model->frames[i].vertices[j].normal[1] = avertexnormals[frame->alias_vertices[j].lightNormalIndex][1];
  148. model->frames[i].vertices[j].normal[2] = avertexnormals[frame->alias_vertices[j].lightNormalIndex][2];
  149. //model->frames[i].vertices[j].lightNormalIndex = frame->alias_vertices[j].lightNormalIndex;
  150. }
  151. }
  152. }
  153. /* read gl commands */
  154. fseek (file, model->header.offsetGlCommands, SEEK_SET);
  155. if (model->header.numGlCommands)
  156. {
  157. model->glCommandBuffer = (int *) malloc (sizeof (int) * model->header.numGlCommands);
  158. if (!model->glCommandBuffer)
  159. {
  160. md2_freeModel (model);
  161. return 0;
  162. }
  163. fread (model->glCommandBuffer, sizeof (int), model->header.numGlCommands, file);
  164. }
  165. fclose (file);
  166. return model;
  167. }
  168. /*
  169.  * free model
  170.  */
  171. void
  172. md2_freeModel (md2_model_t *model)
  173. {
  174. if (model)
  175. {
  176. if (model->skins)
  177. free (model->skins);
  178. if (model->texCoords)
  179. free (model->texCoords);
  180. if (model->triangles)
  181. free (model->triangles);
  182. if (model->frames)
  183. {
  184. int i;
  185. for (i = 0; i < model->header.numFrames; i++)
  186. {
  187. if (model->frames[i].vertices)
  188. free (model->frames[i].vertices);
  189. }
  190. free (model->frames);
  191. }
  192. if (model->glCommandBuffer)
  193. free (model->glCommandBuffer);
  194. free (model);
  195. }
  196. }
  197. /*
  198.  * set draw style, either with glcommands, interpolated
  199.  */
  200. void
  201. md2_setStyle (int glcmds, int interp)
  202. {
  203. g_glcmds = glcmds;
  204. g_interp = interp;
  205. }
  206. /*
  207.  * center model
  208.  *
  209.  */
  210. void
  211. md2_getBoundingBox (md2_model_t *model, float *minmax)
  212. {
  213. int i;
  214. float minx, maxx;
  215. float miny, maxy;
  216. float minz, maxz;
  217. minx = miny = minz = 999999.0f;
  218. maxx = maxy = maxz = -999999.0f;
  219. /* get bounding box */
  220. for (i = 0; i < model->header.numVertices; i++)
  221. {
  222. md2_triangleVertex_t *v = &model->frames[0].vertices[i];
  223. if (v->vertex[0] < minx)
  224. minx = v->vertex[0];
  225. else if (v->vertex[0] > maxx)
  226. maxx = v->vertex[0];
  227. if (v->vertex[1] < miny)
  228. miny = v->vertex[1];
  229. else if (v->vertex[1] > maxy)
  230. maxy = v->vertex[1];
  231. if (v->vertex[2] < minz)
  232. minz = v->vertex[2];
  233. else if (v->vertex[2] > maxz)
  234. maxz = v->vertex[2];
  235. }
  236. minmax[0] = minx;
  237. minmax[1] = maxx;
  238. minmax[2] = miny;
  239. minmax[3] = maxy;
  240. minmax[4] = minz;
  241. minmax[5] = maxz;
  242. }
  243. /*
  244.  * draw normal
  245.  *
  246.  */
  247. void
  248. _md2_drawModel (md2_model_t *model, int frame)
  249. {
  250. int i;
  251. md2_frame_t *f = &model->frames[frame];
  252. glBegin (GL_TRIANGLES);
  253. for (i = 0; i < model->header.numTriangles; i++)
  254. {
  255. md2_triangle_t *t = &model->triangles[i];
  256. //int lightNormalIndex;
  257. /*
  258. lightNormalIndex = f->vertices[t->vertexIndices[0]].lightNormalIndex;
  259. glNormal3f (avertexnormals[lightNormalIndex][0],
  260. avertexnormals[lightNormalIndex][2],
  261. -avertexnormals[lightNormalIndex][1]);
  262. */
  263. glNormal3f (f->vertices[t->vertexIndices[0]].normal[0],
  264. f->vertices[t->vertexIndices[0]].normal[2],
  265. -f->vertices[t->vertexIndices[0]].normal[1]);
  266. glTexCoord2f ((float) model->texCoords[t->textureIndices[0]].s / (float) model->header.skinWidth,
  267. (float) model->texCoords[t->textureIndices[0]].t / (float) model->header.skinHeight);
  268. glVertex3f (f->vertices[t->vertexIndices[0]].vertex[0],
  269. f->vertices[t->vertexIndices[0]].vertex[1],
  270. f->vertices[t->vertexIndices[0]].vertex[2]);
  271. /*
  272. lightNormalIndex = f->vertices[t->vertexIndices[1]].lightNormalIndex;
  273. glNormal3f (avertexnormals[lightNormalIndex][0],
  274. avertexnormals[lightNormalIndex][2],
  275. -avertexnormals[lightNormalIndex][1]);
  276. */
  277. glNormal3f (f->vertices[t->vertexIndices[1]].normal[0],
  278. f->vertices[t->vertexIndices[1]].normal[2],
  279. -f->vertices[t->vertexIndices[1]].normal[1]);
  280. glTexCoord2f ((float) model->texCoords[t->textureIndices[1]].s / (float) model->header.skinWidth,
  281. (float) model->texCoords[t->textureIndices[1]].t / (float) model->header.skinHeight);
  282. glVertex3f (f->vertices[t->vertexIndices[1]].vertex[0],
  283. f->vertices[t->vertexIndices[1]].vertex[1],
  284. f->vertices[t->vertexIndices[1]].vertex[2]);
  285. /*
  286. lightNormalIndex = f->vertices[t->vertexIndices[2]].lightNormalIndex;
  287. glNormal3f (avertexnormals[lightNormalIndex][0],
  288. avertexnormals[lightNormalIndex][2],
  289. -avertexnormals[lightNormalIndex][1]);
  290. */
  291. glNormal3f (f->vertices[t->vertexIndices[2]].normal[0],
  292. f->vertices[t->vertexIndices[2]].normal[2],
  293. -f->vertices[t->vertexIndices[2]].normal[1]);
  294. glTexCoord2f ((float) model->texCoords[t->textureIndices[2]].s / (float) model->header.skinWidth,
  295. (float) model->texCoords[t->textureIndices[2]].t / (float) model->header.skinHeight);
  296. glVertex3f (f->vertices[t->vertexIndices[2]].vertex[0],
  297. f->vertices[t->vertexIndices[2]].vertex[1],
  298. f->vertices[t->vertexIndices[2]].vertex[2]);
  299. }
  300. glEnd ();
  301. }
  302. /*
  303.  * draw interpolated
  304.  *
  305.  */
  306. void
  307. _md2_drawModeli (md2_model_t *model, int frame1, int frame2, float pol)
  308. {
  309. int i;
  310. md2_frame_t *f1 = &model->frames[frame1];
  311. md2_frame_t *f2 = &model->frames[frame2];
  312. glBegin (GL_TRIANGLES);
  313. for (i = 0; i < model->header.numTriangles; i++)
  314. {
  315. md2_triangle_t *t = &model->triangles[i];
  316. //int lightNormalIndex1, lightNormalIndex2;
  317. float x1, y1, z1, x2, y2, z2;
  318. float *n1, *n2;
  319. glTexCoord2f ((float) model->texCoords[t->textureIndices[0]].s / (float) model->header.skinWidth,
  320. (float) model->texCoords[t->textureIndices[0]].t / (float) model->header.skinHeight);
  321. /*
  322. lightNormalIndex1 = f1->vertices[t->vertexIndices[0]].lightNormalIndex;
  323. lightNormalIndex2 = f2->vertices[t->vertexIndices[0]].lightNormalIndex;
  324. glNormal3f ((1.0f - pol) * avertexnormals[lightNormalIndex1][0] + pol * avertexnormals[lightNormalIndex2][0],
  325. (1.0f - pol) * avertexnormals[lightNormalIndex1][2] + pol * avertexnormals[lightNormalIndex2][2],
  326. (1.0f - pol) * -avertexnormals[lightNormalIndex1][1] + pol * -avertexnormals[lightNormalIndex2][1]);
  327. */
  328. n1 = f1->vertices[t->vertexIndices[0]].normal;
  329. n2 = f2->vertices[t->vertexIndices[0]].normal;
  330. glNormal3f ((1.0f - pol) * n1[0] + pol * n2[0],
  331. (1.0f - pol) * n1[2] + pol * n2[2],
  332. (1.0f - pol) * -n1[1] + pol * -n2[1]);
  333. x1 = f1->vertices[t->vertexIndices[0]].vertex[0];
  334. y1 = f1->vertices[t->vertexIndices[0]].vertex[1];
  335. z1 = f1->vertices[t->vertexIndices[0]].vertex[2];
  336. x2 = f2->vertices[t->vertexIndices[0]].vertex[0];
  337. y2 = f2->vertices[t->vertexIndices[0]].vertex[1];
  338. z2 = f2->vertices[t->vertexIndices[0]].vertex[2];
  339. glVertex3f (x1 + pol * (x2 - x1),
  340. y1 + pol * (y2 - y1),
  341. z1 + pol * (z2 - z1));
  342. glTexCoord2f ((float) model->texCoords[t->textureIndices[1]].s / (float) model->header.skinWidth,
  343. (float) model->texCoords[t->textureIndices[1]].t / (float) model->header.skinHeight);
  344. /*
  345. lightNormalIndex1 = f1->vertices[t->vertexIndices[1]].lightNormalIndex;
  346. lightNormalIndex2 = f2->vertices[t->vertexIndices[1]].lightNormalIndex;
  347. glNormal3f ((1.0f - pol) * avertexnormals[lightNormalIndex1][0] + pol * avertexnormals[lightNormalIndex2][0],
  348. (1.0f - pol) * avertexnormals[lightNormalIndex1][2] + pol * avertexnormals[lightNormalIndex2][2],
  349. (1.0f - pol) * -avertexnormals[lightNormalIndex1][1] + pol * -avertexnormals[lightNormalIndex2][1]);
  350. */
  351. n1 = f1->vertices[t->vertexIndices[1]].normal;
  352. n2 = f2->vertices[t->vertexIndices[1]].normal;
  353. glNormal3f ((1.0f - pol) * n1[0] + pol * n2[0],
  354. (1.0f - pol) * n1[2] + pol * n2[2],
  355. (1.0f - pol) * -n1[1] + pol * -n2[1]);
  356. x1 = f1->vertices[t->vertexIndices[1]].vertex[0];
  357. y1 = f1->vertices[t->vertexIndices[1]].vertex[1];
  358. z1 = f1->vertices[t->vertexIndices[1]].vertex[2];
  359. x2 = f2->vertices[t->vertexIndices[1]].vertex[0];
  360. y2 = f2->vertices[t->vertexIndices[1]].vertex[1];
  361. z2 = f2->vertices[t->vertexIndices[1]].vertex[2];
  362. glVertex3f (x1 + pol * (x2 - x1),
  363. y1 + pol * (y2 - y1),
  364. z1 + pol * (z2 - z1));
  365. glTexCoord2f ((float) model->texCoords[t->textureIndices[2]].s / (float) model->header.skinWidth,
  366. (float) model->texCoords[t->textureIndices[2]].t / (float) model->header.skinHeight);
  367. /*
  368. lightNormalIndex1 = f1->vertices[t->vertexIndices[2]].lightNormalIndex;
  369. lightNormalIndex2 = f2->vertices[t->vertexIndices[2]].lightNormalIndex;
  370. glNormal3f ((1.0f - pol) * avertexnormals[lightNormalIndex1][0] + pol * avertexnormals[lightNormalIndex2][0],
  371. (1.0f - pol) * avertexnormals[lightNormalIndex1][2] + pol * avertexnormals[lightNormalIndex2][2],
  372. (1.0f - pol) * -avertexnormals[lightNormalIndex1][1] + pol * -avertexnormals[lightNormalIndex2][1]);
  373. */
  374. n1 = f1->vertices[t->vertexIndices[2]].normal;
  375. n2 = f2->vertices[t->vertexIndices[2]].normal;
  376. glNormal3f ((1.0f - pol) * n1[0] + pol * n2[0],
  377. (1.0f - pol) * n1[2] + pol * n2[2],
  378. (1.0f - pol) * -n1[1] + pol * -n2[1]);
  379. x1 = f1->vertices[t->vertexIndices[2]].vertex[0];
  380. y1 = f1->vertices[t->vertexIndices[2]].vertex[1];
  381. z1 = f1->vertices[t->vertexIndices[2]].vertex[2];
  382. x2 = f2->vertices[t->vertexIndices[2]].vertex[0];
  383. y2 = f2->vertices[t->vertexIndices[2]].vertex[1];
  384. z2 = f2->vertices[t->vertexIndices[2]].vertex[2];
  385. glVertex3f (x1 + pol * (x2 - x1),
  386. y1 + pol * (y2 - y1),
  387. z1 + pol * (z2 - z1));
  388. }
  389. glEnd ();
  390. }
  391. /*
  392.  * draw with glcommands
  393.  *
  394.  */
  395. void
  396. _md2_drawModelg (md2_model_t *model, int frame)
  397. {
  398. int i = 0;
  399. int val = model->glCommandBuffer[i++];
  400. while (val != 0)
  401. {
  402. int count;
  403. if (val > 0)
  404. {
  405. glBegin (GL_TRIANGLE_STRIP);
  406. count = val;
  407. }
  408. else
  409. {
  410. glBegin (GL_TRIANGLE_FAN);
  411. count = -val;
  412. }
  413. while (count--)
  414. {
  415. float s = *(float *) &model->glCommandBuffer[i++];
  416. float t = *(float *) &model->glCommandBuffer[i++];
  417. int index = model->glCommandBuffer[i++];
  418. //int lightNormalIndex = model->frames[frame].vertices[index].lightNormalIndex;
  419. glTexCoord2f (s, t);
  420. /*
  421. glNormal3f (avertexnormals[lightNormalIndex][0],
  422. avertexnormals[lightNormalIndex][2],
  423. -avertexnormals[lightNormalIndex][1]);
  424. */
  425. glNormal3f (model->frames[frame].vertices[index].normal[0],
  426. model->frames[frame].vertices[index].normal[2],
  427. -model->frames[frame].vertices[index].normal[1]);
  428. glVertex3f (model->frames[frame].vertices[index].vertex[0],
  429. model->frames[frame].vertices[index].vertex[1],
  430. model->frames[frame].vertices[index].vertex[2]);
  431. }
  432. glEnd ();
  433. val = model->glCommandBuffer[i++];
  434. }
  435. }
  436. /*
  437.  * draw with glcommands, interpolated
  438.  *
  439.  */
  440. void
  441. _md2_drawModelgi (md2_model_t *model, int frame1, int frame2, float pol)
  442. {
  443. int i = 0;
  444. int val = model->glCommandBuffer[i++];
  445. while (val != 0)
  446. {
  447. int count;
  448. if (val > 0)
  449. {
  450. glBegin (GL_TRIANGLE_STRIP);
  451. count = val;
  452. }
  453. else
  454. {
  455. glBegin (GL_TRIANGLE_FAN);
  456. count = -val;
  457. }
  458. while (count--)
  459. {
  460. float s = *(float *) &model->glCommandBuffer[i++];
  461. float t = *(float *) &model->glCommandBuffer[i++];
  462. int index = model->glCommandBuffer[i++];
  463. //int lightNormalIndex1 = model->frames[frame1].vertices[index].lightNormalIndex;
  464. //int lightNormalIndex2 = model->frames[frame2].vertices[index].lightNormalIndex;
  465. float x1, y1, z1, x2, y2, z2;
  466. float *n1 = model->frames[frame1].vertices[index].normal;
  467. float *n2 = model->frames[frame2].vertices[index].normal;
  468. /* texcoords don't need to be interpolated */
  469. glTexCoord2f (s, t);
  470. /* interpolate light normal */
  471. /*
  472. glNormal3f ((1.0f - pol) * avertexnormals[lightNormalIndex1][0] + pol * avertexnormals[lightNormalIndex2][0],
  473. (1.0f - pol) * avertexnormals[lightNormalIndex1][2] + pol * avertexnormals[lightNormalIndex2][2],
  474. (1.0f - pol) * -avertexnormals[lightNormalIndex1][1] + pol * -avertexnormals[lightNormalIndex2][1]);
  475. */
  476. glNormal3f ((1.0f - pol) * n1[0] + pol * n2[0],
  477. (1.0f - pol) * n1[2] + pol * n2[2],
  478. (1.0f - pol) * -n1[1] + pol * -n2[1]);
  479. /* interpolate vertices */
  480. x1 = model->frames[frame1].vertices[index].vertex[0];
  481. y1 = model->frames[frame1].vertices[index].vertex[1];
  482. z1 = model->frames[frame1].vertices[index].vertex[2];
  483. x2 = model->frames[frame2].vertices[index].vertex[0];
  484. y2 = model->frames[frame2].vertices[index].vertex[1];
  485. z2 = model->frames[frame2].vertices[index].vertex[2];
  486. glVertex3f (x1 + pol * (x2 - x1),
  487. y1 + pol * (y2 - y1),
  488. z1 + pol * (z2 - z1));
  489. }
  490. glEnd ();
  491. val = model->glCommandBuffer[i++];
  492. }
  493. }
  494. /*
  495.  * draw model
  496.  */
  497. void
  498. md2_drawModel (md2_model_t *model, int frame1, int frame2, float pol)
  499. {
  500. if (g_glcmds)
  501. {
  502. if (g_interp)
  503. _md2_drawModelgi (model, frame1, frame2, pol);
  504. else
  505. _md2_drawModelg (model, frame1);
  506. }
  507. else
  508. {
  509. if (g_interp)
  510. _md2_drawModeli (model, frame1, frame2, pol);
  511. else
  512. _md2_drawModel (model, frame1);
  513. }
  514. }
  515. void
  516. _makeFacetNormal (md2_model_t *model, md2_triangle_t *t, int frame, float *fn)
  517. {
  518. float v1[3], v2[3];
  519. double angle;
  520. v1[0] = model->frames[frame].vertices[t->vertexIndices[1]].vertex[0] - model->frames[frame].vertices[t->vertexIndices[0]].vertex[0];
  521. v1[1] = model->frames[frame].vertices[t->vertexIndices[1]].vertex[1] - model->frames[frame].vertices[t->vertexIndices[0]].vertex[1];
  522. v1[2] = model->frames[frame].vertices[t->vertexIndices[1]].vertex[2] - model->frames[frame].vertices[t->vertexIndices[0]].vertex[2];
  523. v2[0] = model->frames[frame].vertices[t->vertexIndices[2]].vertex[0] - model->frames[frame].vertices[t->vertexIndices[0]].vertex[0];
  524. v2[1] = model->frames[frame].vertices[t->vertexIndices[2]].vertex[1] - model->frames[frame].vertices[t->vertexIndices[0]].vertex[1];
  525. v2[2] = model->frames[frame].vertices[t->vertexIndices[2]].vertex[2] - model->frames[frame].vertices[t->vertexIndices[0]].vertex[2];
  526. angle = 1;
  527. fn[0] = (v1[1] * v2[2] - v1[2] * v2[1]) * (float) angle;
  528. fn[1] = (v1[2] * v2[0] - v1[0] * v2[2]) * (float) angle;
  529. fn[2] = (v1[0] * v2[1] - v1[1] * v2[0]) * (float) angle;
  530. }
  531. void
  532. _normalize (float *n)
  533. {
  534. float l = (float) sqrt (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
  535. if (l != 0.0f)
  536. {
  537. n[0] /= l;
  538. n[1] /= l;
  539. n[2] /= l;
  540. }
  541. }
  542. void
  543. md2_generateLightNormals (md2_model_t *model)
  544. {
  545. int i;
  546. if (!model)
  547. return;
  548. for (i = 0; i < model->header.numFrames; i++)
  549. {
  550. int j;
  551. /* clear all normals */
  552. for (j = 0; j < model->header.numVertices; j++)
  553. {
  554. model->frames[i].vertices[j].normal[0] = 0.0f;
  555. model->frames[i].vertices[j].normal[1] = 0.0f;
  556. model->frames[i].vertices[j].normal[2] = 0.0f;
  557. }
  558. /* calc normals */
  559. for (j = 0; j < model->header.numTriangles; j++)
  560. {
  561. int k;
  562. float facetnormal[3];
  563. _makeFacetNormal (model, &model->triangles[j], i, facetnormal);
  564. for (k = 0; k < 3; k++)
  565. {
  566. model->frames[i].vertices[model->triangles[j].vertexIndices[k]].normal[0] -= facetnormal[0];
  567. model->frames[i].vertices[model->triangles[j].vertexIndices[k]].normal[1] += facetnormal[2];
  568. model->frames[i].vertices[model->triangles[j].vertexIndices[k]].normal[2] -= facetnormal[1];
  569. }
  570. }
  571. /* normalize normals */
  572. for (j = 0; j < model->header.numVertices; j++)
  573. _normalize (model->frames[i].vertices[j].normal);
  574. }
  575. }
  576. int
  577. md2_getAnimationCount (md2_model_t *model)
  578. {
  579. int i, j, pos;
  580. int count;
  581. int lastId;
  582. char name[16], last[16];
  583. strcpy (last, model->frames[0].name);
  584. pos = strlen (last) - 1;
  585. j = 0;
  586. while (last[pos] >= '0' && last[pos] <= '9' && j < 2)
  587. {
  588. pos--;
  589. j++;
  590. }
  591. last[pos + 1] = '';
  592. lastId = 0;
  593. count = 0;
  594. for (i = 0; i <= model->header.numFrames; i++)
  595. {
  596. if (i == model->header.numFrames)
  597. strcpy (name, ""); // some kind of a sentinel
  598. else
  599. strcpy (name, model->frames[i].name);
  600. pos = strlen (name) - 1;
  601. j = 0;
  602. while (name[pos] >= '0' && name[pos] <= '9' && j < 2)
  603. {
  604. pos--;
  605. j++;
  606. }
  607. name[pos + 1] = '';
  608. if (strcmp (last, name))
  609. {
  610. strcpy (last, name);
  611. count++;
  612. }
  613. }
  614. return count;
  615. }
  616. const char *
  617. md2_getAnimationName (md2_model_t *model, int animation)
  618. {
  619. int i, j, pos;
  620. int count;
  621. int lastId;
  622. static char last[32];
  623. char name[32];
  624. strcpy (last, model->frames[0].name);
  625. pos = strlen (last) - 1;
  626. j = 0;
  627. while (last[pos] >= '0' && last[pos] <= '9' && j < 2)
  628. {
  629. pos--;
  630. j++;
  631. }
  632. last[pos + 1] = '';
  633. lastId = 0;
  634. count = 0;
  635. for (i = 0; i <= model->header.numFrames; i++)
  636. {
  637. if (i == model->header.numFrames)
  638. strcpy (name, ""); // some kind of a sentinel
  639. else
  640. strcpy (name, model->frames[i].name);
  641. pos = strlen (name) - 1;
  642. j = 0;
  643. while (name[pos] >= '0' && name[pos] <= '9' && j < 2)
  644. {
  645. pos--;
  646. j++;
  647. }
  648. name[pos + 1] = '';
  649. if (strcmp (last, name))
  650. {
  651. if (count == animation)
  652. return last;
  653. strcpy (last, name);
  654. count++;
  655. }
  656. }
  657. return 0;
  658. }
  659. void
  660. md2_getAnimationFrames (md2_model_t *model, int animation, int *startFrame, int *endFrame)
  661. {
  662. int i, j, pos;
  663. int count, numFrames, frameCount;
  664. int lastId;
  665. char name[16], last[16];
  666. strcpy (last, model->frames[0].name);
  667. pos = strlen (last) - 1;
  668. j = 0;
  669. while (last[pos] >= '0' && last[pos] <= '9' && j < 2)
  670. {
  671. pos--;
  672. j++;
  673. }
  674. last[pos + 1] = '';
  675. lastId = 0;
  676. count = 0;
  677. numFrames = 0;
  678. frameCount = 0;
  679. for (i = 0; i <= model->header.numFrames; i++)
  680. {
  681. if (i == model->header.numFrames)
  682. strcpy (name, ""); // some kind of a sentinel
  683. else
  684. strcpy (name, model->frames[i].name);
  685. pos = strlen (name) - 1;
  686. j = 0;
  687. while (name[pos] >= '0' && name[pos] <= '9' && j < 2)
  688. {
  689. pos--;
  690. j++;
  691. }
  692. name[pos + 1] = '';
  693. if (strcmp (last, name))
  694. {
  695. strcpy (last, name);
  696. if (count == animation)
  697. {
  698. *startFrame = frameCount - numFrames;
  699. *endFrame = frameCount - 1;
  700. return;
  701. }
  702. count++;
  703. numFrames = 0;
  704. }
  705. frameCount++;
  706. numFrames++;
  707. }
  708. *startFrame = *endFrame = 0;
  709. }
  710. #if 0
  711. void
  712. md2_printModelInfo (md2_model_t *model)
  713. {
  714. int i;
  715. printf ("magic:tt%dn", model->header.magic);
  716. printf ("version:tt%dn", model->header.version);
  717. printf ("skinWidth:tt%dn", model->header.skinWidth);
  718. printf ("skinHeight:tt%dn", model->header.skinHeight);
  719. printf ("frameSize:tt%dn", model->header.frameSize);
  720. printf ("numSkins:tt%dn", model->header.numSkins);
  721. printf ("numVertices:tt%dn", model->header.numVertices);
  722. printf ("numTexCoords:tt%dn", model->header.numTexCoords);
  723. printf ("numTriangles:tt%dn", model->header.numTriangles);
  724. printf ("numGlCommands:tt%dn", model->header.numGlCommands);
  725. printf ("numFrames:tt%dn", model->header.numFrames);
  726. printf ("offsetSkins:tt%dn", model->header.offsetSkins);
  727. printf ("offsetTexCoords:t%dn", model->header.offsetTexCoords);
  728. printf ("offsetTriangles:t%dn", model->header.offsetTriangles);
  729. printf ("offsetFrames:tt%dn", model->header.offsetFrames);
  730. printf ("offsetGlCommands:t%dn", model->header.offsetGlCommands);
  731. printf ("offsetEnd:tt%dn", model->header.offsetEnd);
  732. for (i = 0; i < model->header.numFrames; i++)
  733. printf ("%s ", model->frames[i].name);
  734. }
  735. int
  736. main (int argc, char *argv[])
  737. {
  738. md2_model_t *model;
  739. if (argc != 2)
  740. {
  741. printf ("usage: %s <model.md2>n", argv[0]);
  742. return -1;
  743. }
  744. model = md2_readModel (argv[1]);
  745. if (!model)
  746. {
  747. printf ("hoopsn");
  748. return -1;
  749. }
  750. md2_printModelInfo (model);
  751. md2_freeModel (model);
  752. return 0;
  753. }
  754. #endif