2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 //=================================================================
46 trivert_t v[MAX_VERTS];
47 QDataJoint_t joints[NUM_CLUSTERS]; // ,this
50 // ,and all of this should get out of here, need to use new stuff in fmodels instead
52 typedef struct IntListNode_s
55 struct IntListNode_s *next;
56 } IntListNode_t; // gaak
60 float scale[3]; // multiply byte verts by this
61 float translate[3]; // then add this
62 } PartialAliasFrame_t;
67 int *clusters[NUM_CLUSTERS];
68 IntListNode_t *vertLists[NUM_CLUSTERS];
69 int num_verts[NUM_CLUSTERS + 1];
70 int new_num_verts[NUM_CLUSTERS + 1];
74 //================================================================
76 frame_t g_frames[MAX_FRAMES];
82 float scale_up; // set by $scale
83 vec3_t adjust; // set by $origin
84 int g_fixedwidth, g_fixedheight; // set by $skinsize
90 dstvert_t base_st[MAX_VERTS];
91 dtriangle_t triangles[MAX_TRIANGLES];
93 static int triangle_st[MAX_TRIANGLES][3][2];
95 // the command list holds counts, s/t values, and xyz indexes
96 // that are valid for every frame
100 int used[MAX_TRIANGLES];
102 char g_skins[MAX_MD2SKINS][64];
104 char cdarchive[1024];
105 char cdpartial[1024];
108 char modelname[64]; // empty unless $modelname issued (players)
110 extern char *g_outputDir;
112 #define NUMVERTEXNORMALS 162
114 float avertexnormals[NUMVERTEXNORMALS][3] =
119 unsigned char pic[SKINPAGE_HEIGHT*SKINPAGE_WIDTH], pic_palette[768];
121 FILE *headerouthandle = NULL;
123 //==============================================================
130 static void ClearModel (void)
132 memset (&model, 0, sizeof(model));
135 jointed = NOT_JOINTED;
138 VectorCopy (vec3_origin, adjust);
139 g_fixedwidth = g_fixedheight = 0;
144 void H_printf(char *fmt, ...)
149 if (!headerouthandle)
151 sprintf (name, "%s/tris.h", cddir);
152 headerouthandle = SafeOpenWrite (name);
153 fprintf(headerouthandle, "// %s\n\n", cddir);
154 fprintf(headerouthandle, "// This file generated by qdata - Do NOT Modify\n\n");
157 va_start (argptr, fmt);
158 vfprintf (headerouthandle, fmt, argptr);
168 void WriteCommonModelFile (FILE *modelouthandle, PartialAliasFrame_t *outFrames)
175 byte buffer[MAX_VERTS*4+128];
179 model.version = ALIAS_VERSION;
180 model.framesize = (int)&((daliasframe_t *)0)->verts[model.num_xyz];
181 model.num_glcmds = numcommands;
182 model.ofs_skins = sizeof(dmdl_t);
183 model.ofs_st = model.ofs_skins + model.num_skins * MAX_SKINNAME;
184 model.ofs_tris = model.ofs_st + model.num_st*sizeof(dstvert_t);
185 model.ofs_frames = model.ofs_tris + model.num_tris*sizeof(dtriangle_t);
186 model.ofs_glcmds = model.ofs_frames + model.num_frames*model.framesize;
187 model.ofs_end = model.ofs_glcmds + model.num_glcmds*sizeof(int);
189 // write out the model header
191 for (i=0 ; i<sizeof(dmdl_t)/4 ; i++)
192 ((int *)&modeltemp)[i] = LittleLong (((int *)&model)[i]);
194 SafeWrite (modelouthandle, &modeltemp, sizeof(modeltemp));
197 // write out the skin names
199 SafeWrite (modelouthandle, g_skins, model.num_skins * MAX_SKINNAME);
202 // write out the texture coordinates
205 for (i=0 ; i<model.num_st ; i++)
207 base_st[i].s = LittleShort (base_st[i].s);
208 base_st[i].t = LittleShort (base_st[i].t);
211 SafeWrite (modelouthandle, base_st, model.num_st * sizeof(base_st[0]));
214 // write out the triangles
216 for (i=0 ; i<model.num_tris ; i++)
221 for (j=0 ; j<3 ; j++)
223 tri.index_xyz[j] = LittleShort (triangles[i].index_xyz[j]);
224 tri.index_st[j] = LittleShort (triangles[i].index_st[j]);
227 SafeWrite (modelouthandle, &tri, sizeof(tri));
231 // write out the frames
233 for (i=0 ; i<model.num_frames ; i++)
236 out = (daliasframe_t *)buffer;
238 strcpy (out->name, in->name);
239 for (j=0 ; j<3 ; j++)
241 out->scale[j] = (in->maxs[j] - in->mins[j])/255;
242 out->translate[j] = in->mins[j];
246 outFrames[i].scale[j] = out->scale[j];
247 outFrames[i].translate[j] = out->translate[j];
251 for (j=0 ; j<model.num_xyz ; j++)
253 // all of these are byte values, so no need to deal with endianness
254 out->verts[j].lightnormalindex = in->v[j].lightnormalindex;
256 for (k=0 ; k<3 ; k++)
258 // scale to byte values & min/max check
259 v = Q_rint ( (in->v[j].v[k] - out->translate[k]) / out->scale[k] );
261 // clamp, so rounding doesn't wrap from 255.6 to 0
266 out->verts[j].v[k] = v;
270 for (j=0 ; j<3 ; j++)
272 out->scale[j] = LittleFloat (out->scale[j]);
273 out->translate[j] = LittleFloat (out->translate[j]);
276 SafeWrite (modelouthandle, out, model.framesize);
282 SafeWrite (modelouthandle, commands, numcommands*4);
290 void WriteModelFile (FILE *modelouthandle)
292 model.ident = IDALIASHEADER;
294 WriteCommonModelFile(modelouthandle, NULL);
299 WriteJointedModelFile
302 void WriteJointedModelFile (FILE *modelouthandle)
308 IntListNode_t *current, *toFree;
309 PartialAliasFrame_t outFrames[MAX_FRAMES];
311 model.ident = IDJOINTEDALIASHEADER;
313 WriteCommonModelFile(modelouthandle, outFrames);
316 SafeWrite(modelouthandle, &jointed, sizeof(int));
319 SafeWrite(modelouthandle, &numJointsForSkeleton[jointed], sizeof(int));
321 // number of verts in each cluster
322 SafeWrite(modelouthandle, &new_num_verts[1], sizeof(int)*numJointsForSkeleton[jointed]);
325 for(i = 0; i < new_num_verts[0]; ++i)
327 current = vertLists[i];
330 SafeWrite (modelouthandle, ¤t->data, sizeof(int));
332 current = current->next;
333 free(toFree); // freeing of memory allocated in ReplaceClusterIndex called in Cmd_Base
337 for (i=0 ; i<model.num_frames ; i++)
341 for (j = 0 ; j < new_num_verts[0]; ++j)
343 for (k=0 ; k<3 ; k++)
345 // scale to byte values & min/max check
346 v = Q_rint ( (in->joints[j].placement.origin[k] - outFrames[i].translate[k]) / outFrames[i].scale[k] );
348 // clamp, so rounding doesn't wrap from 255.6 to 0
359 // write out origin as a float (there's only a few per model, so it's not really
361 SafeWrite (modelouthandle, &v, sizeof(float));
364 for (k=0 ; k<3 ; k++)
366 v = Q_rint ( (in->joints[j].placement.direction[k] - outFrames[i].translate[k]) / outFrames[i].scale[k] );
368 // clamp, so rounding doesn't wrap from 255.6 to 0
379 // write out origin as a float (there's only a few per model, so it's not really
381 SafeWrite (modelouthandle, &v, sizeof(float));
384 for (k=0 ; k<3 ; k++)
386 v = Q_rint ( (in->joints[j].placement.up[k] - outFrames[i].translate[k]) / outFrames[i].scale[k] );
388 // clamp, so rounding doesn't wrap from 255.6 to 0
399 // write out origin as a float (there's only a few per model, so it's not really
401 SafeWrite (modelouthandle, &v, sizeof(float));
412 static void WriteModelFile (FILE *modelouthandle)
419 byte buffer[MAX_VERTS*4+128];
423 model.ident = IDALIASHEADER;
424 model.version = ALIAS_VERSION;
425 model.framesize = (int)&((daliasframe_t *)0)->verts[model.num_xyz];
426 model.num_glcmds = numcommands;
427 model.ofs_skins = sizeof(dmdl_t);
428 model.ofs_st = model.ofs_skins + model.num_skins * MAX_SKINNAME;
429 model.ofs_tris = model.ofs_st + model.num_st*sizeof(dstvert_t);
430 model.ofs_frames = model.ofs_tris + model.num_tris*sizeof(dtriangle_t);
431 model.ofs_glcmds = model.ofs_frames + model.num_frames*model.framesize;
432 model.ofs_end = model.ofs_glcmds + model.num_glcmds*4;
435 // write out the model header
437 for (i=0 ; i<sizeof(dmdl_t)/4 ; i++)
438 ((int *)&modeltemp)[i] = LittleLong (((int *)&model)[i]);
440 SafeWrite (modelouthandle, &modeltemp, sizeof(modeltemp));
443 // write out the skin names
445 SafeWrite (modelouthandle, g_skins, model.num_skins * MAX_SKINNAME);
448 // write out the texture coordinates
451 for (i=0 ; i<model.num_st ; i++)
453 base_st[i].s = LittleShort (base_st[i].s);
454 base_st[i].t = LittleShort (base_st[i].t);
457 SafeWrite (modelouthandle, base_st, model.num_st * sizeof(base_st[0]));
460 // write out the triangles
462 for (i=0 ; i<model.num_tris ; i++)
467 for (j=0 ; j<3 ; j++)
469 tri.index_xyz[j] = LittleShort (triangles[i].index_xyz[j]);
470 tri.index_st[j] = LittleShort (triangles[i].index_st[j]);
473 SafeWrite (modelouthandle, &tri, sizeof(tri));
477 // write out the frames
479 for (i=0 ; i<model.num_frames ; i++)
482 out = (daliasframe_t *)buffer;
484 strcpy (out->name, in->name);
485 for (j=0 ; j<3 ; j++)
487 out->scale[j] = (in->maxs[j] - in->mins[j])/255;
488 out->translate[j] = in->mins[j];
491 for (j=0 ; j<model.num_xyz ; j++)
493 // all of these are byte values, so no need to deal with endianness
494 out->verts[j].lightnormalindex = in->v[j].lightnormalindex;
496 for (k=0 ; k<3 ; k++)
498 // scale to byte values & min/max check
499 v = Q_rint ( (in->v[j].v[k] - out->translate[k]) / out->scale[k] );
501 // clamp, so rounding doesn't wrap from 255.6 to 0
506 out->verts[j].v[k] = v;
510 for (j=0 ; j<3 ; j++)
512 out->scale[j] = LittleFloat (out->scale[j]);
513 out->translate[j] = LittleFloat (out->translate[j]);
516 SafeWrite (modelouthandle, out, model.framesize);
522 SafeWrite (modelouthandle, commands, numcommands*4);
531 void FinishModel (void)
533 FILE *modelouthandle;
537 if (!model.num_frames)
541 // copy to release directory tree if doing a release build
546 sprintf (name, "%s", modelname);
548 sprintf (name, "%s/tris.md2", cdpartial);
551 for (i=0 ; i<model.num_skins ; i++)
553 ReleaseFile (g_skins[i]);
555 model.num_frames = 0;
560 // write the model output file
563 sprintf (name, "%s%s", g_outputDir, modelname);
565 sprintf (name, "%s/tris.md2", g_outputDir);
566 printf ("saving to %s\n", name);
568 modelouthandle = SafeOpenWrite (name);
571 if(jointed != NOT_JOINTED)
572 WriteJointedModelFile(modelouthandle);
575 WriteModelFile(modelouthandle);
577 printf ("%3dx%3d skin\n", model.skinwidth, model.skinheight);
578 printf ("First frame boundaries:\n");
579 printf (" minimum x: %3f\n", g_frames[0].mins[0]);
580 printf (" maximum x: %3f\n", g_frames[0].maxs[0]);
581 printf (" minimum y: %3f\n", g_frames[0].mins[1]);
582 printf (" maximum y: %3f\n", g_frames[0].maxs[1]);
583 printf (" minimum z: %3f\n", g_frames[0].mins[2]);
584 printf (" maximum z: %3f\n", g_frames[0].maxs[2]);
585 printf ("%4d vertices\n", model.num_xyz);
586 printf ("%4d triangles\n", model.num_tris);
587 printf ("%4d frame\n", model.num_frames);
588 printf ("%4d glverts\n", numglverts);
589 printf ("%4d glcmd\n", model.num_glcmds);
590 printf ("%4d skins\n", model.num_skins);
591 printf ("file size: %d\n", (int)ftell (modelouthandle) );
592 printf ("---------------------\n");
594 fclose (modelouthandle);
596 // finish writing header file
599 // scale_up is usefull to allow step distances to be adjusted
600 H_printf("#define MODEL_SCALE\t\t%f\n", scale_up);
602 fclose (headerouthandle);
603 headerouthandle = NULL;
608 =================================================================
610 ALIAS MODEL DISPLAY LIST GENERATION
612 =================================================================
625 static int StripLength (int starttri, int startv)
630 dtriangle_t *last, *check;
635 last = &triangles[starttri];
637 strip_xyz[0] = last->index_xyz[(startv)%3];
638 strip_xyz[1] = last->index_xyz[(startv+1)%3];
639 strip_xyz[2] = last->index_xyz[(startv+2)%3];
640 strip_st[0] = last->index_st[(startv)%3];
641 strip_st[1] = last->index_st[(startv+1)%3];
642 strip_st[2] = last->index_st[(startv+2)%3];
644 strip_tris[0] = starttri;
647 m1 = last->index_xyz[(startv+2)%3];
648 st1 = last->index_st[(startv+2)%3];
649 m2 = last->index_xyz[(startv+1)%3];
650 st2 = last->index_st[(startv+1)%3];
652 // look for a matching triangle
654 for (j=starttri+1, check=&triangles[starttri+1]
655 ; j<model.num_tris ; j++, check++)
657 for (k=0 ; k<3 ; k++)
659 if (check->index_xyz[k] != m1)
661 if (check->index_st[k] != st1)
663 if (check->index_xyz[ (k+1)%3 ] != m2)
665 if (check->index_st[ (k+1)%3 ] != st2)
668 // this is the next part of the fan
670 // if we can't use this triangle, this tristrip is done
677 m2 = check->index_xyz[ (k+2)%3 ];
678 st2 = check->index_st[ (k+2)%3 ];
682 m1 = check->index_xyz[ (k+2)%3 ];
683 st1 = check->index_st[ (k+2)%3 ];
686 strip_xyz[stripcount+2] = check->index_xyz[ (k+2)%3 ];
687 strip_st[stripcount+2] = check->index_st[ (k+2)%3 ];
688 strip_tris[stripcount] = j;
697 // clear the temp used flags
698 for (j=starttri+1 ; j<model.num_tris ; j++)
711 static int FanLength (int starttri, int startv)
716 dtriangle_t *last, *check;
721 last = &triangles[starttri];
723 strip_xyz[0] = last->index_xyz[(startv)%3];
724 strip_xyz[1] = last->index_xyz[(startv+1)%3];
725 strip_xyz[2] = last->index_xyz[(startv+2)%3];
726 strip_st[0] = last->index_st[(startv)%3];
727 strip_st[1] = last->index_st[(startv+1)%3];
728 strip_st[2] = last->index_st[(startv+2)%3];
730 strip_tris[0] = starttri;
733 m1 = last->index_xyz[(startv+0)%3];
734 st1 = last->index_st[(startv+0)%3];
735 m2 = last->index_xyz[(startv+2)%3];
736 st2 = last->index_st[(startv+2)%3];
739 // look for a matching triangle
741 for (j=starttri+1, check=&triangles[starttri+1]
742 ; j<model.num_tris ; j++, check++)
744 for (k=0 ; k<3 ; k++)
746 if (check->index_xyz[k] != m1)
748 if (check->index_st[k] != st1)
750 if (check->index_xyz[ (k+1)%3 ] != m2)
752 if (check->index_st[ (k+1)%3 ] != st2)
755 // this is the next part of the fan
757 // if we can't use this triangle, this tristrip is done
762 m2 = check->index_xyz[ (k+2)%3 ];
763 st2 = check->index_st[ (k+2)%3 ];
765 strip_xyz[stripcount+2] = m2;
766 strip_st[stripcount+2] = st2;
767 strip_tris[stripcount] = j;
776 // clear the temp used flags
777 for (j=starttri+1 ; j<model.num_tris ; j++)
790 Generate a list of trifans or strips
791 for the model, which holds for all frames
794 static void BuildGlCmds (void)
799 int len, bestlen, besttype;
810 memset (used, 0, sizeof(used));
811 for (i=0 ; i<model.num_tris ; i++)
813 // pick an unused triangle and start the trifan
818 for (type = 0 ; type < 2 ; type++)
821 for (startv =0 ; startv < 3 ; startv++)
824 len = StripLength (i, startv);
826 len = FanLength (i, startv);
831 for (j=0 ; j<bestlen+2 ; j++)
833 best_st[j] = strip_st[j];
834 best_xyz[j] = strip_xyz[j];
836 for (j=0 ; j<bestlen ; j++)
837 best_tris[j] = strip_tris[j];
842 // mark the tris on the best strip/fan as used
843 for (j=0 ; j<bestlen ; j++)
844 used[best_tris[j]] = 1;
847 commands[numcommands++] = (bestlen+2);
849 commands[numcommands++] = -(bestlen+2);
851 numglverts += bestlen+2;
853 for (j=0 ; j<bestlen+2 ; j++)
855 // emit a vertex into the reorder buffer
858 // emit s/t coords into the commands stream
862 s = (s + 0.5) / model.skinwidth;
863 t = (t + 0.5) / model.skinheight;
865 *(float *)&commands[numcommands++] = s;
866 *(float *)&commands[numcommands++] = t;
867 *(int *)&commands[numcommands++] = best_xyz[j];
871 commands[numcommands++] = 0; // end of list marker
876 ===============================================================
880 ===============================================================
887 Builds the triangle_st array for the base frame and
888 model.skinwidth / model.skinheight
890 FIXME: allow this to be loaded from a file for
895 static void OldBuildST (triangle_t *ptri, int numtri)
898 int width, height, iwidth, iheight, swidth;
900 float s_scale, t_scale;
904 vec3_t vtemp1, vtemp2, normal;
907 // find bounds of all the verts on the base frame
909 ClearBounds (mins, maxs);
911 for (i=0 ; i<numtri ; i++)
912 for (j=0 ; j<3 ; j++)
913 AddPointToBounds (ptri[i].verts[j], mins, maxs);
915 for (i=0 ; i<3 ; i++)
917 mins[i] = floor(mins[i]);
918 maxs[i] = ceil(maxs[i]);
921 width = maxs[0] - mins[0];
922 height = maxs[2] - mins[2];
927 if (width*scale >= 150)
928 scale = 150.0 / width;
929 if (height*scale >= 190)
930 scale = 190.0 / height;
932 s_scale = t_scale = scale;
934 iwidth = ceil(width*s_scale);
935 iheight = ceil(height*t_scale);
942 iwidth = g_fixedwidth / 2;
943 iheight = g_fixedheight;
945 s_scale = (float)(iwidth-4) / width;
946 t_scale = (float)(iheight-4) / height;
950 // determine which side of each triangle to map the texture to
952 for (i=0 ; i<numtri ; i++)
954 VectorSubtract (ptri[i].verts[0], ptri[i].verts[1], vtemp1);
955 VectorSubtract (ptri[i].verts[2], ptri[i].verts[1], vtemp2);
956 CrossProduct (vtemp1, vtemp2, normal);
968 for (j=0 ; j<3 ; j++)
970 pbasevert = ptri[i].verts[j];
972 triangle_st[i][j][0] = Q_rint((pbasevert[0] - mins[0]) * s_scale + basex);
973 triangle_st[i][j][1] = Q_rint((maxs[2] - pbasevert[2]) * t_scale + basey);
977 // make the width a multiple of 4; some hardware requires this, and it ensures
978 // dword alignment for each scan
980 model.skinwidth = (swidth + 3) & ~3;
981 model.skinheight = iheight;
985 //==========================================================================
989 //==========================================================================
991 void DrawScreen(float s_scale, float t_scale, float iwidth, float iheight)
998 scrpos = &pic[(INFO_Y-2)*SKINPAGE_WIDTH];
999 for(i = 0; i < SKINPAGE_WIDTH; i++)
1004 sprintf(buffer, "GENSKIN: ");
1005 DrawTextChar(16, INFO_Y, buffer);
1007 sprintf(buffer, "( %03d * %03d ) SCALE %f %f, SKINWIDTH %d,"
1008 " SKINHEIGHT %d", (int)ScaleWidth, (int)ScaleHeight, s_scale, t_scale, (int)iwidth*2, (int)iheight);
1009 DrawTextChar(80, INFO_Y, buffer);
1016 Builds the triangle_st array for the base frame and
1017 model.skinwidth / model.skinheight
1019 FIXME: allow this to be loaded from a file for
1023 void BuildST (triangle_t *ptri, int numtri, qboolean DrawSkin)
1026 int width, height, iwidth, iheight, swidth;
1031 vec3_t vtemp1, vtemp2, normal;
1032 float s_scale, t_scale;
1037 // find bounds of all the verts on the base frame
1039 ClearBounds (mins, maxs);
1041 for (i=0 ; i<numtri ; i++)
1042 for (j=0 ; j<3 ; j++)
1043 AddPointToBounds (ptri[i].verts[j], mins, maxs);
1045 for (i=0 ; i<3 ; i++)
1047 mins[i] = floor(mins[i]);
1048 maxs[i] = ceil(maxs[i]);
1051 width = maxs[0] - mins[0];
1052 height = maxs[2] - mins[2];
1055 scWidth = (ScaleWidth/2)*SCALE_ADJUST_FACTOR;
1056 scHeight = ScaleHeight*SCALE_ADJUST_FACTOR;
1058 scale = scWidth/width;
1060 if(height*scale >= scHeight)
1062 scale = scHeight/height;
1065 iwidth = ceil(width*scale)+4;
1066 iheight = ceil(height*scale)+4;
1068 s_scale = (float)(iwidth-4) / width;
1069 t_scale = (float)(iheight-4) / height;
1073 DrawScreen(s_scale, t_scale, iwidth, iheight);
1076 /* if (!g_fixedwidth)
1079 if (width*scale >= 150)
1080 scale = 150.0 / width;
1081 if (height*scale >= 190)
1082 scale = 190.0 / height;
1084 s_scale = t_scale = scale;
1086 iwidth = ceil(width*s_scale);
1087 iheight = ceil(height*t_scale);
1094 iwidth = g_fixedwidth / 2;
1095 iheight = g_fixedheight;
1097 s_scale = (float)(iwidth-4) / width;
1098 t_scale = (float)(iheight-4) / height;
1102 // determine which side of each triangle to map the texture to
1104 for (i=0 ; i<numtri ; i++)
1108 for (j=0 ; j<3 ; j++)
1110 triangle_st[i][j][0] = Q_rint(ptri[i].uv[j][0]*iwidth);
1111 triangle_st[i][j][1] = Q_rint((1.0f-ptri[i].uv[j][1])*iheight);
1116 VectorSubtract (ptri[i].verts[0], ptri[i].verts[1], vtemp1);
1117 VectorSubtract (ptri[i].verts[2], ptri[i].verts[1], vtemp2);
1118 CrossProduct (vtemp1, vtemp2, normal);
1130 for (j=0 ; j<3 ; j++)
1132 pbasevert = ptri[i].verts[j];
1134 triangle_st[i][j][0] = Q_rint((pbasevert[0] - mins[0]) * s_scale + basex);
1135 triangle_st[i][j][1] = Q_rint((maxs[2] - pbasevert[2]) * t_scale + basey);
1139 DrawLine(triangle_st[i][0][0], triangle_st[i][0][1],
1140 triangle_st[i][1][0], triangle_st[i][1][1]);
1141 DrawLine(triangle_st[i][1][0], triangle_st[i][1][1],
1142 triangle_st[i][2][0], triangle_st[i][2][1]);
1143 DrawLine(triangle_st[i][2][0], triangle_st[i][2][1],
1144 triangle_st[i][0][0], triangle_st[i][0][1]);
1147 // make the width a multiple of 4; some hardware requires this, and it ensures
1148 // dword alignment for each scan
1151 model.skinwidth = (swidth + 3) & ~3;
1152 model.skinheight = iheight;
1156 static void ReplaceClusterIndex(int newIndex, int oldindex, int **clusters,
1157 IntListNode_t **vertLists, int *num_verts, int *new_num_verts)
1160 IntListNode_t *next;
1162 for(j = 0; j < num_verts[0]; ++j)
1164 for(i = 0; i < num_verts[j+1]; ++i)
1166 if(clusters[j][i] == oldindex)
1168 ++new_num_verts[j+1];
1170 next = vertLists[j];
1172 vertLists[j] = (IntListNode_t *) SafeMalloc(sizeof(IntListNode_t), "ReplaceClusterIndex");
1173 // Currently freed in WriteJointedModelFile only
1175 vertLists[j]->data = newIndex;
1176 vertLists[j]->next = next;
1187 void Cmd_Base (void)
1189 vec3_t base_xyz[MAX_VERTS];
1199 GetScriptToken (false);
1201 if (g_skipmodel || g_release || g_archive)
1204 printf ("---------------------\n");
1206 sprintf (file1, "%s/%s", cdpartial, token);
1207 printf ("%s ", file1);
1209 ExpandPathAndArchive (file1);
1211 sprintf (file1, "%s/%s", cddir, token);
1213 sprintf (file1, "%s/%s.%s", cdarchive, token, trifileext);
1214 printf ("%s\n", file1);
1216 ExpandPathAndArchive (file1);
1218 sprintf (file1, "%s/%s.%s", cddir, token, trifileext);
1220 time1 = FileTime (file1);
1222 Error ("%s doesn't exist", file1);
1225 // load the base triangles
1228 Load3DSTriangleList (file1, &ptri, &model.num_tris, NULL, NULL);
1230 LoadTriangleList (file1, &ptri, &model.num_tris, NULL, NULL);
1233 GetScriptToken (false);
1234 sprintf (file2, "%s/%s.pcx", cddir, token);
1235 // sprintf (trans_file, "%s/!%s_a.pcx", cddir, token);
1237 printf ("skin: %s\n", file2);
1238 Load256Image (file2, &BasePixels, &BasePalette, &BaseWidth, &BaseHeight);
1240 if (BaseWidth != SKINPAGE_WIDTH || BaseHeight != SKINPAGE_HEIGHT)
1242 if (g_allow_newskin)
1244 ScaleWidth = BaseWidth;
1245 ScaleHeight = BaseHeight;
1249 Error("Invalid skin page size: (%d,%d) should be (%d,%d)",
1250 BaseWidth,BaseHeight,SKINPAGE_WIDTH,SKINPAGE_HEIGHT);
1255 ScaleWidth = (float)ExtractNumber(BasePixels, ENCODED_WIDTH_X,
1257 ScaleHeight = (float)ExtractNumber(BasePixels, ENCODED_HEIGHT_X,
1262 // get the ST values
1264 BuildST (ptri, model.num_tris,false);
1267 // run through all the base triangles, storing each unique vertex in the
1268 // base vertex list and setting the indirect triangles to point to the base
1271 for (i=0 ; i<model.num_tris ; i++)
1273 for (j=0 ; j<3 ; j++)
1275 // get the xyz index
1276 for (k=0 ; k<model.num_xyz ; k++)
1277 if (VectorCompare (ptri[i].verts[j], base_xyz[k]))
1278 break; // this vertex is already in the base vertex list
1280 if (k == model.num_xyz)
1282 VectorCopy (ptri[i].verts[j], base_xyz[model.num_xyz]);
1285 ReplaceClusterIndex(k, ptri[i].indicies[j], (int **)&clusters, (IntListNode_t **)&vertLists, (int *)&num_verts, (int *)&new_num_verts);
1290 triangles[i].index_xyz[j] = k;
1293 for (k=0 ; k<model.num_st ; k++)
1294 if (triangle_st[i][j][0] == base_st[k].s
1295 && triangle_st[i][j][1] == base_st[k].t)
1296 break; // this vertex is already in the base vertex list
1298 if (k == model.num_st)
1300 base_st[model.num_st].s = triangle_st[i][j][0];
1301 base_st[model.num_st].t = triangle_st[i][j][1];
1305 triangles[i].index_st[j] = k;
1309 // build triangle strips / fans
1313 //===============================================================
1315 char *FindFrameFile (char *frame)
1319 static char retname[1024];
1324 if (strstr (frame, "."))
1325 return frame; // allready in dot format
1327 // split 'run1' into 'run' and '1'
1328 s = frame + strlen(frame)-1;
1330 while (s != frame && *s >= '0' && *s <= '9')
1333 strcpy (suffix, s+1);
1334 strcpy (base, frame);
1335 base[s-frame+1] = 0;
1337 sprintf (file1, "%s/%s%s.%s",cddir, base, suffix, "hrc");
1338 time1 = FileTime (file1);
1341 sprintf (retname, "%s%s.%s", base, suffix, "hrc");
1345 sprintf (file1, "%s/%s%s.%s",cddir, base, suffix, "asc");
1346 time1 = FileTime (file1);
1349 sprintf (retname, "%s%s.%s", base, suffix, "asc");
1353 sprintf (file1, "%s/%s%s.%s",cddir, base, suffix, "tri");
1354 time1 = FileTime (file1);
1357 sprintf (retname, "%s%s.%s", base, suffix, "tri");
1361 sprintf (file1, "%s/%s%s.%s",cddir, base, suffix, "3ds");
1362 time1 = FileTime (file1);
1365 sprintf (retname, "%s%s.%s", base, suffix, "3ds");
1369 sprintf (file1, "%s/%s%s.%s",cddir, base, suffix, "htr");
1370 time1 = FileTime (file1);
1373 sprintf (retname, "%s%s.%s", base, suffix, "htr");
1377 // check for 'run.1'
1378 sprintf (file1, "%s/%s.%s",cddir, base, suffix);
1379 time1 = FileTime (file1);
1382 sprintf (retname, "%s.%s", base, suffix);
1386 Error ("frame %s could not be found",frame);
1395 static void GrabFrame (char *frame)
1399 trivert_t *ptrivert;
1403 vertexnormals_t vnorms[MAX_VERTS];
1407 // the frame 'run1' will be looked for as either
1408 // run.1 or run1.tri, so the new alias sequence save
1409 // feature an be used
1410 framefile = FindFrameFile (frame);
1412 sprintf (file1, "%s/%s", cdarchive, framefile);
1413 ExpandPathAndArchive (file1);
1415 sprintf (file1, "%s/%s",cddir, framefile);
1417 printf ("grabbing %s ", file1);
1419 if (model.num_frames >= MAX_FRAMES)
1420 Error ("model.num_frames >= MAX_FRAMES");
1421 fr = &g_frames[model.num_frames];
1424 strcpy (fr->name, frame);
1430 Load3DSTriangleList (file1, &ptri, &num_tris, NULL, NULL);
1432 LoadTriangleList (file1, &ptri, &num_tris, NULL, NULL);
1434 if (num_tris != model.num_tris)
1435 Error ("%s: number of triangles doesn't match base frame\n", file1);
1438 // allocate storage for the frame's vertices
1442 for (i=0 ; i<model.num_xyz ; i++)
1444 vnorms[i].numnormals = 0;
1445 VectorClear (vnorms[i].normalsum);
1447 ClearBounds (fr->mins, fr->maxs);
1450 // store the frame's vertices in the same order as the base. This assumes the
1451 // triangles and vertices in this frame are in exactly the same order as in the
1454 for (i=0 ; i<num_tris ; i++)
1456 vec3_t vtemp1, vtemp2, normal;
1459 VectorSubtract (ptri[i].verts[0], ptri[i].verts[1], vtemp1);
1460 VectorSubtract (ptri[i].verts[2], ptri[i].verts[1], vtemp2);
1461 CrossProduct (vtemp1, vtemp2, normal);
1463 VectorNormalize (normal, normal);
1465 // rotate the normal so the model faces down the positive x axis
1467 normal[0] = -normal[1];
1470 for (j=0 ; j<3 ; j++)
1472 index_xyz = triangles[i].index_xyz[j];
1474 // rotate the vertices so the model faces down the positive x axis
1475 // also adjust the vertices to the desired origin
1476 ptrivert[index_xyz].v[0] = ((-ptri[i].verts[j][1]) * scale_up) +
1478 ptrivert[index_xyz].v[1] = (ptri[i].verts[j][0] * scale_up) +
1480 ptrivert[index_xyz].v[2] = (ptri[i].verts[j][2] * scale_up) +
1483 AddPointToBounds (ptrivert[index_xyz].v, fr->mins, fr->maxs);
1485 VectorAdd (vnorms[index_xyz].normalsum, normal, vnorms[index_xyz].normalsum);
1486 vnorms[index_xyz].numnormals++;
1491 // calculate the vertex normals, match them to the template list, and store the
1492 // index of the best match
1494 for (i=0 ; i<model.num_xyz ; i++)
1502 c = vnorms[i].numnormals;
1504 Error ("Vertex with no triangles attached");
1506 VectorScale (vnorms[i].normalsum, 1.0/c, v);
1507 VectorNormalize (v, v);
1512 for (j=0 ; j<NUMVERTEXNORMALS ; j++)
1516 dot = DotProduct (v, avertexnormals[j]);
1524 ptrivert[i].lightnormalindex = maxdotindex;
1535 void GrabJointedFrame(char *frame)
1541 framefile = FindFrameFile (frame);
1543 sprintf (file1, "%s/%s", cdarchive, framefile);
1544 ExpandPathAndArchive (file1);
1546 sprintf (file1, "%s/%s",cddir, framefile);
1548 printf ("grabbing %s\n", file1);
1550 fr = &g_frames[model.num_frames - 1]; // last frame read in
1552 LoadJointList(file1, fr->joints, jointed);
1560 void GrabGlobals(char *frame)
1566 framefile = FindFrameFile (frame);
1568 sprintf (file1, "%s/%s", cdarchive, framefile);
1569 ExpandPathAndArchive (file1);
1571 sprintf (file1, "%s/%s",cddir, framefile);
1573 printf ("grabbing %s\n", file1);
1575 fr = &g_frames[model.num_frames - 1]; // last frame read in
1585 void Cmd_Frame (void)
1587 while (ScriptTokenAvailable())
1589 GetScriptToken (false);
1592 if (g_release || g_archive)
1594 model.num_frames = 1; // don't skip the writeout
1598 H_printf("#define FRAME_%-16s\t%i\n", token, model.num_frames);
1608 Skins aren't actually stored in the file, only a reference
1609 is saved out to the header file.
1612 void Cmd_Skin (void)
1619 char name[1024], savename[1024];
1621 GetScriptToken (false);
1623 if (model.num_skins == MAX_MD2SKINS)
1624 Error ("model.num_skins == MAX_MD2SKINS");
1630 sprintf (name, "%s/%s.pcx", cddir, token);
1631 sprintf (savename, "%s/!%s.pcx", g_outputDir, token);
1632 sprintf (g_skins[model.num_skins], "%s/!%s.pcx", cdpartial, token);
1634 sprintf (name, "%s/%s.lbm", cdarchive, token);
1635 strcpy (name, ExpandPathAndArchive( name ) );
1636 // sprintf (name, "%s/%s.lbm", cddir, token);
1638 if (ScriptTokenAvailable())
1640 GetScriptToken (false);
1641 sprintf (g_skins[model.num_skins], "%s.pcx", token);
1642 sprintf (savename, "%s%s.pcx", g_outputDir, g_skins[model.num_skins]);
1646 sprintf (savename, "%s/%s.pcx", g_outputDir, token);
1647 sprintf (g_skins[model.num_skins], "%s/%s.pcx", cdpartial, token);
1653 if (g_skipmodel || g_release || g_archive)
1657 printf ("loading %s\n", name);
1658 Load256Image (name, &pixels, &palette, &width, &height);
1659 // RemapZero (pixels, palette, width, height);
1661 // crop it to the proper size
1662 cropped = (byte *) SafeMalloc (model.skinwidth*model.skinheight, "Cmd_Skin");
1663 for (y=0 ; y<model.skinheight ; y++)
1665 memcpy (cropped+y*model.skinwidth,
1666 pixels+y*width, model.skinwidth);
1669 // save off the new image
1670 printf ("saving %s\n", savename);
1671 CreatePath (savename);
1672 WritePCXfile (savename, cropped, model.skinwidth,
1673 model.skinheight, palette);
1686 void Cmd_Origin (void)
1688 // rotate points into frame of reference so model points down the
1690 GetScriptToken (false);
1691 adjust[1] = -atof (token);
1693 GetScriptToken (false);
1694 adjust[0] = atof (token);
1696 GetScriptToken (false);
1697 adjust[2] = -atof (token);
1706 void Cmd_ScaleUp (void)
1708 GetScriptToken (false);
1709 scale_up = atof (token);
1710 if (g_skipmodel || g_release || g_archive)
1713 printf ("Scale up: %f\n", scale_up);
1721 Set a skin size other than the default
1724 void Cmd_Skinsize (void)
1726 GetScriptToken (false);
1727 g_fixedwidth = atoi(token);
1728 GetScriptToken (false);
1729 g_fixedheight = atoi(token);
1736 Gives a different name/location for the file, instead of the cddir
1739 void Cmd_Modelname (void)
1741 GetScriptToken (false);
1742 strcpy (modelname, token);
1757 GetScriptToken (false);
1759 // this is a silly mess...
1760 sprintf (cdpartial, "models/%s", token);
1761 sprintf (cdarchive, "%smodels/%s", gamedir+strlen(qdir), token);
1762 sprintf (cddir, "%s%s", gamedir, cdpartial);
1764 // Since we also changed directories on the output side (for mirror) make sure the outputdir is set properly too.
1765 sprintf(temp, "%s%s", g_outputDir, cdpartial);
1766 strcpy(g_outputDir, temp);
1768 // if -only was specified and this cd doesn't match,
1769 // skip the model (you only need to match leading chars,
1770 // so you could regrab all monsters with -only monsters)
1773 if (strncmp(token, g_only, strlen(g_only)))
1776 printf ("skipping %s\n", cdpartial);
1789 GetScriptToken (false);
1791 printf ("---------------------\n");
1792 sprintf (file1, "%s/%s", cdpartial, token);
1793 printf ("%s\n", file1);
1795 ExpandPathAndArchive (file1);
1797 sprintf (file1, "%s/%s", cddir, token);
1799 LoadClusters(file1, (int **)&clusters, (int *)&num_verts, jointed);
1801 new_num_verts[0] = num_verts[0];
1806 // Model construction cover functions.
1807 void MODELCMD_Modelname (int modeltype)
1810 modeltype = g_forcemodel;
1826 void MODELCMD_Cd (int modeltype)
1829 modeltype = g_forcemodel;
1842 void MODELCMD_Origin (int modeltype)
1845 modeltype = g_forcemodel;
1848 /* switch(modeltype)
1860 void MODELCMD_Cluster (int modeltype)
1863 modeltype = g_forcemodel;
1876 void MODELCMD_Base (int modeltype)
1879 modeltype = g_forcemodel;
1892 void MODELCMD_BaseST (int modeltype)
1895 modeltype = g_forcemodel;
1908 void MODELCMD_ScaleUp (int modeltype)
1911 modeltype = g_forcemodel;
1914 /* switch(modeltype)
1926 void MODELCMD_Frame (int modeltype)
1929 modeltype = g_forcemodel;
1942 void MODELCMD_Skin (int modeltype)
1945 modeltype = g_forcemodel;
1958 void MODELCMD_Skinsize (int modeltype)
1961 modeltype = g_forcemodel;
1977 void MODELCMD_Skeleton (int modeltype)
1980 modeltype = g_forcemodel;
1992 void MODELCMD_BeginGroup(int modeltype)
1995 modeltype = g_forcemodel;
2007 void MODELCMD_EndGroup(int modeltype)
2010 modeltype = g_forcemodel;
2022 void MODELCMD_Referenced(int modeltype)
2025 modeltype = g_forcemodel;
2037 void MODELCMD_NodeOrder(int modeltype)
2040 modeltype = g_forcemodel;