summaryrefslogtreecommitdiff
path: root/opengl/generate.cpp
blob: 53a67288bd7d69a3a27d84dd1b0e87f89f7e7c8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
// Generate.cpp: implementation of the Generate class.
//
//////////////////////////////////////////////////////////////////////

#include <sstream>
#include <algorithm>
#include <cctype>
#include <cstdarg>
#include <cfloat>

#include <w32dlib/w32dlib.h>
#include "generate.h"
#include "msLib.h"

#define INDENT	"    "
#define EOL	"\r\n"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Generate::Generate(msModel *model) : m_model(model),

				     m_cogx(0),
				     m_cogy(0),
				     m_cogz(0),

				     m_appcogx(0),
				     m_appcogy(0),
				     m_appcogz(0),

				     m_max_x(FLT_MIN),
				     m_min_x(FLT_MAX),
				     m_max_y(FLT_MIN),
				     m_min_y(FLT_MAX),
				     m_max_z(FLT_MIN),
				     m_min_z(FLT_MAX),

				     m_blitzMax(false),
				     m_normals(false),
				     m_indent(0)
{
    CalcModelBounds();
}

Generate::~Generate()
{
}

//////////////////////////////////////////////////////////////////////
// Public members
//////////////////////////////////////////////////////////////////////

std::string Generate::Info()
{
    std::ostringstream str;

    str << "Create files : " << EOL;

    if (m_blitzMax)
    {
	str << "\t" << m_dir << "\\" << m_funcName << ".bmx" << EOL << EOL;
    }
    else
    {
	str << "\t" << m_dir << "\\" << m_funcName << ".c" << EOL
	    << "\t" << m_dir << "\\" << m_funcName << ".h" << EOL << EOL;
    }

    str	<< "Centre of gravity : "
	<< (m_cogx-m_appcogx) << ","
	<< (m_cogy-m_appcogy) << ","
	<< (m_cogz-m_appcogz) << EOL
    	<< "Lowest X co-ord  : " << m_min_x-m_appcogx << EOL
    	<< "Highest X co-ord : " << m_max_x-m_appcogx << EOL
    	<< "Lowest Y co-ord  : " << m_min_y-m_appcogx << EOL
    	<< "Highest Y co-ord : " << m_max_y-m_appcogx << EOL
    	<< "Lowest Z co-ord  : " << m_min_z-m_appcogx << EOL
    	<< "Highest Z co-ord : " << m_max_z-m_appcogx << EOL;

    return str.str();
}

void Generate::MakeFiles(HWND parent)
{
    if (m_normals)
    {
	for(int m=0;m<m_model->nNumMeshes;m++)
	{
	    msMesh *mesh=m_model->pMeshes+m;

	    if (mesh->nMaterialIndex<0)
	    {
		W32DLib::Common::Error
		    (parent,0,
		     "Materials must be assigned if the object is to be lit");

		return;
	    }
	}
    }

    if (m_blitzMax)
    {
    	GenerateBlitzMax(parent);
    }
    else
    {
    	GenerateC(parent);
    }
}


void Generate::Setup(const std::string& dir,
		     const std::string& funcname,
		     bool texture,
		     bool info,
		     bool forceCOG,
		     bool blitzMax,
		     bool vertex_normals)
{
    m_dir=dir;
    m_funcName=funcname;
    m_useTexture=texture;
    m_useInfo=info;
    m_forceCOG=forceCOG;
    m_blitzMax=blitzMax;
    m_normals=vertex_normals;

    if (m_forceCOG)
    {
	m_appcogx=m_cogx;
	m_appcogy=m_cogy;
	m_appcogz=m_cogz;
    }
    else
    {
	m_appcogx=0;
	m_appcogy=0;
	m_appcogz=0;
    }
}


std::string Generate::GenerateFuncname()
{
    std::string fn;
    char *name;

    msMesh *mesh=msModel_GetMeshAt(m_model,0);

    name=mesh->szName;

    for(int f=0;name[f];f++)
    {
	if (std::isalnum(name[f]) || (f && name[f]=='_'))
	{
	    fn+=name[f];
	}
    }

    if (fn=="")
    {
	fn="object";
    }

    return fn;
}


//////////////////////////////////////////////////////////////////////
// Private members
//////////////////////////////////////////////////////////////////////

