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 );
152 char *strupr (char *start)
165 //==============================================================
172 static void ClearModel( void ){
173 memset( &fmheader, 0, sizeof( fmheader ) );
177 VectorCopy( vec3_origin, adjust );
178 g_fixedwidth = g_fixedheight = 0;
187 ClearSkeletalModel();
191 extern void H_printf( char *fmt, ... );
194 void WriteHeader( FILE *FH, char *Ident, int Version, int Size, void *Data ){
196 static long pos = -1;
199 if ( Size == 0 ) { // Don't write out empty packets
204 CurrentPos = ftell( FH );
205 Size = CurrentPos - pos + sizeof( header_t );
206 fseek( FH, pos, SEEK_SET );
209 else if ( Size == -1 ) {
213 memset( &header,0,sizeof( header ) );
214 strcpy( header.ident,Ident );
215 header.version = Version;
218 SafeWrite( FH, &header, sizeof( header ) );
221 SafeWrite( FH, Data, Size );
226 fseek( FH, 0, SEEK_END );
235 static void WriteModelFile( FILE *modelouthandle ){
240 byte buffer[MAX_FM_VERTS * 4 + 128];
243 IntListNode_t *current, *toFree;
244 qboolean framesWritten = false;
245 size_t temp,size = 0;
247 // probably should do this dynamically one of these days
250 float scale[3]; // multiply byte verts by this
251 float translate[3]; // then add this
252 } outFrames[MAX_FM_FRAMES];
254 #define DATA_SIZE 0x60000 // 384K had better be enough, particularly for the reference points
255 byte data[DATA_SIZE];
256 byte data2[DATA_SIZE];
258 fmheader.num_glcmds = numcommands;
259 fmheader.framesize = (int)&( (fmaliasframe_t *)0 )->verts[fmheader.num_xyz];
261 WriteHeader( modelouthandle, FM_HEADER_NAME, FM_HEADER_VER, sizeof( fmheader ), &fmheader );
264 // write out the skin names
267 WriteHeader( modelouthandle, FM_SKIN_NAME, FM_SKIN_VER, fmheader.num_skins * MAX_FM_SKINNAME, g_skins );
270 // write out the texture coordinates
273 for ( i = 0 ; i < fmheader.num_st ; i++ )
275 base_st[i].s = LittleShort( base_st[i].s );
276 base_st[i].t = LittleShort( base_st[i].t );
279 WriteHeader( modelouthandle, FM_ST_NAME, FM_ST_VER, fmheader.num_st * sizeof( base_st[0] ), base_st );
282 // write out the triangles
284 WriteHeader( modelouthandle, FM_TRI_NAME, FM_TRI_VER, fmheader.num_tris * sizeof( fmtriangle_t ), NULL );
286 for ( i = 0 ; i < fmheader.num_tris ; i++ )
291 for ( j = 0 ; j < 3 ; j++ )
293 tri.index_xyz[j] = LittleShort( triangles[i].index_xyz[j] );
294 tri.index_st[j] = LittleShort( triangles[i].index_st[j] );
297 SafeWrite( modelouthandle, &tri, sizeof( tri ) );
302 // write out the frames
304 WriteHeader( modelouthandle, FM_FRAME_NAME, FM_FRAME_VER, fmheader.num_frames * fmheader.framesize, NULL );
305 // WriteHeader(modelouthandle, FM_FRAME_NAME, FM_FRAME_VER, -1, NULL);
307 for ( i = 0 ; i < fmheader.num_frames ; i++ )
310 out = (fmaliasframe_t *)buffer;
312 strcpy( out->name, in->name );
313 for ( j = 0 ; j < 3 ; j++ )
315 out->scale[j] = ( in->maxs[j] - in->mins[j] ) / 255;
316 out->translate[j] = in->mins[j];
318 outFrames[i].scale[j] = out->scale[j];
319 outFrames[i].translate[j] = out->translate[j];
322 for ( j = 0 ; j < fmheader.num_xyz ; j++ )
324 // all of these are byte values, so no need to deal with endianness
325 out->verts[j].lightnormalindex = in->v[j].lightnormalindex;
327 for ( k = 0 ; k < 3 ; k++ )
329 // scale to byte values & min/max check
330 v = Q_rint( ( in->v[j].v[k] - out->translate[k] ) / out->scale[k] );
332 // clamp, so rounding doesn't wrap from 255.6 to 0
339 out->verts[j].v[k] = v;
343 for ( j = 0 ; j < 3 ; j++ )
345 out->scale[j] = LittleFloat( out->scale[j] );
346 out->translate[j] = LittleFloat( out->translate[j] );
349 SafeWrite( modelouthandle, out, fmheader.framesize );
352 // Go back and finish the header
353 // WriteHeader(modelouthandle, FM_FRAME_NAME, FM_FRAME_VER, -1, NULL);
357 WriteHeader( modelouthandle, FM_SHORT_FRAME_NAME, FM_SHORT_FRAME_VER,FRAME_NAME_LEN * fmheader.num_frames, NULL );
358 for ( i = 0 ; i < fmheader.num_frames ; i++ )
361 SafeWrite( modelouthandle,in->name,FRAME_NAME_LEN );
363 WriteHeader( modelouthandle, FM_NORMAL_NAME, FM_NORMAL_VER,fmheader.num_xyz, NULL );
365 for ( j = 0 ; j < fmheader.num_xyz ; j++ )
366 SafeWrite( modelouthandle,&in->v[j].lightnormalindex,1 );
372 WriteHeader( modelouthandle, FM_GLCMDS_NAME, FM_GLCMDS_VER, numcommands * 4, commands );
375 // write out mesh nodes
377 for ( i = 0; i < fmheader.num_mesh_nodes; i++ )
379 memcpy( mesh_nodes[i].tris, pmnodes[i].tris, sizeof( mesh_nodes[i].tris ) );
380 memcpy( mesh_nodes[i].verts, pmnodes[i].verts, sizeof( mesh_nodes[i].verts ) );
381 mesh_nodes[i].start_glcmds = LittleShort( (short)pmnodes[i].start_glcmds );
382 mesh_nodes[i].num_glcmds = LittleShort( (short)pmnodes[i].num_glcmds );
385 WriteHeader( modelouthandle, FM_MESH_NAME, FM_MESH_VER, sizeof( fmmeshnode_t ) * fmheader.num_mesh_nodes, mesh_nodes );
395 char *mat; fmheader.num_xyz*3*g->degrees*sizeof(char)
396 char *ccomp; g->num_frames*g->degrees*sizeof(char)
397 char *cbase; fmheader.num_xyz*3*sizeof(unsigned char)
398 float *cscale; g->degrees*sizeof(float)
399 float *coffset; g->degrees*sizeof(float)
400 float trans[3]; 3*sizeof(float)
401 float scale[3]; 3*sizeof(float)
406 size = sizeof( int ) + fmheader.num_frames * sizeof( int );
407 for ( k = 0; k < num_groups; k++ )
410 size += sizeof( int ) * 3;
411 size += fmheader.num_xyz * 3 * g->degrees * sizeof( char );
412 size += g->num_frames * g->degrees * sizeof( char );
413 size += fmheader.num_xyz * 3 * sizeof( unsigned char );
414 size += g->degrees * sizeof( float );
415 size += g->degrees * sizeof( float );
416 size += 12 * sizeof( float );
418 WriteHeader( modelouthandle, FM_COMP_NAME, FM_COMP_VER,size, NULL );
419 SafeWrite( modelouthandle,&num_groups,sizeof( int ) );
420 SafeWrite( modelouthandle,frame_to_group,sizeof( int ) * fmheader.num_frames );
422 for ( k = 0; k < num_groups; k++ )
425 tmp = LittleLong( g->start_frame );
426 SafeWrite( modelouthandle,&tmp,sizeof( int ) );
427 tmp = LittleLong( g->num_frames );
428 SafeWrite( modelouthandle,&tmp,sizeof( int ) );
429 tmp = LittleLong( g->degrees );
430 SafeWrite( modelouthandle,&tmp,sizeof( int ) );
432 SafeWrite( modelouthandle,g->mat,fmheader.num_xyz * 3 * g->degrees * sizeof( char ) );
433 SafeWrite( modelouthandle,g->ccomp,g->num_frames * g->degrees * sizeof( char ) );
434 SafeWrite( modelouthandle,g->cbase,fmheader.num_xyz * 3 * sizeof( unsigned char ) );
435 SafeWrite( modelouthandle,g->cscale,g->degrees * sizeof( float ) );
436 SafeWrite( modelouthandle,g->coffset,g->degrees * sizeof( float ) );
437 SafeWrite( modelouthandle,g->trans,3 * sizeof( float ) );
438 SafeWrite( modelouthandle,g->scale,3 * sizeof( float ) );
439 SafeWrite( modelouthandle,g->bmin,3 * sizeof( float ) );
440 SafeWrite( modelouthandle,g->bmax,3 * sizeof( float ) );
449 // write the skeletal info
450 if ( g_skelModel.type != SKEL_NULL ) {
453 temp = sizeof( int ); // change this to a byte
454 memcpy( data + size, &g_skelModel.type, temp );
458 temp = sizeof( int ); // change this to a byte
459 memcpy( data + size, &numJointsInSkeleton[g_skelModel.type], temp );
462 // number of verts in each joint cluster
463 temp = sizeof( int ) * numJointsInSkeleton[g_skelModel.type]; // change this to shorts
464 memcpy( data + size, &g_skelModel.new_num_verts[1], temp );
468 for ( i = 0; i < numJointsInSkeleton[g_skelModel.type]; ++i )
470 current = g_skelModel.vertLists[i];
473 temp = sizeof( int ); // change this to a short
474 memcpy( data + size, ¤t->data, temp );
477 current = current->next;
478 free( toFree ); // freeing of memory allocated in ReplaceClusterIndex called in Cmd_Base
482 if ( !num_groups ) { // joints are stored with regular verts for compressed models
483 framesWritten = true;
485 temp = sizeof( int ); // change this to a byte
486 memcpy( data + size, &framesWritten, temp );
489 for ( i = 0; i < fmheader.num_frames; ++i )
493 for ( j = 0 ; j < numJointsInSkeleton[g_skelModel.type]; ++j )
495 for ( k = 0 ; k < 3 ; k++ )
497 // scale to byte values & min/max check
498 v = Q_rint( ( in->joints[j].placement.origin[k] - outFrames[i].translate[k] ) / outFrames[i].scale[k] );
500 // write out origin as a float since they arn't clamped
501 temp = sizeof( float ); // change this to a short
502 assert( size + temp < DATA_SIZE );
503 memcpy( data + size, &v, temp );
507 for ( k = 0 ; k < 3 ; k++ )
509 v = Q_rint( ( in->joints[j].placement.direction[k] - outFrames[i].translate[k] ) / outFrames[i].scale[k] );
511 // write out origin as a float since they arn't clamped
512 temp = sizeof( float ); // change this to a short
513 assert( size + temp < DATA_SIZE );
514 memcpy( data + size, &v, temp );
518 for ( k = 0 ; k < 3 ; k++ )
520 v = Q_rint( ( in->joints[j].placement.up[k] - outFrames[i].translate[k] ) / outFrames[i].scale[k] );
522 // write out origin as a float since they arn't clamped
523 temp = sizeof( float ); // change this to a short
524 assert( size + temp < DATA_SIZE );
525 memcpy( data + size, &v, temp );
534 temp = sizeof( int ); // change this to a byte
535 memcpy( data + size, &framesWritten, temp );
539 WriteHeader( modelouthandle, FM_SKELETON_NAME, FM_SKELETON_VER, size, data );
542 if ( g_skelModel.references != REF_NULL ) {
546 if ( RefPointNum <= 0 ) { // Hard-coded labels
547 refnum = numReferences[g_skelModel.references];
550 { // Labels indicated in QDT
551 refnum = RefPointNum;
554 temp = sizeof( int ); // change this to a byte
555 memcpy( data2 + size, &g_skelModel.references, temp );
559 framesWritten = true;
561 temp = sizeof( int ); // change this to a byte
562 memcpy( data2 + size, &framesWritten, temp );
565 for ( i = 0; i < fmheader.num_frames; ++i )
569 for ( j = 0 ; j < refnum; ++j )
571 for ( k = 0 ; k < 3 ; k++ )
573 // scale to byte values & min/max check
574 v = Q_rint( ( in->references[j].placement.origin[k] - outFrames[i].translate[k] ) / outFrames[i].scale[k] );
576 // write out origin as a float since they arn't clamped
577 temp = sizeof( float ); // change this to a short
578 assert( size + temp < DATA_SIZE );
579 memcpy( data2 + size, &v, temp );
583 for ( k = 0 ; k < 3 ; k++ )
585 v = Q_rint( ( in->references[j].placement.direction[k] - outFrames[i].translate[k] ) / outFrames[i].scale[k] );
587 // write out origin as a float since they arn't clamped
588 temp = sizeof( float ); // change this to a short
589 assert( size + temp < DATA_SIZE );
590 memcpy( data2 + size, &v, temp );
594 for ( k = 0 ; k < 3 ; k++ )
596 v = Q_rint( ( in->references[j].placement.up[k] - outFrames[i].translate[k] ) / outFrames[i].scale[k] );
598 // write out origin as a float since they arn't clamped
599 temp = sizeof( float ); // change this to a short
600 assert( size + temp < DATA_SIZE );
601 memcpy( data2 + size, &v, temp );
607 else // FINISH ME: references need to be stored with regular verts for compressed models
609 framesWritten = false;
611 temp = sizeof( int ); // change this to a byte
612 memcpy( data2 + size, &framesWritten, temp );
616 WriteHeader( modelouthandle, FM_REFERENCES_NAME, FM_REFERENCES_VER, size, data2 );
620 static void CompressFrames(){
626 for ( i = 0; i < fmheader.num_frames; i++ )
628 while ( i >= groups[j].start_frame + groups[j].num_frames && j < num_groups - 1 )
630 frame_to_group[i] = j;
633 for ( k = 0; k < num_groups; k++ )
637 printf( "\nCompressing Frames for group %i...\n", k );
638 AnimCompressInit( g->num_frames,fmheader.num_xyz,g->degrees );
639 for ( i = 0; i < g->num_frames; i++ )
641 in = &g_frames[i + g->start_frame];
642 for ( j = 0; j < fmheader.num_xyz; j++ )
643 AnimSetFrame( i,j,in->v[j].v[0],in->v[j].v[1],in->v[j].v[2] );
646 g->mat = (char *) SafeMalloc( fmheader.num_xyz * 3 * g->degrees * sizeof( char ), "CompressFrames" );
647 g->ccomp = (char *) SafeMalloc( g->num_frames * g->degrees * sizeof( char ), "CompressFrames" );
648 g->cbase = (char *) SafeMalloc( fmheader.num_xyz * 3 * sizeof( unsigned char ), "CompressFrames" );
649 g->cscale = (float *) SafeMalloc( g->degrees * sizeof( float ), "CompressFrames" );
650 g->coffset = (float *) SafeMalloc( g->degrees * sizeof( float ), "CompressFrames" );
651 AnimCompressToBytes( g->trans,g->scale,g->mat,g->ccomp,g->cbase,g->cscale,g->coffset,g->bmin,g->bmax );
656 static void OptimizeVertices( void ){
657 qboolean vert_used[MAX_FM_VERTS];
658 short vert_replacement[MAX_FM_VERTS];
659 int i,j,k,l,pos,bit,set_pos,set_bit;
663 static IntListNode_t *newVertLists[NUM_CLUSTERS];
664 static int newNum_verts[NUM_CLUSTERS];
665 IntListNode_t *current, *next;
667 printf( "Optimizing vertices..." );
669 memset( vert_used, 0, sizeof( vert_used ) );
671 if ( g_skelModel.clustered == true ) {
672 memset( newNum_verts, 0, sizeof( newNum_verts ) );
673 memset( newVertLists, 0, sizeof( newVertLists ) );
678 // search for common points among all the frames
679 for ( i = 0 ; i < fmheader.num_frames ; i++ )
683 for ( j = 0; j < fmheader.num_xyz; j++ )
685 for ( k = 0,Found = false; k < j; k++ )
686 { // starting from the beginning always ensures vert_replacement points to the first point in the array
687 if ( in->v[j].v[0] == in->v[k].v[0] &&
688 in->v[j].v[1] == in->v[k].v[1] &&
689 in->v[j].v[2] == in->v[k].v[2] ) {
691 vert_replacement[j] = k;
698 if ( !vert_used[j] ) {
706 // recompute the light normals
707 for ( i = 0 ; i < fmheader.num_frames ; i++ )
711 for ( j = 0; j < fmheader.num_xyz; j++ )
713 if ( !vert_used[j] ) {
714 k = vert_replacement[j];
716 VectorAdd( in->v[j].vnorm.normalsum, in->v[k].vnorm.normalsum, in->v[k].vnorm.normalsum );
717 in->v[k].vnorm.numnormals += in->v[j].vnorm.numnormals++;
721 for ( j = 0 ; j < fmheader.num_xyz ; j++ )
728 c = in->v[j].vnorm.numnormals;
730 Error( "Vertex with no triangles attached" );
733 VectorScale( in->v[j].vnorm.normalsum, 1.0 / c, v );
734 VectorNormalize( v, v );
739 for ( k = 0 ; k < NUMVERTEXNORMALS ; k++ )
743 dot = DotProduct( v, avertexnormals[k] );
744 if ( dot > maxdot ) {
750 in->v[j].lightnormalindex = maxdotindex;
754 // create substitution list
756 for ( i = 0; i < fmheader.num_xyz; i++ )
758 if ( vert_used[i] ) {
759 vert_replacement[i] = num_unique;
764 vert_replacement[i] = vert_replacement[vert_replacement[i]];
767 // vert_replacement[i] is the new index, i is the old index
768 // need to add the new index to the cluster list if old index was in it
769 if ( g_skelModel.clustered == true ) {
770 for ( k = 0; k < numJointsInSkeleton[g_skelModel.type]; ++k )
772 for ( l = 0, current = g_skelModel.vertLists[k];
773 l < g_skelModel.new_num_verts[k + 1]; ++l, current = current->next )
775 if ( current->data == i ) {
776 IntListNode_t *current2;
778 qboolean added = false;
780 for ( m = 0, current2 = newVertLists[k]; m < newNum_verts[k + 1];
781 ++m, current2 = current2->next )
783 if ( current2->data == vert_replacement[i] ) {
790 ++newNum_verts[k + 1];
792 next = newVertLists[k];
794 newVertLists[k] = (IntListNode_t *) SafeMalloc( sizeof( IntListNode_t ), "OptimizeVertices" );
795 // freed after model write out
797 newVertLists[k]->data = vert_replacement[i];
798 newVertLists[k]->next = next;
808 for ( i = 0 ; i < fmheader.num_frames ; i++ )
812 for ( j = 0; j < fmheader.num_xyz; j++ )
814 in->v[vert_replacement[j]] = in->v[j];
819 for ( i = 0; i < numJointsInSkeleton[g_skelModel.type]; ++i )
821 IntListNode_t *toFree;
822 current = g_skelModel.vertLists[i];
827 current = current->next;
828 free( toFree ); // freeing of memory allocated in ReplaceClusterIndex called in Cmd_Base
831 g_skelModel.vertLists[i] = newVertLists[i];
832 g_skelModel.new_num_verts[i + 1] = newNum_verts[i + 1];
836 for ( k = 0; k < numJointsInSkeleton[g_skelModel.type]; ++k )
838 for ( l = 0, current = g_skelModel.vertLists[k];
839 l < g_skelModel.new_num_verts[k + 1]; ++l, current = current->next )
841 IntListNode_t *current2;
844 for ( m = l + 1, current2 = current->next; m < newNum_verts[k + 1];
845 ++m, current2 = current2->next )
847 if ( current->data == current2->data ) {
848 printf( "Warning duplicate vertex: %d\n", current->data );
856 for ( i = 0; i < fmheader.num_mesh_nodes; i++ )
857 { // reset the vert bits
858 memset( pmnodes[i].verts,0,sizeof( pmnodes[i].verts ) );
861 // repleace the master triangle list vertex indexes and update the vert bits for each mesh node
862 for ( i = 0 ; i < fmheader.num_tris ; i++ )
865 bit = 1 << ( i & 7 );
867 for ( j = 0 ; j < 3 ; j++ )
869 set_bit = set_pos = triangles[i].index_xyz[j] = vert_replacement[triangles[i].index_xyz[j]];
872 set_bit = 1 << ( set_bit & 7 );
874 for ( k = 0; k < fmheader.num_mesh_nodes; k++ )
876 if ( !( pmnodes[k].tris[pos] & bit ) ) {
879 pmnodes[k].verts[set_pos] |= set_bit;
884 for ( i = 0; i < numcommands; i++ )
892 for ( i++; j; j--,i += 3 )
894 commands[i + 2] = vert_replacement[commands[i + 2]];
899 printf( "Reduced by %d\n",fmheader.num_xyz - num_unique );
901 fmheader.num_xyz = num_unique;
903 // tack on the reference verts to the regular verts
904 if ( g_skelModel.references != REF_NULL ) {
909 if ( RefPointNum <= 0 ) { // Hard-coded labels
910 refnum = numReferences[g_skelModel.references];
913 { // Labels indicated in QDT
914 refnum = RefPointNum;
918 for ( i = 0; i < fmheader.num_frames; ++i )
921 index = fmheader.num_xyz;
923 for ( j = 0 ; j < refnum; ++j )
925 VectorCopy( in->references[j].placement.origin, in->v[index].v );
928 VectorCopy( in->references[j].placement.direction, in->v[index].v );
931 VectorCopy( in->references[j].placement.up, in->v[index].v );
936 fmheader.num_xyz += refnum * 3;
939 // tack on the skeletal joint verts to the regular verts
940 if ( g_skelModel.type != SKEL_NULL ) {
944 for ( i = 0; i < fmheader.num_frames; ++i )
947 index = fmheader.num_xyz;
949 for ( j = 0 ; j < numJointsInSkeleton[g_skelModel.type]; ++j )
951 VectorCopy( in->joints[j].placement.origin, in->v[index].v );
954 VectorCopy( in->joints[j].placement.direction, in->v[index].v );
957 VectorCopy( in->joints[j].placement.up, in->v[index].v );
962 fmheader.num_xyz += numJointsInSkeleton[g_skelModel.type] * 3;
975 void FMFinishModel( void ){
976 FILE *modelouthandle;
977 int i,j,length,tris,verts,bit,pos,total_tris,total_verts;
981 if ( !fmheader.num_frames ) {
986 // copy to release directory tree if doing a release build
989 if ( modelname[0] ) {
990 sprintf( name, "%s", modelname );
993 sprintf( name, "%s/tris.fm", cdpartial );
997 for ( i = 0 ; i < fmheader.num_skins ; i++ )
999 ReleaseFile( g_skins[i] );
1001 fmheader.num_frames = 0;
1008 for ( i = 0; i < fmheader.num_tris; i++ )
1009 if ( translucent[i] ) {
1013 if ( !g_no_opimizations ) {
1018 // write the model output file
1020 if ( modelname[0] ) {
1021 sprintf( name, "%s%s", g_outputDir, modelname );
1024 sprintf( name, "%s/tris.fm", g_outputDir );
1026 printf( "saving to %s\n", name );
1028 modelouthandle = SafeOpenWrite( name );
1030 WriteModelFile( modelouthandle );
1032 printf( "%3dx%3d skin\n", fmheader.skinwidth, fmheader.skinheight );
1033 printf( "First frame boundaries:\n" );
1034 printf( " minimum x: %3f\n", g_frames[0].mins[0] );
1035 printf( " maximum x: %3f\n", g_frames[0].maxs[0] );
1036 printf( " minimum y: %3f\n", g_frames[0].mins[1] );
1037 printf( " maximum y: %3f\n", g_frames[0].maxs[1] );
1038 printf( " minimum z: %3f\n", g_frames[0].mins[2] );
1039 printf( " maximum z: %3f\n", g_frames[0].maxs[2] );
1040 printf( "%4d vertices\n", fmheader.num_xyz );
1041 printf( "%4d triangles, %4d of them translucent\n", fmheader.num_tris, trans_count );
1042 printf( "%4d frame\n", fmheader.num_frames );
1043 printf( "%4d glverts\n", numglverts );
1044 printf( "%4d glcmd\n", fmheader.num_glcmds );
1045 printf( "%4d skins\n", fmheader.num_skins );
1046 printf( "%4d mesh nodes\n", fmheader.num_mesh_nodes );
1047 printf( "wasted pixels: %d / %d (%5.2f Percent)\n",total_skin_pixels - skin_pixels_used,
1048 total_skin_pixels, (double)( total_skin_pixels - skin_pixels_used ) / (double)total_skin_pixels * 100.0 );
1050 printf( "file size: %d\n", (int)ftell( modelouthandle ) );
1051 printf( "---------------------\n" );
1054 if ( fmheader.num_mesh_nodes ) {
1055 total_tris = total_verts = 0;
1056 printf( "Node Name Tris Verts\n" );
1057 printf( "--------------------------------- ---- -----\n" );
1058 for ( i = 0; i < fmheader.num_mesh_nodes; i++ )
1062 for ( j = 0; j < MAXTRIANGLES; j++ )
1065 bit = 1 << ( ( j ) & 7 );
1066 if ( pmnodes[i].tris[pos] & bit ) {
1070 for ( j = 0; j < MAX_FM_VERTS; j++ )
1073 bit = 1 << ( ( j ) & 7 );
1074 if ( pmnodes[i].verts[pos] & bit ) {
1079 printf( "%-33s %4d %5d\n",pmnodes[i].name,tris,verts );
1082 total_verts += verts;
1084 printf( "--------------------------------- ---- -----\n" );
1085 printf( "%-33s %4d %5d\n","TOTALS",total_tris,total_verts );
1088 fclose( modelouthandle );
1090 // finish writing header file
1093 // scale_up is usefull to allow step distances to be adjusted
1094 H_printf( "#define MODEL_SCALE\t\t%f\n", scale_up );
1097 if ( fmheader.num_mesh_nodes ) {
1099 H_printf( "#define NUM_MESH_NODES\t\t%d\n\n",fmheader.num_mesh_nodes );
1100 for ( i = 0; i < fmheader.num_mesh_nodes; i++ )
1102 strcpy( name, pmnodes[i].name );
1104 length = strlen( name );
1105 for ( j = 0; j < length; j++ )
1107 if ( name[j] == ' ' ) {
1111 H_printf( "#define MESH_%s\t\t%d\n", name, i );
1115 fclose( headerouthandle );
1116 headerouthandle = NULL;
1122 =================================================================
1124 ALIAS MODEL DISPLAY LIST GENERATION
1126 =================================================================
1129 extern int strip_xyz[128];
1130 extern int strip_st[128];
1131 extern int strip_tris[128];
1132 extern int stripcount;
1139 static int StripLength( int starttri, int startv, int num_tris, int node ){
1143 fmtriangle_t *last, *check;
1149 last = &triangles[starttri];
1151 strip_xyz[0] = last->index_xyz[( startv ) % 3];
1152 strip_xyz[1] = last->index_xyz[( startv + 1 ) % 3];
1153 strip_xyz[2] = last->index_xyz[( startv + 2 ) % 3];
1154 strip_st[0] = last->index_st[( startv ) % 3];
1155 strip_st[1] = last->index_st[( startv + 1 ) % 3];
1156 strip_st[2] = last->index_st[( startv + 2 ) % 3];
1158 strip_tris[0] = starttri;
1161 m1 = last->index_xyz[( startv + 2 ) % 3];
1162 st1 = last->index_st[( startv + 2 ) % 3];
1163 m2 = last->index_xyz[( startv + 1 ) % 3];
1164 st2 = last->index_st[( startv + 1 ) % 3];
1166 // look for a matching triangle
1168 for ( j = starttri + 1, check = &triangles[starttri + 1]
1169 ; j < num_tris ; j++, check++ )
1172 bit = 1 << ( j & 7 );
1173 if ( !( pmnodes[node].tris[pos] & bit ) ) {
1176 for ( k = 0 ; k < 3 ; k++ )
1178 if ( check->index_xyz[k] != m1 ) {
1181 if ( check->index_st[k] != st1 ) {
1184 if ( check->index_xyz[ ( k + 1 ) % 3 ] != m2 ) {
1187 if ( check->index_st[ ( k + 1 ) % 3 ] != st2 ) {
1191 // this is the next part of the fan
1193 // if we can't use this triangle, this tristrip is done
1194 if ( used[j] || translucent[j] != translucent[starttri] ) {
1199 if ( stripcount & 1 ) {
1200 m2 = check->index_xyz[ ( k + 2 ) % 3 ];
1201 st2 = check->index_st[ ( k + 2 ) % 3 ];
1205 m1 = check->index_xyz[ ( k + 2 ) % 3 ];
1206 st1 = check->index_st[ ( k + 2 ) % 3 ];
1209 strip_xyz[stripcount + 2] = check->index_xyz[ ( k + 2 ) % 3 ];
1210 strip_st[stripcount + 2] = check->index_st[ ( k + 2 ) % 3 ];
1211 strip_tris[stripcount] = j;
1220 // clear the temp used flags
1221 for ( j = starttri + 1 ; j < num_tris ; j++ )
1222 if ( used[j] == 2 ) {
1235 static int FanLength( int starttri, int startv, int num_tris, int node ){
1239 fmtriangle_t *last, *check;
1245 last = &triangles[starttri];
1247 strip_xyz[0] = last->index_xyz[( startv ) % 3];
1248 strip_xyz[1] = last->index_xyz[( startv + 1 ) % 3];
1249 strip_xyz[2] = last->index_xyz[( startv + 2 ) % 3];
1250 strip_st[0] = last->index_st[( startv ) % 3];
1251 strip_st[1] = last->index_st[( startv + 1 ) % 3];
1252 strip_st[2] = last->index_st[( startv + 2 ) % 3];
1254 strip_tris[0] = starttri;
1257 m1 = last->index_xyz[( startv + 0 ) % 3];
1258 st1 = last->index_st[( startv + 0 ) % 3];
1259 m2 = last->index_xyz[( startv + 2 ) % 3];
1260 st2 = last->index_st[( startv + 2 ) % 3];
1263 // look for a matching triangle
1265 for ( j = starttri + 1, check = &triangles[starttri + 1]
1266 ; j < num_tris ; j++, check++ )
1269 bit = 1 << ( j & 7 );
1270 if ( !( pmnodes[node].tris[pos] & bit ) ) {
1273 for ( k = 0 ; k < 3 ; k++ )
1275 if ( check->index_xyz[k] != m1 ) {
1278 if ( check->index_st[k] != st1 ) {
1281 if ( check->index_xyz[ ( k + 1 ) % 3 ] != m2 ) {
1284 if ( check->index_st[ ( k + 1 ) % 3 ] != st2 ) {
1288 // this is the next part of the fan
1290 // if we can't use this triangle, this tristrip is done
1291 if ( used[j] || translucent[j] != translucent[starttri] ) {
1296 m2 = check->index_xyz[ ( k + 2 ) % 3 ];
1297 st2 = check->index_st[ ( k + 2 ) % 3 ];
1299 strip_xyz[stripcount + 2] = m2;
1300 strip_st[stripcount + 2] = st2;
1301 strip_tris[stripcount] = j;
1310 // clear the temp used flags
1311 for ( j = starttri + 1 ; j < num_tris ; j++ )
1312 if ( used[j] == 2 ) {
1325 Generate a list of trifans or strips
1326 for the model, which holds for all frames
1329 static void BuildGlCmds( void ){
1333 int len, bestlen, besttype;
1336 int best_tris[1024];
1348 for ( l = 0; l < fmheader.num_mesh_nodes; l++ )
1350 memset( used, 0, sizeof( used ) );
1352 pmnodes[l].start_glcmds = numcommands;
1354 for ( trans_check = 0; trans_check < 2; trans_check++ )
1356 for ( i = 0 ; i < fmheader.num_tris ; i++ )
1359 bit = 1 << ( i & 7 );
1360 if ( !( pmnodes[l].tris[pos] & bit ) ) {
1364 // pick an unused triangle and start the trifan
1365 if ( used[i] || trans_check != translucent[i] ) {
1370 for ( type = 0 ; type < 2 ; type++ )
1373 for ( startv = 0 ; startv < 3 ; startv++ )
1376 len = StripLength( i, startv, fmheader.num_tris, l );
1379 len = FanLength( i, startv, fmheader.num_tris, l );
1381 if ( len > bestlen ) {
1384 for ( j = 0 ; j < bestlen + 2 ; j++ )
1386 best_st[j] = strip_st[j];
1387 best_xyz[j] = strip_xyz[j];
1389 for ( j = 0 ; j < bestlen ; j++ )
1390 best_tris[j] = strip_tris[j];
1395 // mark the tris on the best strip/fan as used
1396 for ( j = 0 ; j < bestlen ; j++ )
1397 used[best_tris[j]] = 1;
1399 if ( besttype == 1 ) {
1400 commands[numcommands++] = ( bestlen + 2 );
1403 commands[numcommands++] = -( bestlen + 2 );
1406 numglverts += bestlen + 2;
1408 for ( j = 0 ; j < bestlen + 2 ; j++ )
1410 // emit a vertex into the reorder buffer
1413 // emit s/t coords into the commands stream
1417 s = ( s ) / fmheader.skinwidth;
1418 t = ( t ) / fmheader.skinheight;
1420 *(float *)&commands[numcommands++] = s;
1421 *(float *)&commands[numcommands++] = t;
1422 *(int *)&commands[numcommands++] = best_xyz[j];
1426 commands[numcommands++] = 0; // end of list marker
1427 pmnodes[l].num_glcmds = numcommands - pmnodes[l].start_glcmds;
1433 ===============================================================
1437 ===============================================================
1441 #define LINE_NORMAL 1
1443 #define LINE_DOTTED 3
1446 #define ASCII_SPACE 32
1448 int LineType = LINE_NORMAL;
1449 extern unsigned char pic[SKINPAGE_HEIGHT * SKINPAGE_WIDTH], pic_palette[768];
1450 unsigned char LineColor = 255;
1451 int ScaleWidth, ScaleHeight;
1454 static char *CharDefs[] =
1456 "-------------------------",
1457 "-------------------------", // !
1458 "-------------------------", // "
1459 "-------------------------", // #
1460 "-------------------------", // $
1461 "-------------------------", // %
1462 "-------------------------", // &
1463 "--*----*-----------------", // '
1464 "-*---*----*----*-----*---", // (
1465 "*-----*----*----*---*----", // )
1466 "-----*--*--**---**--*--*-", // *
1467 "-------------------------", // +
1468 "----------------**--**---", // ,
1469 "-------------------------", // -
1470 "----------------**---**--", // .
1471 "-------------------------", // /
1472 " *** * *** * *** * *** ", // 0
1474 "**** * *** * *****",
1475 "**** * *** ***** ",
1476 " ** * * * * ***** * ",
1477 "**** * **** ***** ",
1478 " *** * **** * * *** ",
1480 " *** * * *** * * *** ",
1481 " *** * * **** * *** ", // 9
1482 "-**---**--------**---**--", // :
1483 "-------------------------", // ;
1484 "-------------------------", // <
1485 "-------------------------", // =
1486 "-------------------------", // >
1487 "-------------------------", // ?
1488 "-------------------------", // @
1489 "-***-*---*******---**---*", // A
1490 "****-*---*****-*---*****-",
1491 "-*****----*----*-----****",
1492 "****-*---**---**---*****-",
1493 "******----****-*----*****",
1494 "******----****-*----*----",
1495 "-*****----*--***---*-****",
1496 "*---**---*******---**---*",
1497 "-***---*----*----*---***-",
1498 "----*----*----**---*-***-",
1499 "-*--*-*-*--**---*-*--*--*",
1500 "-*----*----*----*----****",
1501 "*---***-***-*-**---**---*",
1502 "*---***--**-*-**--***---*",
1503 "-***-*---**---**---*-***-",
1504 "****-*---*****-*----*----",
1505 "-***-*---**---*-***----**",
1506 "****-*---*****-*-*--*--**",
1507 "-*****-----***-----*****-",
1508 "*****--*----*----*----*--",
1509 "*---**---**---**---******",
1510 "*---**---**---*-*-*---*--",
1511 "*---**---**-*-***-***---*",
1512 "*---*-*-*---*---*-*-*---*",
1513 "*---**---*-*-*---*----*--",
1514 "*****---*---*---*---*****" // Z
1517 void DrawLine( int x1, int y1, int x2, int y2 ){
1521 float xfrac, yfrac, xstep, ystep;
1530 count = adx > ady ? adx : ady;
1533 if ( count > 300 ) {
1534 printf( "Bad count\n" );
1535 return; // don't ever hang up on bad data
1541 xstep = (float)dx / count;
1542 ystep = (float)dy / count;
1549 if ( xfrac < SKINPAGE_WIDTH && yfrac < SKINPAGE_HEIGHT ) {
1550 pic[(int)yfrac * SKINPAGE_WIDTH + (int)xfrac] = LineColor;
1555 } while ( count > 0 );
1560 for ( u = -0.1 ; u <= 0.9 ; u += 0.999 )
1562 for ( v = -0.1 ; v <= 0.9 ; v += 0.999 )
1566 if ( sx < SKINPAGE_WIDTH && sy < SKINPAGE_HEIGHT ) {
1567 pic[sy * SKINPAGE_WIDTH + sx] = LineColor;
1574 } while ( count > 0 );
1579 if ( count & 1 && xfrac < SKINPAGE_WIDTH &&
1580 yfrac < SKINPAGE_HEIGHT ) {
1581 pic[(int)yfrac * SKINPAGE_WIDTH + (int)xfrac] = LineColor;
1586 } while ( count > 0 );
1589 Error( "Unknown <linetype> %d.\n", LineType );
1593 //==========================================================================
1597 //==========================================================================
1599 static void DrawCharacter( int x, int y, int character ){
1603 character = toupper( character );
1604 if ( character < ASCII_SPACE || character > 'Z' ) {
1605 character = ASCII_SPACE;
1607 character -= ASCII_SPACE;
1608 for ( def = CharDefs[character], r = 0; r < 5; r++ )
1610 for ( c = 0; c < 5; c++ )
1612 pic[( y + r ) * SKINPAGE_WIDTH + x + c] = *def++ == '*' ? 255 : 0;
1617 //==========================================================================
1621 //==========================================================================
1623 void DrawTextChar( int x, int y, char *text ){
1626 while ( ( c = *text++ ) != '\0' )
1628 DrawCharacter( x, y, c );
1634 extern void DrawScreen( float s_scale, float t_scale, float iwidth, float iheight );
1636 //==========================================================================
1639 static int ExtractDigit( byte *pic, int x, int y ){
1647 backColor = pic[( SKINPAGE_HEIGHT - 1 ) * SKINPAGE_WIDTH];
1648 DigitDefs = &CharDefs['0' - ASCII_SPACE];
1651 for ( r = 0; r < 5; r++ )
1653 for ( c = 0; c < 5; c++ )
1655 *buffer++ = ( pic[( y + r ) * SKINPAGE_WIDTH + x + c] == backColor ) ? ' ' : '*';
1659 for ( i = 0; i < 10; i++ )
1661 if ( strcmp( DigitDefs[i], digString ) == 0 ) {
1666 Error( "Unable to extract scaling info from skin PCX." );
1670 //==========================================================================
1673 int ExtractNumber( byte *pic, int x, int y ){
1674 return ExtractDigit( pic, x, y ) * 100 + ExtractDigit( pic, x + 6, y ) * 10 + ExtractDigit( pic, x + 12, y );
1685 Builds the triangle_st array for the base frame and
1686 fmheader.skinwidth / fmheader.skinheight
1688 FIXME: allow this to be loaded from a file for
1692 static void BuildST( triangle_t *ptri, int numtri, qboolean DrawSkin ){
1695 int width, height, iwidth, iheight, swidth;
1700 vec3_t vtemp1, vtemp2, normal;
1701 float s_scale, t_scale;
1708 // find bounds of all the verts on the base frame
1710 ClearBounds( mins, maxs );
1711 backface_flag = false;
1713 if ( ptri[0].HasUV ) { // if we have the uv already, we don't want to double up or scale
1714 iwidth = ScaleWidth;
1715 iheight = ScaleHeight;
1717 t_scale = s_scale = 1.0;
1721 for ( i = 0 ; i < numtri ; i++ )
1722 for ( j = 0 ; j < 3 ; j++ )
1723 AddPointToBounds( ptri[i].verts[j], mins, maxs );
1725 for ( i = 0 ; i < 3 ; i++ )
1727 mins[i] = floor( mins[i] );
1728 maxs[i] = ceil( maxs[i] );
1731 width = maxs[0] - mins[0];
1732 height = maxs[2] - mins[2];
1734 for ( i = 0 ; i < numtri ; i++ )
1736 VectorSubtract( ptri[i].verts[0], ptri[i].verts[1], vtemp1 );
1737 VectorSubtract( ptri[i].verts[2], ptri[i].verts[1], vtemp2 );
1738 CrossProduct( vtemp1, vtemp2, normal );
1740 if ( normal[1] > 0 ) {
1741 backface_flag = true;
1745 scWidth = ScaleWidth * SCALE_ADJUST_FACTOR;
1746 if ( backface_flag ) { //we are doubling
1750 scHeight = ScaleHeight * SCALE_ADJUST_FACTOR;
1752 scale = scWidth / width;
1754 if ( height * scale >= scHeight ) {
1755 scale = scHeight / height;
1758 iwidth = ceil( width * scale ) + 4;
1759 iheight = ceil( height * scale ) + 4;
1761 s_scale = (float)( iwidth - 4 ) / width;
1762 t_scale = (float)( iheight - 4 ) / height;
1766 if ( backface_flag ) {
1767 DrawScreen( s_scale, t_scale, iwidth * 2, iheight );
1770 DrawScreen( s_scale, t_scale, iwidth, iheight );
1773 if ( backface_flag ) {
1774 skinwidth = iwidth * 2;
1779 skinheight = iheight;
1782 /* if (!g_fixedwidth)
1785 if (width*scale >= 150)
1786 scale = 150.0 / width;
1787 if (height*scale >= 190)
1788 scale = 190.0 / height;
1790 s_scale = t_scale = scale;
1792 iwidth = ceil(width*s_scale);
1793 iheight = ceil(height*t_scale);
1800 iwidth = g_fixedwidth / 2;
1801 iheight = g_fixedheight;
1803 s_scale = (float)(iwidth-4) / width;
1804 t_scale = (float)(iheight-4) / height;
1808 // determine which side of each triangle to map the texture to
1811 for ( i = 0 ; i < numtri ; i++ )
1813 if ( ptri[i].HasUV ) {
1814 for ( j = 0 ; j < 3 ; j++ )
1816 triangle_st[i][j][0] = Q_rint( ptri[i].uv[j][0] * skinwidth );
1817 triangle_st[i][j][1] = Q_rint( ( 1.0f - ptri[i].uv[j][1] ) * skinheight );
1822 VectorSubtract( ptri[i].verts[0], ptri[i].verts[1], vtemp1 );
1823 VectorSubtract( ptri[i].verts[2], ptri[i].verts[1], vtemp2 );
1824 CrossProduct( vtemp1, vtemp2, normal );
1826 if ( normal[1] > 0 ) {
1834 for ( j = 0 ; j < 3 ; j++ )
1836 pbasevert = ptri[i].verts[j];
1838 triangle_st[i][j][0] = Q_rint( ( pbasevert[0] - mins[0] ) * s_scale + basex );
1839 triangle_st[i][j][1] = Q_rint( ( maxs[2] - pbasevert[2] ) * t_scale + basey );
1844 DrawLine( triangle_st[i][0][0], triangle_st[i][0][1],
1845 triangle_st[i][1][0], triangle_st[i][1][1] );
1846 DrawLine( triangle_st[i][1][0], triangle_st[i][1][1],
1847 triangle_st[i][2][0], triangle_st[i][2][1] );
1848 DrawLine( triangle_st[i][2][0], triangle_st[i][2][1],
1849 triangle_st[i][0][0], triangle_st[i][0][1] );
1853 // make the width a multiple of 4; some hardware requires this, and it ensures
1854 // dword alignment for each scan
1857 if ( backface_flag ) {
1860 fmheader.skinwidth = ( swidth + 3 ) & ~3;
1861 fmheader.skinheight = iheight;
1863 skin_width = iwidth;
1864 skin_height = iheight;
1868 static void BuildNewST( triangle_t *ptri, int numtri, qboolean DrawSkin ){
1871 for ( i = 0 ; i < numtri ; i++ )
1873 if ( ptri[i].HasUV ) {
1874 for ( j = 0 ; j < 3 ; j++ )
1876 triangle_st[i][j][0] = Q_rint( ptri[i].uv[j][0] * ( ScaleWidth - 1 ) );
1877 triangle_st[i][j][1] = Q_rint( ( 1.0f - ptri[i].uv[j][1] ) * ( ScaleHeight - 1 ) );
1882 DrawLine( triangle_st[i][0][0], triangle_st[i][0][1],
1883 triangle_st[i][1][0], triangle_st[i][1][1] );
1884 DrawLine( triangle_st[i][1][0], triangle_st[i][1][1],
1885 triangle_st[i][2][0], triangle_st[i][2][1] );
1886 DrawLine( triangle_st[i][2][0], triangle_st[i][2][1],
1887 triangle_st[i][0][0], triangle_st[i][0][1] );
1891 // make the width a multiple of 4; some hardware requires this, and it ensures
1892 // dword alignment for each scan
1894 fmheader.skinwidth = ( ScaleWidth + 3 ) & ~3;
1895 fmheader.skinheight = ScaleHeight;
1897 skin_width = ScaleWidth;
1898 skin_height = ScaleHeight;
1905 byte *BasePixels,*TransPixels;
1906 int BaseWidth, BaseHeight, TransWidth, TransHeight;
1907 qboolean BaseTrueColor;
1908 static qboolean SetPixel = false;
1910 int CheckTransRecursiveTri( int *lp1, int *lp2, int *lp3 ){
1915 d = lp2[0] - lp1[0];
1916 if ( d < -1 || d > 1 ) {
1919 d = lp2[1] - lp1[1];
1920 if ( d < -1 || d > 1 ) {
1924 d = lp3[0] - lp2[0];
1925 if ( d < -1 || d > 1 ) {
1928 d = lp3[1] - lp2[1];
1929 if ( d < -1 || d > 1 ) {
1933 d = lp1[0] - lp3[0];
1934 if ( d < -1 || d > 1 ) {
1937 d = lp1[1] - lp3[1];
1938 if ( d < -1 || d > 1 ) {
1948 return 0; // entire tri is filled
1958 new[0] = ( lp1[0] + lp2[0] ) >> 1;
1959 new[1] = ( lp1[1] + lp2[1] ) >> 1;
1961 // draw the point if splitting a leading edge
1962 if ( lp2[1] > lp1[1] ) {
1965 if ( ( lp2[1] == lp1[1] ) && ( lp2[0] < lp1[0] ) ) {
1970 assert( ( new[1] * BaseWidth ) + new[0] < BaseWidth * BaseHeight );
1972 if ( BaseTrueColor ) {
1973 BasePixels[( ( new[1] * BaseWidth ) + new[0] ) * 4] = 1;
1977 BasePixels[( new[1] * BaseWidth ) + new[0]] = 1;
1982 if ( TransPixels ) {
1983 if ( TransPixels[( new[1] * TransWidth ) + new[0]] != 255 ) {
1987 else if ( BaseTrueColor ) {
1988 if ( BasePixels[( ( ( new[1] * BaseWidth ) + new[0] ) * 4 ) + 3] != 255 ) {
1994 // pixel = BasePixels[(new[1]*BaseWidth) + new[0]];
1999 // recursively continue
2000 if ( CheckTransRecursiveTri( lp3, lp1, new ) ) {
2004 return CheckTransRecursiveTri( lp3, new, lp2 );
2007 static void ReplaceClusterIndex( int newIndex, int oldindex, int **clusters,
2008 IntListNode_t **vertLists, int *num_verts, int *new_num_verts ){
2010 IntListNode_t *next;
2012 for ( j = 0; j < numJointsInSkeleton[g_skelModel.type]; ++j )
2014 if ( !clusters[j] ) {
2018 for ( i = 0; i < num_verts[j + 1]; ++i )
2020 if ( clusters[j][i] == oldindex ) {
2021 ++new_num_verts[j + 1];
2023 next = vertLists[j];
2025 vertLists[j] = (IntListNode_t *) SafeMalloc( sizeof( IntListNode_t ), "ReplaceClusterIndex" );
2026 // Currently freed in WriteJointedModelFile only
2028 vertLists[j]->data = newIndex;
2029 vertLists[j]->next = next;
2035 #define FUDGE_EPSILON 0.002
2037 qboolean VectorFudgeCompare( vec3_t v1, vec3_t v2 ){
2040 for ( i = 0 ; i < 3 ; i++ )
2041 if ( fabs( v1[i] - v2[i] ) > FUDGE_EPSILON ) {
2053 void Cmd_FMBase( qboolean GetST ){
2054 triangle_t *ptri, *st_tri;
2059 char file1[1024],file2[1024],trans_file[1024], stfile[1024], extension[256];
2060 vec3_t base_xyz[MAX_FM_VERTS];
2065 GetScriptToken( false );
2067 if ( g_skipmodel || g_release || g_archive ) {
2071 printf( "---------------------\n" );
2072 sprintf( file1, "%s/%s.%s", cdarchive, token, trifileext );
2073 printf( "%s ", file1 );
2075 ExpandPathAndArchive( file1 );
2077 // Use the input filepath for this one.
2078 sprintf( file1, "%s/%s", cddir, token );
2080 // time1 = FileTime (file1);
2082 // Error ("%s doesn't exist", file1);
2085 // load the base triangles
2088 Load3DSTriangleList( file1, &ptri, &fmheader.num_tris, &pmnodes, &fmheader.num_mesh_nodes );
2091 LoadTriangleList( file1, &ptri, &fmheader.num_tris, &pmnodes, &fmheader.num_mesh_nodes );
2094 if ( g_ignoreTriUV ) {
2095 for ( i = 0; i < fmheader.num_tris; i++ )
2101 GetScriptToken( false );
2102 sprintf( file2, "%s/%s", cddir, token );
2103 sprintf( trans_file, "%s/!%s_a.pcx", cddir, token );
2105 ExtractFileExtension( file2, extension );
2106 if ( extension[0] == 0 ) {
2107 strcat( file2, ".pcx" );
2109 printf( "skin: %s\n", file2 );
2111 BaseTrueColor = LoadAnyImage( file2, &BasePixels, &BasePalette, &BaseWidth, &BaseHeight );
2114 if ( BaseWidth != SKINPAGE_WIDTH || BaseHeight != SKINPAGE_HEIGHT ) {
2115 if ( g_allow_newskin ) {
2116 ScaleWidth = BaseWidth;
2117 ScaleHeight = BaseHeight;
2122 Error( "Invalid skin page size: (%d,%d) should be (%d,%d)",
2123 BaseWidth,BaseHeight,SKINPAGE_WIDTH,SKINPAGE_HEIGHT );
2126 else if ( !BaseTrueColor ) {
2127 ScaleWidth = (float)ExtractNumber( BasePixels, ENCODED_WIDTH_X,
2129 ScaleHeight = (float)ExtractNumber( BasePixels, ENCODED_HEIGHT_X,
2134 Error( "Texture coordinates not supported on true color image" );
2138 GetScriptToken( false );
2140 sprintf( stfile, "%s/%s.%s", cdarchive, token, trifileext );
2141 printf( "ST: %s ", stfile );
2143 sprintf( stfile, "%s/%s", cddir, token );
2146 Load3DSTriangleList( stfile, &st_tri, &num_st_tris, NULL, NULL );
2149 LoadTriangleList( stfile, &st_tri, &num_st_tris, NULL, NULL );
2152 if ( num_st_tris != fmheader.num_tris ) {
2153 Error( "num st tris mismatch: st %d / base %d", num_st_tris, fmheader.num_tris );
2156 printf( " matching triangles...\n" );
2157 for ( i = 0; i < fmheader.num_tris; i++ )
2160 for ( j = 0; j < num_st_tris; j++ )
2162 for ( x = 0; x < 3; x++ )
2164 for ( y = 0; y < 3; y++ )
2169 for ( z = 0; z < 3; z++ )
2171 if ( z == x || z == y ) {
2175 if ( VectorFudgeCompare( ptri[i].verts[0], st_tri[j].verts[x] ) &&
2176 VectorFudgeCompare( ptri[i].verts[1], st_tri[j].verts[y] ) &&
2177 VectorFudgeCompare( ptri[i].verts[2], st_tri[j].verts[z] ) ) {
2180 ptri[i].HasUV = st_tri[k].HasUV;
2181 ptri[i].uv[0][0] = st_tri[k].uv[x][0];
2182 ptri[i].uv[0][1] = st_tri[k].uv[x][1];
2183 ptri[i].uv[1][0] = st_tri[k].uv[y][0];
2184 ptri[i].uv[1][1] = st_tri[k].uv[y][1];
2185 ptri[i].uv[2][0] = st_tri[k].uv[z][0];
2186 ptri[i].uv[2][1] = st_tri[k].uv[z][1];
2189 else if ( k != j ) {
2190 printf( "Duplicate triangle %d found in st file: %d and %d\n",i,k,j );
2191 printf( " (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f)\n",
2192 ptri[i].verts[0][0],ptri[i].verts[0][1],ptri[i].verts[0][2],
2193 ptri[i].verts[1][0],ptri[i].verts[1][1],ptri[i].verts[1][2],
2194 ptri[i].verts[2][0],ptri[i].verts[2][1],ptri[i].verts[2][2] );
2195 printf( " (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f)\n",
2196 st_tri[k].verts[0][0],st_tri[k].verts[0][1],st_tri[k].verts[0][2],
2197 st_tri[k].verts[1][0],st_tri[k].verts[1][1],st_tri[k].verts[1][2],
2198 st_tri[k].verts[2][0],st_tri[k].verts[2][1],st_tri[k].verts[2][2] );
2199 printf( " (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f) (%0.3f %0.3f %0.3f)\n",
2200 st_tri[j].verts[0][0],st_tri[j].verts[0][1],st_tri[j].verts[0][2],
2201 st_tri[j].verts[1][0],st_tri[j].verts[1][1],st_tri[j].verts[1][2],
2202 st_tri[j].verts[2][0],st_tri[j].verts[2][1],st_tri[j].verts[2][2] );
2210 printf( "No matching triangle %d\n",i );
2217 // get the ST values
2219 if ( ptri && ptri[0].HasUV ) {
2221 Error( "Base has UVs with old style skin page\nMaybe you want to use -ignoreUV" );
2225 BuildNewST( ptri, fmheader.num_tris, false );
2231 Error( "Base has new style skin without UVs" );
2235 BuildST( ptri, fmheader.num_tris, false );
2240 if ( !BaseTrueColor ) {
2241 FH = fopen( trans_file,"rb" );
2244 Load256Image( trans_file, &TransPixels, NULL, &TransWidth, &TransHeight );
2245 if ( TransWidth != fmheader.skinwidth || TransHeight != fmheader.skinheight ) {
2246 Error( "source image %s dimensions (%d,%d) are not the same as alpha image (%d,%d)\n",file2,fmheader.skinwidth,fmheader.skinheight,TransWidth,TransHeight );
2252 // run through all the base triangles, storing each unique vertex in the
2253 // base vertex list and setting the indirect triangles to point to the base
2256 for ( l = 0; l < fmheader.num_mesh_nodes; l++ )
2258 for ( i = 0 ; i < fmheader.num_tris ; i++ )
2261 bit = 1 << ( i & 7 );
2262 if ( !( pmnodes[l].tris[pos] & bit ) ) {
2266 for ( j = 0 ; j < 3 ; j++ )
2268 // get the xyz index
2269 for ( k = 0 ; k < fmheader.num_xyz ; k++ )
2271 if ( VectorCompare( ptri[i].verts[j], base_xyz[k] ) ) {
2272 break; // this vertex is already in the base vertex list
2276 if ( k == fmheader.num_xyz ) { // new index
2277 VectorCopy( ptri[i].verts[j], base_xyz[fmheader.num_xyz] );
2279 if ( pmnodes[l].clustered == true ) {
2280 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 );
2287 bit = 1 << ( k & 7 );
2288 pmnodes[l].verts[pos] |= bit;
2290 triangles[i].index_xyz[j] = k;
2293 for ( k = 0 ; k < fmheader.num_st ; k++ )
2295 if ( triangle_st[i][j][0] == base_st[k].s
2296 && triangle_st[i][j][1] == base_st[k].t ) {
2297 break; // this vertex is already in the base vertex list
2301 if ( k == fmheader.num_st ) { // new index
2302 base_st[fmheader.num_st].s = triangle_st[i][j][0];
2303 base_st[fmheader.num_st].t = triangle_st[i][j][1];
2307 triangles[i].index_st[j] = k;
2310 if ( TransPixels || BaseTrueColor ) {
2311 translucent[i] = CheckTransRecursiveTri( triangle_st[i][0], triangle_st[i][1], triangle_st[i][2] );
2315 translucent[i] = false;
2320 if ( !BaseTrueColor ) {
2322 memset( BasePixels,0,BaseWidth * BaseHeight );
2323 for ( i = 0 ; i < fmheader.num_tris ; i++ )
2325 CheckTransRecursiveTri( triangle_st[i][0], triangle_st[i][1], triangle_st[i][2] );
2329 skin_pixels_used = 0;
2330 for ( i = 0; i < fmheader.skinheight; i++ )
2332 for ( j = 0; j < fmheader.skinwidth; j++ )
2334 skin_pixels_used += BasePixels[( i * BaseWidth ) + j];
2337 total_skin_pixels = fmheader.skinheight * fmheader.skinwidth;
2342 memset( BasePixels,0,BaseWidth * BaseHeight * 4 );
2343 for ( i = 0 ; i < fmheader.num_tris ; i++ )
2345 CheckTransRecursiveTri( triangle_st[i][0], triangle_st[i][1], triangle_st[i][2] );
2349 skin_pixels_used = 0;
2350 for ( i = 0; i < fmheader.skinheight; i++ )
2352 for ( j = 0; j < fmheader.skinwidth; j++ )
2354 skin_pixels_used += BasePixels[( ( i * BaseWidth ) + j ) * 4];
2357 total_skin_pixels = fmheader.skinheight * fmheader.skinwidth;
2360 // build triangle strips / fans
2363 if ( TransPixels ) {
2364 free( TransPixels );
2367 if ( BasePalette ) {
2368 free( BasePalette );
2373 void Cmd_FMNodeOrder( void ){
2374 mesh_node_t *newnodes, *pos;
2378 Error( "Base has not been established yet" );
2381 pos = newnodes = malloc( sizeof( mesh_node_t ) * fmheader.num_mesh_nodes );
2383 for ( i = 0; i < fmheader.num_mesh_nodes; i++ )
2385 GetScriptToken( false );
2387 for ( j = 0; j < fmheader.num_mesh_nodes; j++ )
2389 if ( strcmpi( pmnodes[j].name, token ) == 0 ) {
2395 if ( j >= fmheader.num_mesh_nodes ) {
2396 Error( "Node '%s' not in base list!\n", token );
2404 //===============================================================
2406 extern char *FindFrameFile( char *frame );
2414 void GrabFrame( char *frame ){
2417 fmtrivert_t *ptrivert;
2424 // the frame 'run1' will be looked for as either
2425 // run.1 or run1.tri, so the new alias sequence save
2426 // feature an be used
2427 framefile = FindFrameFile( frame );
2429 sprintf( file1, "%s/%s", cdarchive, framefile );
2430 ExpandPathAndArchive( file1 );
2432 sprintf( file1, "%s/%s",cddir, framefile );
2434 printf( "grabbing %s ", file1 );
2436 if ( fmheader.num_frames >= MAX_FM_FRAMES ) {
2437 Error( "fmheader.num_frames >= MAX_FM_FRAMES" );
2439 fr = &g_frames[fmheader.num_frames];
2440 fmheader.num_frames++;
2442 strcpy( fr->name, frame );
2448 Load3DSTriangleList( file1, &ptri, &num_tris, NULL, NULL );
2451 LoadTriangleList( file1, &ptri, &num_tris, NULL, NULL );
2454 if ( num_tris != fmheader.num_tris ) {
2455 Error( "%s: number of triangles (%d) doesn't match base frame (%d)\n", file1, num_tris, fmheader.num_tris );
2459 // allocate storage for the frame's vertices
2463 for ( i = 0 ; i < fmheader.num_xyz ; i++ )
2465 ptrivert[i].vnorm.numnormals = 0;
2466 VectorClear( ptrivert[i].vnorm.normalsum );
2468 ClearBounds( fr->mins, fr->maxs );
2471 // store the frame's vertices in the same order as the base. This assumes the
2472 // triangles and vertices in this frame are in exactly the same order as in the
2475 for ( i = 0 ; i < num_tris ; i++ )
2477 vec3_t vtemp1, vtemp2, normal;
2480 VectorSubtract( ptri[i].verts[0], ptri[i].verts[1], vtemp1 );
2481 VectorSubtract( ptri[i].verts[2], ptri[i].verts[1], vtemp2 );
2482 CrossProduct( vtemp1, vtemp2, normal );
2484 VectorNormalize( normal, normal );
2486 // rotate the normal so the model faces down the positive x axis
2488 normal[0] = -normal[1];
2491 for ( j = 0 ; j < 3 ; j++ )
2493 index_xyz = triangles[i].index_xyz[j];
2495 // rotate the vertices so the model faces down the positive x axis
2496 // also adjust the vertices to the desired origin
2497 ptrivert[index_xyz].v[0] = ( ( -ptri[i].verts[j][1] ) * scale_up ) +
2499 ptrivert[index_xyz].v[1] = ( ptri[i].verts[j][0] * scale_up ) +
2501 ptrivert[index_xyz].v[2] = ( ptri[i].verts[j][2] * scale_up ) +
2504 AddPointToBounds( ptrivert[index_xyz].v, fr->mins, fr->maxs );
2506 VectorAdd( ptrivert[index_xyz].vnorm.normalsum, normal, ptrivert[index_xyz].vnorm.normalsum );
2507 ptrivert[index_xyz].vnorm.numnormals++;
2512 // calculate the vertex normals, match them to the template list, and store the
2513 // index of the best match
2515 for ( i = 0 ; i < fmheader.num_xyz ; i++ )
2523 c = ptrivert[i].vnorm.numnormals;
2525 Error( "Vertex with no triangles attached" );
2528 VectorScale( ptrivert[i].vnorm.normalsum, 1.0 / c, v );
2529 VectorNormalize( v, v );
2534 for ( j = 0 ; j < NUMVERTEXNORMALS ; j++ )
2538 dot = DotProduct( v, avertexnormals[j] );
2539 if ( dot > maxdot ) {
2545 ptrivert[i].lightnormalindex = maxdotindex;
2556 void Cmd_FMFrame( void ){
2557 while ( ScriptTokenAvailable() )
2559 GetScriptToken( false );
2560 if ( g_skipmodel ) {
2563 if ( g_release || g_archive ) {
2564 fmheader.num_frames = 1; // don't skip the writeout
2568 H_printf( "#define FRAME_%-16s\t%i\n", token, fmheader.num_frames );
2570 if ( ( g_skelModel.type != SKEL_NULL ) || ( g_skelModel.references != REF_NULL ) ) {
2571 GrabModelTransform( token );
2576 if ( g_skelModel.type != SKEL_NULL ) {
2577 GrabSkeletalFrame( token );
2580 if ( g_skelModel.references != REF_NULL ) {
2581 GrabReferencedFrame( token );
2584 // need to add the up and dir points to the frame bounds here
2585 // using AddPointToBounds (ptrivert[index_xyz].v, fr->mins, fr->maxs);
2586 // then remove fudge in determining scale on frame write out
2594 Skins aren't actually stored in the file, only a reference
2595 is saved out to the header file.
2598 void Cmd_FMSkin( void ){
2604 char name[1024], savename[1024], transname[1024], extension[256];
2610 GetScriptToken( false );
2612 if ( fmheader.num_skins == MAX_FM_SKINS ) {
2613 Error( "fmheader.num_skins == MAX_FM_SKINS" );
2616 if ( g_skipmodel ) {
2620 sprintf( name, "%s/%s", cdarchive, token );
2621 strcpy( name, ExpandPathAndArchive( name ) );
2622 // sprintf (name, "%s/%s.lbm", cddir, token);
2624 if ( ScriptTokenAvailable() ) {
2625 GetScriptToken( false );
2626 sprintf( g_skins[fmheader.num_skins], "!%s", token );
2627 sprintf( savename, "%s!%s", g_outputDir, token );
2628 sprintf( transname, "%s!%s_a.pcx", gamedir, token );
2632 sprintf( g_skins[fmheader.num_skins], "%s/!%s", cdpartial, token );
2633 sprintf( savename, "%s/!%s", g_outputDir, token );
2634 sprintf( transname, "%s/!%s_a.pcx", cddir, token );
2637 fmheader.num_skins++;
2639 if ( g_skipmodel || g_release || g_archive ) {
2644 printf( "loading %s\n", name );
2645 ExtractFileExtension( name, extension );
2646 if ( extension[0] == 0 ) {
2647 strcat( name, ".pcx" );
2651 TrueColor = LoadAnyImage( name, &pixels, &palette, &width, &height );
2652 // RemapZero (pixels, palette, width, height);
2654 // crop it to the proper size
2657 cropped = (byte *) SafeMalloc( fmheader.skinwidth * fmheader.skinheight, "Cmd_FMSkin" );
2658 for ( y = 0 ; y < fmheader.skinheight ; y++ )
2660 memcpy( cropped + y * fmheader.skinwidth,
2661 pixels + y * width, fmheader.skinwidth );
2665 FH = fopen( transname,"rb" );
2669 strcat( g_skins[fmheader.num_skins - 1],".pcx" );
2670 strcat( savename,".pcx" );
2672 // save off the new image
2673 printf( "saving %s\n", savename );
2674 CreatePath( savename );
2675 WritePCXfile( savename, cropped, fmheader.skinwidth, fmheader.skinheight, palette );
2681 qtex = CreateMip( cropped, fmheader.skinwidth, fmheader.skinheight, palette, &size, true );
2683 strcat( g_skins[fmheader.num_skins - 1],".m8" );
2684 strcat( savename,".m8" );
2686 printf( "saving %s\n", savename );
2687 CreatePath( savename );
2688 SaveFile( savename, (byte *)qtex, size );
2691 strcat( g_skins[fmheader.num_skins - 1],".pcx" );
2692 strcat( savename,".pcx" );
2694 // save off the new image
2695 printf( "saving %s\n", savename );
2696 CreatePath( savename );
2697 WritePCXfile( savename, cropped, fmheader.skinwidth, fmheader.skinheight, palette );
2703 cropped = (byte *) SafeMalloc( fmheader.skinwidth * fmheader.skinheight * 4, "Cmd_FMSkin" );
2704 for ( y = 0 ; y < fmheader.skinheight ; y++ )
2706 memcpy( cropped + ( ( y * fmheader.skinwidth ) * 4 ), pixels + ( y * width * 4 ), fmheader.skinwidth * 4 );
2709 qtex32 = CreateMip32( (unsigned *)cropped, fmheader.skinwidth, fmheader.skinheight, &size, true );
2711 StripExtension( g_skins[fmheader.num_skins - 1] );
2712 strcat( g_skins[fmheader.num_skins - 1],".m32" );
2713 StripExtension( savename );
2714 strcat( savename,".m32" );
2716 printf( "saving %s\n", savename );
2717 CreatePath( savename );
2718 SaveFile( savename, (byte *)qtex32, size );
2734 void Cmd_FMCd( void ){
2740 GetScriptToken( false );
2742 // this is a silly mess...
2743 sprintf( cdpartial, "models/%s", token );
2744 sprintf( cdarchive, "%smodels/%s", gamedir + strlen( qdir ), token );
2745 sprintf( cddir, "%s%s", gamedir, cdpartial );
2747 // Since we also changed directories on the output side (for mirror) make sure the outputdir is set properly too.
2748 sprintf( temp, "%s%s", g_outputDir, cdpartial );
2749 strcpy( g_outputDir, temp );
2751 // if -only was specified and this cd doesn't match,
2752 // skip the model (you only need to match leading chars,
2753 // so you could regrab all monsters with -only monsters)
2757 if ( strncmp( token, g_only, strlen( g_only ) ) ) {
2759 printf( "skipping %s\n", cdpartial );
2766 //=======================
2768 //=======================
2770 void NewGen (char *ModelFile, char *OutputName, int width, int height)
2772 trigroup_t *triangles;
2774 triangle_t *grouptris;
2775 mesh_node_t *pmnodes;
2779 vec3_t aveNorm, crossvect;
2780 vec3_t diffvect1, diffvect2;
2783 vec3_t base, zaxis, yaxis;
2784 vec3_t uvwMin, uvwMax;
2785 vec3_t groupMin, groupMax;
2788 float *uFinal, *vFinal;
2789 unsigned char *newpic;
2791 int finalstart = 0, finalcount = 0;
2792 int xbase = 0, xwidth = 0, ywidth = 0;
2793 int *todo, *done, finished;
2794 int i, j, k, l; //counters
2795 int groupnum, numtris, numverts, num;
2800 for ( i = 0; i<3; i++)
2819 LoadTriangleList (ModelFile, &ptri, &fmheader.num_tris, &pmnodes, &fmheader.num_mesh_nodes);
2821 todo = (int*)SafeMalloc(fmheader.num_tris*sizeof(int), "NewGen");
2822 done = (int*)SafeMalloc(fmheader.num_tris*sizeof(int), "NewGen");
2823 triangles = (trigroup_t*)SafeMalloc(fmheader.num_tris*sizeof(trigroup_t), "NewGen");
2825 for ( i=0; i < fmheader.num_tris; i++)
2829 triangles[i].triangle = ptri[i];
2830 triangles[i].group = 0;
2835 // transitive closure algorithm follows
2836 // put all triangles who transitively share vertices into separate groups
2840 for ( i = 0; i < fmheader.num_tris; i++)
2847 if ( i == fmheader.num_tris)
2856 for ( i = 0; i < fmheader.num_tris; i++)
2861 triangles[i].group = groupnum;
2863 for ( j = 0; j < fmheader.num_tris; j++)
2865 if ((!done[j]) && (ShareVertex(triangles[i],triangles[j])))
2876 uFinal = (float*)SafeMalloc(3*fmheader.num_tris*sizeof(float), "NewGen");
2877 vFinal = (float*)SafeMalloc(3*fmheader.num_tris*sizeof(float), "NewGen");
2879 grpfile = fopen("grpdebug.txt","w");
2882 for (i = 0; i < groupnum; i++)
2885 fprintf(grpfile,"Group Number: %d\n", i);
2887 numtris = GetNumTris(triangles, i); // number of triangles in group i
2888 numverts = numtris * 3;
2890 fprintf(grpfile,"%d triangles.\n", numtris);
2892 vertices = (vec3_t*)SafeMalloc(numverts*sizeof(vec3_t), "NewGen");
2893 uvs = (vec3_t*)SafeMalloc(numverts*sizeof(vec3_t), "NewGen");
2894 grouptris = (triangle_t*)SafeMalloc(numtris*sizeof(triangle_t), "NewGen");
2896 for (count = 0; count < fmheader.num_tris; count++)
2898 if (triangles[count].group == i)
2900 fprintf(grpfile,"Triangle %d\n", count);
2903 fprintf(grpfile,"\n");
2908 GetOneGroup(triangles, i, grouptris);
2911 for (j = 0; j < numtris; j++)
2913 VectorCopy(grouptris[j].verts[0], v0);
2914 VectorCopy(grouptris[j].verts[1], v1);
2915 VectorCopy(grouptris[j].verts[2], v2);
2916 VectorSubtract(v1, v0, diffvect1);
2917 VectorSubtract(v2, v1, diffvect2);
2918 CrossProduct( diffvect1, diffvect2, crossvect);
2919 VectorAdd(aveNorm, crossvect, aveNorm);
2920 VectorCopy(v0,vertices[num]);
2922 VectorCopy(v1,vertices[num]);
2923 num++; // add routine to add only verts that
2924 VectorCopy(v2,vertices[num]);
2925 num++; // have not already been added
2929 // figure out the best plane projections
2930 DOsvdPlane ((float*)vertices, num, (float *)&n, (float *)&base);
2932 if (DotProduct(aveNorm,n) < 0.0f)
2934 VectorScale(n, -1.0f, n);
2936 VectorNormalize(n,n);
2937 if (fabs(n[2]) < .57)
2939 CrossProduct( zaxis, n, crossvect);
2940 VectorCopy(crossvect, u);
2944 CrossProduct( yaxis, n, crossvect);
2945 VectorCopy(crossvect, u);
2947 VectorNormalize(u,u);
2948 CrossProduct( n, u, crossvect);
2949 VectorCopy(crossvect, v);
2950 VectorNormalize(v,v);
2954 for ( j = 0; j < 3; j++)
2956 groupMin[j] = 1e30f;
2957 groupMax[j] = -1e30f;
2960 for ( j = 0; j < numtris; j++)
2962 for ( k = 0; k < 3; k++)
2964 VectorCopy(grouptris[j].verts[k],v0);
2965 VectorSubtract(v0, base, v0);
2966 uvw[0] = DotProduct(v0, u);
2967 uvw[1] = DotProduct(v0, v);
2968 uvw[2] = DotProduct(v0, n);
2969 VectorCopy(uvw,uvs[num]);
2971 for ( l = 0; l < 3; l++)
2973 if (uvw[l] < groupMin[l])
2975 groupMin[l] = uvw[l];
2977 if (uvw[l] > groupMax[l])
2979 groupMax[l] = uvw[l];
2985 xwidth = ceil(0 - groupMin[0]) + 2; // move right of origin and avoid overlap
2986 ywidth = ceil(0 - groupMin[1]) + 2; // move "above" origin
2988 for ( j=0; j < numverts; j++)
2990 uFinal[finalcount] = uvs[j][0] + xwidth + xbase;
2991 vFinal[finalcount] = uvs[j][1] + ywidth;
2992 if (uFinal[finalcount] < uvwMin[0])
2994 uvwMin[0] = uFinal[finalcount];
2996 if (uFinal[finalcount] > uvwMax[0])
2998 uvwMax[0] = uFinal[finalcount];
3000 if (vFinal[finalcount] < uvwMin[1])
3002 uvwMin[1] = vFinal[finalcount];
3004 if (vFinal[finalcount] > uvwMax[1])
3006 uvwMax[1] = vFinal[finalcount];
3011 fprintf(grpfile,"svdPlaned Group min: ( %f , %f )\n",groupMin[0] + xwidth + xbase, groupMin[1] + ywidth);
3012 fprintf(grpfile,"svdPlaned Group max: ( %f , %f )\n",groupMax[0] + xwidth + xbase, groupMax[1] + ywidth);
3014 finalcount = finalstart;
3016 for ( count = 0; count < numverts; count++)
3018 fprintf(grpfile,"Vertex %d: ( %f , %f , %f )\n",count,vertices[count][0],vertices[count][1],vertices[count][2]);
3019 fprintf(grpfile,"svdPlaned: ( %f , %f )\n",uFinal[finalcount],vFinal[finalcount++]);
3022 finalstart = finalcount;
3024 fprintf(grpfile,"\n");
3030 xbase += ceil(groupMax[0] - groupMin[0]) + 2;
3034 fprintf(grpfile,"Global Min ( %f , %f )\n",uvwMin[0],uvwMin[1]);
3035 fprintf(grpfile,"Global Max ( %f , %f )\n",uvwMax[0],uvwMax[1]);
3038 ScaleTris(uvwMin, uvwMax, width, height, uFinal, vFinal, finalcount);
3040 for (k = 0; k < finalcount; k++)
3042 fprintf(grpfile, "scaled vertex %d: ( %f , %f )\n",k,uFinal[k],vFinal[k]);
3045 // i've got the array of vertices in uFinal and vFinal. Now I need to write them and draw lines
3047 datasize = width * height*sizeof(unsigned char);
3048 newpic = (unsigned char*)SafeMalloc(datasize, "NewGen");
3049 memset(newpic,0,datasize);
3050 memset(pic_palette,0,sizeof(pic_palette));
3051 pic_palette[767] = pic_palette[766] = pic_palette[765] = 255;
3054 while (k < finalcount)
3056 NewDrawLine(uFinal[k], vFinal[k], uFinal[k+1], vFinal[k+1], newpic, width, height);
3058 NewDrawLine(uFinal[k], vFinal[k], uFinal[k+1], vFinal[k+1], newpic, width, height);
3060 NewDrawLine(uFinal[k], vFinal[k], uFinal[k-2], vFinal[k-2], newpic, width, height);
3062 fprintf(grpfile, "output tri with verts %d, %d, %d", k-2, k-1, k);
3065 WritePCXfile (OutputName, newpic, width, height, pic_palette);
3075 void NewDrawLine(int x1, int y1, int x2, int y2, unsigned char* picture, int width, int height)
3080 float xfrac, yfrac, xstep, ystep;
3081 unsigned long sx, sy;
3089 count = adx > ady ? adx : ady;
3094 printf("Bad count\n");
3095 return; // don't ever hang up on bad data
3101 xstep = (float)dx/count;
3102 ystep = (float)dy/count;
3109 if(xfrac < width && yfrac < height)
3111 picture[(long)yfrac*width+(long)xfrac] = LineColor;
3116 } while (count > 0);
3121 for (u=-0.1 ; u<=0.9 ; u+=0.999)
3123 for (v=-0.1 ; v<=0.9 ; v+=0.999)
3127 if(sx < width && sy < height)
3129 picture[sy*width+sx] = LineColor;
3136 } while (count > 0);
3141 if(count&1 && xfrac < width &&
3144 picture[(long)yfrac*width+(long)xfrac] = LineColor;
3149 } while (count > 0);
3152 Error("Unknown <linetype> %d.\n", LineType);
3156 void ScaleTris( vec3_t min, vec3_t max, int Width, int Height, float* u, float* v, int verts ){
3159 float hscale, vscale;
3165 hscale = ( Width - 2 ) / max[0];
3166 vscale = ( Height - 2 ) / max[1];
3169 if ( scale > vscale ) {
3172 for ( i = 0; i < verts; i++ )
3181 void GetOneGroup( trigroup_t *tris, int grp, triangle_t* triangles ){
3186 for ( i = 0; i < fmheader.num_tris; i++ )
3188 if ( tris[i].group == grp ) {
3189 triangles[j++] = tris[i].triangle;
3196 int GetNumTris( trigroup_t *tris, int grp ){
3201 for ( i = 0; i < fmheader.num_tris; i++ )
3203 if ( tris[i].group == grp ) {
3211 int ShareVertex( trigroup_t trione, trigroup_t tritwo ){
3217 for ( i = 0; i < 3; i++ )
3219 for ( j = 0; j < 3; j++ )
3221 if ( DistBetween( trione.triangle.verts[i],tritwo.triangle.verts[j] ) < TRIVERT_DIST ) {
3230 float DistBetween( vec3_t point1, vec3_t point2 ){
3233 dist = ( point1[0] - point2[0] );
3235 dist += ( point1[1] - point2[1] ) * ( point1[1] - point2[1] );
3236 dist += ( point1[2] - point2[2] ) * ( point1[2] - point2[2] );
3237 dist = sqrt( dist );
3242 void GenSkin( char *ModelFile, char *OutputName, int Width, int Height ){
3244 mesh_node_t *pmnodes;
3250 LoadTriangleList( ModelFile, &ptri, &fmheader.num_tris, &pmnodes, &fmheader.num_mesh_nodes );
3251 if ( g_ignoreTriUV ) {
3252 for ( i = 0; i < fmheader.num_tris; i++ )
3258 memset( pic,0,sizeof( pic ) );
3259 memset( pic_palette,0,sizeof( pic_palette ) );
3260 pic_palette[767] = pic_palette[766] = pic_palette[765] = 255;
3263 ScaleHeight = Height;
3265 BuildST( ptri, fmheader.num_tris, true );
3267 WritePCXfile( OutputName, pic, SKINPAGE_WIDTH, SKINPAGE_HEIGHT, pic_palette );
3269 printf( "Gen Skin Stats:\n" );
3270 printf( " Input Base: %s\n",ModelFile );
3271 printf( " Input Dimensions: %d,%d\n",Width,Height );
3273 printf( " Output File: %s\n",OutputName );
3274 printf( " Output Dimensions: %d,%d\n",ScaleWidth,ScaleHeight );
3276 if ( fmheader.num_mesh_nodes ) {
3277 printf( "\nNodes:\n" );
3278 for ( i = 0; i < fmheader.num_mesh_nodes; i++ )
3280 printf( " %s\n",pmnodes[i].name );
3289 void Cmd_FMBeginGroup( void ){
3290 GetScriptToken( false );
3292 g_no_opimizations = false;
3294 groups[num_groups].start_frame = fmheader.num_frames;
3295 groups[num_groups].num_frames = 0;
3297 groups[num_groups].degrees = atol( token );
3298 if ( groups[num_groups].degrees < 1 || groups[num_groups].degrees > 32 ) {
3299 Error( "Degrees of freedom out of range: %d",groups[num_groups].degrees );
3303 void Cmd_FMEndGroup( void ){
3304 groups[num_groups].num_frames = fmheader.num_frames - groups[num_groups].start_frame;
3306 if ( num_groups < MAX_GROUPS - 1 ) {
3311 Error( "Number of compression groups exceded: %i\n", MAX_GROUPS );