2 Copyright (C) 1999-2007 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 extern 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 );
151 //==============================================================
158 static void ClearModel( void ){
159 memset( &fmheader, 0, sizeof( fmheader ) );
163 VectorCopy( vec3_origin, adjust );
164 g_fixedwidth = g_fixedheight = 0;
173 ClearSkeletalModel();
177 extern void H_printf( char *fmt, ... );
180 void WriteHeader( FILE *FH, char *Ident, int Version, int Size, void *Data ){
182 static long pos = -1;
185 if ( Size == 0 ) { // Don't write out empty packets
190 CurrentPos = ftell( FH );
191 Size = CurrentPos - pos + sizeof( header_t );
192 fseek( FH, pos, SEEK_SET );
195 else if ( Size == -1 ) {
199 memset( &header,0,sizeof( header ) );
200 strcpy( header.ident,Ident );
201 header.version = Version;
204 SafeWrite( FH, &header, sizeof( header ) );
207 SafeWrite( FH, Data, Size );
212 fseek( FH, 0, SEEK_END );
221 static void WriteModelFile( FILE *modelouthandle ){
226 byte buffer[MAX_FM_VERTS * 4 + 128];
229 IntListNode_t *current, *toFree;
230 qboolean framesWritten = false;
231 size_t temp,size = 0;
233 // probably should do this dynamically one of these days
236 float scale[3]; // multiply byte verts by this
237 float translate[3]; // then add this
238 } outFrames[MAX_FM_FRAMES];
240 #define DATA_SIZE 0x60000 // 384K had better be enough, particularly for the reference points
241 byte data[DATA_SIZE];
242 byte data2[DATA_SIZE];
244 fmheader.num_glcmds = numcommands;
245 fmheader.framesize = (int)&( (fmaliasframe_t *)0 )->verts[fmheader.num_xyz];
247 WriteHeader( modelouthandle, FM_HEADER_NAME, FM_HEADER_VER, sizeof( fmheader ), &fmheader );
250 // write out the skin names
253 WriteHeader( modelouthandle, FM_SKIN_NAME, FM_SKIN_VER, fmheader.num_skins * MAX_FM_SKINNAME, g_skins );
256 // write out the texture coordinates
259 for ( i = 0 ; i < fmheader.num_st ; i++ )
261 base_st[i].s = LittleShort( base_st[i].s );
262 base_st[i].t = LittleShort( base_st[i].t );
265 WriteHeader( modelouthandle, FM_ST_NAME, FM_ST_VER, fmheader.num_st * sizeof( base_st[0] ), base_st );
268 // write out the triangles
270 WriteHeader( modelouthandle, FM_TRI_NAME, FM_TRI_VER, fmheader.num_tris * sizeof( fmtriangle_t ), NULL );
272 for ( i = 0 ; i < fmheader.num_tris ; i++ )
277 for ( j = 0 ; j < 3 ; j++ )
279 tri.index_xyz[j] = LittleShort( triangles[i].index_xyz[j] );
280 tri.index_st[j] = LittleShort( triangles[i].index_st[j] );
283 SafeWrite( modelouthandle, &tri, sizeof( tri ) );
288 // write out the frames
290 WriteHeader( modelouthandle, FM_FRAME_NAME, FM_FRAME_VER, fmheader.num_frames * fmheader.framesize, NULL );
291 // WriteHeader(modelouthandle, FM_FRAME_NAME, FM_FRAME_VER, -1, NULL);
293 for ( i = 0 ; i < fmheader.num_frames ; i++ )
296 out = (fmaliasframe_t *)buffer;
298 strcpy( out->name, in->name );
299 for ( j = 0 ; j < 3 ; j++ )
301 out->scale[j] = ( in->maxs[j] - in->mins[j] ) / 255;
302 out->translate[j] = in->mins[j];
304 outFrames[i].scale[j] = out->scale[j];
305 outFrames[i].translate[j] = out->translate[j];
308 for ( j = 0 ; j < fmheader.num_xyz ; j++ )
310 // all of these are byte values, so no need to deal with endianness
311 out->verts[j].lightnormalindex = in->v[j].lightnormalindex;
313 for ( k = 0 ; k < 3 ; k++ )
315 // scale to byte values & min/max check
316 v = Q_rint( ( in->v[j].v[k] - out->translate[k] ) / out->scale[k] );
318 // clamp, so rounding doesn't wrap from 255.6 to 0
325 out->verts[j].v[k] = v;
329 for ( j = 0 ; j < 3 ; j++ )
331 out->scale[j] = LittleFloat( out->scale[j] );
332 out->translate[j] = LittleFloat( out->translate[j] );
335 SafeWrite( modelouthandle, out, fmheader.framesize );
338 // Go back and finish the header
339 // WriteHeader(modelouthandle, FM_FRAME_NAME, FM_FRAME_VER, -1, NULL);
343 WriteHeader( modelouthandle, FM_SHORT_FRAME_NAME, FM_SHORT_FRAME_VER,FRAME_NAME_LEN * fmheader.num_frames, NULL );
344 for ( i = 0 ; i < fmheader.num_frames ; i++ )
347 SafeWrite( modelouthandle,in->name,FRAME_NAME_LEN );
349 WriteHeader( modelouthandle, FM_NORMAL_NAME, FM_NORMAL_VER,fmheader.num_xyz, NULL );
351 for ( j = 0 ; j < fmheader.num_xyz ; j++ )
352 SafeWrite( modelouthandle,&in->v[j].lightnormalindex,1 );
358 WriteHeader( modelouthandle, FM_GLCMDS_NAME, FM_GLCMDS_VER, numcommands * 4, commands );
361 // write out mesh nodes
363 for ( i = 0; i < fmheader.num_mesh_nodes; i++ )
365 memcpy( mesh_nodes[i].tris, pmnodes[i].tris, sizeof( mesh_nodes[i].tris ) );
366 memcpy( mesh_nodes[i].verts, pmnodes[i].verts, sizeof( mesh_nodes[i].verts ) );
367 mesh_nodes[i].start_glcmds = LittleShort( (short)pmnodes[i].start_glcmds );
368 mesh_nodes[i].num_glcmds = LittleShort( (short)pmnodes[i].num_glcmds );
371 WriteHeader( modelouthandle, FM_MESH_NAME, FM_MESH_VER, sizeof( fmmeshnode_t ) * fmheader.num_mesh_nodes, mesh_nodes );
381 char *mat; fmheader.num_xyz*3*g->degrees*sizeof(char)
382 char *ccomp; g->num_frames*g->degrees*sizeof(char)
383 char *cbase; fmheader.num_xyz*3*sizeof(unsigned char)
384 float *cscale; g->degrees*sizeof(float)
385 float *coffset; g->degrees*sizeof(float)
386 float trans[3]; 3*sizeof(float)
387 float scale[3]; 3*sizeof(float)
392 size = sizeof( int ) + fmheader.num_frames * sizeof( int );
393 for ( k = 0; k < num_groups; k++ )
396 size += sizeof( int ) * 3;
397 size += fmheader.num_xyz * 3 * g->degrees * sizeof( char );
398 size += g->num_frames * g->degrees * sizeof( char );
399 size += fmheader.num_xyz * 3 * sizeof( unsigned char );
400 size += g->degrees * sizeof( float );
401 size += g->degrees * sizeof( float );
402 size += 12 * sizeof( float );
404 WriteHeader( modelouthandle, FM_COMP_NAME, FM_COMP_VER,size, NULL );
405 SafeWrite( modelouthandle,&num_groups,sizeof( int ) );
406 SafeWrite( modelouthandle,frame_to_group,sizeof( int ) * fmheader.num_frames );
408 for ( k = 0; k < num_groups; k++ )
411 tmp = LittleLong( g->start_frame );
412 SafeWrite( modelouthandle,&tmp,sizeof( int ) );
413 tmp = LittleLong( g->num_frames );
414 SafeWrite( modelouthandle,&tmp,sizeof( int ) );
415 tmp = LittleLong( g->degrees );
416 SafeWrite( modelouthandle,&tmp,sizeof( int ) );
418 SafeWrite( modelouthandle,g->mat,fmheader.num_xyz * 3 * g->degrees * sizeof( char ) );
419 SafeWrite( modelouthandle,g->ccomp,g->num_frames * g->degrees * sizeof( char ) );
420 SafeWrite( modelouthandle,g->cbase,fmheader.num_xyz * 3 * sizeof( unsigned char ) );
421 SafeWrite( modelouthandle,g->cscale,g->degrees * sizeof( float ) );
422 SafeWrite( modelouthandle,g->coffset,g->degrees * sizeof( float ) );
423 SafeWrite( modelouthandle,g->trans,3 * sizeof( float ) );
424 SafeWrite( modelouthandle,g->scale,3 * sizeof( float ) );
425 SafeWrite( modelouthandle,g->bmin,3 * sizeof( float ) );
426 SafeWrite( modelouthandle,g->bmax,3 * sizeof( float ) );
435 // write the skeletal info
436 if ( g_skelModel.type != SKEL_NULL ) {
439 temp = sizeof( int ); // change this to a byte
440 memcpy( data + size, &g_skelModel.type, temp );
444 temp = sizeof( int ); // change this to a byte
445 memcpy( data + size, &numJointsInSkeleton[g_skelModel.type], temp );
448 // number of verts in each joint cluster
449 temp = sizeof( int ) * numJointsInSkeleton[g_skelModel.type]; // change this to shorts
450 memcpy( data + size, &g_skelModel.new_num_verts[1], temp );
454 for ( i = 0; i < numJointsInSkeleton[g_skelModel.type]; ++i )
456 current = g_skelModel.vertLists[i];
459 temp = sizeof( int ); // change this to a short
460 memcpy( data + size, ¤t->data, temp );
463 current = current->next;
464 free( toFree ); // freeing of memory allocated in ReplaceClusterIndex called in Cmd_Base
468 if ( !num_groups ) { // joints are stored with regular verts for compressed models
469 framesWritten = true;
471 temp = sizeof( int ); // change this to a byte
472 memcpy( data + size, &framesWritten, temp );
475 for ( i = 0; i < fmheader.num_frames; ++i )
479 for ( j = 0 ; j < numJointsInSkeleton[g_skelModel.type]; ++j )
481 for ( k = 0 ; k < 3 ; k++ )
483 // scale to byte values & min/max check
484 v = Q_rint( ( in->joints[j].placement.origin[k] - outFrames[i].translate[k] ) / outFrames[i].scale[k] );
486 // write out origin as a float since they arn't clamped
487 temp = sizeof( float ); // change this to a short
488 assert( size + temp < DATA_SIZE );
489 memcpy( data + size, &v, temp );
493 for ( k = 0 ; k < 3 ; k++ )
495 v = Q_rint( ( in->joints[j].placement.direction[k] - outFrames[i].translate[k] ) / outFrames[i].scale[k] );
497 // write out origin as a float since they arn't clamped
498 temp = sizeof( float ); // change this to a short
499 assert( size + temp < DATA_SIZE );
500 memcpy( data + size, &v, temp );
504 for ( k = 0 ; k < 3 ; k++ )
506 v = Q_rint( ( in->joints[j].placement.up[k] - outFrames[i].translate[k] ) / outFrames[i].scale[k] );
508 // write out origin as a float since they arn't clamped
509 temp = sizeof( float ); // change this to a short
510 assert( size + temp < DATA_SIZE );
511 memcpy( data + size, &v, temp );
520 temp = sizeof( int ); // change this to a byte
521 memcpy( data + size, &framesWritten, temp );
525 WriteHeader( modelouthandle, FM_SKELETON_NAME, FM_SKELETON_VER, size, data );
528 if ( g_skelModel.references != REF_NULL ) {
532 if ( RefPointNum <= 0 ) { // Hard-coded labels
533 refnum = numReferences[g_skelModel.references];
536 { // Labels indicated in QDT
537 refnum = RefPointNum;
540 temp = sizeof( int ); // change this to a byte
541 memcpy( data2 + size, &g_skelModel.references, temp );
545 framesWritten = true;
547 temp = sizeof( int ); // change this to a byte
548 memcpy( data2 + size, &framesWritten, temp );
551 for ( i = 0; i < fmheader.num_frames; ++i )
555 for ( j = 0 ; j < refnum; ++j )
557 for ( k = 0 ; k < 3 ; k++ )
559 // scale to byte values & min/max check
560 v = Q_rint( ( in->references[j].placement.origin[k] - outFrames[i].translate[k] ) / outFrames[i].scale[k] );
562 // write out origin as a float since they arn't clamped
563 temp = sizeof( float ); // change this to a short
564 assert( size + temp < DATA_SIZE );
565 memcpy( data2 + size, &v, temp );
569 for ( k = 0 ; k < 3 ; k++ )
571 v = Q_rint( ( in->references[j].placement.direction[k] - outFrames[i].translate[k] ) / outFrames[i].scale[k] );
573 // write out origin as a float since they arn't clamped
574 temp = sizeof( float ); // change this to a short
575 assert( size + temp < DATA_SIZE );
576 memcpy( data2 + size, &v, temp );
580 for ( k = 0 ; k < 3 ; k++ )
582 v = Q_rint( ( in->references[j].placement.up[k] - outFrames[i].translate[k] ) / outFrames[i].scale[k] );
584 // write out origin as a float since they arn't clamped
585 temp = sizeof( float ); // change this to a short
586 assert( size + temp < DATA_SIZE );
587 memcpy( data2 + size, &v, temp );
593 else // FINISH ME: references need to be stored with regular verts for compressed models
595 framesWritten = false;
597 temp = sizeof( int ); // change this to a byte
598 memcpy( data2 + size, &framesWritten, temp );
602 WriteHeader( modelouthandle, FM_REFERENCES_NAME, FM_REFERENCES_VER, size, data2 );
606 static void CompressFrames(){
612 for ( i = 0; i < fmheader.num_frames; i++ )
614 while ( i >= groups[j].start_frame + groups[j].num_frames && j < num_groups - 1 )
616 frame_to_group[i] = j;
619 for ( k = 0; k < num_groups; k++ )
623 printf( "\nCompressing Frames for group %i...\n", k );
624 AnimCompressInit( g->num_frames,fmheader.num_xyz,g->degrees );
625 for ( i = 0; i < g->num_frames; i++ )
627 in = &g_frames[i + g->start_frame];
628 for ( j = 0; j < fmheader.num_xyz; j++ )
629 AnimSetFrame( i,j,in->v[j].v[0],in->v[j].v[1],in->v[j].v[2] );
632 g->mat = (char *) SafeMalloc( fmheader.num_xyz * 3 * g->degrees * sizeof( char ), "CompressFrames" );
633 g->ccomp = (char *) SafeMalloc( g->num_frames * g->degrees * sizeof( char ), "CompressFrames" );
634 g->cbase = (char *) SafeMalloc( fmheader.num_xyz * 3 * sizeof( unsigned char ), "CompressFrames" );
635 g->cscale = (float *) SafeMalloc( g->degrees * sizeof( float ), "CompressFrames" );
636 g->coffset = (float *) SafeMalloc( g->degrees * sizeof( float ), "CompressFrames" );
637 AnimCompressToBytes( g->trans,g->scale,g->mat,g->ccomp,g->cbase,g->cscale,g->coffset,g->bmin,g->bmax );
642 static void OptimizeVertices( void ){
643 qboolean vert_used[MAX_FM_VERTS];
644 short vert_replacement[MAX_FM_VERTS];
645 int i,j,k,l,pos,bit,set_pos,set_bit;
649 static IntListNode_t *newVertLists[NUM_CLUSTERS];
650 static int newNum_verts[NUM_CLUSTERS];
651 IntListNode_t *current, *next;
653 printf( "Optimizing vertices..." );
655 memset( vert_used, 0, sizeof( vert_used ) );
657 if ( g_skelModel.clustered == true ) {
658 memset( newNum_verts, 0, sizeof( newNum_verts ) );
659 memset( newVertLists, 0, sizeof( newVertLists ) );
664 // search for common points among all the frames
665 for ( i = 0 ; i < fmheader.num_frames ; i++ )
669 for ( j = 0; j < fmheader.num_xyz; j++ )
671 for ( k = 0,Found = false; k < j; k++ )
672 { // starting from the beginning always ensures vert_replacement points to the first point in the array
673 if ( in->v[j].v[0] == in->v[k].v[0] &&
674 in->v[j].v[1] == in->v[k].v[1] &&
675 in->v[j].v[2] == in->v[k].v[2] ) {
677 vert_replacement[j] = k;
684 if ( !vert_used[j] ) {
692 // recompute the light normals
693 for ( i = 0 ; i < fmheader.num_frames ; i++ )
697 for ( j = 0; j < fmheader.num_xyz; j++ )
699 if ( !vert_used[j] ) {
700 k = vert_replacement[j];
702 VectorAdd( in->v[j].vnorm.normalsum, in->v[k].vnorm.normalsum, in->v[k].vnorm.normalsum );
703 in->v[k].vnorm.numnormals += in->v[j].vnorm.numnormals++;
707 for ( j = 0 ; j < fmheader.num_xyz ; j++ )
714 c = in->v[j].vnorm.numnormals;
716 Error( "Vertex with no triangles attached" );
719 VectorScale( in->v[j].vnorm.normalsum, 1.0 / c, v );
720 VectorNormalize( v, v );
725 for ( k = 0 ; k < NUMVERTEXNORMALS ; k++ )
729 dot = DotProduct( v, avertexnormals[k] );
730 if ( dot > maxdot ) {
736 in->v[j].lightnormalindex = maxdotindex;
740 // create substitution list
742 for ( i = 0; i < fmheader.num_xyz; i++ )
744 if ( vert_used[i] ) {
745 vert_replacement[i] = num_unique;
750 vert_replacement[i] = vert_replacement[vert_replacement[i]];
753 // vert_replacement[i] is the new index, i is the old index
754 // need to add the new index to the cluster list if old index was in it
755 if ( g_skelModel.clustered == true ) {
756 for ( k = 0; k < numJointsInSkeleton[g_skelModel.type]; ++k )
758 for ( l = 0, current = g_skelModel.vertLists[k];
759 l < g_skelModel.new_num_verts[k + 1]; ++l, current = current->next )
761 if ( current->data == i ) {
762 IntListNode_t *current2;
764 qboolean added = false;
766 for ( m = 0, current2 = newVertLists[k]; m < newNum_verts[k + 1];
767 ++m, current2 = current2->next )
769 if ( current2->data == vert_replacement[i] ) {
776 ++newNum_verts[k + 1];
778 next = newVertLists[k];
780 newVertLists[k] = (IntListNode_t *) SafeMalloc( sizeof( IntListNode_t ), "OptimizeVertices" );
781 // freed after model write out
783 newVertLists[k]->data = vert_replacement[i];
784 newVertLists[k]->next = next;
794 for ( i = 0 ; i < fmheader.num_frames ; i++ )
798 for ( j = 0; j < fmheader.num_xyz; j++ )
800 in->v[vert_replacement[j]] = in->v[j];
805 for ( i = 0; i < numJointsInSkeleton[g_skelModel.type]; ++i )
807 IntListNode_t *toFree;
808 current = g_skelModel.vertLists[i];
813 current = current->next;
814 free( toFree ); // freeing of memory allocated in ReplaceClusterIndex called in Cmd_Base
817 g_skelModel.vertLists[i] = newVertLists[i];
818 g_skelModel.new_num_verts[i + 1] = newNum_verts[i + 1];
822 for ( k = 0; k < numJointsInSkeleton[g_skelModel.type]; ++k )
824 for ( l = 0, current = g_skelModel.vertLists[k];
825 l < g_skelModel.new_num_verts[k + 1]; ++l, current = current->next )
827 IntListNode_t *current2;
830 for ( m = l + 1, current2 = current->next; m < newNum_verts[k + 1];
831 ++m, current2 = current2->next )
833 if ( current->data == current2->data ) {
834 printf( "Warning duplicate vertex: %d\n", current->data );
842 for ( i = 0; i < fmheader.num_mesh_nodes; i++ )
843 { // reset the vert bits
844 memset( pmnodes[i].verts,0,sizeof( pmnodes[i].verts ) );
847 // repleace the master triangle list vertex indexes and update the vert bits for each mesh node
848 for ( i = 0 ; i < fmheader.num_tris ; i++ )
851 bit = 1 << ( i & 7 );
853 for ( j = 0 ; j < 3 ; j++ )
855 set_bit = set_pos = triangles[i].index_xyz[j] = vert_replacement[triangles[i].index_xyz[j]];
858 set_bit = 1 << ( set_bit & 7 );
860 for ( k = 0; k < fmheader.num_mesh_nodes; k++ )
862 if ( !( pmnodes[k].tris[pos] & bit ) ) {
865 pmnodes[k].verts[set_pos] |= set_bit;
870 for ( i = 0; i < numcommands; i++ )
878 for ( i++; j; j--,i += 3 )
880 commands[i + 2] = vert_replacement[commands[i + 2]];
885 printf( "Reduced by %d\n",fmheader.num_xyz - num_unique );
887 fmheader.num_xyz = num_unique;
889 // tack on the reference verts to the regular verts
890 if ( g_skelModel.references != REF_NULL ) {
895 if ( RefPointNum <= 0 ) { // Hard-coded labels
896 refnum = numReferences[g_skelModel.references];
899 { // Labels indicated in QDT
900 refnum = RefPointNum;
904 for ( i = 0; i < fmheader.num_frames; ++i )
907 index = fmheader.num_xyz;
909 for ( j = 0 ; j < refnum; ++j )
911 VectorCopy( in->references[j].placement.origin, in->v[index].v );
914 VectorCopy( in->references[j].placement.direction, in->v[index].v );
917 VectorCopy( in->references[j].placement.up, in->v[index].v );
922 fmheader.num_xyz += refnum * 3;
925 // tack on the skeletal joint verts to the regular verts
926 if ( g_skelModel.type != SKEL_NULL ) {
930 for ( i = 0; i < fmheader.num_frames; ++i )
933 index = fmheader.num_xyz;
935 for ( j = 0 ; j < numJointsInSkeleton[g_skelModel.type]; ++j )
937 VectorCopy( in->joints[j].placement.origin, in->v[index].v );
940 VectorCopy( in->joints[j].placement.direction, in->v[index].v );
943 VectorCopy( in->joints[j].placement.up, in->v[index].v );
948 fmheader.num_xyz += numJointsInSkeleton[g_skelModel.type] * 3;
961 void FMFinishModel( void ){
962 FILE *modelouthandle;
963 int i,j,length,tris,verts,bit,pos,total_tris,total_verts;
967 if ( !fmheader.num_frames ) {
972 // copy to release directory tree if doing a release build
975 if ( modelname[0] ) {
976 sprintf( name, "%s", modelname );
979 sprintf( name, "%s/tris.fm", cdpartial );
983 for ( i = 0 ; i < fmheader.num_skins ; i++ )
985 ReleaseFile( g_skins[i] );
987 fmheader.num_frames = 0;
994 for ( i = 0; i < fmheader.num_tris; i++ )
995 if ( translucent[i] ) {
999 if ( !g_no_opimizations ) {
1004 // write the model output file
1006 if ( modelname[0] ) {
1007 sprintf( name, "%s%s", g_outputDir, modelname );
1010 sprintf( name, "%s/tris.fm", g_outputDir );
1012 printf( "saving to %s\n", name );
1014 modelouthandle = SafeOpenWrite( name );
1016 WriteModelFile( modelouthandle );
1018 printf( "%3dx%3d skin\n", fmheader.skinwidth, fmheader.skinheight );
1019 printf( "First frame boundaries:\n" );
1020 printf( " minimum x: %3f\n", g_frames[0].mins[0] );
1021 printf( " maximum x: %3f\n", g_frames[0].maxs[0] );
1022 printf( " minimum y: %3f\n", g_frames[0].mins[1] );
1023 printf( " maximum y: %3f\n", g_frames[0].maxs[1] );
1024 printf( " minimum z: %3f\n", g_frames[0].mins[2] );
1025 printf( " maximum z: %3f\n", g_frames[0].maxs[2] );
1026 printf( "%4d vertices\n", fmheader.num_xyz );
1027 printf( "%4d triangles, %4d of them translucent\n", fmheader.num_tris, trans_count );
1028 printf( "%4d frame\n", fmheader.num_frames );
1029 printf( "%4d glverts\n", numglverts );
1030 printf( "%4d glcmd\n", fmheader.num_glcmds );
1031 printf( "%4d skins\n", fmheader.num_skins );
1032 printf( "%4d mesh nodes\n", fmheader.num_mesh_nodes );
1033 printf( "wasted pixels: %d / %d (%5.2f Percent)\n",total_skin_pixels - skin_pixels_used,
1034 total_skin_pixels, (double)( total_skin_pixels - skin_pixels_used ) / (double)total_skin_pixels * 100.0 );
1036 printf( "file size: %d\n", (int)ftell( modelouthandle ) );
1037 printf( "---------------------\n" );
1040 if ( fmheader.num_mesh_nodes ) {
1041 total_tris = total_verts = 0;
1042 printf( "Node Name Tris Verts\n" );
1043 printf( "--------------------------------- ---- -----\n" );
1044 for ( i = 0; i < fmheader.num_mesh_nodes; i++ )
1048 for ( j = 0; j < MAXTRIANGLES; j++ )
1051 bit = 1 << ( ( j ) & 7 );
1052 if ( pmnodes[i].tris[pos] & bit ) {
1056 for ( j = 0; j < MAX_FM_VERTS; j++ )
1059 bit = 1 << ( ( j ) & 7 );
1060 if ( pmnodes[i].verts[pos] & bit ) {
1065 printf( "%-33s %4d %5d\n",pmnodes[i].name,tris,verts );
1068 total_verts += verts;
1070 printf( "--------------------------------- ---- -----\n" );
1071 printf( "%-33s %4d %5d\n","TOTALS",total_tris,total_verts );
1074 fclose( modelouthandle );
1076 // finish writing header file
1079 // scale_up is usefull to allow step distances to be adjusted
1080 H_printf( "#define MODEL_SCALE\t\t%f\n", scale_up );
1083 if ( fmheader.num_mesh_nodes ) {
1085 H_printf( "#define NUM_MESH_NODES\t\t%d\n\n",fmheader.num_mesh_nodes );
1086 for ( i = 0; i < fmheader.num_mesh_nodes; i++ )
1088 strcpy( name, pmnodes[i].name );
1090 length = strlen( name );
1091 for ( j = 0; j < length; j++ )
1093 if ( name[j] == ' ' ) {
1097 H_printf( "#define MESH_%s\t\t%d\n", name, i );
1101 fclose( headerouthandle );
1102 headerouthandle = NULL;
1108 =================================================================
1110 ALIAS MODEL DISPLAY LIST GENERATION
1112 =================================================================
1115 extern int strip_xyz[128];
1116 extern int strip_st[128];
1117 extern int strip_tris[128];
1118 extern int stripcount;
1125 static int StripLength( int starttri, int startv, int num_tris, int node ){
1129 fmtriangle_t *last, *check;
1135 last = &triangles[starttri];
1137 strip_xyz[0] = last->index_xyz[( startv ) % 3];
1138 strip_xyz[1] = last->index_xyz[( startv + 1 ) % 3];
1139 strip_xyz[2] = last->index_xyz[( startv + 2 ) % 3];
1140 strip_st[0] = last->index_st[( startv ) % 3];
1141 strip_st[1] = last->index_st[( startv + 1 ) % 3];
1142 strip_st[2] = last->index_st[( startv + 2 ) % 3];
1144 strip_tris[0] = starttri;
1147 m1 = last->index_xyz[( startv + 2 ) % 3];
1148 st1 = last->index_st[( startv + 2 ) % 3];
1149 m2 = last->index_xyz[( startv + 1 ) % 3];
1150 st2 = last->index_st[( startv + 1 ) % 3];
1152 // look for a matching triangle
1154 for ( j = starttri + 1, check = &triangles[starttri + 1]
1155 ; j < num_tris ; j++, check++ )
1158 bit = 1 << ( j & 7 );
1159 if ( !( pmnodes[node].tris[pos] & bit ) ) {
1162 for ( k = 0 ; k < 3 ; k++ )
1164 if ( check->index_xyz[k] != m1 ) {
1167 if ( check->index_st[k] != st1 ) {
1170 if ( check->index_xyz[ ( k + 1 ) % 3 ] != m2 ) {
1173 if ( check->index_st[ ( k + 1 ) % 3 ] != st2 ) {
1177 // this is the next part of the fan
1179 // if we can't use this triangle, this tristrip is done
1180 if ( used[j] || translucent[j] != translucent[starttri] ) {
1185 if ( stripcount & 1 ) {
1186 m2 = check->index_xyz[ ( k + 2 ) % 3 ];
1187 st2 = check->index_st[ ( k + 2 ) % 3 ];
1191 m1 = check->index_xyz[ ( k + 2 ) % 3 ];
1192 st1 = check->index_st[ ( k + 2 ) % 3 ];
1195 strip_xyz[stripcount + 2] = check->index_xyz[ ( k + 2 ) % 3 ];
1196 strip_st[stripcount + 2] = check->index_st[ ( k + 2 ) % 3 ];
1197 strip_tris[stripcount] = j;
1206 // clear the temp used flags
1207 for ( j = starttri + 1 ; j < num_tris ; j++ )
1208 if ( used[j] == 2 ) {
1221 static int FanLength( int starttri, int startv, int num_tris, int node ){
1225 fmtriangle_t *last, *check;
1231 last = &triangles[starttri];
1233 strip_xyz[0] = last->index_xyz[( startv ) % 3];
1234 strip_xyz[1] = last->index_xyz[( startv + 1 ) % 3];
1235 strip_xyz[2] = last->index_xyz[( startv + 2 ) % 3];
1236 strip_st[0] = last->index_st[( startv ) % 3];
1237 strip_st[1] = last->index_st[( startv + 1 ) % 3];
1238 strip_st[2] = last->index_st[( startv + 2 ) % 3];
1240 strip_tris[0] = starttri;
1243 m1 = last->index_xyz[( startv + 0 ) % 3];
1244 st1 = last->index_st[( startv + 0 ) % 3];
1245 m2 = last->index_xyz[( startv + 2 ) % 3];
1246 st2 = last->index_st[( startv + 2 ) % 3];
1249 // look for a matching triangle
1251 for ( j = starttri + 1, check = &triangles[starttri + 1]
1252 ; j < num_tris ; j++, check++ )
1255 bit = 1 << ( j & 7 );
1256 if ( !( pmnodes[node].tris[pos] & bit ) ) {
1259 for ( k = 0 ; k < 3 ; k++ )
1261 if ( check->index_xyz[k] != m1 ) {
1264 if ( check->index_st[k] != st1 ) {
1267 if ( check->index_xyz[ ( k + 1 ) % 3 ] != m2 ) {
1270 if ( check->index_st[ ( k + 1 ) % 3 ] != st2 ) {
1274 // this is the next part of the fan
1276 // if we can't use this triangle, this tristrip is done
1277 if ( used[j] || translucent[j] != translucent[starttri] ) {
1282 m2 = check->index_xyz[ ( k + 2 ) % 3 ];
1283 st2 = check->index_st[ ( k + 2 ) % 3 ];
1285 strip_xyz[stripcount + 2] = m2;
1286 strip_st[stripcount + 2] = st2;
1287 strip_tris[stripcount] = j;
1296 // clear the temp used flags
1297 for ( j = starttri + 1 ; j < num_tris ; j++ )
1298 if ( used[j] == 2 ) {
1311 Generate a list of trifans or strips
1312 for the model, which holds for all frames
1315 static void BuildGlCmds( void ){
1319 int len, bestlen, besttype;
1322 int best_tris[1024];
1334 for ( l = 0; l < fmheader.num_mesh_nodes; l++ )
1336 memset( used, 0, sizeof( used ) );
1338 pmnodes[l].start_glcmds = numcommands;
1340 for ( trans_check = 0; trans_check < 2; trans_check++ )
1342 for ( i = 0 ; i < fmheader.num_tris ; i++ )
1345 bit = 1 << ( i & 7 );
1346 if ( !( pmnodes[l].tris[pos] & bit ) ) {
1350 // pick an unused triangle and start the trifan
1351 if ( used[i] || trans_check != translucent[i] ) {
1356 for ( type = 0 ; type < 2 ; type++ )
1359 for ( startv = 0 ; startv < 3 ; startv++ )
1362 len = StripLength( i, startv, fmheader.num_tris, l );
1365 len = FanLength( i, startv, fmheader.num_tris, l );
1367 if ( len > bestlen ) {
1370 for ( j = 0 ; j < bestlen + 2 ; j++ )
1372 best_st[j] = strip_st[j];
1373 best_xyz[j] = strip_xyz[j];
1375 for ( j = 0 ; j < bestlen ; j++ )
1376 best_tris[j] = strip_tris[j];
1381 // mark the tris on the best strip/fan as used
1382 for ( j = 0 ; j < bestlen ; j++ )
1383 used[best_tris[j]] = 1;
1385 if ( besttype == 1 ) {
1386 commands[numcommands++] = ( bestlen + 2 );
1389 commands[numcommands++] = -( bestlen + 2 );
1392 numglverts += bestlen + 2;
1394 for ( j = 0 ; j < bestlen + 2 ; j++ )
1396 // emit a vertex into the reorder buffer
1399 // emit s/t coords into the commands stream
1403 s = ( s ) / fmheader.skinwidth;
1404 t = ( t ) / fmheader.skinheight;
1406 *(float *)&commands[numcommands++] = s;
1407 *(float *)&commands[numcommands++] = t;
1408 *(int *)&commands[numcommands++] = best_xyz[j];
1412 commands[numcommands++] = 0; // end of list marker
1413 pmnodes[l].num_glcmds = numcommands - pmnodes[l].start_glcmds;
1419 ===============================================================
1423 ===============================================================
1427 #define LINE_NORMAL 1
1429 #define LINE_DOTTED 3
1432 #define ASCII_SPACE 32
1434 int LineType = LINE_NORMAL;
1435 extern unsigned char pic[SKINPAGE_HEIGHT * SKINPAGE_WIDTH], pic_palette[768];
1436 unsigned char LineColor = 255;
1437 int ScaleWidth, ScaleHeight;
1440 static char *CharDefs[] =
1442 "-------------------------",
1443 "-------------------------", // !
1444 "-------------------------", // "
1445 "-------------------------", // #
1446 "-------------------------", // $
1447 "-------------------------", // %
1448 "-------------------------", // &
1449 "--*----*-----------------", // '
1450 "-*---*----*----*-----*---", // (
1451 "*-----*----*----*---*----", // )
1452 "-----*--*--**---**--*--*-", // *
1453 "-------------------------", // +
1454 "----------------**--**---", // ,
1455 "-------------------------", // -
1456 "----------------**---**--", // .
1457 "-------------------------", // /
1458 " *** * *** * *** * *** ", // 0
1460 "**** * *** * *****",
1461 "**** * *** ***** ",
1462 " ** * * * * ***** * ",
1463 "**** * **** ***** ",
1464 " *** * **** * * *** ",
1466 " *** * * *** * * *** ",
1467 " *** * * **** * *** ", // 9
1468 "-**---**--------**---**--", // :
1469 "-------------------------", // ;
1470 "-------------------------", // <
1471 "-------------------------", // =
1472 "-------------------------", // >
1473 "-------------------------", // ?
1474 "-------------------------", // @
1475 "-***-*---*******---**---*", // A
1476 "****-*---*****-*---*****-",
1477 "-*****----*----*-----****",
1478 "****-*---**---**---*****-",
1479 "******----****-*----*****",
1480 "******----****-*----*----",
1481 "-*****----*--***---*-****",
1482 "*---**---*******---**---*",
1483 "-***---*----*----*---***-",
1484 "----*----*----**---*-***-",
1485 "-*--*-*-*--**---*-*--*--*",
1486 "-*----*----*----*----****",
1487 "*---***-***-*-**---**---*",
1488 "*---***--**-*-**--***---*",
1489 "-***-*---**---**---*-***-",
1490 "****-*---*****-*----*----",
1491 "-***-*---**---*-***----**",
1492 "****-*---*****-*-*--*--**",
1493 "-*****-----***-----*****-",
1494 "*****--*----*----*----*--",
1495 "*---**---**---**---******",
1496 "*---**---**---*-*-*---*--",
1497 "*---**---**-*-***-***---*",
1498 "*---*-*-*---*---*-*-*---*",
1499 "*---**---*-*-*---*----*--",
1500 "*****---*---*---*---*****" // Z
1503 void DrawLine( int x1, int y1, int x2, int y2 ){
1507 float xfrac, yfrac, xstep, ystep;
1516 count = adx > ady ? adx : ady;
1519 if ( count > 300 ) {
1520 printf( "Bad count\n" );
1521 return; // don't ever hang up on bad data
1527 xstep = (float)dx / count;
1528 ystep = (float)dy / count;
1535 if ( xfrac < SKINPAGE_WIDTH && yfrac < SKINPAGE_HEIGHT ) {
1536 pic[(int)yfrac * SKINPAGE_WIDTH + (int)xfrac] = LineColor;
1541 } while ( count > 0 );
1546 for ( u = -0.1 ; u <= 0.9 ; u += 0.999 )
1548 for ( v = -0.1 ; v <= 0.9 ; v += 0.999 )
1552 if ( sx < SKINPAGE_WIDTH && sy < SKINPAGE_HEIGHT ) {
1553 pic[sy * SKINPAGE_WIDTH + sx] = LineColor;
1560 } while ( count > 0 );
1565 if ( count & 1 && xfrac < SKINPAGE_WIDTH &&
1566 yfrac < SKINPAGE_HEIGHT ) {
1567 pic[(int)yfrac * SKINPAGE_WIDTH + (int)xfrac] = LineColor;
1572 } while ( count > 0 );
1575 Error( "Unknown <linetype> %d.\n", LineType );
1579 //==========================================================================
1583 //==========================================================================
1585 static void DrawCharacter( int x, int y, int character ){
1589 character = toupper( character );
1590 if ( character < ASCII_SPACE || character > 'Z' ) {
1591 character = ASCII_SPACE;
1593 character -= ASCII_SPACE;
1594 for ( def = CharDefs[character], r = 0; r < 5; r++ )
1596 for ( c = 0; c < 5; c++ )
1598 pic[( y + r ) * SKINPAGE_WIDTH + x + c] = *def++ == '*' ? 255 : 0;
1603 //==========================================================================
1607 //==========================================================================
1609 void DrawTextChar( int x, int y, char *text ){
1612 while ( ( c = *text++ ) != '\0' )
1614 DrawCharacter( x, y, c );
1620 extern void DrawScreen( float s_scale, float t_scale, float iwidth, float iheight );
1622 //==========================================================================
1625 static int ExtractDigit( byte *pic, int x, int y ){
1633 backColor = pic[( SKINPAGE_HEIGHT - 1 ) * SKINPAGE_WIDTH];
1634 DigitDefs = &CharDefs['0' - ASCII_SPACE];
1637 for ( r = 0; r < 5; r++ )
1639 for ( c = 0; c < 5; c++ )
1641 *buffer++ = ( pic[( y + r ) * SKINPAGE_WIDTH + x + c] == backColor ) ? ' ' : '*';
1645 for ( i = 0; i < 10; i++ )
1647 if ( strcmp( DigitDefs[i], digString ) == 0 ) {
1652 Error( "Unable to extract scaling info from skin PCX." );
1656 //==========================================================================
1659 int ExtractNumber( byte *pic, int x, int y ){
1660 return ExtractDigit( pic, x, y ) * 100 + ExtractDigit( pic, x + 6, y ) * 10 + ExtractDigit( pic, x + 12, y );
1671 Builds the triangle_st array for the base frame and
1672 fmheader.skinwidth / fmheader.skinheight
1674 FIXME: allow this to be loaded from a file for
1678 static void BuildST( triangle_t *ptri, int numtri, qboolean DrawSkin ){
1681 int width, height, iwidth, iheight, swidth;
1686 vec3_t vtemp1, vtemp2, normal;
1687 float s_scale, t_scale;
1694 // find bounds of all the verts on the base frame
1696 ClearBounds( mins, maxs );
1697 backface_flag = false;
1699 if ( ptri[0].HasUV ) { // if we have the uv already, we don't want to double up or scale
1700 iwidth = ScaleWidth;
1701 iheight = ScaleHeight;
1703 t_scale = s_scale = 1.0;
1707 for ( i = 0 ; i < numtri ; i++ )
1708 for ( j = 0 ; j < 3 ; j++ )
1709 AddPointToBounds( ptri[i].verts[j], mins, maxs );
1711 for ( i = 0 ; i < 3 ; i++ )
1713 mins[i] = floor( mins[i] );
1714 maxs[i] = ceil( maxs[i] );
1717 width = maxs[0] - mins[0];
1718 height = maxs[2] - mins[2];
1720 for ( i = 0 ; i < numtri ; i++ )
1722 VectorSubtract( ptri[i].verts[0], ptri[i].verts[1], vtemp1 );
1723 VectorSubtract( ptri[i].verts[2], ptri[i].verts[1], vtemp2 );
1724 CrossProduct( vtemp1, vtemp2, normal );
1726 if ( normal[1] > 0 ) {
1727 backface_flag = true;
1731 scWidth = ScaleWidth * SCALE_ADJUST_FACTOR;
1732 if ( backface_flag ) { //we are doubling
1736 scHeight = ScaleHeight * SCALE_ADJUST_FACTOR;
1738 scale = scWidth / width;
1740 if ( height * scale >= scHeight ) {
1741 scale = scHeight / height;
1744 iwidth = ceil( width * scale ) + 4;
1745 iheight = ceil( height * scale ) + 4;
1747 s_scale = (float)( iwidth - 4 ) / width;
1748 t_scale = (float)( iheight - 4 ) / height;
1752 if ( backface_flag ) {
1753 DrawScreen( s_scale, t_scale, iwidth * 2, iheight );
1756 DrawScreen( s_scale, t_scale, iwidth, iheight );
1759 if ( backface_flag ) {
1760 skinwidth = iwidth * 2;
1765 skinheight = iheight;
1768 /* if (!g_fixedwidth)
1771 if (width*scale >= 150)
1772 scale = 150.0 / width;
1773 if (height*scale >= 190)
1774 scale = 190.0 / height;
1776 s_scale = t_scale = scale;
1778 iwidth = ceil(width*s_scale);
1779 iheight = ceil(height*t_scale);
1786 iwidth = g_fixedwidth / 2;
1787 iheight = g_fixedheight;
1789 s_scale = (float)(iwidth-4) / width;
1790 t_scale = (float)(iheight-4) / height;
1794 // determine which side of each triangle to map the texture to
1797 for ( i = 0 ; i < numtri ; i++ )
1799 if ( ptri[i].HasUV ) {
1800 for ( j = 0 ; j < 3 ; j++ )
1802 triangle_st[i][j][0] = Q_rint( ptri[i].uv[j][0] * skinwidth );
1803 triangle_st[i][j][1] = Q_rint( ( 1.0f - ptri[i].uv[j][1] ) * skinheight );
1808 VectorSubtract( ptri[i].verts[0], ptri[i].verts[1], vtemp1 );
1809 VectorSubtract( ptri[i].verts[2], ptri[i].verts[1], vtemp2 );
1810 CrossProduct( vtemp1, vtemp2, normal );
1812 if ( normal[1] > 0 ) {
1820 for ( j = 0 ; j < 3 ; j++ )
1822 pbasevert = ptri[i].verts[j];
1824 triangle_st[i][j][0] = Q_rint( ( pbasevert[0] - mins[0] ) * s_scale + basex );
1825 triangle_st[i][j][1] = Q_rint( ( maxs[2] - pbasevert[2] ) * t_scale + basey );
1830 DrawLine( triangle_st[i][0][0], triangle_st[i][0][1],
1831 triangle_st[i][1][0], triangle_st[i][1][1] );
1832 DrawLine( triangle_st[i][1][0], triangle_st[i][1][1],
1833 triangle_st[i][2][0], triangle_st[i][2][1] );
1834 DrawLine( triangle_st[i][2][0], triangle_st[i][2][1],
1835 triangle_st[i][0][0], triangle_st[i][0][1] );
1839 // make the width a multiple of 4; some hardware requires this, and it ensures
1840 // dword alignment for each scan
1843 if ( backface_flag ) {
1846 fmheader.skinwidth = ( swidth + 3 ) & ~3;
1847 fmheader.skinheight = iheight;
1849 skin_width = iwidth;
1850 skin_height = iheight;
1854 static void BuildNewST( triangle_t *ptri, int numtri, qboolean DrawSkin ){
1857 for ( i = 0 ; i < numtri ; i++ )
1859 if ( ptri[i].HasUV ) {
1860 for ( j = 0 ; j < 3 ; j++ )
1862 triangle_st[i][j][0] = Q_rint( ptri[i].uv[j][0] * ( ScaleWidth - 1 ) );
1863 triangle_st[i][j][1] = Q_rint( ( 1.0f - ptri[i].uv[j][1] ) * ( ScaleHeight - 1 ) );
1868 DrawLine( triangle_st[i][0][0], triangle_st[i][0][1],
1869 triangle_st[i][1][0], triangle_st[i][1][1] );
1870 DrawLine( triangle_st[i][1][0], triangle_st[i][1][1],
1871 triangle_st[i][2][0], triangle_st[i][2][1] );
1872 DrawLine( triangle_st[i][2][0], triangle_st[i][2][1],
1873 triangle_st[i][0][0], triangle_st[i][0][1] );
1877 // make the width a multiple of 4; some hardware requires this, and it ensures
1878 // dword alignment for each scan
1880 fmheader.skinwidth = ( ScaleWidth + 3 ) & ~3;
1881 fmheader.skinheight = ScaleHeight;
1883 skin_width = ScaleWidth;
1884 skin_height = ScaleHeight;
1891 byte *BasePixels,*TransPixels;
1892 int BaseWidth, BaseHeight, TransWidth, TransHeight;
1893 qboolean BaseTrueColor;
1894 static qboolean SetPixel = false;
1896 int CheckTransRecursiveTri( int *lp1, int *lp2, int *lp3 ){
1901 d = lp2[0] - lp1[0];
1902 if ( d < -1 || d > 1 ) {
1905 d = lp2[1] - lp1[1];
1906 if ( d < -1 || d > 1 ) {
1910 d = lp3[0] - lp2[0];
1911 if ( d < -1 || d > 1 ) {
1914 d = lp3[1] - lp2[1];
1915 if ( d < -1 || d > 1 ) {
1919 d = lp1[0] - lp3[0];
1920 if ( d < -1 || d > 1 ) {
1923 d = lp1[1] - lp3[1];
1924 if ( d < -1 || d > 1 ) {
1934 return 0; // entire tri is filled
1944 new[0] = ( lp1[0] + lp2[0] ) >> 1;
1945 new[1] = ( lp1[1] + lp2[1] ) >> 1;
1947 // draw the point if splitting a leading edge
1948 if ( lp2[1] > lp1[1] ) {
1951 if ( ( lp2[1] == lp1[1] ) && ( lp2[0] < lp1[0] ) ) {
1956 assert( ( new[1] * BaseWidth ) + new[0] < BaseWidth * BaseHeight );
1958 if ( BaseTrueColor ) {
1959 BasePixels[( ( new[1] * BaseWidth ) + new[0] ) * 4] = 1;
1963 BasePixels[( new[1] * BaseWidth ) + new[0]] = 1;
1968 if ( TransPixels ) {
1969 if ( TransPixels[( new[1] * TransWidth ) + new[0]] != 255 ) {
1973 else if ( BaseTrueColor ) {
1974 if ( BasePixels[( ( ( new[1] * BaseWidth ) + new[0] ) * 4 ) + 3] != 255 ) {
1980 // pixel = BasePixels[(new[1]*BaseWidth) + new[0]];
1985 // recursively continue
1986 if ( CheckTransRecursiveTri( lp3, lp1, new ) ) {
1990 return CheckTransRecursiveTri( lp3, new, lp2 );
1993 static void ReplaceClusterIndex( int newIndex, int oldindex, int **clusters,
1994 IntListNode_t **vertLists, int *num_verts, int *new_num_verts ){
1996 IntListNode_t *next;
1998 for ( j = 0; j < numJointsInSkeleton[g_skelModel.type]; ++j )
2000 if ( !clusters[j] ) {
2004 for ( i = 0; i < num_verts[j + 1]; ++i )
2006 if ( clusters[j][i] == oldindex ) {
2007 ++new_num_verts[j + 1];
2009 next = vertLists[j];
2011 vertLists[j] = (IntListNode_t *) SafeMalloc( sizeof( IntListNode_t ), "ReplaceClusterIndex" );
2012 // Currently freed in WriteJointedModelFile only
2014 vertLists[j]->data = newIndex;
2015 vertLists[j]->next = next;
2021 #define FUDGE_EPSILON 0.002
2023 qboolean VectorFudgeCompare( vec3_t v1, vec3_t v2 ){
2026 for ( i = 0 ; i < 3 ; i++ )
2027 if ( fabs( v1[i] - v2[i] ) > FUDGE_EPSILON ) {
2039 void Cmd_FMBase( qboolean GetST ){
2040 triangle_t *ptri, *st_tri;
2045 char file1[1024],file2[1024],trans_file[1024], stfile[1024], extension[256];
2046 vec3_t base_xyz[MAX_FM_VERTS];
2051 GetScriptToken( false );
2053 if ( g_skipmodel || g_release || g_archive ) {
2057 printf( "---------------------\n" );
2058 sprintf( file1, "%s/%s.%s", cdarchive, token, trifileext );
2059 printf( "%s ", file1 );
2061 ExpandPathAndArchive( file1 );
2063 // Use the input filepath for this one.
2064 sprintf( file1, "%s/%s", cddir, token );
2066 // time1 = FileTime (file1);
2068 // Error ("%s doesn't exist", file1);
2071 // load the base triangles
2074 Load3DSTriangleList( file1, &ptri, &fmheader.num_tris, &pmnodes, &fmheader.num_mesh_nodes );
2077 LoadTriangleList( file1, &ptri, &fmheader.num_tris, &pmnodes, &fmheader.num_mesh_nodes );
2080 if ( g_ignoreTriUV ) {
2081 for ( i = 0; i < fmheader.num_tris; i++ )
2087 GetScriptToken( false );
2088 sprintf( file2, "%s/%s", cddir, token );
2089 sprintf( trans_file, "%s/!%s_a.pcx", cddir, token );
2091 ExtractFileExtension( file2, extension );
2092 if ( extension[0] == 0 ) {
2093 strcat( file2, ".pcx" );
2095 printf( "skin: %s\n", file2 );
2097 BaseTrueColor = LoadAnyImage( file2, &BasePixels, &BasePalette, &BaseWidth, &BaseHeight );
2100 if ( BaseWidth != SKINPAGE_WIDTH || BaseHeight != SKINPAGE_HEIGHT ) {
2101 if ( g_allow_newskin ) {
2102 ScaleWidth = BaseWidth;
2103 ScaleHeight = BaseHeight;
2108 Error( "Invalid skin page size: (%d,%d) should be (%d,%d)",
2109 BaseWidth,BaseHeight,SKINPAGE_WIDTH,SKINPAGE_HEIGHT );
2112 else if ( !BaseTrueColor ) {
2113 ScaleWidth = (float)ExtractNumber( BasePixels, ENCODED_WIDTH_X,
2115 ScaleHeight = (float)ExtractNumber( BasePixels, ENCODED_HEIGHT_X,
2120 Error( "Texture coordinates not supported on true color image" );
2124 GetScriptToken( false );
2126 sprintf( stfile, "%s/%s.%s", cdarchive, token, trifileext );
2127 printf( "ST: %s ", stfile );
2129 sprintf( stfile, "%s/%s", cddir, token );
2132 Load3DSTriangleList( stfile, &st_tri, &num_st_tris, NULL, NULL );
2135 LoadTriangleList( stfile, &st_tri, &num_st_tris, NULL, NULL );
2138 if ( num_st_tris != fmheader.num_tris ) {
2139 Error( "num st tris mismatch: st %d / base %d", num_st_tris, fmheader.num_tris );
2142 printf( " matching triangles...\n" );
2143 for ( i = 0; i < fmheader.num_tris; i++ )
2146 for ( j = 0; j < num_st_tris; j++ )
2148 for ( x = 0; x < 3; x++ )
2150 for ( y = 0; y < 3; y++ )
2155 for ( z = 0; z < 3; z++ )
2157 if ( z == x || z == y ) {
2161 if ( VectorFudgeCompare( ptri[i].verts[0], st_tri[j].verts[x] ) &&
2162 VectorFudgeCompare( ptri[i].verts[1], st_tri[j].verts[y] ) &&
2163 VectorFudgeCompare( ptri[i].verts[2], st_tri[j].verts[z] ) ) {
2166 ptri[i].HasUV = st_tri[k].HasUV;
2167 ptri[i].uv[0][0] = st_tri[k].uv[x][0];
2168 ptri[i].uv[0][1] = st_tri[k].uv[x][1];
2169 ptri[i].uv[1][0] = st_tri[k].uv[y][0];
2170 ptri[i].uv[1][1] = st_tri[k].uv[y][1];
2171 ptri[i].uv[2][0] = st_tri[k].uv[z][0];
2172 ptri[i].uv[2][1] = st_tri[k].uv[z][1];
2175 else if ( k != j ) {
2176 printf( "Duplicate triangle %d found in st file: %d and %d\n",i,k,j );
2177 printf( " (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f)\n",
2178 ptri[i].verts[0][0],ptri[i].verts[0][1],ptri[i].verts[0][2],
2179 ptri[i].verts[1][0],ptri[i].verts[1][1],ptri[i].verts[1][2],
2180 ptri[i].verts[2][0],ptri[i].verts[2][1],ptri[i].verts[2][2] );
2181 printf( " (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f)\n",
2182 st_tri[k].verts[0][0],st_tri[k].verts[0][1],st_tri[k].verts[0][2],
2183 st_tri[k].verts[1][0],st_tri[k].verts[1][1],st_tri[k].verts[1][2],
2184 st_tri[k].verts[2][0],st_tri[k].verts[2][1],st_tri[k].verts[2][2] );
2185 printf( " (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f)\n",
2186 st_tri[j].verts[0][0],st_tri[j].verts[0][1],st_tri[j].verts[0][2],
2187 st_tri[j].verts[1][0],st_tri[j].verts[1][1],st_tri[j].verts[1][2],
2188 st_tri[j].verts[2][0],st_tri[j].verts[2][1],st_tri[j].verts[2][2] );
2196 printf( "No matching triangle %d\n",i );
2203 // get the ST values
2205 if ( ptri && ptri[0].HasUV ) {
2207 Error( "Base has UVs with old style skin page\nMaybe you want to use -ignoreUV" );
2211 BuildNewST( ptri, fmheader.num_tris, false );
2217 Error( "Base has new style skin without UVs" );
2221 BuildST( ptri, fmheader.num_tris, false );
2226 if ( !BaseTrueColor ) {
2227 FH = fopen( trans_file,"rb" );
2230 Load256Image( trans_file, &TransPixels, NULL, &TransWidth, &TransHeight );
2231 if ( TransWidth != fmheader.skinwidth || TransHeight != fmheader.skinheight ) {
2232 Error( "source image %s dimensions (%d,%d) are not the same as alpha image (%d,%d)\n",file2,fmheader.skinwidth,fmheader.skinheight,TransWidth,TransHeight );
2238 // run through all the base triangles, storing each unique vertex in the
2239 // base vertex list and setting the indirect triangles to point to the base
2242 for ( l = 0; l < fmheader.num_mesh_nodes; l++ )
2244 for ( i = 0 ; i < fmheader.num_tris ; i++ )
2247 bit = 1 << ( i & 7 );
2248 if ( !( pmnodes[l].tris[pos] & bit ) ) {
2252 for ( j = 0 ; j < 3 ; j++ )
2254 // get the xyz index
2255 for ( k = 0 ; k < fmheader.num_xyz ; k++ )
2257 if ( VectorCompare( ptri[i].verts[j], base_xyz[k] ) ) {
2258 break; // this vertex is already in the base vertex list
2262 if ( k == fmheader.num_xyz ) { // new index
2263 VectorCopy( ptri[i].verts[j], base_xyz[fmheader.num_xyz] );
2265 if ( pmnodes[l].clustered == true ) {
2266 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 );
2273 bit = 1 << ( k & 7 );
2274 pmnodes[l].verts[pos] |= bit;
2276 triangles[i].index_xyz[j] = k;
2279 for ( k = 0 ; k < fmheader.num_st ; k++ )
2281 if ( triangle_st[i][j][0] == base_st[k].s
2282 && triangle_st[i][j][1] == base_st[k].t ) {
2283 break; // this vertex is already in the base vertex list
2287 if ( k == fmheader.num_st ) { // new index
2288 base_st[fmheader.num_st].s = triangle_st[i][j][0];
2289 base_st[fmheader.num_st].t = triangle_st[i][j][1];
2293 triangles[i].index_st[j] = k;
2296 if ( TransPixels || BaseTrueColor ) {
2297 translucent[i] = CheckTransRecursiveTri( triangle_st[i][0], triangle_st[i][1], triangle_st[i][2] );
2301 translucent[i] = false;
2306 if ( !BaseTrueColor ) {
2308 memset( BasePixels,0,BaseWidth * BaseHeight );
2309 for ( i = 0 ; i < fmheader.num_tris ; i++ )
2311 CheckTransRecursiveTri( triangle_st[i][0], triangle_st[i][1], triangle_st[i][2] );
2315 skin_pixels_used = 0;
2316 for ( i = 0; i < fmheader.skinheight; i++ )
2318 for ( j = 0; j < fmheader.skinwidth; j++ )
2320 skin_pixels_used += BasePixels[( i * BaseWidth ) + j];
2323 total_skin_pixels = fmheader.skinheight * fmheader.skinwidth;
2328 memset( BasePixels,0,BaseWidth * BaseHeight * 4 );
2329 for ( i = 0 ; i < fmheader.num_tris ; i++ )
2331 CheckTransRecursiveTri( triangle_st[i][0], triangle_st[i][1], triangle_st[i][2] );
2335 skin_pixels_used = 0;
2336 for ( i = 0; i < fmheader.skinheight; i++ )
2338 for ( j = 0; j < fmheader.skinwidth; j++ )
2340 skin_pixels_used += BasePixels[( ( i * BaseWidth ) + j ) * 4];
2343 total_skin_pixels = fmheader.skinheight * fmheader.skinwidth;
2346 // build triangle strips / fans
2349 if ( TransPixels ) {
2350 free( TransPixels );
2353 if ( BasePalette ) {
2354 free( BasePalette );
2359 void Cmd_FMNodeOrder( void ){
2360 mesh_node_t *newnodes, *pos;
2364 Error( "Base has not been established yet" );
2367 pos = newnodes = malloc( sizeof( mesh_node_t ) * fmheader.num_mesh_nodes );
2369 for ( i = 0; i < fmheader.num_mesh_nodes; i++ )
2371 GetScriptToken( false );
2373 for ( j = 0; j < fmheader.num_mesh_nodes; j++ )
2375 if ( strcmpi( pmnodes[j].name, token ) == 0 ) {
2381 if ( j >= fmheader.num_mesh_nodes ) {
2382 Error( "Node '%s' not in base list!\n", token );
2390 //===============================================================
2392 extern char *FindFrameFile( char *frame );
2400 void GrabFrame( char *frame ){
2403 fmtrivert_t *ptrivert;
2410 // the frame 'run1' will be looked for as either
2411 // run.1 or run1.tri, so the new alias sequence save
2412 // feature an be used
2413 framefile = FindFrameFile( frame );
2415 sprintf( file1, "%s/%s", cdarchive, framefile );
2416 ExpandPathAndArchive( file1 );
2418 sprintf( file1, "%s/%s",cddir, framefile );
2420 printf( "grabbing %s ", file1 );
2422 if ( fmheader.num_frames >= MAX_FM_FRAMES ) {
2423 Error( "fmheader.num_frames >= MAX_FM_FRAMES" );
2425 fr = &g_frames[fmheader.num_frames];
2426 fmheader.num_frames++;
2428 strcpy( fr->name, frame );
2434 Load3DSTriangleList( file1, &ptri, &num_tris, NULL, NULL );
2437 LoadTriangleList( file1, &ptri, &num_tris, NULL, NULL );
2440 if ( num_tris != fmheader.num_tris ) {
2441 Error( "%s: number of triangles (%d) doesn't match base frame (%d)\n", file1, num_tris, fmheader.num_tris );
2445 // allocate storage for the frame's vertices
2449 for ( i = 0 ; i < fmheader.num_xyz ; i++ )
2451 ptrivert[i].vnorm.numnormals = 0;
2452 VectorClear( ptrivert[i].vnorm.normalsum );
2454 ClearBounds( fr->mins, fr->maxs );
2457 // store the frame's vertices in the same order as the base. This assumes the
2458 // triangles and vertices in this frame are in exactly the same order as in the
2461 for ( i = 0 ; i < num_tris ; i++ )
2463 vec3_t vtemp1, vtemp2, normal;
2466 VectorSubtract( ptri[i].verts[0], ptri[i].verts[1], vtemp1 );
2467 VectorSubtract( ptri[i].verts[2], ptri[i].verts[1], vtemp2 );
2468 CrossProduct( vtemp1, vtemp2, normal );
2470 VectorNormalize( normal, normal );
2472 // rotate the normal so the model faces down the positive x axis
2474 normal[0] = -normal[1];
2477 for ( j = 0 ; j < 3 ; j++ )
2479 index_xyz = triangles[i].index_xyz[j];
2481 // rotate the vertices so the model faces down the positive x axis
2482 // also adjust the vertices to the desired origin
2483 ptrivert[index_xyz].v[0] = ( ( -ptri[i].verts[j][1] ) * scale_up ) +
2485 ptrivert[index_xyz].v[1] = ( ptri[i].verts[j][0] * scale_up ) +
2487 ptrivert[index_xyz].v[2] = ( ptri[i].verts[j][2] * scale_up ) +
2490 AddPointToBounds( ptrivert[index_xyz].v, fr->mins, fr->maxs );
2492 VectorAdd( ptrivert[index_xyz].vnorm.normalsum, normal, ptrivert[index_xyz].vnorm.normalsum );
2493 ptrivert[index_xyz].vnorm.numnormals++;
2498 // calculate the vertex normals, match them to the template list, and store the
2499 // index of the best match
2501 for ( i = 0 ; i < fmheader.num_xyz ; i++ )
2509 c = ptrivert[i].vnorm.numnormals;
2511 Error( "Vertex with no triangles attached" );
2514 VectorScale( ptrivert[i].vnorm.normalsum, 1.0 / c, v );
2515 VectorNormalize( v, v );
2520 for ( j = 0 ; j < NUMVERTEXNORMALS ; j++ )
2524 dot = DotProduct( v, avertexnormals[j] );
2525 if ( dot > maxdot ) {
2531 ptrivert[i].lightnormalindex = maxdotindex;
2542 void Cmd_FMFrame( void ){
2543 while ( ScriptTokenAvailable() )
2545 GetScriptToken( false );
2546 if ( g_skipmodel ) {
2549 if ( g_release || g_archive ) {
2550 fmheader.num_frames = 1; // don't skip the writeout
2554 H_printf( "#define FRAME_%-16s\t%i\n", token, fmheader.num_frames );
2556 if ( ( g_skelModel.type != SKEL_NULL ) || ( g_skelModel.references != REF_NULL ) ) {
2557 GrabModelTransform( token );
2562 if ( g_skelModel.type != SKEL_NULL ) {
2563 GrabSkeletalFrame( token );
2566 if ( g_skelModel.references != REF_NULL ) {
2567 GrabReferencedFrame( token );
2570 // need to add the up and dir points to the frame bounds here
2571 // using AddPointToBounds (ptrivert[index_xyz].v, fr->mins, fr->maxs);
2572 // then remove fudge in determining scale on frame write out
2580 Skins aren't actually stored in the file, only a reference
2581 is saved out to the header file.
2584 void Cmd_FMSkin( void ){
2590 char name[1024], savename[1024], transname[1024], extension[256];
2596 GetScriptToken( false );
2598 if ( fmheader.num_skins == MAX_FM_SKINS ) {
2599 Error( "fmheader.num_skins == MAX_FM_SKINS" );
2602 if ( g_skipmodel ) {
2606 sprintf( name, "%s/%s", cdarchive, token );
2607 strcpy( name, ExpandPathAndArchive( name ) );
2608 // sprintf (name, "%s/%s.lbm", cddir, token);
2610 if ( ScriptTokenAvailable() ) {
2611 GetScriptToken( false );
2612 sprintf( g_skins[fmheader.num_skins], "!%s", token );
2613 sprintf( savename, "%s!%s", g_outputDir, token );
2614 sprintf( transname, "%s!%s_a.pcx", gamedir, token );
2618 sprintf( g_skins[fmheader.num_skins], "%s/!%s", cdpartial, token );
2619 sprintf( savename, "%s/!%s", g_outputDir, token );
2620 sprintf( transname, "%s/!%s_a.pcx", cddir, token );
2623 fmheader.num_skins++;
2625 if ( g_skipmodel || g_release || g_archive ) {
2630 printf( "loading %s\n", name );
2631 ExtractFileExtension( name, extension );
2632 if ( extension[0] == 0 ) {
2633 strcat( name, ".pcx" );
2637 TrueColor = LoadAnyImage( name, &pixels, &palette, &width, &height );
2638 // RemapZero (pixels, palette, width, height);
2640 // crop it to the proper size
2643 cropped = (byte *) SafeMalloc( fmheader.skinwidth * fmheader.skinheight, "Cmd_FMSkin" );
2644 for ( y = 0 ; y < fmheader.skinheight ; y++ )
2646 memcpy( cropped + y * fmheader.skinwidth,
2647 pixels + y * width, fmheader.skinwidth );
2651 FH = fopen( transname,"rb" );
2655 strcat( g_skins[fmheader.num_skins - 1],".pcx" );
2656 strcat( savename,".pcx" );
2658 // save off the new image
2659 printf( "saving %s\n", savename );
2660 CreatePath( savename );
2661 WritePCXfile( savename, cropped, fmheader.skinwidth, fmheader.skinheight, palette );
2667 qtex = CreateMip( cropped, fmheader.skinwidth, fmheader.skinheight, palette, &size, true );
2669 strcat( g_skins[fmheader.num_skins - 1],".m8" );
2670 strcat( savename,".m8" );
2672 printf( "saving %s\n", savename );
2673 CreatePath( savename );
2674 SaveFile( savename, (byte *)qtex, size );
2677 strcat( g_skins[fmheader.num_skins - 1],".pcx" );
2678 strcat( savename,".pcx" );
2680 // save off the new image
2681 printf( "saving %s\n", savename );
2682 CreatePath( savename );
2683 WritePCXfile( savename, cropped, fmheader.skinwidth, fmheader.skinheight, palette );
2689 cropped = (byte *) SafeMalloc( fmheader.skinwidth * fmheader.skinheight * 4, "Cmd_FMSkin" );
2690 for ( y = 0 ; y < fmheader.skinheight ; y++ )
2692 memcpy( cropped + ( ( y * fmheader.skinwidth ) * 4 ), pixels + ( y * width * 4 ), fmheader.skinwidth * 4 );
2695 qtex32 = CreateMip32( (unsigned *)cropped, fmheader.skinwidth, fmheader.skinheight, &size, true );
2697 StripExtension( g_skins[fmheader.num_skins - 1] );
2698 strcat( g_skins[fmheader.num_skins - 1],".m32" );
2699 StripExtension( savename );
2700 strcat( savename,".m32" );
2702 printf( "saving %s\n", savename );
2703 CreatePath( savename );
2704 SaveFile( savename, (byte *)qtex32, size );
2720 void Cmd_FMCd( void ){
2726 GetScriptToken( false );
2728 // this is a silly mess...
2729 sprintf( cdpartial, "models/%s", token );
2730 sprintf( cdarchive, "%smodels/%s", gamedir + strlen( qdir ), token );
2731 sprintf( cddir, "%s%s", gamedir, cdpartial );
2733 // Since we also changed directories on the output side (for mirror) make sure the outputdir is set properly too.
2734 sprintf( temp, "%s%s", g_outputDir, cdpartial );
2735 strcpy( g_outputDir, temp );
2737 // if -only was specified and this cd doesn't match,
2738 // skip the model (you only need to match leading chars,
2739 // so you could regrab all monsters with -only monsters)
2743 if ( strncmp( token, g_only, strlen( g_only ) ) ) {
2745 printf( "skipping %s\n", cdpartial );
2752 //=======================
2754 //=======================
2756 void NewGen (char *ModelFile, char *OutputName, int width, int height)
2758 trigroup_t *triangles;
2760 triangle_t *grouptris;
2761 mesh_node_t *pmnodes;
2765 vec3_t aveNorm, crossvect;
2766 vec3_t diffvect1, diffvect2;
2769 vec3_t base, zaxis, yaxis;
2770 vec3_t uvwMin, uvwMax;
2771 vec3_t groupMin, groupMax;
2774 float *uFinal, *vFinal;
2775 unsigned char *newpic;
2777 int finalstart = 0, finalcount = 0;
2778 int xbase = 0, xwidth = 0, ywidth = 0;
2779 int *todo, *done, finished;
2780 int i, j, k, l; //counters
2781 int groupnum, numtris, numverts, num;
2786 for ( i = 0; i<3; i++)
2805 LoadTriangleList (ModelFile, &ptri, &fmheader.num_tris, &pmnodes, &fmheader.num_mesh_nodes);
2807 todo = (int*)SafeMalloc(fmheader.num_tris*sizeof(int), "NewGen");
2808 done = (int*)SafeMalloc(fmheader.num_tris*sizeof(int), "NewGen");
2809 triangles = (trigroup_t*)SafeMalloc(fmheader.num_tris*sizeof(trigroup_t), "NewGen");
2811 for ( i=0; i < fmheader.num_tris; i++)
2815 triangles[i].triangle = ptri[i];
2816 triangles[i].group = 0;
2821 // transitive closure algorithm follows
2822 // put all triangles who transitively share vertices into separate groups
2826 for ( i = 0; i < fmheader.num_tris; i++)
2833 if ( i == fmheader.num_tris)
2842 for ( i = 0; i < fmheader.num_tris; i++)
2847 triangles[i].group = groupnum;
2849 for ( j = 0; j < fmheader.num_tris; j++)
2851 if ((!done[j]) && (ShareVertex(triangles[i],triangles[j])))
2862 uFinal = (float*)SafeMalloc(3*fmheader.num_tris*sizeof(float), "NewGen");
2863 vFinal = (float*)SafeMalloc(3*fmheader.num_tris*sizeof(float), "NewGen");
2865 grpfile = fopen("grpdebug.txt","w");
2868 for (i = 0; i < groupnum; i++)
2871 fprintf(grpfile,"Group Number: %d\n", i);
2873 numtris = GetNumTris(triangles, i); // number of triangles in group i
2874 numverts = numtris * 3;
2876 fprintf(grpfile,"%d triangles.\n", numtris);
2878 vertices = (vec3_t*)SafeMalloc(numverts*sizeof(vec3_t), "NewGen");
2879 uvs = (vec3_t*)SafeMalloc(numverts*sizeof(vec3_t), "NewGen");
2880 grouptris = (triangle_t*)SafeMalloc(numtris*sizeof(triangle_t), "NewGen");
2882 for (count = 0; count < fmheader.num_tris; count++)
2884 if (triangles[count].group == i)
2886 fprintf(grpfile,"Triangle %d\n", count);
2889 fprintf(grpfile,"\n");
2894 GetOneGroup(triangles, i, grouptris);
2897 for (j = 0; j < numtris; j++)
2899 VectorCopy(grouptris[j].verts[0], v0);
2900 VectorCopy(grouptris[j].verts[1], v1);
2901 VectorCopy(grouptris[j].verts[2], v2);
2902 VectorSubtract(v1, v0, diffvect1);
2903 VectorSubtract(v2, v1, diffvect2);
2904 CrossProduct( diffvect1, diffvect2, crossvect);
2905 VectorAdd(aveNorm, crossvect, aveNorm);
2906 VectorCopy(v0,vertices[num]);
2908 VectorCopy(v1,vertices[num]);
2909 num++; // add routine to add only verts that
2910 VectorCopy(v2,vertices[num]);
2911 num++; // have not already been added
2915 // figure out the best plane projections
2916 DOsvdPlane ((float*)vertices, num, (float *)&n, (float *)&base);
2918 if (DotProduct(aveNorm,n) < 0.0f)
2920 VectorScale(n, -1.0f, n);
2922 VectorNormalize(n,n);
2923 if (fabs(n[2]) < .57)
2925 CrossProduct( zaxis, n, crossvect);
2926 VectorCopy(crossvect, u);
2930 CrossProduct( yaxis, n, crossvect);
2931 VectorCopy(crossvect, u);
2933 VectorNormalize(u,u);
2934 CrossProduct( n, u, crossvect);
2935 VectorCopy(crossvect, v);
2936 VectorNormalize(v,v);
2940 for ( j = 0; j < 3; j++)
2942 groupMin[j] = 1e30f;
2943 groupMax[j] = -1e30f;
2946 for ( j = 0; j < numtris; j++)
2948 for ( k = 0; k < 3; k++)
2950 VectorCopy(grouptris[j].verts[k],v0);
2951 VectorSubtract(v0, base, v0);
2952 uvw[0] = DotProduct(v0, u);
2953 uvw[1] = DotProduct(v0, v);
2954 uvw[2] = DotProduct(v0, n);
2955 VectorCopy(uvw,uvs[num]);
2957 for ( l = 0; l < 3; l++)
2959 if (uvw[l] < groupMin[l])
2961 groupMin[l] = uvw[l];
2963 if (uvw[l] > groupMax[l])
2965 groupMax[l] = uvw[l];
2971 xwidth = ceil(0 - groupMin[0]) + 2; // move right of origin and avoid overlap
2972 ywidth = ceil(0 - groupMin[1]) + 2; // move "above" origin
2974 for ( j=0; j < numverts; j++)
2976 uFinal[finalcount] = uvs[j][0] + xwidth + xbase;
2977 vFinal[finalcount] = uvs[j][1] + ywidth;
2978 if (uFinal[finalcount] < uvwMin[0])
2980 uvwMin[0] = uFinal[finalcount];
2982 if (uFinal[finalcount] > uvwMax[0])
2984 uvwMax[0] = uFinal[finalcount];
2986 if (vFinal[finalcount] < uvwMin[1])
2988 uvwMin[1] = vFinal[finalcount];
2990 if (vFinal[finalcount] > uvwMax[1])
2992 uvwMax[1] = vFinal[finalcount];
2997 fprintf(grpfile,"svdPlaned Group min: ( %f , %f )\n",groupMin[0] + xwidth + xbase, groupMin[1] + ywidth);
2998 fprintf(grpfile,"svdPlaned Group max: ( %f , %f )\n",groupMax[0] + xwidth + xbase, groupMax[1] + ywidth);
3000 finalcount = finalstart;
3002 for ( count = 0; count < numverts; count++)
3004 fprintf(grpfile,"Vertex %d: ( %f , %f , %f )\n",count,vertices[count][0],vertices[count][1],vertices[count][2]);
3005 fprintf(grpfile,"svdPlaned: ( %f , %f )\n",uFinal[finalcount],vFinal[finalcount++]);
3008 finalstart = finalcount;
3010 fprintf(grpfile,"\n");
3016 xbase += ceil(groupMax[0] - groupMin[0]) + 2;
3020 fprintf(grpfile,"Global Min ( %f , %f )\n",uvwMin[0],uvwMin[1]);
3021 fprintf(grpfile,"Global Max ( %f , %f )\n",uvwMax[0],uvwMax[1]);
3024 ScaleTris(uvwMin, uvwMax, width, height, uFinal, vFinal, finalcount);
3026 for (k = 0; k < finalcount; k++)
3028 fprintf(grpfile, "scaled vertex %d: ( %f , %f )\n",k,uFinal[k],vFinal[k]);
3031 // i've got the array of vertices in uFinal and vFinal. Now I need to write them and draw lines
3033 datasize = width * height*sizeof(unsigned char);
3034 newpic = (unsigned char*)SafeMalloc(datasize, "NewGen");
3035 memset(newpic,0,datasize);
3036 memset(pic_palette,0,sizeof(pic_palette));
3037 pic_palette[767] = pic_palette[766] = pic_palette[765] = 255;
3040 while (k < finalcount)
3042 NewDrawLine(uFinal[k], vFinal[k], uFinal[k+1], vFinal[k+1], newpic, width, height);
3044 NewDrawLine(uFinal[k], vFinal[k], uFinal[k+1], vFinal[k+1], newpic, width, height);
3046 NewDrawLine(uFinal[k], vFinal[k], uFinal[k-2], vFinal[k-2], newpic, width, height);
3048 fprintf(grpfile, "output tri with verts %d, %d, %d", k-2, k-1, k);
3051 WritePCXfile (OutputName, newpic, width, height, pic_palette);
3061 void NewDrawLine(int x1, int y1, int x2, int y2, unsigned char* picture, int width, int height)
3066 float xfrac, yfrac, xstep, ystep;
3067 unsigned long sx, sy;
3075 count = adx > ady ? adx : ady;
3080 printf("Bad count\n");
3081 return; // don't ever hang up on bad data
3087 xstep = (float)dx/count;
3088 ystep = (float)dy/count;
3095 if(xfrac < width && yfrac < height)
3097 picture[(long)yfrac*width+(long)xfrac] = LineColor;
3102 } while (count > 0);
3107 for (u=-0.1 ; u<=0.9 ; u+=0.999)
3109 for (v=-0.1 ; v<=0.9 ; v+=0.999)
3113 if(sx < width && sy < height)
3115 picture[sy*width+sx] = LineColor;
3122 } while (count > 0);
3127 if(count&1 && xfrac < width &&
3130 picture[(long)yfrac*width+(long)xfrac] = LineColor;
3135 } while (count > 0);
3138 Error("Unknown <linetype> %d.\n", LineType);
3142 void ScaleTris( vec3_t min, vec3_t max, int Width, int Height, float* u, float* v, int verts ){
3145 float hscale, vscale;
3151 hscale = ( Width - 2 ) / max[0];
3152 vscale = ( Height - 2 ) / max[1];
3155 if ( scale > vscale ) {
3158 for ( i = 0; i < verts; i++ )
3167 void GetOneGroup( trigroup_t *tris, int grp, triangle_t* triangles ){
3172 for ( i = 0; i < fmheader.num_tris; i++ )
3174 if ( tris[i].group == grp ) {
3175 triangles[j++] = tris[i].triangle;
3182 int GetNumTris( trigroup_t *tris, int grp ){
3187 for ( i = 0; i < fmheader.num_tris; i++ )
3189 if ( tris[i].group == grp ) {
3197 int ShareVertex( trigroup_t trione, trigroup_t tritwo ){
3203 for ( i = 0; i < 3; i++ )
3205 for ( j = 0; j < 3; j++ )
3207 if ( DistBetween( trione.triangle.verts[i],tritwo.triangle.verts[j] ) < TRIVERT_DIST ) {
3216 float DistBetween( vec3_t point1, vec3_t point2 ){
3219 dist = ( point1[0] - point2[0] );
3221 dist += ( point1[1] - point2[1] ) * ( point1[1] - point2[1] );
3222 dist += ( point1[2] - point2[2] ) * ( point1[2] - point2[2] );
3223 dist = sqrt( dist );
3228 void GenSkin( char *ModelFile, char *OutputName, int Width, int Height ){
3230 mesh_node_t *pmnodes;
3236 LoadTriangleList( ModelFile, &ptri, &fmheader.num_tris, &pmnodes, &fmheader.num_mesh_nodes );
3237 if ( g_ignoreTriUV ) {
3238 for ( i = 0; i < fmheader.num_tris; i++ )
3244 memset( pic,0,sizeof( pic ) );
3245 memset( pic_palette,0,sizeof( pic_palette ) );
3246 pic_palette[767] = pic_palette[766] = pic_palette[765] = 255;
3249 ScaleHeight = Height;
3251 BuildST( ptri, fmheader.num_tris, true );
3253 WritePCXfile( OutputName, pic, SKINPAGE_WIDTH, SKINPAGE_HEIGHT, pic_palette );
3255 printf( "Gen Skin Stats:\n" );
3256 printf( " Input Base: %s\n",ModelFile );
3257 printf( " Input Dimensions: %d,%d\n",Width,Height );
3259 printf( " Output File: %s\n",OutputName );
3260 printf( " Output Dimensions: %d,%d\n",ScaleWidth,ScaleHeight );
3262 if ( fmheader.num_mesh_nodes ) {
3263 printf( "\nNodes:\n" );
3264 for ( i = 0; i < fmheader.num_mesh_nodes; i++ )
3266 printf( " %s\n",pmnodes[i].name );
3275 void Cmd_FMBeginGroup( void ){
3276 GetScriptToken( false );
3278 g_no_opimizations = false;
3280 groups[num_groups].start_frame = fmheader.num_frames;
3281 groups[num_groups].num_frames = 0;
3283 groups[num_groups].degrees = atol( token );
3284 if ( groups[num_groups].degrees < 1 || groups[num_groups].degrees > 32 ) {
3285 Error( "Degrees of freedom out of range: %d",groups[num_groups].degrees );
3289 void Cmd_FMEndGroup( void ){
3290 groups[num_groups].num_frames = fmheader.num_frames - groups[num_groups].start_frame;
3292 if ( num_groups < MAX_GROUPS - 1 ) {
3297 Error( "Number of compression groups exceded: %i\n", MAX_GROUPS );