const char *Generate::Space(int len)
{
    static int cyc=0;
    static char buff[10][128];
    int f;

    cyc=(cyc+1)%10;

    for(f=0;f<len && f < 126;f++)
    {
	buff[cyc][f]=' ';
    }

    buff[cyc][f]=0;

    return &buff[cyc][0];
}


void Generate::Output(const char *fmt, ...)
{
    if (m_fp==0)
    {
    	return;
    }

    if (*fmt!='\n')
    {
	for(int f=0;f<m_indent;f++)
	{
	    std::fprintf(m_fp,"%s",INDENT);
	}
    }

    va_list va;

    va_start(va,fmt);
    std::vfprintf(m_fp,fmt,va);
    va_end(va);
}


void Generate::CalcModelBounds()
{
    for(int m=0;m<m_model->nNumMeshes;m++)
    {
	msMesh *mesh;

	mesh=m_model->pMeshes+m;

	for(int t=0;t<mesh->nNumTriangles;t++)
	{
	    msTriangle *tri;

	    tri=mesh->pTriangles+t;

	    if (!(tri->nFlags&eHidden))
	    {
		for(int i=0;i<3;i++)
		{
		    msVertex *v=mesh->pVertices+tri->nVertexIndices[i];

		    m_min_x=std::min(m_min_x,v->Vertex[0]);
		    m_max_x=std::max(m_max_x,v->Vertex[0]);

		    m_min_y=std::min(m_min_y,v->Vertex[1]);
		    m_max_y=std::max(m_max_y,v->Vertex[1]);

		    m_min_z=std::min(m_min_z,v->Vertex[2]);
		    m_max_z=std::max(m_max_z,v->Vertex[2]);
		}
	    }
	}
    }

    m_cogx=m_min_x+(m_max_x-m_min_x)/2;
    m_cogy=m_min_y+(m_max_y-m_min_y)/2;
    m_cogz=m_min_z+(m_max_z-m_min_z)/2;
}


