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
22 #include "qd_fmodel.h"
24 #include "qd_skeletons.h"
25 #include "skeletons.h"
28 #include "reference.h"
33 ========================================================================
35 .FM triangle flexible model file format
37 ========================================================================
40 //=================================================================
42 #define NUMVERTEXNORMALS 162
44 extern float avertexnormals[NUMVERTEXNORMALS][3];
46 #define MAX_GROUPS 128
54 #define TRIVERT_DIST .1
72 //================================================================
78 extern char g_skins[MAX_FM_SKINS][64];
81 extern fmstvert_t base_st[MAX_FM_VERTS];
84 extern fmtriangle_t triangles[MAX_FM_TRIANGLES];
87 fmframe_t g_frames[MAX_FM_FRAMES];
88 //fmframe_t *g_FMframes;
91 extern int commands[16384];
92 extern int numcommands;
96 // varibles set by commands
98 extern float scale_up; // set by $scale
99 extern vec3_t adjust; // set by $origin
100 extern int g_fixedwidth, g_fixedheight; // set by $skinsize
101 extern char modelname[64]; // set by $modelname
104 extern char *g_outputDir;
108 mesh_node_t *pmnodes = NULL;
109 fmmeshnode_t mesh_nodes[MAX_FM_MESH_NODES];
111 fmgroup_t groups[MAX_GROUPS];
113 int frame_to_group[MAX_FM_FRAMES];
116 // variables set by command line arguments
118 qboolean g_no_opimizations = false;
124 static int triangle_st[MAX_FM_TRIANGLES][3][2];
127 // number of gl vertices
128 extern int numglverts;
129 // indicates if a triangle has already been used in a glcmd
130 extern int used[MAX_FM_TRIANGLES];
131 // indicates if a triangle has translucency in it or not
132 static qboolean translucent[MAX_FM_TRIANGLES];
134 // main output file handle
135 extern FILE *headerouthandle;
136 // output sizes of buildst()
137 static int skin_width, skin_height;
141 static int total_skin_pixels;
142 static int skin_pixels_used;
144 int ShareVertex( trigroup_t trione, trigroup_t tritwo);
145 float DistBetween(vec3_t point1, vec3_t point2);
146 int GetNumTris( trigroup_t *tris, int group);
147 void GetOneGroup(trigroup_t *tris, int grp, triangle_t* triangles);
148 void ScaleTris( vec3_t min, vec3_t max, int Width, int Height, float* u, float* v, int verts);
149 void NewDrawLine(int x1, int y1, int x2, int y2, unsigned char* picture, int width, int height);
153 void strupr(char *string)
157 for (i=0 ; i<strlen(string); i++)
164 //==============================================================
171 static void ClearModel (void)
173 memset (&fmheader, 0, sizeof(fmheader));
177 VectorCopy (vec3_origin, adjust);
178 g_fixedwidth = g_fixedheight = 0;
188 ClearSkeletalModel();
192 extern void H_printf(char *fmt, ...);
195 void WriteHeader(FILE *FH, char *Ident, int Version, int Size, void *Data)
198 static long pos = -1;
202 { // Don't write out empty packets
208 CurrentPos = ftell(FH);
209 Size = CurrentPos - pos + sizeof(header_t);
210 fseek(FH, pos, SEEK_SET);
218 memset(&header,0,sizeof(header));
219 strcpy(header.ident,Ident);
220 header.version = Version;
223 SafeWrite (FH, &header, sizeof(header));
227 SafeWrite (FH, Data, Size);
233 fseek(FH, 0, SEEK_END);
242 static void WriteModelFile (FILE *modelouthandle)
248 byte buffer[MAX_FM_VERTS*4+128];
251 IntListNode_t *current, *toFree;
252 qboolean framesWritten = false;
253 size_t temp ,size = 0;
255 // probably should do this dynamically one of these days
258 float scale[3]; // multiply byte verts by this
259 float translate[3]; // then add this
260 } outFrames[MAX_FM_FRAMES];
262 #define DATA_SIZE 0x60000 // 384K had better be enough, particularly for the reference points
263 byte data[DATA_SIZE];
264 byte data2[DATA_SIZE];
266 fmheader.num_glcmds = numcommands;
267 fmheader.framesize = (int)&((fmaliasframe_t *)0)->verts[fmheader.num_xyz];
269 WriteHeader(modelouthandle, FM_HEADER_NAME, FM_HEADER_VER, sizeof(fmheader), &fmheader);
272 // write out the skin names
275 WriteHeader(modelouthandle, FM_SKIN_NAME, FM_SKIN_VER, fmheader.num_skins * MAX_FM_SKINNAME, g_skins);
278 // write out the texture coordinates
281 for (i=0 ; i<fmheader.num_st ; i++)
283 base_st[i].s = LittleShort (base_st[i].s);
284 base_st[i].t = LittleShort (base_st[i].t);
287 WriteHeader(modelouthandle, FM_ST_NAME, FM_ST_VER, fmheader.num_st * sizeof(base_st[0]), base_st);
290 // write out the triangles
292 WriteHeader(modelouthandle, FM_TRI_NAME, FM_TRI_VER, fmheader.num_tris * sizeof(fmtriangle_t), NULL);
294 for (i=0 ; i<fmheader.num_tris ; i++)
299 for (j=0 ; j<3 ; j++)
301 tri.index_xyz[j] = LittleShort (triangles[i].index_xyz[j]);
302 tri.index_st[j] = LittleShort (triangles[i].index_st[j]);
305 SafeWrite (modelouthandle, &tri, sizeof(tri));
311 // write out the frames
313 WriteHeader(modelouthandle, FM_FRAME_NAME, FM_FRAME_VER, fmheader.num_frames * fmheader.framesize, NULL);
314 // WriteHeader(modelouthandle, FM_FRAME_NAME, FM_FRAME_VER, -1, NULL);
316 for (i=0 ; i<fmheader.num_frames ; i++)
319 out = (fmaliasframe_t *)buffer;
321 strcpy (out->name, in->name);
322 for (j=0 ; j<3 ; j++)
324 out->scale[j] = (in->maxs[j] - in->mins[j])/255;
325 out->translate[j] = in->mins[j];
327 outFrames[i].scale[j] = out->scale[j];
328 outFrames[i].translate[j] = out->translate[j];
331 for (j=0 ; j<fmheader.num_xyz ; j++)
333 // all of these are byte values, so no need to deal with endianness
334 out->verts[j].lightnormalindex = in->v[j].lightnormalindex;
336 for (k=0 ; k<3 ; k++)
338 // scale to byte values & min/max check
339 v = Q_rint ( (in->v[j].v[k] - out->translate[k]) / out->scale[k] );
341 // clamp, so rounding doesn't wrap from 255.6 to 0
346 out->verts[j].v[k] = v;
350 for (j=0 ; j<3 ; j++)
352 out->scale[j] = LittleFloat (out->scale[j]);
353 out->translate[j] = LittleFloat (out->translate[j]);
356 SafeWrite (modelouthandle, out, fmheader.framesize);
359 // Go back and finish the header
360 // WriteHeader(modelouthandle, FM_FRAME_NAME, FM_FRAME_VER, -1, NULL);
364 WriteHeader(modelouthandle, FM_SHORT_FRAME_NAME, FM_SHORT_FRAME_VER,FRAME_NAME_LEN*fmheader.num_frames, NULL);
365 for (i=0 ; i<fmheader.num_frames ; i++)
368 SafeWrite (modelouthandle,in->name,FRAME_NAME_LEN);
370 WriteHeader(modelouthandle, FM_NORMAL_NAME, FM_NORMAL_VER,fmheader.num_xyz, NULL);
372 for (j=0 ; j<fmheader.num_xyz ; j++)
373 SafeWrite (modelouthandle,&in->v[j].lightnormalindex,1);
379 WriteHeader(modelouthandle, FM_GLCMDS_NAME, FM_GLCMDS_VER, numcommands*4, commands);
382 // write out mesh nodes
384 for(i=0;i<fmheader.num_mesh_nodes;i++)
386 memcpy(mesh_nodes[i].tris, pmnodes[i].tris, sizeof(mesh_nodes[i].tris));
387 memcpy(mesh_nodes[i].verts, pmnodes[i].verts, sizeof(mesh_nodes[i].verts));
388 mesh_nodes[i].start_glcmds = LittleShort((short)pmnodes[i].start_glcmds);
389 mesh_nodes[i].num_glcmds = LittleShort((short)pmnodes[i].num_glcmds);
392 WriteHeader(modelouthandle, FM_MESH_NAME, FM_MESH_VER, sizeof(fmmeshnode_t) * fmheader.num_mesh_nodes, mesh_nodes);
403 char *mat; fmheader.num_xyz*3*g->degrees*sizeof(char)
404 char *ccomp; g->num_frames*g->degrees*sizeof(char)
405 char *cbase; fmheader.num_xyz*3*sizeof(unsigned char)
406 float *cscale; g->degrees*sizeof(float)
407 float *coffset; g->degrees*sizeof(float)
408 float trans[3]; 3*sizeof(float)
409 float scale[3]; 3*sizeof(float)
414 size=sizeof(int)+fmheader.num_frames*sizeof(int);
415 for (k=0;k<num_groups;k++)
419 size+=fmheader.num_xyz*3*g->degrees*sizeof(char);
420 size+=g->num_frames*g->degrees*sizeof(char);
421 size+=fmheader.num_xyz*3*sizeof(unsigned char);
422 size+=g->degrees*sizeof(float);
423 size+=g->degrees*sizeof(float);
424 size+=12*sizeof(float);
426 WriteHeader(modelouthandle, FM_COMP_NAME, FM_COMP_VER,size, NULL);
427 SafeWrite (modelouthandle,&num_groups,sizeof(int));
428 SafeWrite (modelouthandle,frame_to_group,sizeof(int)*fmheader.num_frames);
430 for (k=0;k<num_groups;k++)
433 tmp=LittleLong(g->start_frame);
434 SafeWrite (modelouthandle,&tmp,sizeof(int));
435 tmp=LittleLong(g->num_frames);
436 SafeWrite (modelouthandle,&tmp,sizeof(int));
437 tmp=LittleLong(g->degrees);
438 SafeWrite (modelouthandle,&tmp,sizeof(int));
440 SafeWrite (modelouthandle,g->mat,fmheader.num_xyz*3*g->degrees*sizeof(char));
441 SafeWrite (modelouthandle,g->ccomp,g->num_frames*g->degrees*sizeof(char));
442 SafeWrite (modelouthandle,g->cbase,fmheader.num_xyz*3*sizeof(unsigned char));
443 SafeWrite (modelouthandle,g->cscale,g->degrees*sizeof(float));
444 SafeWrite (modelouthandle,g->coffset,g->degrees*sizeof(float));
445 SafeWrite (modelouthandle,g->trans,3*sizeof(float));
446 SafeWrite (modelouthandle,g->scale,3*sizeof(float));
447 SafeWrite (modelouthandle,g->bmin,3*sizeof(float));
448 SafeWrite (modelouthandle,g->bmax,3*sizeof(float));
457 // write the skeletal info
458 if(g_skelModel.type != SKEL_NULL)
462 temp = sizeof(int); // change this to a byte
463 memcpy(data + size, &g_skelModel.type, temp);
467 temp = sizeof(int); // change this to a byte
468 memcpy(data + size, &numJointsInSkeleton[g_skelModel.type], temp);
471 // number of verts in each joint cluster
472 temp = sizeof(int)*numJointsInSkeleton[g_skelModel.type]; // change this to shorts
473 memcpy(data + size, &g_skelModel.new_num_verts[1], temp);
477 for(i = 0; i < numJointsInSkeleton[g_skelModel.type]; ++i)
479 current = g_skelModel.vertLists[i];
482 temp = sizeof(int); // change this to a short
483 memcpy(data + size, ¤t->data, temp);
486 current = current->next;
487 free(toFree); // freeing of memory allocated in ReplaceClusterIndex called in Cmd_Base
491 if(!num_groups) // joints are stored with regular verts for compressed models
493 framesWritten = true;
495 temp = sizeof(int); // change this to a byte
496 memcpy(data + size, &framesWritten, temp);
499 for (i = 0; i < fmheader.num_frames; ++i)
503 for (j = 0 ; j < numJointsInSkeleton[g_skelModel.type]; ++j)
505 for (k=0 ; k<3 ; k++)
507 // scale to byte values & min/max check
508 v = Q_rint ( (in->joints[j].placement.origin[k] - outFrames[i].translate[k]) / outFrames[i].scale[k] );
510 // write out origin as a float since they arn't clamped
511 temp = sizeof(float); // change this to a short
512 assert(size+temp < DATA_SIZE);
513 memcpy(data + size, &v, temp);
517 for (k=0 ; k<3 ; k++)
519 v = Q_rint ( (in->joints[j].placement.direction[k] - outFrames[i].translate[k]) / outFrames[i].scale[k] );
521 // write out origin as a float since they arn't clamped
522 temp = sizeof(float); // change this to a short
523 assert(size+temp < DATA_SIZE);
524 memcpy(data + size, &v, temp);
528 for (k=0 ; k<3 ; k++)
530 v = Q_rint ( (in->joints[j].placement.up[k] - outFrames[i].translate[k]) / outFrames[i].scale[k] );
532 // write out origin as a float since they arn't clamped
533 temp = sizeof(float); // change this to a short
534 assert(size+temp < DATA_SIZE);
535 memcpy(data + size, &v, temp);
544 temp = sizeof(int); // change this to a byte
545 memcpy(data + size, &framesWritten, temp);
549 WriteHeader(modelouthandle, FM_SKELETON_NAME, FM_SKELETON_VER, size, data);
552 if(g_skelModel.references != REF_NULL)
557 if (RefPointNum <= 0)
558 { // Hard-coded labels
559 refnum = numReferences[g_skelModel.references];
562 { // Labels indicated in QDT
563 refnum = RefPointNum;
566 temp = sizeof(int); // change this to a byte
567 memcpy(data2 + size, &g_skelModel.references, temp);
572 framesWritten = true;
574 temp = sizeof(int); // change this to a byte
575 memcpy(data2 + size, &framesWritten, temp);
578 for (i = 0; i < fmheader.num_frames; ++i)
582 for (j = 0 ; j < refnum; ++j)
584 for (k=0 ; k<3 ; k++)
586 // scale to byte values & min/max check
587 v = Q_rint ( (in->references[j].placement.origin[k] - outFrames[i].translate[k]) / outFrames[i].scale[k] );
589 // write out origin as a float since they arn't clamped
590 temp = sizeof(float); // change this to a short
591 assert(size+temp < DATA_SIZE);
592 memcpy(data2 + size, &v, temp);
596 for (k=0 ; k<3 ; k++)
598 v = Q_rint ( (in->references[j].placement.direction[k] - outFrames[i].translate[k]) / outFrames[i].scale[k] );
600 // write out origin as a float since they arn't clamped
601 temp = sizeof(float); // change this to a short
602 assert(size+temp < DATA_SIZE);
603 memcpy(data2 + size, &v, temp);
607 for (k=0 ; k<3 ; k++)
609 v = Q_rint ( (in->references[j].placement.up[k] - outFrames[i].translate[k]) / outFrames[i].scale[k] );
611 // write out origin as a float since they arn't clamped
612 temp = sizeof(float); // change this to a short
613 assert(size+temp < DATA_SIZE);
614 memcpy(data2 + size, &v, temp);
620 else // FINISH ME: references need to be stored with regular verts for compressed models
622 framesWritten = false;
624 temp = sizeof(int); // change this to a byte
625 memcpy(data2 + size, &framesWritten, temp);
629 WriteHeader(modelouthandle, FM_REFERENCES_NAME, FM_REFERENCES_VER, size, data2);
633 static void CompressFrames()
640 for (i=0;i<fmheader.num_frames;i++)
642 while (i>=groups[j].start_frame+groups[j].num_frames&&j<num_groups-1)
647 for (k=0;k<num_groups;k++)
651 printf("\nCompressing Frames for group %i...\n", k);
652 AnimCompressInit(g->num_frames,fmheader.num_xyz,g->degrees);
653 for (i=0;i<g->num_frames;i++)
655 in = &g_frames[i+g->start_frame];
656 for (j=0;j<fmheader.num_xyz;j++)
657 AnimSetFrame(i,j,in->v[j].v[0],in->v[j].v[1],in->v[j].v[2]);
660 g->mat= (char *) SafeMalloc(fmheader.num_xyz*3*g->degrees*sizeof(char), "CompressFrames");
661 g->ccomp=(char *) SafeMalloc(g->num_frames*g->degrees*sizeof(char), "CompressFrames");
662 g->cbase=(char *) SafeMalloc(fmheader.num_xyz*3*sizeof(unsigned char), "CompressFrames");
663 g->cscale=(float *) SafeMalloc(g->degrees*sizeof(float), "CompressFrames");
664 g->coffset=(float *) SafeMalloc(g->degrees*sizeof(float), "CompressFrames");
665 AnimCompressToBytes(g->trans,g->scale,g->mat,g->ccomp,g->cbase,g->cscale,g->coffset,g->bmin,g->bmax);
670 static void OptimizeVertices(void)
672 qboolean vert_used[MAX_FM_VERTS];
673 short vert_replacement[MAX_FM_VERTS];
674 int i,j,k,l,pos,bit,set_pos,set_bit;
678 static IntListNode_t *newVertLists[NUM_CLUSTERS];
679 static int newNum_verts[NUM_CLUSTERS];
680 IntListNode_t *current, *next;
682 printf("Optimizing vertices...");
684 memset(vert_used, 0, sizeof(vert_used));
686 if(g_skelModel.clustered == true)
688 memset(newNum_verts, 0, sizeof(newNum_verts));
689 memset(newVertLists, 0, sizeof(newVertLists));
694 // search for common points among all the frames
695 for (i=0 ; i<fmheader.num_frames ; i++)
699 for(j=0;j<fmheader.num_xyz;j++)
701 for(k=0,Found=false;k<j;k++)
702 { // starting from the beginning always ensures vert_replacement points to the first point in the array
703 if (in->v[j].v[0] == in->v[k].v[0] &&
704 in->v[j].v[1] == in->v[k].v[1] &&
705 in->v[j].v[2] == in->v[k].v[2])
708 vert_replacement[j] = k;
725 // recompute the light normals
726 for (i=0 ; i<fmheader.num_frames ; i++)
730 for(j=0;j<fmheader.num_xyz;j++)
734 k = vert_replacement[j];
736 VectorAdd (in->v[j].vnorm.normalsum, in->v[k].vnorm.normalsum, in->v[k].vnorm.normalsum);
737 in->v[k].vnorm.numnormals += in->v[j].vnorm.numnormals++;
741 for (j=0 ; j<fmheader.num_xyz ; j++)
748 c = in->v[j].vnorm.numnormals;
750 Error ("Vertex with no triangles attached");
752 VectorScale (in->v[j].vnorm.normalsum, 1.0/c, v);
753 VectorNormalize (v, v);
758 for (k=0 ; k<NUMVERTEXNORMALS ; k++)
762 dot = DotProduct (v, avertexnormals[k]);
770 in->v[j].lightnormalindex = maxdotindex;
774 // create substitution list
776 for(i=0;i<fmheader.num_xyz;i++)
780 vert_replacement[i] = num_unique;
785 vert_replacement[i] = vert_replacement[vert_replacement[i]];
788 // vert_replacement[i] is the new index, i is the old index
789 // need to add the new index to the cluster list if old index was in it
790 if(g_skelModel.clustered == true)
792 for(k = 0; k < numJointsInSkeleton[g_skelModel.type]; ++k)
794 for(l = 0, current = g_skelModel.vertLists[k];
795 l < g_skelModel.new_num_verts[k+1]; ++l, current = current->next)
797 if(current->data == i)
799 IntListNode_t *current2;
801 qboolean added = false;
803 for(m = 0, current2 = newVertLists[k]; m < newNum_verts[k+1];
804 ++m, current2 = current2->next)
806 if(current2->data == vert_replacement[i])
817 next = newVertLists[k];
819 newVertLists[k] = (IntListNode_t *) SafeMalloc(sizeof(IntListNode_t), "OptimizeVertices");
820 // freed after model write out
822 newVertLists[k]->data = vert_replacement[i];
823 newVertLists[k]->next = next;
833 for (i=0 ; i<fmheader.num_frames ; i++)
837 for(j=0;j<fmheader.num_xyz;j++)
839 in->v[vert_replacement[j]] = in->v[j];
844 for(i = 0; i < numJointsInSkeleton[g_skelModel.type]; ++i)
846 IntListNode_t *toFree;
847 current = g_skelModel.vertLists[i];
852 current = current->next;
853 free(toFree); // freeing of memory allocated in ReplaceClusterIndex called in Cmd_Base
856 g_skelModel.vertLists[i] = newVertLists[i];
857 g_skelModel.new_num_verts[i+1] = newNum_verts[i+1];
861 for(k = 0; k < numJointsInSkeleton[g_skelModel.type]; ++k)
863 for(l = 0, current = g_skelModel.vertLists[k];
864 l < g_skelModel.new_num_verts[k+1]; ++l, current = current->next)
866 IntListNode_t *current2;
869 for(m = l+1, current2 = current->next; m < newNum_verts[k+1];
870 ++m, current2 = current2->next)
872 if(current->data == current2->data)
874 printf("Warning duplicate vertex: %d\n", current->data);
882 for(i=0;i<fmheader.num_mesh_nodes;i++)
883 { // reset the vert bits
884 memset(pmnodes[i].verts,0,sizeof(pmnodes[i].verts));
887 // repleace the master triangle list vertex indexes and update the vert bits for each mesh node
888 for (i=0 ; i<fmheader.num_tris ; i++)
893 for (j=0 ; j<3 ; j++)
895 set_bit = set_pos = triangles[i].index_xyz[j] = vert_replacement[triangles[i].index_xyz[j]];
898 set_bit = 1 << (set_bit & 7);
900 for(k=0;k<fmheader.num_mesh_nodes;k++)
902 if (!(pmnodes[k].tris[pos] & bit))
906 pmnodes[k].verts[set_pos] |= set_bit;
911 for (i=0;i<numcommands;i++)
919 commands[i+2] = vert_replacement[commands[i+2]];
924 printf("Reduced by %d\n",fmheader.num_xyz - num_unique);
926 fmheader.num_xyz = num_unique;
929 // tack on the reference verts to the regular verts
930 if(g_skelModel.references != REF_NULL)
936 if (RefPointNum <= 0)
937 { // Hard-coded labels
938 refnum = numReferences[g_skelModel.references];
941 { // Labels indicated in QDT
942 refnum = RefPointNum;
946 for (i = 0; i < fmheader.num_frames; ++i)
949 index = fmheader.num_xyz;
951 for (j = 0 ; j < refnum; ++j)
953 VectorCopy(in->references[j].placement.origin, in->v[index].v);
956 VectorCopy(in->references[j].placement.direction, in->v[index].v);
959 VectorCopy(in->references[j].placement.up, in->v[index].v);
964 fmheader.num_xyz += refnum*3;
967 // tack on the skeletal joint verts to the regular verts
968 if(g_skelModel.type != SKEL_NULL)
973 for (i = 0; i < fmheader.num_frames; ++i)
976 index = fmheader.num_xyz;
978 for (j = 0 ; j < numJointsInSkeleton[g_skelModel.type]; ++j)
980 VectorCopy(in->joints[j].placement.origin, in->v[index].v);
983 VectorCopy(in->joints[j].placement.direction, in->v[index].v);
986 VectorCopy(in->joints[j].placement.up, in->v[index].v);
991 fmheader.num_xyz += numJointsInSkeleton[g_skelModel.type]*3;
1004 void FMFinishModel (void)
1006 FILE *modelouthandle;
1007 int i,j,length,tris,verts,bit,pos,total_tris,total_verts;
1011 if (!fmheader.num_frames)
1015 // copy to release directory tree if doing a release build
1020 sprintf (name, "%s", modelname);
1022 sprintf (name, "%s/tris.fm", cdpartial);
1025 for (i=0 ; i<fmheader.num_skins ; i++)
1027 ReleaseFile (g_skins[i]);
1029 fmheader.num_frames = 0;
1036 for(i=0;i<fmheader.num_tris;i++)
1040 if (!g_no_opimizations)
1046 // write the model output file
1049 sprintf (name, "%s%s", g_outputDir, modelname);
1051 sprintf (name, "%s/tris.fm", g_outputDir);
1052 printf ("saving to %s\n", name);
1054 modelouthandle = SafeOpenWrite (name);
1056 WriteModelFile (modelouthandle);
1058 printf ("%3dx%3d skin\n", fmheader.skinwidth, fmheader.skinheight);
1059 printf ("First frame boundaries:\n");
1060 printf (" minimum x: %3f\n", g_frames[0].mins[0]);
1061 printf (" maximum x: %3f\n", g_frames[0].maxs[0]);
1062 printf (" minimum y: %3f\n", g_frames[0].mins[1]);
1063 printf (" maximum y: %3f\n", g_frames[0].maxs[1]);
1064 printf (" minimum z: %3f\n", g_frames[0].mins[2]);
1065 printf (" maximum z: %3f\n", g_frames[0].maxs[2]);
1066 printf ("%4d vertices\n", fmheader.num_xyz);
1067 printf ("%4d triangles, %4d of them translucent\n", fmheader.num_tris, trans_count);
1068 printf ("%4d frame\n", fmheader.num_frames);
1069 printf ("%4d glverts\n", numglverts);
1070 printf ("%4d glcmd\n", fmheader.num_glcmds);
1071 printf ("%4d skins\n", fmheader.num_skins);
1072 printf ("%4d mesh nodes\n", fmheader.num_mesh_nodes);
1073 printf ("wasted pixels: %d / %d (%5.2f Percent)\n",total_skin_pixels - skin_pixels_used,
1074 total_skin_pixels, (double)(total_skin_pixels - skin_pixels_used) / (double)total_skin_pixels * 100.0);
1076 printf ("file size: %d\n", (int)ftell (modelouthandle) );
1077 printf ("---------------------\n");
1081 if (fmheader.num_mesh_nodes)
1083 total_tris = total_verts = 0;
1084 printf("Node Name Tris Verts\n");
1085 printf("--------------------------------- ---- -----\n");
1086 for(i=0;i<fmheader.num_mesh_nodes;i++)
1090 for(j=0;j<MAXTRIANGLES;j++)
1093 bit = 1 << ((j) & 7 );
1094 if (pmnodes[i].tris[pos] & bit)
1099 for(j=0;j<MAX_FM_VERTS;j++)
1102 bit = 1 << ((j) & 7 );
1103 if (pmnodes[i].verts[pos] & bit)
1109 printf("%-33s %4d %5d\n",pmnodes[i].name,tris,verts);
1112 total_verts += verts;
1114 printf("--------------------------------- ---- -----\n");
1115 printf("%-33s %4d %5d\n","TOTALS",total_tris,total_verts);
1118 fclose (modelouthandle);
1120 // finish writing header file
1123 // scale_up is usefull to allow step distances to be adjusted
1124 H_printf("#define MODEL_SCALE\t\t%f\n", scale_up);
1127 if (fmheader.num_mesh_nodes)
1130 H_printf("#define NUM_MESH_NODES\t\t%d\n\n",fmheader.num_mesh_nodes);
1131 for(i=0;i<fmheader.num_mesh_nodes;i++)
1133 strcpy(name, pmnodes[i].name);
1135 length = strlen(name);
1136 for(j=0;j<length;j++)
1143 H_printf("#define MESH_%s\t\t%d\n", name, i);
1147 fclose (headerouthandle);
1148 headerouthandle = NULL;
1154 =================================================================
1156 ALIAS MODEL DISPLAY LIST GENERATION
1158 =================================================================
1161 extern int strip_xyz[128];
1162 extern int strip_st[128];
1163 extern int strip_tris[128];
1164 extern int stripcount;
1171 static int StripLength (int starttri, int startv, int num_tris, int node)
1176 fmtriangle_t *last, *check;
1182 last = &triangles[starttri];
1184 strip_xyz[0] = last->index_xyz[(startv)%3];
1185 strip_xyz[1] = last->index_xyz[(startv+1)%3];
1186 strip_xyz[2] = last->index_xyz[(startv+2)%3];
1187 strip_st[0] = last->index_st[(startv)%3];
1188 strip_st[1] = last->index_st[(startv+1)%3];
1189 strip_st[2] = last->index_st[(startv+2)%3];
1191 strip_tris[0] = starttri;
1194 m1 = last->index_xyz[(startv+2)%3];
1195 st1 = last->index_st[(startv+2)%3];
1196 m2 = last->index_xyz[(startv+1)%3];
1197 st2 = last->index_st[(startv+1)%3];
1199 // look for a matching triangle
1201 for (j=starttri+1, check=&triangles[starttri+1]
1202 ; j<num_tris ; j++, check++)
1205 bit = 1 << (j & 7 );
1206 if (!(pmnodes[node].tris[pos] & bit))
1210 for (k=0 ; k<3 ; k++)
1212 if (check->index_xyz[k] != m1)
1214 if (check->index_st[k] != st1)
1216 if (check->index_xyz[ (k+1)%3 ] != m2)
1218 if (check->index_st[ (k+1)%3 ] != st2)
1221 // this is the next part of the fan
1223 // if we can't use this triangle, this tristrip is done
1224 if (used[j] || translucent[j] != translucent[starttri])
1230 m2 = check->index_xyz[ (k+2)%3 ];
1231 st2 = check->index_st[ (k+2)%3 ];
1235 m1 = check->index_xyz[ (k+2)%3 ];
1236 st1 = check->index_st[ (k+2)%3 ];
1239 strip_xyz[stripcount+2] = check->index_xyz[ (k+2)%3 ];
1240 strip_st[stripcount+2] = check->index_st[ (k+2)%3 ];
1241 strip_tris[stripcount] = j;
1250 // clear the temp used flags
1251 for (j=starttri+1 ; j<num_tris ; j++)
1264 static int FanLength (int starttri, int startv, int num_tris, int node)
1269 fmtriangle_t *last, *check;
1275 last = &triangles[starttri];
1277 strip_xyz[0] = last->index_xyz[(startv)%3];
1278 strip_xyz[1] = last->index_xyz[(startv+1)%3];
1279 strip_xyz[2] = last->index_xyz[(startv+2)%3];
1280 strip_st[0] = last->index_st[(startv)%3];
1281 strip_st[1] = last->index_st[(startv+1)%3];
1282 strip_st[2] = last->index_st[(startv+2)%3];
1284 strip_tris[0] = starttri;
1287 m1 = last->index_xyz[(startv+0)%3];
1288 st1 = last->index_st[(startv+0)%3];
1289 m2 = last->index_xyz[(startv+2)%3];
1290 st2 = last->index_st[(startv+2)%3];
1293 // look for a matching triangle
1295 for (j=starttri+1, check=&triangles[starttri+1]
1296 ; j<num_tris ; j++, check++)
1299 bit = 1 << (j & 7 );
1300 if (!(pmnodes[node].tris[pos] & bit))
1304 for (k=0 ; k<3 ; k++)
1306 if (check->index_xyz[k] != m1)
1308 if (check->index_st[k] != st1)
1310 if (check->index_xyz[ (k+1)%3 ] != m2)
1312 if (check->index_st[ (k+1)%3 ] != st2)
1315 // this is the next part of the fan
1317 // if we can't use this triangle, this tristrip is done
1318 if (used[j] || translucent[j] != translucent[starttri])
1322 m2 = check->index_xyz[ (k+2)%3 ];
1323 st2 = check->index_st[ (k+2)%3 ];
1325 strip_xyz[stripcount+2] = m2;
1326 strip_st[stripcount+2] = st2;
1327 strip_tris[stripcount] = j;
1336 // clear the temp used flags
1337 for (j=starttri+1 ; j<num_tris ; j++)
1350 Generate a list of trifans or strips
1351 for the model, which holds for all frames
1354 static void BuildGlCmds (void)
1359 int len, bestlen, besttype;
1362 int best_tris[1024];
1374 for(l=0;l<fmheader.num_mesh_nodes;l++)
1376 memset (used, 0, sizeof(used));
1378 pmnodes[l].start_glcmds = numcommands;
1380 for(trans_check = 0; trans_check<2; trans_check++)
1382 for (i=0 ; i < fmheader.num_tris ; i++)
1385 bit = 1 << (i & 7 );
1386 if (!(pmnodes[l].tris[pos] & bit))
1391 // pick an unused triangle and start the trifan
1392 if (used[i] || trans_check != translucent[i])
1398 for (type = 0 ; type < 2 ; type++)
1401 for (startv =0 ; startv < 3 ; startv++)
1404 len = StripLength (i, startv, fmheader.num_tris, l);
1406 len = FanLength (i, startv, fmheader.num_tris, l);
1411 for (j=0 ; j<bestlen+2 ; j++)
1413 best_st[j] = strip_st[j];
1414 best_xyz[j] = strip_xyz[j];
1416 for (j=0 ; j<bestlen ; j++)
1417 best_tris[j] = strip_tris[j];
1422 // mark the tris on the best strip/fan as used
1423 for (j=0 ; j<bestlen ; j++)
1424 used[best_tris[j]] = 1;
1427 commands[numcommands++] = (bestlen+2);
1429 commands[numcommands++] = -(bestlen+2);
1431 numglverts += bestlen+2;
1433 for (j=0 ; j<bestlen+2 ; j++)
1435 // emit a vertex into the reorder buffer
1438 // emit s/t coords into the commands stream
1442 s = (s ) / fmheader.skinwidth;
1443 t = (t ) / fmheader.skinheight;
1445 *(float *)&commands[numcommands++] = s;
1446 *(float *)&commands[numcommands++] = t;
1447 *(int *)&commands[numcommands++] = best_xyz[j];
1451 commands[numcommands++] = 0; // end of list marker
1452 pmnodes[l].num_glcmds = numcommands - pmnodes[l].start_glcmds;
1458 ===============================================================
1462 ===============================================================
1466 #define LINE_NORMAL 1
1468 #define LINE_DOTTED 3
1471 #define ASCII_SPACE 32
1473 int LineType = LINE_NORMAL;
1474 extern unsigned char pic[SKINPAGE_HEIGHT*SKINPAGE_WIDTH], pic_palette[768];
1475 unsigned char LineColor = 255;
1476 int ScaleWidth, ScaleHeight;
1479 static char *CharDefs[] =
1481 "-------------------------",
1482 "-------------------------", // !
1483 "-------------------------", // "
1484 "-------------------------", // #
1485 "-------------------------", // $
1486 "-------------------------", // %
1487 "-------------------------", // &
1488 "--*----*-----------------", // '
1489 "-*---*----*----*-----*---", // (
1490 "*-----*----*----*---*----", // )
1491 "-----*--*--**---**--*--*-", // *
1492 "-------------------------", // +
1493 "----------------**--**---", // ,
1494 "-------------------------", // -
1495 "----------------**---**--", // .
1496 "-------------------------", // /
1497 " *** * *** * *** * *** ", // 0
1499 "**** * *** * *****",
1500 "**** * *** ***** ",
1501 " ** * * * * ***** * ",
1502 "**** * **** ***** ",
1503 " *** * **** * * *** ",
1505 " *** * * *** * * *** ",
1506 " *** * * **** * *** ", // 9
1507 "-**---**--------**---**--", // :
1508 "-------------------------", // ;
1509 "-------------------------", // <
1510 "-------------------------", // =
1511 "-------------------------", // >
1512 "-------------------------", // ?
1513 "-------------------------", // @
1514 "-***-*---*******---**---*", // A
1515 "****-*---*****-*---*****-",
1516 "-*****----*----*-----****",
1517 "****-*---**---**---*****-",
1518 "******----****-*----*****",
1519 "******----****-*----*----",
1520 "-*****----*--***---*-****",
1521 "*---**---*******---**---*",
1522 "-***---*----*----*---***-",
1523 "----*----*----**---*-***-",
1524 "-*--*-*-*--**---*-*--*--*",
1525 "-*----*----*----*----****",
1526 "*---***-***-*-**---**---*",
1527 "*---***--**-*-**--***---*",
1528 "-***-*---**---**---*-***-",
1529 "****-*---*****-*----*----",
1530 "-***-*---**---*-***----**",
1531 "****-*---*****-*-*--*--**",
1532 "-*****-----***-----*****-",
1533 "*****--*----*----*----*--",
1534 "*---**---**---**---******",
1535 "*---**---**---*-*-*---*--",
1536 "*---**---**-*-***-***---*",
1537 "*---*-*-*---*---*-*-*---*",
1538 "*---**---*-*-*---*----*--",
1539 "*****---*---*---*---*****" // Z
1542 void DrawLine(int x1, int y1, int x2, int y2)
1547 float xfrac, yfrac, xstep, ystep;
1556 count = adx > ady ? adx : ady;
1561 printf("Bad count\n");
1562 return; // don't ever hang up on bad data
1568 xstep = (float)dx/count;
1569 ystep = (float)dy/count;
1576 if(xfrac < SKINPAGE_WIDTH && yfrac < SKINPAGE_HEIGHT)
1578 pic[(int)yfrac*SKINPAGE_WIDTH+(int)xfrac] = LineColor;
1583 } while (count > 0);
1588 for (u=-0.1 ; u<=0.9 ; u+=0.999)
1590 for (v=-0.1 ; v<=0.9 ; v+=0.999)
1594 if(sx < SKINPAGE_WIDTH && sy < SKINPAGE_HEIGHT)
1596 pic[sy*SKINPAGE_WIDTH+sx] = LineColor;
1603 } while (count > 0);
1608 if(count&1 && xfrac < SKINPAGE_WIDTH &&
1609 yfrac < SKINPAGE_HEIGHT)
1611 pic[(int)yfrac*SKINPAGE_WIDTH+(int)xfrac] = LineColor;
1616 } while (count > 0);
1619 Error("Unknown <linetype> %d.\n", LineType);
1623 //==========================================================================
1627 //==========================================================================
1629 static void DrawCharacter(int x, int y, int character)
1634 character = toupper(character);
1635 if(character < ASCII_SPACE || character > 'Z')
1637 character = ASCII_SPACE;
1639 character -= ASCII_SPACE;
1640 for(def = CharDefs[character], r = 0; r < 5; r++)
1642 for(c = 0; c < 5; c++)
1644 pic[(y+r)*SKINPAGE_WIDTH+x+c] = *def++ == '*' ? 255 : 0;
1649 //==========================================================================
1653 //==========================================================================
1655 void DrawTextChar(int x, int y, char *text)
1659 while((c = *text++) != '\0')
1661 DrawCharacter(x, y, c);
1667 extern void DrawScreen(float s_scale, float t_scale, float iwidth, float iheight);
1669 //==========================================================================
1672 static int ExtractDigit(byte *pic, int x, int y)
1681 backColor = pic[(SKINPAGE_HEIGHT - 1) * SKINPAGE_WIDTH];
1682 DigitDefs = &CharDefs['0' - ASCII_SPACE];
1685 for(r = 0; r < 5; r++)
1687 for(c = 0; c < 5; c++)
1689 *buffer++ = (pic[(y + r) * SKINPAGE_WIDTH + x + c] == backColor) ? ' ' : '*';
1693 for(i = 0; i < 10; i++)
1695 if(strcmp(DigitDefs[i], digString) == 0)
1701 Error("Unable to extract scaling info from skin PCX.");
1705 //==========================================================================
1708 int ExtractNumber(byte *pic, int x, int y)
1710 return ExtractDigit(pic, x, y) * 100 + ExtractDigit(pic, x + 6, y) * 10 + ExtractDigit(pic, x + 12, y);
1721 Builds the triangle_st array for the base frame and
1722 fmheader.skinwidth / fmheader.skinheight
1724 FIXME: allow this to be loaded from a file for
1728 static void BuildST (triangle_t *ptri, int numtri, qboolean DrawSkin)
1732 int width, height, iwidth, iheight, swidth;
1737 vec3_t vtemp1, vtemp2, normal;
1738 float s_scale, t_scale;
1745 // find bounds of all the verts on the base frame
1747 ClearBounds (mins, maxs);
1748 backface_flag = false;
1750 if (ptri[0].HasUV) // if we have the uv already, we don't want to double up or scale
1752 iwidth = ScaleWidth;
1753 iheight = ScaleHeight;
1755 t_scale = s_scale = 1.0;
1759 for (i=0 ; i<numtri ; i++)
1760 for (j=0 ; j<3 ; j++)
1761 AddPointToBounds (ptri[i].verts[j], mins, maxs);
1763 for (i=0 ; i<3 ; i++)
1765 mins[i] = floor(mins[i]);
1766 maxs[i] = ceil(maxs[i]);
1769 width = maxs[0] - mins[0];
1770 height = maxs[2] - mins[2];
1772 for (i=0 ; i<numtri ; i++)
1774 VectorSubtract (ptri[i].verts[0], ptri[i].verts[1], vtemp1);
1775 VectorSubtract (ptri[i].verts[2], ptri[i].verts[1], vtemp2);
1776 CrossProduct (vtemp1, vtemp2, normal);
1780 backface_flag = true;
1784 scWidth = ScaleWidth*SCALE_ADJUST_FACTOR;
1785 if (backface_flag) //we are doubling
1788 scHeight = ScaleHeight*SCALE_ADJUST_FACTOR;
1790 scale = scWidth/width;
1792 if(height*scale >= scHeight)
1794 scale = scHeight/height;
1797 iwidth = ceil(width*scale)+4;
1798 iheight = ceil(height*scale)+4;
1800 s_scale = (float)(iwidth-4) / width;
1801 t_scale = (float)(iheight-4) / height;
1807 DrawScreen(s_scale, t_scale, iwidth*2, iheight);
1809 DrawScreen(s_scale, t_scale, iwidth, iheight);
1818 /* if (!g_fixedwidth)
1821 if (width*scale >= 150)
1822 scale = 150.0 / width;
1823 if (height*scale >= 190)
1824 scale = 190.0 / height;
1826 s_scale = t_scale = scale;
1828 iwidth = ceil(width*s_scale);
1829 iheight = ceil(height*t_scale);
1836 iwidth = g_fixedwidth / 2;
1837 iheight = g_fixedheight;
1839 s_scale = (float)(iwidth-4) / width;
1840 t_scale = (float)(iheight-4) / height;
1844 // determine which side of each triangle to map the texture to
1847 for (i=0 ; i<numtri ; i++)
1851 for (j=0 ; j<3 ; j++)
1853 triangle_st[i][j][0] = Q_rint(ptri[i].uv[j][0]*skinwidth);
1854 triangle_st[i][j][1] = Q_rint((1.0f-ptri[i].uv[j][1])*skinheight);
1859 VectorSubtract (ptri[i].verts[0], ptri[i].verts[1], vtemp1);
1860 VectorSubtract (ptri[i].verts[2], ptri[i].verts[1], vtemp2);
1861 CrossProduct (vtemp1, vtemp2, normal);
1872 for (j=0 ; j<3 ; j++)
1874 pbasevert = ptri[i].verts[j];
1876 triangle_st[i][j][0] = Q_rint((pbasevert[0] - mins[0]) * s_scale + basex);
1877 triangle_st[i][j][1] = Q_rint((maxs[2] - pbasevert[2]) * t_scale + basey);
1883 DrawLine(triangle_st[i][0][0], triangle_st[i][0][1],
1884 triangle_st[i][1][0], triangle_st[i][1][1]);
1885 DrawLine(triangle_st[i][1][0], triangle_st[i][1][1],
1886 triangle_st[i][2][0], triangle_st[i][2][1]);
1887 DrawLine(triangle_st[i][2][0], triangle_st[i][2][1],
1888 triangle_st[i][0][0], triangle_st[i][0][1]);
1892 // make the width a multiple of 4; some hardware requires this, and it ensures
1893 // dword alignment for each scan
1898 fmheader.skinwidth = (swidth + 3) & ~3;
1899 fmheader.skinheight = iheight;
1901 skin_width = iwidth;
1902 skin_height = iheight;
1906 static void BuildNewST (triangle_t *ptri, int numtri, qboolean DrawSkin)
1910 for (i=0 ; i<numtri ; i++)
1914 for (j=0 ; j<3 ; j++)
1916 triangle_st[i][j][0] = Q_rint(ptri[i].uv[j][0]*(ScaleWidth-1));
1917 triangle_st[i][j][1] = Q_rint((1.0f-ptri[i].uv[j][1])*(ScaleHeight-1));
1923 DrawLine(triangle_st[i][0][0], triangle_st[i][0][1],
1924 triangle_st[i][1][0], triangle_st[i][1][1]);
1925 DrawLine(triangle_st[i][1][0], triangle_st[i][1][1],
1926 triangle_st[i][2][0], triangle_st[i][2][1]);
1927 DrawLine(triangle_st[i][2][0], triangle_st[i][2][1],
1928 triangle_st[i][0][0], triangle_st[i][0][1]);
1932 // make the width a multiple of 4; some hardware requires this, and it ensures
1933 // dword alignment for each scan
1935 fmheader.skinwidth = (ScaleWidth + 3) & ~3;
1936 fmheader.skinheight = ScaleHeight;
1938 skin_width = ScaleWidth;
1939 skin_height = ScaleHeight;
1946 byte *BasePixels,*TransPixels;
1947 int BaseWidth, BaseHeight, TransWidth, TransHeight;
1948 qboolean BaseTrueColor;
1949 static qboolean SetPixel = false;
1951 int CheckTransRecursiveTri (int *lp1, int *lp2, int *lp3)
1957 d = lp2[0] - lp1[0];
1958 if (d < -1 || d > 1)
1960 d = lp2[1] - lp1[1];
1961 if (d < -1 || d > 1)
1964 d = lp3[0] - lp2[0];
1965 if (d < -1 || d > 1)
1967 d = lp3[1] - lp2[1];
1968 if (d < -1 || d > 1)
1971 d = lp1[0] - lp3[0];
1972 if (d < -1 || d > 1)
1974 d = lp1[1] - lp3[1];
1975 if (d < -1 || d > 1)
1986 return 0; // entire tri is filled
1996 new[0] = (lp1[0] + lp2[0]) >> 1;
1997 new[1] = (lp1[1] + lp2[1]) >> 1;
1999 // draw the point if splitting a leading edge
2000 if (lp2[1] > lp1[1])
2002 if ((lp2[1] == lp1[1]) && (lp2[0] < lp1[0]))
2007 assert ((new[1]*BaseWidth) + new[0] < BaseWidth*BaseHeight);
2011 BasePixels[((new[1]*BaseWidth) + new[0]) * 4] = 1;
2015 BasePixels[(new[1]*BaseWidth) + new[0]] = 1;
2022 if (TransPixels[(new[1]*TransWidth) + new[0]] != 255)
2025 else if (BaseTrueColor)
2027 if (BasePixels[(((new[1]*BaseWidth) + new[0]) * 4) + 3] != 255)
2032 // pixel = BasePixels[(new[1]*BaseWidth) + new[0]];
2037 // recursively continue
2038 if (CheckTransRecursiveTri(lp3, lp1, new))
2041 return CheckTransRecursiveTri(lp3, new, lp2);
2044 static void ReplaceClusterIndex(int newIndex, int oldindex, int **clusters,
2045 IntListNode_t **vertLists, int *num_verts, int *new_num_verts)
2048 IntListNode_t *next;
2050 for(j = 0; j < numJointsInSkeleton[g_skelModel.type]; ++j)
2057 for(i = 0; i < num_verts[j+1]; ++i)
2059 if(clusters[j][i] == oldindex)
2061 ++new_num_verts[j+1];
2063 next = vertLists[j];
2065 vertLists[j] = (IntListNode_t *) SafeMalloc(sizeof(IntListNode_t), "ReplaceClusterIndex");
2066 // Currently freed in WriteJointedModelFile only
2068 vertLists[j]->data = newIndex;
2069 vertLists[j]->next = next;
2075 #define FUDGE_EPSILON 0.002
2077 qboolean VectorFudgeCompare (vec3_t v1, vec3_t v2)
2081 for (i=0 ; i<3 ; i++)
2082 if (fabs(v1[i]-v2[i]) > FUDGE_EPSILON)
2093 void Cmd_FMBase (qboolean GetST)
2095 triangle_t *ptri, *st_tri;
2100 char file1[1024],file2[1024],trans_file[1024], stfile[1024], extension[256];
2101 vec3_t base_xyz[MAX_FM_VERTS];
2106 GetScriptToken (false);
2108 if (g_skipmodel || g_release || g_archive)
2111 printf ("---------------------\n");
2112 sprintf (file1, "%s/%s.%s", cdarchive, token, trifileext);
2113 printf ("%s ", file1);
2115 ExpandPathAndArchive (file1);
2117 // Use the input filepath for this one.
2118 sprintf (file1, "%s/%s", cddir, token);
2120 // time1 = FileTime (file1);
2122 // Error ("%s doesn't exist", file1);
2125 // load the base triangles
2128 Load3DSTriangleList (file1, &ptri, &fmheader.num_tris, &pmnodes, &fmheader.num_mesh_nodes);
2130 LoadTriangleList (file1, &ptri, &fmheader.num_tris, &pmnodes, &fmheader.num_mesh_nodes);
2134 for (i=0;i<fmheader.num_tris;i++)
2140 GetScriptToken (false);
2141 sprintf (file2, "%s/%s", cddir, token);
2142 sprintf (trans_file, "%s/!%s_a.pcx", cddir, token);
2144 ExtractFileExtension (file2, extension);
2145 if (extension[0] == 0)
2147 strcat(file2, ".pcx");
2149 printf ("skin: %s\n", file2);
2151 BaseTrueColor = LoadAnyImage (file2, &BasePixels, &BasePalette, &BaseWidth, &BaseHeight);
2154 if (BaseWidth != SKINPAGE_WIDTH || BaseHeight != SKINPAGE_HEIGHT)
2156 if (g_allow_newskin)
2158 ScaleWidth = BaseWidth;
2159 ScaleHeight = BaseHeight;
2164 Error("Invalid skin page size: (%d,%d) should be (%d,%d)",
2165 BaseWidth,BaseHeight,SKINPAGE_WIDTH,SKINPAGE_HEIGHT);
2168 else if (!BaseTrueColor)
2170 ScaleWidth = (float)ExtractNumber(BasePixels, ENCODED_WIDTH_X,
2172 ScaleHeight = (float)ExtractNumber(BasePixels, ENCODED_HEIGHT_X,
2177 Error("Texture coordinates not supported on true color image");
2182 GetScriptToken (false);
2184 sprintf (stfile, "%s/%s.%s", cdarchive, token, trifileext);
2185 printf ("ST: %s ", stfile);
2187 sprintf (stfile, "%s/%s", cddir, token);
2190 Load3DSTriangleList (stfile, &st_tri, &num_st_tris, NULL, NULL);
2192 LoadTriangleList (stfile, &st_tri, &num_st_tris, NULL, NULL);
2194 if (num_st_tris != fmheader.num_tris)
2196 Error ("num st tris mismatch: st %d / base %d", num_st_tris, fmheader.num_tris);
2199 printf(" matching triangles...\n");
2200 for(i=0;i<fmheader.num_tris;i++)
2203 for(j=0;j<num_st_tris;j++)
2215 if (z == x || z == y)
2220 if (VectorFudgeCompare (ptri[i].verts[0], st_tri[j].verts[x]) &&
2221 VectorFudgeCompare (ptri[i].verts[1], st_tri[j].verts[y]) &&
2222 VectorFudgeCompare (ptri[i].verts[2], st_tri[j].verts[z]))
2227 ptri[i].HasUV = st_tri[k].HasUV;
2228 ptri[i].uv[0][0] = st_tri[k].uv[x][0];
2229 ptri[i].uv[0][1] = st_tri[k].uv[x][1];
2230 ptri[i].uv[1][0] = st_tri[k].uv[y][0];
2231 ptri[i].uv[1][1] = st_tri[k].uv[y][1];
2232 ptri[i].uv[2][0] = st_tri[k].uv[z][0];
2233 ptri[i].uv[2][1] = st_tri[k].uv[z][1];
2238 printf("Duplicate triangle %d found in st file: %d and %d\n",i,k,j);
2239 printf(" (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f)\n",
2240 ptri[i].verts[0][0],ptri[i].verts[0][1],ptri[i].verts[0][2],
2241 ptri[i].verts[1][0],ptri[i].verts[1][1],ptri[i].verts[1][2],
2242 ptri[i].verts[2][0],ptri[i].verts[2][1],ptri[i].verts[2][2]);
2243 printf(" (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f)\n",
2244 st_tri[k].verts[0][0],st_tri[k].verts[0][1],st_tri[k].verts[0][2],
2245 st_tri[k].verts[1][0],st_tri[k].verts[1][1],st_tri[k].verts[1][2],
2246 st_tri[k].verts[2][0],st_tri[k].verts[2][1],st_tri[k].verts[2][2]);
2247 printf(" (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f)\n",
2248 st_tri[j].verts[0][0],st_tri[j].verts[0][1],st_tri[j].verts[0][2],
2249 st_tri[j].verts[1][0],st_tri[j].verts[1][1],st_tri[j].verts[1][2],
2250 st_tri[j].verts[2][0],st_tri[j].verts[2][1],st_tri[j].verts[2][2]);
2259 printf("No matching triangle %d\n",i);
2266 // get the ST values
2268 if (ptri && ptri[0].HasUV)
2272 Error("Base has UVs with old style skin page\nMaybe you want to use -ignoreUV");
2276 BuildNewST (ptri, fmheader.num_tris, false);
2283 Error("Base has new style skin without UVs");
2287 BuildST (ptri, fmheader.num_tris, false);
2294 FH = fopen(trans_file,"rb");
2298 Load256Image (trans_file, &TransPixels, NULL, &TransWidth, &TransHeight);
2299 if (TransWidth != fmheader.skinwidth || TransHeight != fmheader.skinheight)
2301 Error ("source image %s dimensions (%d,%d) are not the same as alpha image (%d,%d)\n",file2,fmheader.skinwidth,fmheader.skinheight,TransWidth,TransHeight);
2307 // run through all the base triangles, storing each unique vertex in the
2308 // base vertex list and setting the indirect triangles to point to the base
2311 for(l=0;l<fmheader.num_mesh_nodes;l++)
2313 for (i=0 ; i < fmheader.num_tris ; i++)
2316 bit = 1 << (i & 7 );
2317 if (!(pmnodes[l].tris[pos] & bit))
2322 for (j=0 ; j<3 ; j++)
2324 // get the xyz index
2325 for (k=0 ; k<fmheader.num_xyz ; k++)
2327 if (VectorCompare (ptri[i].verts[j], base_xyz[k]))
2329 break; // this vertex is already in the base vertex list
2333 if (k == fmheader.num_xyz)
2335 VectorCopy (ptri[i].verts[j], base_xyz[fmheader.num_xyz]);
2337 if(pmnodes[l].clustered == true)
2339 ReplaceClusterIndex(k, ptri[i].indicies[j], (int **)&pmnodes[l].clusters, (IntListNode_t **)&g_skelModel.vertLists, (int *)&pmnodes[l].num_verts, (int *)&g_skelModel.new_num_verts);
2347 pmnodes[l].verts[pos] |= bit;
2349 triangles[i].index_xyz[j] = k;
2352 for (k=0 ; k<fmheader.num_st ; k++)
2354 if (triangle_st[i][j][0] == base_st[k].s
2355 && triangle_st[i][j][1] == base_st[k].t)
2357 break; // this vertex is already in the base vertex list
2361 if (k == fmheader.num_st)
2363 base_st[fmheader.num_st].s = triangle_st[i][j][0];
2364 base_st[fmheader.num_st].t = triangle_st[i][j][1];
2368 triangles[i].index_st[j] = k;
2371 if (TransPixels || BaseTrueColor)
2373 translucent[i] = CheckTransRecursiveTri(triangle_st[i][0], triangle_st[i][1], triangle_st[i][2]);
2377 translucent[i] = false;
2385 memset(BasePixels,0,BaseWidth*BaseHeight);
2386 for (i=0 ; i < fmheader.num_tris ; i++)
2388 CheckTransRecursiveTri(triangle_st[i][0], triangle_st[i][1], triangle_st[i][2]);
2392 skin_pixels_used = 0;
2393 for(i=0;i<fmheader.skinheight;i++)
2395 for(j=0;j<fmheader.skinwidth;j++)
2397 skin_pixels_used += BasePixels[(i*BaseWidth) + j];
2400 total_skin_pixels = fmheader.skinheight*fmheader.skinwidth;
2405 memset(BasePixels,0,BaseWidth*BaseHeight*4);
2406 for (i=0 ; i < fmheader.num_tris ; i++)
2408 CheckTransRecursiveTri(triangle_st[i][0], triangle_st[i][1], triangle_st[i][2]);
2412 skin_pixels_used = 0;
2413 for(i=0;i<fmheader.skinheight;i++)
2415 for(j=0;j<fmheader.skinwidth;j++)
2417 skin_pixels_used += BasePixels[((i*BaseWidth) + j)*4];
2420 total_skin_pixels = fmheader.skinheight*fmheader.skinwidth;
2423 // build triangle strips / fans
2438 void Cmd_FMNodeOrder(void)
2440 mesh_node_t *newnodes, *pos;
2445 Error ("Base has not been established yet");
2448 pos = newnodes = malloc(sizeof(mesh_node_t) * fmheader.num_mesh_nodes);
2450 for(i=0;i<fmheader.num_mesh_nodes;i++)
2452 GetScriptToken (false);
2454 for(j=0;j<fmheader.num_mesh_nodes;j++)
2456 if (strcmpi(pmnodes[j].name, token) == 0)
2463 if (j >= fmheader.num_mesh_nodes)
2465 Error("Node '%s' not in base list!\n", token);
2473 //===============================================================
2475 extern char *FindFrameFile (char *frame);
2483 void GrabFrame (char *frame)
2487 fmtrivert_t *ptrivert;
2494 // the frame 'run1' will be looked for as either
2495 // run.1 or run1.tri, so the new alias sequence save
2496 // feature an be used
2497 framefile = FindFrameFile (frame);
2499 sprintf (file1, "%s/%s", cdarchive, framefile);
2500 ExpandPathAndArchive (file1);
2502 sprintf (file1, "%s/%s",cddir, framefile);
2504 printf ("grabbing %s ", file1);
2506 if (fmheader.num_frames >= MAX_FM_FRAMES)
2507 Error ("fmheader.num_frames >= MAX_FM_FRAMES");
2508 fr = &g_frames[fmheader.num_frames];
2509 fmheader.num_frames++;
2511 strcpy (fr->name, frame);
2517 Load3DSTriangleList (file1, &ptri, &num_tris, NULL, NULL);
2519 LoadTriangleList (file1, &ptri, &num_tris, NULL, NULL);
2521 if (num_tris != fmheader.num_tris)
2522 Error ("%s: number of triangles (%d) doesn't match base frame (%d)\n", file1, num_tris, fmheader.num_tris);
2525 // allocate storage for the frame's vertices
2529 for (i=0 ; i<fmheader.num_xyz ; i++)
2531 ptrivert[i].vnorm.numnormals = 0;
2532 VectorClear (ptrivert[i].vnorm.normalsum);
2534 ClearBounds (fr->mins, fr->maxs);
2537 // store the frame's vertices in the same order as the base. This assumes the
2538 // triangles and vertices in this frame are in exactly the same order as in the
2541 for (i=0 ; i<num_tris ; i++)
2543 vec3_t vtemp1, vtemp2, normal;
2546 VectorSubtract (ptri[i].verts[0], ptri[i].verts[1], vtemp1);
2547 VectorSubtract (ptri[i].verts[2], ptri[i].verts[1], vtemp2);
2548 CrossProduct (vtemp1, vtemp2, normal);
2550 VectorNormalize (normal, normal);
2552 // rotate the normal so the model faces down the positive x axis
2554 normal[0] = -normal[1];
2557 for (j=0 ; j<3 ; j++)
2559 index_xyz = triangles[i].index_xyz[j];
2561 // rotate the vertices so the model faces down the positive x axis
2562 // also adjust the vertices to the desired origin
2563 ptrivert[index_xyz].v[0] = ((-ptri[i].verts[j][1]) * scale_up) +
2565 ptrivert[index_xyz].v[1] = (ptri[i].verts[j][0] * scale_up) +
2567 ptrivert[index_xyz].v[2] = (ptri[i].verts[j][2] * scale_up) +
2570 AddPointToBounds (ptrivert[index_xyz].v, fr->mins, fr->maxs);
2572 VectorAdd (ptrivert[index_xyz].vnorm.normalsum, normal, ptrivert[index_xyz].vnorm.normalsum);
2573 ptrivert[index_xyz].vnorm.numnormals++;
2578 // calculate the vertex normals, match them to the template list, and store the
2579 // index of the best match
2581 for (i=0 ; i<fmheader.num_xyz ; i++)
2589 c = ptrivert[i].vnorm.numnormals;
2591 Error ("Vertex with no triangles attached");
2593 VectorScale (ptrivert[i].vnorm.normalsum, 1.0/c, v);
2594 VectorNormalize (v, v);
2599 for (j=0 ; j<NUMVERTEXNORMALS ; j++)
2603 dot = DotProduct (v, avertexnormals[j]);
2611 ptrivert[i].lightnormalindex = maxdotindex;
2622 void Cmd_FMFrame (void)
2624 while (ScriptTokenAvailable())
2626 GetScriptToken (false);
2629 if (g_release || g_archive)
2631 fmheader.num_frames = 1; // don't skip the writeout
2635 H_printf("#define FRAME_%-16s\t%i\n", token, fmheader.num_frames);
2637 if((g_skelModel.type != SKEL_NULL) || (g_skelModel.references != REF_NULL))
2639 GrabModelTransform(token);
2644 if(g_skelModel.type != SKEL_NULL)
2646 GrabSkeletalFrame(token);
2649 if(g_skelModel.references != REF_NULL)
2651 GrabReferencedFrame(token);
2654 // need to add the up and dir points to the frame bounds here
2655 // using AddPointToBounds (ptrivert[index_xyz].v, fr->mins, fr->maxs);
2656 // then remove fudge in determining scale on frame write out
2664 Skins aren't actually stored in the file, only a reference
2665 is saved out to the header file.
2668 void Cmd_FMSkin (void)
2675 char name[1024], savename[1024], transname[1024], extension[256];
2681 GetScriptToken (false);
2683 if (fmheader.num_skins == MAX_FM_SKINS)
2684 Error ("fmheader.num_skins == MAX_FM_SKINS");
2689 sprintf (name, "%s/%s", cdarchive, token);
2690 strcpy (name, ExpandPathAndArchive( name ) );
2691 // sprintf (name, "%s/%s.lbm", cddir, token);
2693 if (ScriptTokenAvailable())
2695 GetScriptToken (false);
2696 sprintf (g_skins[fmheader.num_skins], "!%s", token);
2697 sprintf (savename, "%s!%s", g_outputDir, token);
2698 sprintf (transname, "%s!%s_a.pcx", gamedir, token);
2702 sprintf (g_skins[fmheader.num_skins], "%s/!%s", cdpartial, token);
2703 sprintf (savename, "%s/!%s", g_outputDir, token);
2704 sprintf (transname, "%s/!%s_a.pcx", cddir, token);
2707 fmheader.num_skins++;
2709 if (g_skipmodel || g_release || g_archive)
2713 printf ("loading %s\n", name);
2714 ExtractFileExtension (name, extension);
2715 if (extension[0] == 0)
2717 strcat(name, ".pcx");
2721 TrueColor = LoadAnyImage (name, &pixels, &palette, &width, &height);
2722 // RemapZero (pixels, palette, width, height);
2724 // crop it to the proper size
2728 cropped = (byte *) SafeMalloc (fmheader.skinwidth*fmheader.skinheight, "Cmd_FMSkin");
2729 for (y=0 ; y<fmheader.skinheight ; y++)
2731 memcpy (cropped+y*fmheader.skinwidth,
2732 pixels+y*width, fmheader.skinwidth);
2736 FH = fopen(transname,"rb");
2741 strcat(g_skins[fmheader.num_skins-1],".pcx");
2742 strcat(savename,".pcx");
2744 // save off the new image
2745 printf ("saving %s\n", savename);
2746 CreatePath (savename);
2747 WritePCXfile (savename, cropped, fmheader.skinwidth, fmheader.skinheight, palette);
2753 qtex = CreateMip(cropped, fmheader.skinwidth, fmheader.skinheight, palette, &size, true);
2755 strcat(g_skins[fmheader.num_skins-1],".m8");
2756 strcat(savename,".m8");
2758 printf ("saving %s\n", savename);
2759 CreatePath (savename);
2760 SaveFile (savename, (byte *)qtex, size);
2763 strcat(g_skins[fmheader.num_skins-1],".pcx");
2764 strcat(savename,".pcx");
2766 // save off the new image
2767 printf ("saving %s\n", savename);
2768 CreatePath (savename);
2769 WritePCXfile (savename, cropped, fmheader.skinwidth, fmheader.skinheight, palette);
2775 cropped = (byte *) SafeMalloc (fmheader.skinwidth*fmheader.skinheight*4, "Cmd_FMSkin");
2776 for (y=0 ; y<fmheader.skinheight ; y++)
2778 memcpy (cropped+((y*fmheader.skinwidth)*4), pixels+(y*width*4), fmheader.skinwidth*4);
2781 qtex32 = CreateMip32((unsigned *)cropped, fmheader.skinwidth, fmheader.skinheight, &size, true);
2783 StripExtension(g_skins[fmheader.num_skins-1]);
2784 strcat(g_skins[fmheader.num_skins-1],".m32");
2785 StripExtension(savename);
2786 strcat(savename,".m32");
2788 printf ("saving %s\n", savename);
2789 CreatePath (savename);
2790 SaveFile (savename, (byte *)qtex32, size);
2807 void Cmd_FMCd (void)
2814 GetScriptToken (false);
2816 // this is a silly mess...
2817 sprintf(cdpartial, "models/%s", token);
2818 sprintf(cdarchive, "%smodels/%s", gamedir+strlen(qdir), token);
2819 sprintf(cddir, "%s%s", gamedir, cdpartial);
2821 // Since we also changed directories on the output side (for mirror) make sure the outputdir is set properly too.
2822 sprintf(temp, "%s%s", g_outputDir, cdpartial);
2823 strcpy(g_outputDir, temp);
2825 // if -only was specified and this cd doesn't match,
2826 // skip the model (you only need to match leading chars,
2827 // so you could regrab all monsters with -only monsters)
2830 if (strncmp(token, g_only, strlen(g_only)))
2833 printf ("skipping %s\n", cdpartial);
2840 //=======================
2842 //=======================
2844 void NewGen (char *ModelFile, char *OutputName, int width, int height)
2846 trigroup_t *triangles;
2848 triangle_t *grouptris;
2849 mesh_node_t *pmnodes;
2853 vec3_t aveNorm, crossvect;
2854 vec3_t diffvect1, diffvect2;
2857 vec3_t base, zaxis, yaxis;
2858 vec3_t uvwMin, uvwMax;
2859 vec3_t groupMin, groupMax;
2862 float *uFinal, *vFinal;
2863 unsigned char *newpic;
2865 int finalstart = 0, finalcount = 0;
2866 int xbase = 0, xwidth = 0, ywidth = 0;
2867 int *todo, *done, finished;
2868 int i, j, k, l; //counters
2869 int groupnum, numtris, numverts, num;
2874 for ( i = 0; i<3; i++)
2893 LoadTriangleList (ModelFile, &ptri, &fmheader.num_tris, &pmnodes, &fmheader.num_mesh_nodes);
2895 todo = (int*)SafeMalloc(fmheader.num_tris*sizeof(int), "NewGen");
2896 done = (int*)SafeMalloc(fmheader.num_tris*sizeof(int), "NewGen");
2897 triangles = (trigroup_t*)SafeMalloc(fmheader.num_tris*sizeof(trigroup_t), "NewGen");
2899 for ( i=0; i < fmheader.num_tris; i++)
2903 triangles[i].triangle = ptri[i];
2904 triangles[i].group = 0;
2909 // transitive closure algorithm follows
2910 // put all triangles who transitively share vertices into separate groups
2914 for ( i = 0; i < fmheader.num_tris; i++)
2921 if ( i == fmheader.num_tris)
2930 for ( i = 0; i < fmheader.num_tris; i++)
2935 triangles[i].group = groupnum;
2937 for ( j = 0; j < fmheader.num_tris; j++)
2939 if ((!done[j]) && (ShareVertex(triangles[i],triangles[j])))
2950 uFinal = (float*)SafeMalloc(3*fmheader.num_tris*sizeof(float), "NewGen");
2951 vFinal = (float*)SafeMalloc(3*fmheader.num_tris*sizeof(float), "NewGen");
2953 grpfile = fopen("grpdebug.txt","w");
2956 for (i = 0; i < groupnum; i++)
2959 fprintf(grpfile,"Group Number: %d\n", i);
2961 numtris = GetNumTris(triangles, i); // number of triangles in group i
2962 numverts = numtris * 3;
2964 fprintf(grpfile,"%d triangles.\n", numtris);
2966 vertices = (vec3_t*)SafeMalloc(numverts*sizeof(vec3_t), "NewGen");
2967 uvs = (vec3_t*)SafeMalloc(numverts*sizeof(vec3_t), "NewGen");
2968 grouptris = (triangle_t*)SafeMalloc(numtris*sizeof(triangle_t), "NewGen");
2970 for (count = 0; count < fmheader.num_tris; count++)
2972 if (triangles[count].group == i)
2974 fprintf(grpfile,"Triangle %d\n", count);
2977 fprintf(grpfile,"\n");
2982 GetOneGroup(triangles, i, grouptris);
2985 for (j = 0; j < numtris; j++)
2987 VectorCopy(grouptris[j].verts[0], v0);
2988 VectorCopy(grouptris[j].verts[1], v1);
2989 VectorCopy(grouptris[j].verts[2], v2);
2990 VectorSubtract(v1, v0, diffvect1);
2991 VectorSubtract(v2, v1, diffvect2);
2992 CrossProduct( diffvect1, diffvect2, crossvect);
2993 VectorAdd(aveNorm, crossvect, aveNorm);
2994 VectorCopy(v0,vertices[num]);
2996 VectorCopy(v1,vertices[num]);
2997 num++; // add routine to add only verts that
2998 VectorCopy(v2,vertices[num]);
2999 num++; // have not already been added
3003 // figure out the best plane projections
3004 DOsvdPlane ((float*)vertices, num, (float *)&n, (float *)&base);
3006 if (DotProduct(aveNorm,n) < 0.0f)
3008 VectorScale(n, -1.0f, n);
3010 VectorNormalize(n,n);
3011 if (fabs(n[2]) < .57)
3013 CrossProduct( zaxis, n, crossvect);
3014 VectorCopy(crossvect, u);
3018 CrossProduct( yaxis, n, crossvect);
3019 VectorCopy(crossvect, u);
3021 VectorNormalize(u,u);
3022 CrossProduct( n, u, crossvect);
3023 VectorCopy(crossvect, v);
3024 VectorNormalize(v,v);
3028 for ( j = 0; j < 3; j++)
3030 groupMin[j] = 1e30f;
3031 groupMax[j] = -1e30f;
3034 for ( j = 0; j < numtris; j++)
3036 for ( k = 0; k < 3; k++)
3038 VectorCopy(grouptris[j].verts[k],v0);
3039 VectorSubtract(v0, base, v0);
3040 uvw[0] = DotProduct(v0, u);
3041 uvw[1] = DotProduct(v0, v);
3042 uvw[2] = DotProduct(v0, n);
3043 VectorCopy(uvw,uvs[num]);
3045 for ( l = 0; l < 3; l++)
3047 if (uvw[l] < groupMin[l])
3049 groupMin[l] = uvw[l];
3051 if (uvw[l] > groupMax[l])
3053 groupMax[l] = uvw[l];
3059 xwidth = ceil(0 - groupMin[0]) + 2; // move right of origin and avoid overlap
3060 ywidth = ceil(0 - groupMin[1]) + 2; // move "above" origin
3062 for ( j=0; j < numverts; j++)
3064 uFinal[finalcount] = uvs[j][0] + xwidth + xbase;
3065 vFinal[finalcount] = uvs[j][1] + ywidth;
3066 if (uFinal[finalcount] < uvwMin[0])
3068 uvwMin[0] = uFinal[finalcount];
3070 if (uFinal[finalcount] > uvwMax[0])
3072 uvwMax[0] = uFinal[finalcount];
3074 if (vFinal[finalcount] < uvwMin[1])
3076 uvwMin[1] = vFinal[finalcount];
3078 if (vFinal[finalcount] > uvwMax[1])
3080 uvwMax[1] = vFinal[finalcount];
3085 fprintf(grpfile,"svdPlaned Group min: ( %f , %f )\n",groupMin[0] + xwidth + xbase, groupMin[1] + ywidth);
3086 fprintf(grpfile,"svdPlaned Group max: ( %f , %f )\n",groupMax[0] + xwidth + xbase, groupMax[1] + ywidth);
3088 finalcount = finalstart;
3090 for ( count = 0; count < numverts; count++)
3092 fprintf(grpfile,"Vertex %d: ( %f , %f , %f )\n",count,vertices[count][0],vertices[count][1],vertices[count][2]);
3093 fprintf(grpfile,"svdPlaned: ( %f , %f )\n",uFinal[finalcount],vFinal[finalcount++]);
3096 finalstart = finalcount;
3098 fprintf(grpfile,"\n");
3104 xbase += ceil(groupMax[0] - groupMin[0]) + 2;
3108 fprintf(grpfile,"Global Min ( %f , %f )\n",uvwMin[0],uvwMin[1]);
3109 fprintf(grpfile,"Global Max ( %f , %f )\n",uvwMax[0],uvwMax[1]);
3112 ScaleTris(uvwMin, uvwMax, width, height, uFinal, vFinal, finalcount);
3114 for (k = 0; k < finalcount; k++)
3116 fprintf(grpfile, "scaled vertex %d: ( %f , %f )\n",k,uFinal[k],vFinal[k]);
3119 // i've got the array of vertices in uFinal and vFinal. Now I need to write them and draw lines
3121 datasize = width * height*sizeof(unsigned char);
3122 newpic = (unsigned char*)SafeMalloc(datasize, "NewGen");
3123 memset(newpic,0,datasize);
3124 memset(pic_palette,0,sizeof(pic_palette));
3125 pic_palette[767] = pic_palette[766] = pic_palette[765] = 255;
3128 while (k < finalcount)
3130 NewDrawLine(uFinal[k], vFinal[k], uFinal[k+1], vFinal[k+1], newpic, width, height);
3132 NewDrawLine(uFinal[k], vFinal[k], uFinal[k+1], vFinal[k+1], newpic, width, height);
3134 NewDrawLine(uFinal[k], vFinal[k], uFinal[k-2], vFinal[k-2], newpic, width, height);
3136 fprintf(grpfile, "output tri with verts %d, %d, %d", k-2, k-1, k);
3139 WritePCXfile (OutputName, newpic, width, height, pic_palette);
3149 void NewDrawLine(int x1, int y1, int x2, int y2, unsigned char* picture, int width, int height)
3154 float xfrac, yfrac, xstep, ystep;
3155 unsigned long sx, sy;
3163 count = adx > ady ? adx : ady;
3168 printf("Bad count\n");
3169 return; // don't ever hang up on bad data
3175 xstep = (float)dx/count;
3176 ystep = (float)dy/count;
3183 if(xfrac < width && yfrac < height)
3185 picture[(long)yfrac*width+(long)xfrac] = LineColor;
3190 } while (count > 0);
3195 for (u=-0.1 ; u<=0.9 ; u+=0.999)
3197 for (v=-0.1 ; v<=0.9 ; v+=0.999)
3201 if(sx < width && sy < height)
3203 picture[sy*width+sx] = LineColor;
3210 } while (count > 0);
3215 if(count&1 && xfrac < width &&
3218 picture[(long)yfrac*width+(long)xfrac] = LineColor;
3223 } while (count > 0);
3226 Error("Unknown <linetype> %d.\n", LineType);
3230 void ScaleTris( vec3_t min, vec3_t max, int Width, int Height, float* u, float* v, int verts)
3234 float hscale, vscale;
3240 hscale = (Width-2) / max[0];
3241 vscale = (Height-2) / max[1];
3248 for ( i = 0; i<verts; i++)
3257 void GetOneGroup(trigroup_t *tris, int grp, triangle_t* triangles)
3263 for (i = 0; i < fmheader.num_tris; i++)
3265 if (tris[i].group == grp)
3267 triangles[j++] = tris[i].triangle;
3274 int GetNumTris( trigroup_t *tris, int grp)
3280 for (i = 0; i < fmheader.num_tris; i++)
3282 if (tris[i].group == grp)
3291 int ShareVertex( trigroup_t trione, trigroup_t tritwo)
3298 for ( i = 0; i < 3; i++)
3300 for ( j = 0; j < 3; j++)
3302 if (DistBetween(trione.triangle.verts[i],tritwo.triangle.verts[j]) < TRIVERT_DIST)
3312 float DistBetween(vec3_t point1, vec3_t point2)
3316 dist = (point1[0] - point2[0]);
3318 dist += (point1[1] - point2[1])*(point1[1]-point2[1]);
3319 dist += (point1[2] - point2[2])*(point1[2]-point2[2]);
3325 void GenSkin(char *ModelFile, char *OutputName, int Width, int Height)
3328 mesh_node_t *pmnodes;
3334 LoadTriangleList (ModelFile, &ptri, &fmheader.num_tris, &pmnodes, &fmheader.num_mesh_nodes);
3337 for (i=0;i<fmheader.num_tris;i++)
3343 memset(pic,0,sizeof(pic));
3344 memset(pic_palette,0,sizeof(pic_palette));
3345 pic_palette[767] = pic_palette[766] = pic_palette[765] = 255;
3348 ScaleHeight = Height;
3350 BuildST (ptri, fmheader.num_tris, true);
3352 WritePCXfile (OutputName, pic, SKINPAGE_WIDTH, SKINPAGE_HEIGHT, pic_palette);
3354 printf("Gen Skin Stats:\n");
3355 printf(" Input Base: %s\n",ModelFile);
3356 printf(" Input Dimensions: %d,%d\n",Width,Height);
3358 printf(" Output File: %s\n",OutputName);
3359 printf(" Output Dimensions: %d,%d\n",ScaleWidth,ScaleHeight);
3361 if (fmheader.num_mesh_nodes)
3363 printf("\nNodes:\n");
3364 for(i=0;i<fmheader.num_mesh_nodes;i++)
3366 printf(" %s\n",pmnodes[i].name);
3375 void Cmd_FMBeginGroup (void)
3377 GetScriptToken (false);
3379 g_no_opimizations = false;
3381 groups[num_groups].start_frame = fmheader.num_frames;
3382 groups[num_groups].num_frames = 0;
3384 groups[num_groups].degrees = atol(token);
3385 if (groups[num_groups].degrees < 1 || groups[num_groups].degrees > 32)
3387 Error ("Degrees of freedom out of range: %d",groups[num_groups].degrees);
3391 void Cmd_FMEndGroup (void)
3393 groups[num_groups].num_frames = fmheader.num_frames - groups[num_groups].start_frame;
3395 if(num_groups < MAX_GROUPS - 1)
3401 Error("Number of compression groups exceded: %i\n", MAX_GROUPS);