16 "ZERO",
"ZERO",
"ONE",
"SRCCOLOR",
"INVSRCCOLOR",
"SRCALPHA",
"INVSRCALPHA",
17 "DESTALPHA",
"INVDESTALPHA",
"DESTCOLOR",
"INVDESTCOLOR",
"SRCALPHASAT",
"CONSTALPHA",
"INVCONSTALPHA",
18 "CONSTCOLOR",
"INVCONSTCOLOR"
38 #define fprintf aprintf
44 Texture * LoadTexture(
string nam )
49 Texture * LoadAnimation(
string Name )
72 #define GL_TRIANGLE (0)
74 #define DLISTBEGINSTATE( stat ) \
76 if ( stat && (laststate != stat) ) { \
77 if (laststate != GL_COMPILE) glEnd(); \
84 #define DLISTENDSTATE( stat ) \
86 if ( stat && (laststate != GL_COMPILE) ) { \
88 laststate = GL_COMPILE; \
93 #define DLISTDOVERTEX( num ) \
95 glTexCoord2f( vtx.s, vtx.t ); \
96 glNormal3f( vtx.i, vtx.j, vtx.k ); \
97 glVertex3f( vtx.x*xml.scale.i, \
106 #define DLISTBEGINSTATE( stat )
107 #define DLISTDOVERTEX( num )
108 #define DLISTENDSTATE( stat )
113 #define BEGIN_GL_LINES( expectitems ) \
115 xml.active_list = &xml.lines; xml.active_ind = &xml.lineind; xml.num_vertices = \
116 2; xml.active_list->reserve( 2*expectitems ); xml.active_ind->reserve( 2*expectitems ); \
120 #define BEGIN_GL_TRIANGLES( expectitems ) \
122 xml.active_list = &xml.tris; xml.active_ind = &xml.triind; xml.num_vertices = 3; \
123 xml.active_list->reserve( 3*expectitems ); xml.active_ind->reserve( 3*expectitems ); \
127 #define BEGIN_GL_TRIANGLE( expectitems ) \
128 do {xml.trishade.push_back( flatshade ); } \
131 #define BEGIN_GL_QUADS( expectitems ) \
133 xml.active_list = &xml.quads; xml.active_ind = &xml.quadind; xml.num_vertices = \
134 4; xml.active_list->reserve( 4*expectitems ); xml.active_ind->reserve( 4*expectitems ); \
138 #define BEGIN_GL_QUAD( expectitems ) \
139 do {xml.quadshade.push_back( flatshade ); } \
142 #define BEGIN_GL_TRIANGLE_FAN( expectitems ) \
144 xml.trifans.push_back( std::vector< GFXVertex > () ); xml.active_list = \
145 &xml.trifans.back(); xml.tfancnt = xml.trifanind.size(); xml.active_ind = &xml.trifanind; xml.active_list->reserve( \
146 expectitems ); xml.active_ind->reserve( expectitems ); \
150 #define BEGIN_GL_QUAD_STRIP( expectitems ) \
152 xml.num_vertices = 4; xml.quadstrips.push_back( vector< GFXVertex > () ); \
153 xml.active_list = &xml.quadstrips.back(); xml.qstrcnt = xml.quadstripind.size(); xml.active_ind = &xml.quadstripind; \
154 xml.active_list->reserve( expectitems ); xml.active_ind->reserve( expectitems ); \
158 #define BEGIN_GL_TRIANGLE_STRIP( expectitems ) \
160 xml.num_vertices = 3; xml.tristrips.push_back( vector< GFXVertex > () ); \
161 xml.tstrcnt = xml.tristripind.size(); xml.active_ind = &xml.tristripind; xml.active_list->reserve( expectitems ); \
162 xml.active_ind->reserve( expectitems ); \
166 #define BEGIN_GL_LINE_STRIP( expectitems ) \
168 xml.num_vertices = 2; xml.linestrips.push_back( vector< GFXVertex > () ); \
169 xml.active_list = &xml.linestrips.back(); xml.lstrcnt = xml.linestripind.size(); xml.active_ind = &xml.linestripind; \
170 xml.active_list->reserve( expectitems ); xml.active_ind->reserve( expectitems ); \
175 #define END_GL_TRIANGLES
176 #define END_GL_TRIANGLE
180 #define END_GL_TRIANGLE_FAN \
181 do {xml.nrmltrifan.reserve( xml.trifanind.size()*3 ); \
182 for (unsigned int i = xml.tfancnt+2; i < xml.trifanind.size(); i++) { \
183 xml.nrmltrifan.push_back( xml.trifanind[xml.tfancnt] ); \
184 xml.nrmltrifan.push_back( xml.trifanind[i-1] ); \
185 xml.nrmltrifan.push_back( xml.trifanind[i] ); \
190 #define END_GL_QUAD_STRIP \
191 do {xml.nrmlquadstrip.reserve( xml.quadstripind.size()*(4/2) ); \
192 for (unsigned int i = xml.qstrcnt+3; i < xml.quadstripind.size(); i += 2) { \
193 xml.nrmlquadstrip.push_back( xml.quadstripind[i-3] ); \
194 xml.nrmlquadstrip.push_back( xml.quadstripind[i-2] ); \
195 xml.nrmlquadstrip.push_back( xml.quadstripind[i] ); \
196 xml.nrmlquadstrip.push_back( xml.quadstripind[i-1] ); \
201 #define END_GL_TRIANGLE_STRIP \
202 do {xml.nrmltristrip.reserve( xml.tristripind.size()*3 ); \
203 for (unsigned int i = xml.tstrcnt+2; i < xml.tristripind.size(); i++) { \
204 if ( (i-xml.tstrcnt)%2 ) { \
205 xml.nrmltristrip.push_back( xml.tristripind[i-2] ); \
206 xml.nrmltristrip.push_back( xml.tristripind[i-1] ); \
207 xml.nrmltristrip.push_back( xml.tristripind[i] ); \
210 xml.nrmltristrip.push_back( xml.tristripind[i-1] ); \
211 xml.nrmltristrip.push_back( xml.tristripind[i-2] ); \
212 xml.nrmltristrip.push_back( xml.tristripind[i] ); \
218 #define END_GL_LINE_STRIP \
220 xml.nrmllinstrip.reserve( xml.linestripind.size()*2 ); \
221 for (unsigned int i = xml.lstrcnt+1; i < xml.linestripind.size(); i++) { \
222 xml.nrmllinstrip.push_back( xml.linestripind[i-1] ); \
223 xml.nrmllinstrip.push_back( xml.linestripind[i] ); \
228 #define END_GL_COMPILE
230 #define READSTRING( inmemfile, word32index, stringlen, stringvar ) \
232 char8bit *inmemstring = (char8bit*) (inmemfile+word32index); \
233 char8bit oc = inmemstring[stringlen]; inmemstring[stringlen] = 0; \
234 stringvar = (const char*) inmemstring; \
235 inmemstring[stringlen] = oc; \
236 word32index += (stringlen+3)/4; \
240 #define BEGINSTATE( stat, expectitems ) \
241 do {BEGIN_##stat( expectitems ); DLISTBEGINSTATE( stat ); } \
244 #define ENDSTATE( stat ) \
245 do {END_##stat; DLISTENDSTATE( stat ); } \
248 #define DOVERTEX( num ) \
250 vtx = xml.vertices[ind##num]; \
251 if (!xml.sharevert) { \
256 xml.vertexcount[ind##num] += 1; \
257 if ( (!vtx.i) && (!vtx.j) && (!vtx.k) && !xml.recalc_norm ) \
258 xml.recalc_norm = true; \
259 xml.active_list->push_back( xml.vertex ); \
260 xml.active_ind->push_back( ind##num ); \
261 xml.num_vertices--; \
262 DLISTDOVERTEX( stat ); \
266 template <
typename T >
270 for ( ; ( !vec.empty() ); vec.pop_back() )
271 newvec.push_back( vec.back() );
277 #define bxmfprintf fprintf
278 #define bxmfopen fopen
280 void Mesh::BFXMToXmesh( FILE *Inputfile, FILE *Outputfile, vector< Mesh* > &output,
Vector overallscale,
int fac )
293 #define bxmfprintf fignoref
294 #define bxmfopen fignorefopen
301 const std::vector< std::string > &overrideTextures )
303 Vector overallscale = scalex;
305 FILE *Outputfile = 0;
306 vector< Mesh* >output;
310 vector< OrigMeshLoader >meshes;
320 printf(
"Loading Mesh File: %s\n", Inputfile.
GetFilename().c_str() );
321 fseek( Inputfile, 4+
sizeof (
int32bit), SEEK_SET );
322 fread( &intbuf,
sizeof (
int32bit), 1, Inputfile );
324 inmemfile = (chunk32*) malloc( Inputlength+1 );
326 fprintf( stderr,
"Buffer allocation failed, Aborting" );
330 fread( inmemfile, 1, Inputlength, Inputfile );
334 inmemfile = (chunk32*) malloc( Inputlength );
336 fprintf( stderr,
"Buffer allocation failed, Aborting" );
339 Inputfile.
Read( inmemfile, Inputlength );
351 word32index = (Superheaderlength/4);
353 for (
int32bit recordindex = 0; recordindex < numrecords; recordindex++) {
354 int32bit recordbeginword = word32index;
361 word32index = recordbeginword+(recordheaderlength/4);
363 meshes.back().num = nummeshes;
364 meshes.back().m =
new Mesh[nummeshes];
365 meshes.back().sizes.insert( meshes.back().sizes.begin(), nummeshes, 0 );
367 for (
int32bit meshindex = 0; meshindex < nummeshes; meshindex++) {
368 Mesh *mesh = &meshes.back().m[meshindex];
373 if (recordindex > 0 || meshindex > 0) {
374 char filenamebuf[56];
375 int32bit error = sprintf( filenamebuf,
"%d_%d.xmesh", recordindex, meshindex );
378 string filename = string( filenamebuf );
379 Outputfile =
bxmfopen( filename.c_str(),
"w+" );
382 int32bit meshbeginword = word32index;
415 if (meshheaderlength > 30*4)
419 word32index = meshbeginword+(meshheaderlength/4);
420 int32bit VSAbeginword = word32index;
425 "<Mesh scale=\"%f\" reverse=\"%d\" forcetexture=\"%d\" sharevert=\"%d\" polygonoffset=\"%f\" blendmode=\"%s %s\" alphatest=\"%f\" ",
434 xml.
scale = scale*overallscale;
439 if (alphatest <= 1 && alphatest >= 0)
440 mesh->
alphatest = (
unsigned char) (alphatest*255.0);
441 else if (alphatest > 1)
447 string detailtexturename =
"";
450 READSTRING( inmemfile, word32index, detailtexturenamelen, detailtexturename );
451 if (detailtexturename.size() != 0) {
452 bxmfprintf( Outputfile,
" detailtexture=\"%s\" ", detailtexturename.c_str() );
458 vector< Mesh_vec3f >Detailplanes;
461 for (
int32bit detailplane = 0; detailplane < numdetailplanes; detailplane++) {
470 Detailplanes.push_back( temp );
475 for (
int32bit tex = 0; tex < numtextures; tex++) {
490 READSTRING( inmemfile, word32index, texnamelen, texname );
508 bxmfprintf( Outputfile,
"=\"%s\" ", texname.c_str() );
512 while (mesh->
Decal.size() <= (
unsigned int) texindex)
513 mesh->
Decal.push_back( 0 );
514 while (xml.
decals.size() <= (
unsigned int) texindex) {
516 xml.
decals.push_back( z );
521 xml.
decals[texindex].alpha_name = texname;
525 xml.
decals[texindex].decal_name = texname;
529 xml.
decals[texindex].animated_name = texname;
555 "<Material power=\"%f\" cullface=\"%d\" reflect=\"%d\" lighting=\"%d\" usenormals=\"%d\">\n",
561 bxmfprintf( Outputfile,
"\t<Ambient Red=\"%f\" Green=\"%f\" Blue=\"%f\" Alpha=\"%f\"/>\n", ar, ag, ab, aa );
562 bxmfprintf( Outputfile,
"\t<Diffuse Red=\"%f\" Green=\"%f\" Blue=\"%f\" Alpha=\"%f\"/>\n", dr, dg, db, da );
563 bxmfprintf( Outputfile,
"\t<Emissive Red=\"%f\" Green=\"%f\" Blue=\"%f\" Alpha=\"%f\"/>\n", er, eg, eb, ea );
564 bxmfprintf( Outputfile,
"\t<Specular Red=\"%f\" Green=\"%f\" Blue=\"%f\" Alpha=\"%f\"/>\n", sr, sg, sb, sa );
591 for (
int32bit detplane = 0; (
unsigned int) detplane < Detailplanes.size(); detplane++) {
592 bxmfprintf( Outputfile,
"<DetailPlane x=\"%f\" y=\"%f\" z=\"%f\" />\n", Detailplanes[detplane].
x,
593 Detailplanes[detplane].
y, Detailplanes[detplane].
z );
595 Detailplanes[detplane].y,
596 Detailplanes[detplane].z ) );
600 for (
int32bit logo = 0; logo < numlogos; logo++) {
607 "<Logo type=\"%d\" rotate=\"%f\" size=\"%f\" offset=\"%f\">\n",
617 xml.
logos.push_back( l );
619 xml.
logos.back().rotate = rotation;
624 for (
int32bit ref = 0; ref < numrefs; ref++) {
627 bxmfprintf( Outputfile,
"\t<Ref point=\"%d\" weight=\"%f\"/>\n", refnum, weight );
628 xml.
logos.back().refpnt.push_back( refnum );
629 xml.
logos.back().refweight.push_back( weight );
641 bxmfprintf( Outputfile,
"<LOD size=\"%f\" meshfile=\"%d_%d.xmesh\"/>\n", size, recordindex, index );
642 meshes.back().sizes[
LOD] =
size;
651 for (
int framecount = numLODs+1; framecount < nummeshes; framecount++)
652 bxmfprintf( Outputfile,
"<Frame FrameMeshName=\"%d_%d.xmesh\"/>\n", recordindex, framecount );
654 for (
int32bit anim = 0; anim < numanimdefs; anim++) {
658 READSTRING( inmemfile, word32index, animnamelen, animname );
660 bxmfprintf( Outputfile,
"<AnimationDefinition AnimationName=\"%s\" FPS=\"%f\">\n", animname.c_str(), FPS );
662 vector< int > *framerefs =
new vector< int >;
664 word32index += NUMFIELDSPERANIMATIONDEF;
667 for (
int32bit fref = 0; fref < numframerefs; fref++) {
669 word32index += NUMFIELDSPERREFERENCEDANIMATION;
670 bxmfprintf( Outputfile,
"<AnimationFrameIndex AnimationMeshIndex=\"%d\"/>\n", ref-1-numLODs );
671 framerefs->push_back( ref );
674 bxmfprintf( Outputfile,
"</AnimationDefinition>\n" );
679 word32index = VSAbeginword+(LengthOfArbitraryLengthAttributes/4);
693 if (i == 0 && j == 0 && k == 0) {
697 float ms = i*i+j*j+k*k;
699 float m = 1.0f/
sqrt( ms );
711 word32index += NUMFIELDSPERVERTEX;
714 "<Point>\n\t<Location x=\"%f\" y=\"%f\" z=\"%f\" s=\"%f\" t=\"%f\"/>\n\t<Normal i=\"%f\" j=\"%f\" k=\"%f\"/>\n</Point>\n",
732 static GLenum laststate = GL_COMPILE;
733 mesh->
vlist = glGenLists( 1 );
734 glNewList( mesh->
vlist, GL_COMPILE );
740 for (
int32bit rvert = 0; rvert < numlines; rvert++) {
742 word32index += NUMFIELDSPERPOLYGONSTRUCTURE;
746 word32index += NUMFIELDSPERREFERENCEDVERTEX;
750 word32index += NUMFIELDSPERREFERENCEDVERTEX;
753 "\t<Line flatshade=\"%d\">\n\t\t<Vertex point=\"%d\" s=\"%f\" t=\"%f\"/>\n\t\t<Vertex point=\"%d\" s=\"%f\" t=\"%f\"/>\n\t</Line>\n",
771 for (
int32bit rtvert = 0; rtvert < numtris; rtvert++) {
773 word32index += NUMFIELDSPERPOLYGONSTRUCTURE;
777 word32index += NUMFIELDSPERREFERENCEDVERTEX;
781 word32index += NUMFIELDSPERREFERENCEDVERTEX;
785 word32index += NUMFIELDSPERREFERENCEDVERTEX;
788 "\t<Tri flatshade=\"%d\">\n\t\t<Vertex point=\"%d\" s=\"%f\" t=\"%f\"/>\n\t\t<Vertex point=\"%d\" s=\"%f\" t=\"%f\"/>\n\t\t<Vertex point=\"%d\" s=\"%f\" t=\"%f\"/>\n\t</Tri>\n",
812 for (
int32bit rqvert = 0; rqvert < numquads; rqvert++) {
814 word32index += NUMFIELDSPERPOLYGONSTRUCTURE;
818 word32index += NUMFIELDSPERREFERENCEDVERTEX;
822 word32index += NUMFIELDSPERREFERENCEDVERTEX;
826 word32index += NUMFIELDSPERREFERENCEDVERTEX;
830 word32index += NUMFIELDSPERREFERENCEDVERTEX;
833 "\t<Quad flatshade=\"%d\">\n\t\t<Vertex point=\"%d\" s=\"%f\" t=\"%f\"/>\n\t\t<Vertex point=\"%d\" s=\"%f\" t=\"%f\"/>\n\t\t<Vertex point=\"%d\" s=\"%f\" t=\"%f\"/>\n\t\t<Vertex point=\"%d\" s=\"%f\" t=\"%f\"/>\n\t</Quad>\n",
860 for (
int32bit lstrip = 0; lstrip < numlinestrips; lstrip++) {
863 bxmfprintf( Outputfile,
"\t<Linestrip flatshade=\"%d\">\n", flatshade );
865 BEGINSTATE( GL_LINE_STRIP, numstripelements );
866 word32index += 1+NUMFIELDSPERPOLYGONSTRUCTURE;
867 for (
int32bit elem = 0; elem < numstripelements; elem++) {
871 word32index += NUMFIELDSPERREFERENCEDVERTEX;
872 bxmfprintf( Outputfile,
"\t\t<Vertex point=\"%d\" s=\"%f\" t=\"%f\"/>\n", ind1, s1, t1 );
882 for (
int32bit tstrip = 0; tstrip < numtristrips; tstrip++) {
885 bxmfprintf( Outputfile,
"\t<Tristrip flatshade=\"%d\">\n", flatshade );
886 BEGINSTATE( GL_TRIANGLE_STRIP, numstripelements );
887 word32index += 1+NUMFIELDSPERPOLYGONSTRUCTURE;
888 for (
int32bit elem = 0; elem < numstripelements; elem++) {
892 word32index += NUMFIELDSPERREFERENCEDVERTEX;
893 bxmfprintf( Outputfile,
"\t\t<Vertex point=\"%d\" s=\"%f\" t=\"%f\"/>\n", ind1, s1, t1 );
903 for (
int32bit tfan = 0; tfan < numtrifans; tfan++) {
906 bxmfprintf( Outputfile,
"\t<Trifan flatshade=\"%d\">\n", flatshade );
907 BEGINSTATE( GL_TRIANGLE_FAN, numstripelements );
908 word32index += 1+NUMFIELDSPERPOLYGONSTRUCTURE;
909 for (
int32bit elem = 0; elem < numstripelements; elem++) {
913 word32index += NUMFIELDSPERREFERENCEDVERTEX;
914 bxmfprintf( Outputfile,
"\t\t<Vertex point=\"%d\" s=\"%f\" t=\"%f\"/>\n", ind1, s1, t1 );
924 for (
int32bit qstrip = 0; qstrip < numquadstrips; qstrip++) {
927 bxmfprintf( Outputfile,
"\t<Quadstrip flatshade=\"%d\">\n", flatshade );
928 BEGINSTATE( GL_QUAD_STRIP, numstripelements );
929 word32index += 1+NUMFIELDSPERPOLYGONSTRUCTURE;
930 for (
int32bit elem = 0; elem < numstripelements; elem++) {
934 word32index += NUMFIELDSPERREFERENCEDVERTEX;
935 bxmfprintf( Outputfile,
"\t\t<Vertex point=\"%d\" s=\"%f\" t=\"%f\"/>\n", ind1, s1, t1 );
948 vector< vector< GFXVertex > >::iterator iter;
955 for (iter = xml.
trifans.begin(); iter != xml.
trifans.end(); iter++)
972 word32index = meshbeginword+(meshlength/4);
975 word32index = recordbeginword+(recordlength/4);
976 output.push_back(
new Mesh() );
977 *output.back() = *meshes.back().m;
978 output.back()->orig = meshes.back().m;
979 for (
int i = 0;
i < (
int) meshes.back().sizes.size()-1; ++
i)
980 output.back()->orig[
i+1].lodsize = meshes.back().sizes[
i];
981 output.back()->numlods = output.back()->orig->numlods = meshes.back().num;