void Generate::GenerateC(HWND parent)
{
    int len;
    std::string fname_c;
    std::string fname_h;
    std::string def;
    std::string missing;

    m_indent=0;

    len=m_funcName.length()+6;

    fname_c=m_dir+"\\"+m_funcName;
    fname_h=fname_c+".h";
    fname_c+=".c";

    for(std::string::const_iterator i=m_funcName.begin();
    					i!=m_funcName.end();++i)
    {
	def+=std::toupper(*i);
    }

    // Generate header
    //
    if (!(m_fp=fopen(fname_h.c_str(),"w")))
    {
	W32DLib::Common::Error(parent,0,"Couldn't create header file");
	return;
    }

    Output("%s","/* Code generated with Milkshape Export OpenGL plugin\n*/\n");
    Output("\n");

    Output("#ifndef %s_H\n#define %s_H\n\n",
				    def.c_str(),def.c_str());

    Output("%s","#ifdef _cplusplus\nextern \"C\" {\n#endif\n\n");


    Output("void %s(GLfloat x, GLfloat y, GLfloat z,\n",
					    m_funcName.c_str());
    Output("%sGLfloat rot_x, GLfloat rot_y, GLfloat rot_z",
					    Space(len));

    if (m_useTexture)
    {
	Output(",\n%sGLint texture_id);\n\n", Space(len));
    }
    else
    {
	Output(");\n\n");
    }

    Output("/* Returns true if a display list "
    		"could be created for the model */\n");
    Output("int %s_CreateList(void);\n\n",m_funcName.c_str());

    if (m_useInfo)
    {
	Output("#define %s_COG_X %f\n",def.c_str(),m_cogx-m_appcogx);
	Output("#define %s_COG_Y %f\n",def.c_str(),m_cogy-m_appcogy);
	Output("#define %s_COG_Z %f\n\n",def.c_str(),m_cogz-m_appcogz);

	Output("#define %s_MIN_X %f\n",def.c_str(),m_min_x-m_appcogx);
	Output("#define %s_MAX_X %f\n",def.c_str(),m_max_x-m_appcogx);
	Output("#define %s_MIN_Y %f\n",def.c_str(),m_min_y-m_appcogy);
	Output("#define %s_MAX_Y %f\n",def.c_str(),m_max_y-m_appcogy);
	Output("#define %s_MIN_Z %f\n",def.c_str(),m_min_z-m_appcogz);
	Output("#define %s_MAX_Z %f\n\n",def.c_str(),m_max_z-m_appcogz);

	float sx=m_max_x-m_min_x;
	float sy=m_max_y-m_min_y;
	float sz=m_max_z-m_min_z;

	Output("#define %s_SIZE_X %f\n",def.c_str(),sx);
	Output("#define %s_SIZE_Y %f\n",def.c_str(),sy);
	Output("#define %s_SIZE_Z %f\n\n",def.c_str(),sz);
    }

    Output("#define %s_TEXTURED %d\n\n",def.c_str(),m_useTexture?1:0);
    Output("#define %s_MATERIALS %d\n\n",def.c_str(),m_normals?1:0);

    Output("%s","#ifdef _cplusplus\n}\n#endif\n\n");

    Output("#endif /* %s_H */\n\n",def.c_str());

    Output("%s","/* END OF FILE */\n");

    std::fclose(m_fp);

    // Generate C source
    //
    if (!(m_fp=fopen(fname_c.c_str(),"w")))
    {
	W32DLib::Common::Error(parent,0,"Couldn't create source file");
	return;
    }

    Output("%s","/* Code generated with Milkshape Export OpenGL plugin\n*/\n");

    Output("%s","#include <GL/gl.h>\n\n");

    Output("static GLuint list_id=GL_INVALID_VALUE;\n\n");

    Output("static void Geometry(void)\n");
    Output("{\n");
    m_indent++;

    if (m_normals)
    {
	Output("GLfloat ambient[4];\n");
	Output("GLfloat diffuse[4];\n");
	Output("GLfloat specular[4];\n");
	Output("GLfloat emission[4];\n");
	Output("\n");
    }

    GenerateCommon(missing,";","/*","*/");

    m_indent--;

    Output("}\n\n");

    Output("int %s_CreateList(void)\n",m_funcName.c_str());
    Output("{\n");
    m_indent++;
    Output("list_id=glGenLists(1);\n");
    Output("if (list_id==GL_INVALID_VALUE) return 0;\n");
    Output("glNewList(list_id,GL_COMPILE);\n");
    Output("Geometry();\n");
    Output("glEndList();\n");
    Output("return 1;\n");
    m_indent--;
    Output("}\n\n");


    Output("void %s(GLfloat x, GLfloat y, GLfloat z,\n",
					    m_funcName.c_str());
    Output("%sGLfloat rot_x, GLfloat rot_y, GLfloat rot_z",
					    Space(len));

    if (m_useTexture)
    {
	Output(",\n%sGLint texture_id)\n", Space(len));
    }
    else
    {
	Output(")\n");
    }

    Output("{\n");

    m_indent++;

    Output("glPushMatrix();\n");
    Output("glTranslatef(x,y,z);\n");
    Output("glRotatef(rot_x,1,0,0);\n");
    Output("glRotatef(rot_y,0,1,0);\n");
    Output("glRotatef(rot_z,0,0,1);\n");

    if (m_useTexture)
    {
	Output("glBindTexture(GL_TEXTURE_2D,texture_id);\n");
    }

    Output("\n");

    Output("if (list_id==GL_INVALID_VALUE)\n");
    Output("{\n");
    m_indent++;
    Output("Geometry();\n");
    m_indent--;
    Output("}\n");
    Output("else\n");
    Output("{\n");
    m_indent++;
    Output("glCallList(list_id);\n");
    m_indent--;
    Output("}\n");

    Output("\n");
    Output("glPopMatrix();\n");

    m_indent--;

    Output("}\n");

    std::fclose(m_fp);

    if (missing.length()>0)
    {
	missing="The following meshes had no material:\n\n"+missing;
	missing+="\n\nThey were given the colour white.";

	W32DLib::Common::Message(parent,0,missing);
    }
}


