-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTankGeometryMgr.cxx
492 lines (406 loc) · 14.2 KB
/
TankGeometryMgr.cxx
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
/* bzflag
* Copyright (c) 1993-2021 Tim Riker
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the license found in the file
* named COPYING that should have accompanied this file.
*
* THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
// bzflag common headers
#include "common.h"
#include "global.h"
// interface header
#include "TankGeometryMgr.h"
// system headers
#include <stdlib.h>
#include <math.h>
#include <string>
// common implementation headers
//#include "SceneRenderer.h"
//#include "StateDatabase.h"
//#include "BZDBCache.h"
//#include "OpenGLGState.h"
// use the namespaces
using namespace TankGeometryMgr;
using namespace TankGeometryEnums;
using namespace TankGeometryUtils;
// Local Variables
// ---------------
// the display lists
static GLuint displayLists[LastTankShadow][LastTankLOD]
[LastTankSize][LastTankPart];
// triangle counts
static int partTriangles[LastTankShadow][LastTankLOD]
[LastTankSize][LastTankPart];
// the scaling factors
static GLfloat scaleFactors[LastTankSize][3] =
{
{1.0f, 1.0f, 1.0f}, // Normal
{1.0f, 1.0f, 1.0f}, // Obese
{1.0f, 1.0f, 1.0f}, // Tiny
{1.0f, 0.001f, 1.0f}, // Narrow
{1.0f, 1.0f, 1.0f} // Thief
};
// the current scaling factors
static const float* currentScaleFactor = scaleFactors[Normal];
// the current shadow mode (used to remove glNormal3f and glTexcoord2f calls)
static TankShadow shadowMode = ShadowOn;
// arrays of functions to avoid large switch statements
typedef int (*partFunction)(void);
static const partFunction partFunctions[LastTankLOD][BasicTankParts] =
{
{
nullptr,
nullptr,
nullptr,
nullptr,
nullptr
},
{
nullptr,
nullptr,
nullptr,
nullptr,
nullptr
},
{
buildHighBody,
buildHighBarrel,
buildHighTurret,
buildHighLCasing,
buildHighRCasing
}
};
// Local Function Prototypes
// -------------------------
static void setupScales();
static void freeContext(void *data);
static void initContext(void *data);
static void bzdbCallback(const std::string& str, void *data);
/****************************************************************************/
// TankGeometryMgr Functions
// -------------------------
void TankGeometryMgr::init()
{
// initialize the lists to invalid
for (int shadow = 0; shadow < LastTankShadow; shadow++)
{
for (int lod = 0; lod < LastTankLOD; lod++)
{
for (int size = 0; size < LastTankSize; size++)
{
for (int part = 0; part < LastTankPart; part++)
{
displayLists[shadow][lod][size][part] = INVALID_GL_LIST_ID;
partTriangles[shadow][lod][size][part] = 0;
}
}
}
}
// install the BZDB callbacks
// This MUST be done after BZDB has been initialized in main()
BZDB.addCallback (StateDatabase::BZDB_OBESEFACTOR, bzdbCallback, NULL);
BZDB.addCallback (StateDatabase::BZDB_TINYFACTOR, bzdbCallback, NULL);
BZDB.addCallback (StateDatabase::BZDB_THIEFTINYFACTOR, bzdbCallback, NULL);
BZDB.addCallback ("animatedTreads", bzdbCallback, NULL);
// install the context initializer
OpenGLGState::registerContextInitializer (freeContext, initContext, NULL);
// setup the scaleFactors
setupScales();
return;
}
void TankGeometryMgr::kill()
{
// remove the BZDB callbacks
BZDB.removeCallback (StateDatabase::BZDB_OBESEFACTOR, bzdbCallback, NULL);
BZDB.removeCallback (StateDatabase::BZDB_TINYFACTOR, bzdbCallback, NULL);
BZDB.removeCallback (StateDatabase::BZDB_THIEFTINYFACTOR, bzdbCallback, NULL);
BZDB.removeCallback ("animatedTreads", bzdbCallback, NULL);
// remove the context initializer callback
OpenGLGState::unregisterContextInitializer(freeContext, initContext, NULL);
return;
}
void TankGeometryMgr::deleteLists()
{
// delete the lists that have been aquired
for (int shadow = 0; shadow < LastTankShadow; shadow++)
{
for (int lod = 0; lod < LastTankLOD; lod++)
{
for (int size = 0; size < LastTankSize; size++)
{
for (int part = 0; part < LastTankPart; part++)
{
GLuint& list = displayLists[shadow][lod][size][part];
if (list != INVALID_GL_LIST_ID)
{
glDeleteLists(list, 1);
list = INVALID_GL_LIST_ID;
}
}
}
}
}
return;
}
void TankGeometryMgr::buildLists()
{
// setup the tread style
setTreadStyle(BZDB.evalInt("treadStyle"));
// setup the scale factors
setupScales();
currentScaleFactor = scaleFactors[Normal];
const bool animated = BZDBCache::animatedTreads;
// setup the quality level
const int divisionLevels[4][2] = // wheel divs, tread divs
{
{4, 4}, // low
{8, 16}, // med
{12, 24}, // high
{16, 32} // experimental
};
int quality = RENDERER.useQuality();
if (quality < 0)
quality = 0;
else if (quality > 3)
quality = 3;
int wheelDivs = divisionLevels[quality][0];
int treadDivs = divisionLevels[quality][1];
int shadow = ShadowOff; // GLWRITER - HARDCODED -- for (int shadow = 0; shadow < LastTankShadow; shadow++)
{
int lod = HighTankLOD; // GLWRITER - HARDCODED -- for (int lod = 0; lod < LastTankLOD; lod++)
{
for (int size = 0; size < LastTankSize; size++)
{
// GLWRITER - START
std::string filename = "tank";
switch(size)
{
case Obese:
filename += "_obese";
break;
case Tiny:
filename += "_tiny";
break;
case Narrow:
filename += "_narrow";
break;
case Thief:
filename += "_thief";
break;
default:
break;
}
// Instruct OpenGL Writer to open a set of output files
glWriterFileOpen(filename);
// GLWRITER - STOP
// only do the basics, unless we're making an animated tank
int lastPart = BasicTankParts;
if (animated)
lastPart = HighTankParts;
// set the shadow mode for the doNormal3f() and doTexcoord2f() calls
shadowMode = (TankShadow) shadow;
for (int part = 0; part < lastPart; part++)
{
// GLWRITER - START
// Set the texture for each part
switch(part)
{
case Body:
glWriterSetCurrentMaterial("body");
break;
case Barrel:
glWriterSetCurrentMaterial("barrel");
break;
case Turret:
glWriterSetCurrentMaterial("turret");
break;
case LeftCasing:
case RightCasing:
glWriterSetCurrentMaterial("casing");
break;
case LeftTread:
case RightTread:
glWriterSetCurrentMaterial("tread");
break;
case LeftWheel0:
case LeftWheel1:
case LeftWheel2:
case LeftWheel3:
case RightWheel0:
case RightWheel1:
case RightWheel2:
case RightWheel3:
glWriterSetCurrentMaterial("wheel");
break;
default:
exit(-3);
}
// GLWRITER - STOP
if ((part == Barrel) && (lod == MedTankLOD))
continue;
GLuint& list = displayLists[shadow][lod][size][part];
int& count = partTriangles[shadow][lod][size][part];
// get a new OpenGL display list
list = glGenLists(1);
glNewList(list, GL_COMPILE);
// setup the scale factor
currentScaleFactor = scaleFactors[size];
if ((part <= Turret) || (!animated))
{
// the basic parts
count = partFunctions[lod][part]();
}
else
{
// the animated parts
if (part == LeftCasing)
count = buildHighLCasingAnim();
else if (part == RightCasing)
count = buildHighRCasingAnim();
else if (part == LeftTread)
count = buildHighLTread(treadDivs);
else if (part == RightTread)
count = buildHighRTread(treadDivs);
else if ((part >= LeftWheel0) && (part <= LeftWheel3))
{
int wheel = part - LeftWheel0;
count = buildHighLWheel(wheel, (float)wheel * (float)(M_PI / 2.0),
wheelDivs);
}
else if ((part >= RightWheel0) && (part <= RightWheel3))
{
int wheel = part - RightWheel0;
count = buildHighRWheel(wheel, (float)wheel * (float)(M_PI / 2.0),
wheelDivs);
}
}
// end of the list
glEndList();
} // part
// GLWRITER - START
glWriterFileClose();
// GLWRITER - STOP
} // size
} // lod
} // shadow
return;
}
GLuint TankGeometryMgr::getPartList(TankGeometryEnums::TankShadow shadow,
TankGeometryEnums::TankPart part,
TankGeometryEnums::TankSize size,
TankGeometryEnums::TankLOD lod)
{
if ((part == Barrel) && (lod == MedTankLOD))
lod = LowTankLOD;
return displayLists[shadow][lod][size][part];
}
int TankGeometryMgr::getPartTriangleCount(TankGeometryEnums::TankShadow sh,
TankGeometryEnums::TankPart part,
TankGeometryEnums::TankSize size,
TankGeometryEnums::TankLOD lod)
{
if ((part == Barrel) && (lod == MedTankLOD))
lod = LowTankLOD;
return partTriangles[sh][lod][size][part];
}
const float* TankGeometryMgr::getScaleFactor(TankSize size)
{
return scaleFactors[size];
}
/****************************************************************************/
// Local Functions
// ---------------
static void bzdbCallback(const std::string& UNUSED(name), void * UNUSED(data))
{
deleteLists();
buildLists();
return;
}
static void freeContext(void * UNUSED(data))
{
// delete all of the lists
deleteLists();
return;
}
static void initContext(void * UNUSED(data))
{
buildLists();
return;
}
static void setupScales()
{
float scale;
scaleFactors[Normal][0] = BZDBCache::tankLength;
scale = (float)atof(BZDB.getDefault(StateDatabase::BZDB_TANKLENGTH).c_str());
scaleFactors[Normal][0] /= scale;
scaleFactors[Normal][1] = BZDBCache::tankWidth;
scale = (float)atof(BZDB.getDefault(StateDatabase::BZDB_TANKWIDTH).c_str());
scaleFactors[Normal][1] /= scale;
scaleFactors[Normal][2] = BZDBCache::tankHeight;
scale = (float)atof(BZDB.getDefault(StateDatabase::BZDB_TANKHEIGHT).c_str());
scaleFactors[Normal][2] /= scale;
scale = BZDB.eval(StateDatabase::BZDB_OBESEFACTOR);
scaleFactors[Obese][0] = scale * scaleFactors[Normal][0];
scaleFactors[Obese][1] = scale * scaleFactors[Normal][1];
scaleFactors[Obese][2] = scaleFactors[Normal][2];
scale = BZDB.eval(StateDatabase::BZDB_TINYFACTOR);
scaleFactors[Tiny][0] = scale * scaleFactors[Normal][0];
scaleFactors[Tiny][1] = scale * scaleFactors[Normal][1];
scaleFactors[Tiny][2] = scaleFactors[Normal][2];
scale = BZDB.eval(StateDatabase::BZDB_THIEFTINYFACTOR);
scaleFactors[Thief][0] = scale * scaleFactors[Normal][0];
scaleFactors[Thief][1] = scale * scaleFactors[Normal][1];
scaleFactors[Thief][2] = scaleFactors[Normal][2];
scaleFactors[Narrow][0] = scaleFactors[Normal][0];
scaleFactors[Narrow][1] = 0.001f;
scaleFactors[Narrow][2] = scaleFactors[Normal][2];
return;
}
/****************************************************************************/
// TankGeometryUtils Functions
// ---------------------------
void TankGeometryUtils::doVertex3f(GLfloat x, GLfloat y, GLfloat z)
{
const float* scale = currentScaleFactor;
x = x * scale[0];
y = y * scale[1];
z = z * scale[2];
glVertex3f(x, y, z);
return;
}
void TankGeometryUtils::doNormal3f(GLfloat x, GLfloat y, GLfloat z)
{
if (shadowMode == ShadowOn)
return;
const float* scale = currentScaleFactor;
GLfloat sx = x * scale[0];
GLfloat sy = y * scale[1];
GLfloat sz = z * scale[2];
const GLfloat d = sqrtf ((sx * sx) + (sy * sy) + (sz * sz));
if (d > 1.0e-5f)
{
x *= scale[0] / d;
y *= scale[1] / d;
z *= scale[2] / d;
}
glNormal3f(x, y, z);
return;
}
void TankGeometryUtils::doTexCoord2f(GLfloat x, GLfloat y)
{
if (shadowMode == ShadowOn)
return;
glTexCoord2f(x, y);
return;
}
// Local Variables: ***
// mode: C++ ***
// tab-width: 4 ***
// c-basic-offset: 4 ***
// indent-tabs-mode: nil ***
// End: ***
// ex: shiftwidth=4 tabstop=4