void Generate::GenerateBlitzMax(HWND parent)
{
    std::string fname_bmx;
    std::string missing;

    fname_bmx=m_dir+"\\"+m_funcName+".bmx";

    m_indent=0;

    // Generate class
    //
    if (!(m_fp=fopen(fname_bmx.c_str(),"w")))
    {
	W32DLib::Common::Error(parent,0,"Couldn't create source file");
	return;
    }

    Output("%s","' Code generated with Milkshape Export OpenGL plugin\n'\n");
    Output("\n");

    Output("Type %s\n",m_funcName.c_str());
    Output("\n");

    m_indent++;

    if (m_useInfo)
    {
	Output("' Consts relating to the model's size and centre of gravity\n");
	Output("'\n");
	Output("Const COG_X:Float=%f\n",m_cogx-m_appcogx);
	Output("Const COG_Y:Float=%f\n",m_cogy-m_appcogy);
	Output("Const COG_Z:Float=%f\n\n",m_cogz-m_appcogz);

	Output("Const MIN_X:Float=%f\n",m_min_x-m_appcogx);
	Output("Const MAX_X:Float=%f\n",m_max_x-m_appcogx);
	Output("Const MIN_Y:Float=%f\n",m_min_y-m_appcogy);
	Output("Const MAX_Y:Float=%f\n",m_max_y-m_appcogy);
	Output("Const MIN_Z:Float=%f\n",m_min_z-m_appcogz);
	Output("Const MAX_Z:Float=%f\n\n",m_max_z-m_appcogz);

	float sx=m_max_x-m_min_x;
	float sy=m_max_y-m_min_y;
	float sz=m_max_z-m_min_z;

	Output("Const SIZE_X:Float=%f\n",sx);
	Output("Const SIZE_Y:Float=%f\n",sy);
	Output("Const SIZE_Z:Float=%f\n\n",sz);

	Output("const TEXTURED:Int=%s\n",m_useTexture ? "true":"false");
	Output("const MATERIALS:Int=%s\n",m_normals ? "true":"false");

	Output("\n");
    }

    Output("' These fields control the position/orientation of the object\n");
    Output("'\n");
    Output("Field x:Float\n");
    Output("Field y:Float\n");
    Output("Field z:Float\n");
    Output("Field rot_x:Float\n");
    Output("Field rot_y:Float\n");
    Output("Field rot_z:Float\n");
    Output("\n");

    Output("' This field should be considered private\n");
    Output("'\n");
    Output("Global list_id:Int=GL_INVALID_VALUE\n");
    Output("\n");

    Output("' Use this function to create an instance\n");
    Output("'\n");
    Output("Function Create:%s()\n",m_funcName.c_str());
    m_indent++;
    Output("Local o:%s = New %s\n",m_funcName.c_str(),m_funcName.c_str());
    Output("o.x=0\n");
    Output("o.y=0\n");
    Output("o.z=0\n");
    Output("o.rot_x=0\n");
    Output("o.rot_y=0\n");
    Output("o.rot_z=0\n");
    Output("return o\n");
    m_indent--;
    Output("End Function\n");
    Output("\n");

    Output("' Use this function to setup a display list.\n");
    Output("' Returns True it the display list is created.\n");
    Output("'\n");
    Output("Function CreateDisplayList:Int()\n");
    m_indent++;
    Output("list_id=glGenLists(1)\n");
    Output("if (list_id=GL_INVALID_VALUE) then return False\n");
    Output("glNewList(list_id,GL_COMPILE)\n");
    Output("Geometry()\n");
    Output("glEndList()\n");
    Output("return True\n");
    m_indent--;
    Output("End Function\n");
    Output("\n");

    Output("' Use this member to draw the object\n");
    Output("'\n");

    if (m_useTexture)
    {
    	Output("Method Render(texture_id:Int)\n");
    }
    else
    {
    	Output("Method Render()\n");
    }

    m_indent++;

    Output("glPushMatrix()\n");
    Output("glTranslatef(x,y,z)\n");
    Output("glRotatef(rot_x,1,0,0)\n");
    Output("glRotatef(rot_y,0,1,0)\n");
    Output("glRotatef(rot_z,0,0,1)\n");

    if (m_useTexture)
    {
	Output("glBindTexture(GL_TEXTURE_2D,texture_id)\n");
    }

    Output("\n");

    Output("If (list_id=GL_INVALID_VALUE)\n");
    m_indent++;
    Output("Geometry()\n");
    m_indent--;
    Output("Else\n");
    m_indent++;
    Output("glCallList(list_id)\n");
    m_indent--;
    Output("End If\n");
    Output("\n");
    Output("glPopMatrix()\n");

    m_indent--;

    Output("End Method\n");

    Output("\n");

    Output("' This function should be considered private.\n");
    Output("'\n");
    Output("Function Geometry()\n");

    m_indent++;

    if (m_normals)
    {
	Output("Local ambient:Float[4]\n");
	Output("Local diffuse:Float[4]\n");
	Output("Local specular:Float[4]\n");
	Output("Local emission:Float[4]\n");
	Output("\n");
    }

    GenerateCommon(missing,"","'","");

    m_indent--;

    Output("End Function\n");

    m_indent--;

    Output("End Type\n");

    std::fclose(m_fp);

    if (missing.length()>0)
    {
	missing="The following meshes had no material:\n\n"+missing;
	missing+="\n\nThey were given the colour white.";

	W32DLib::Common::Message(parent,0,missing);
    }
}


void Generate::GenerateCommon(std::string& missing,
			      const char *term,
			      const char *start_comment,
			      const char *end_comment)
{
    int last_mat=-1;

    Output("glBegin(GL_TRIANGLES)%s\n",term);
    Output("\n");

    for(int m=0;m<m_model->nNumMeshes;m++)
    {
	msMesh *mesh=m_model->pMeshes+m;

	Output("%s Mesh %d - %s %s\n",
			start_comment,m+1,mesh->szName,end_comment);
	Output("\n");

	if (mesh->nMaterialIndex<0)
	{
	    missing+=mesh->szName;
	    missing+="\n";

	    Output("glColor3f(1.0f,1.0f,1.0f)%s\n",term);
	    last_mat=-1;
	}
	else
	{
	    if (mesh->nMaterialIndex!=last_mat)
	    {
		msMaterial *mat=m_model->pMaterials+mesh->nMaterialIndex;

		GenerateMaterial(mat,missing,term);

	    	last_mat=mesh->nMaterialIndex;
	    }
	}

	for(int t=0;t<mesh->nNumTriangles;t++)
	{
	    msTriangle *tri;

	    tri=mesh->pTriangles+t;

	    if (!(tri->nFlags&eHidden))
	    {
		// To allow vertex order to be tweaked...
		//
		static int order[3]={0,1,2};

		for(int i=0;i<3;i++)
		{
		    msVertex *v=mesh->pVertices+tri->nVertexIndices[order[i]];

		    if (m_useTexture)
		    {
			Output("glTexCoord2f(%f,%f)%s\n",v->u,v->v,term);
		    }

		    if (m_normals)
		    {
			msVec3 n;
			
			msMesh_GetVertexNormalAt(mesh,
						 tri->nNormalIndices[order[i]],
						 n);

			Output("glNormal3f(%f,%f,%f)%s\n",
						n[0],
						n[1],
						n[2],
						term);
		    }

		    Output("glVertex3f(%f,%f,%f)%s\n",
					    v->Vertex[0]-m_appcogx,
					    v->Vertex[1]-m_appcogy,
					    v->Vertex[2]-m_appcogz,
					    term);
		}
	    }
	}

	Output("\n");
    }

    Output("glEnd()%s\n",term);
}


void Generate::GenerateMaterial(msMaterial *mat,
				std::string& missing,
				const char *term)
{
    if (m_normals)
    {
    	int f;

	for(f=0;f<4;f++)
	{
	    Output("ambient[%d]=%f%s\n",f,mat->Ambient[f],term);
	    Output("diffuse[%d]=%f%s\n",f,mat->Diffuse[f],term);
	    Output("specular[%d]=%f%s\n",f,mat->Specular[f],term);
	    Output("emission[%d]=%f%s\n",f,mat->Emissive[f],term);
	}

	Output("glMaterialf(GL_FRONT,GL_SHININESS,%f)%s\n",
		    mat->fShininess,term);

	Output("glMaterialfv(GL_FRONT,GL_AMBIENT,ambient)%s\n",term);
	Output("glMaterialfv(GL_FRONT,GL_DIFFUSE,diffuse)%s\n",term);
	Output("glMaterialfv(GL_FRONT,GL_SPECULAR,specular)%s\n",term);
	Output("glMaterialfv(GL_FRONT,GL_EMISSION,emission)%s\n",term);
    }
    else
    {
	Output("glColor3f(%f,%f,%f)%s\n",
				mat->Diffuse[0],
				mat->Diffuse[1],
				mat->Diffuse[2],
				term);
    }
}

// END OF FILE