forked from Warzone2100/warzone2100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket3d.cpp
469 lines (412 loc) · 12.9 KB
/
bucket3d.cpp
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
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2012 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file bucket3d.c
*
* Stores object render calls in a linked list renders after bucket sorting objects.
*/
#include "lib/framework/frame.h"
#include "lib/ivis_opengl/piematrix.h"
#include "atmos.h"
#include "bucket3d.h"
#include "component.h"
#include "display3d.h"
#include "effects.h"
#include "map.h"
#include "miscimd.h"
#include <algorithm>
#define CLIP_LEFT ((SDWORD)0)
#define CLIP_RIGHT ((SDWORD)pie_GetVideoBufferWidth())
#define CLIP_TOP ((SDWORD)0)
#define CLIP_BOTTOM ((SDWORD)pie_GetVideoBufferHeight())
//scale depth = 1<<FP12_SHIFT>>STRETCHED_Z_SHIFT<<xpshift
// Gerard - HACK Multiplied by 7 to fix clipping
// someone needs to take a good look at the radius calculation
#define SCALE_DEPTH (FP12_MULTIPLIER*7)
struct BUCKET_TAG
{
bool operator <(BUCKET_TAG const &b) const { return actualZ > b.actualZ; } // Sort in reverse z order.
RENDER_TYPE objectType; //type of object held
void * pObject; //pointer to the object
int32_t actualZ;
};
static std::vector<BUCKET_TAG> bucketArray;
static SDWORD bucketCalculateZ(RENDER_TYPE objectType, void* pObject)
{
SDWORD z = 0, radius;
Vector2i pixel;
Vector3i position;
UDWORD droidSize;
DROID *psDroid;
BODY_STATS *psBStats;
SIMPLE_OBJECT *psSimpObj;
COMPONENT_OBJECT *psCompObj;
const iIMDShape *pImd;
Spacetime spacetime;
switch(objectType)
{
case RENDER_PARTICLE:
position.x = ((ATPART*)pObject)->position.x;
position.y = ((ATPART*)pObject)->position.y;
position.z = ((ATPART*)pObject)->position.z;
position.x = position.x - player.p.x;
position.z = -(position.z - player.p.z);
position.y = position.y;
/* 16 below is HACK!!! */
z = pie_RotateProject(&position,&pixel) - 16;
if (z > 0)
{
//particle use the image radius
radius = ((ATPART*)pObject)->imd->radius;
radius *= SCALE_DEPTH;
radius /= z;
if ((pixel.x + radius < CLIP_LEFT) || (pixel.x - radius > CLIP_RIGHT)
|| (pixel.y + radius < CLIP_TOP) || (pixel.y - radius > CLIP_BOTTOM))
{
z = -1;
}
}
break;
case RENDER_PROJECTILE:
if(((PROJECTILE*)pObject)->psWStats->weaponSubClass == WSC_FLAME ||
((PROJECTILE*)pObject)->psWStats->weaponSubClass == WSC_COMMAND ||
((PROJECTILE*)pObject)->psWStats->weaponSubClass == WSC_EMP)
{
/* We don't do projectiles from these guys, cos there's an effect instead */
z = -1;
}
else
{
//the weapon stats holds the reference to which graphic to use
pImd = ((PROJECTILE*)pObject)->psWStats->pInFlightGraphic;
psSimpObj = (SIMPLE_OBJECT*) pObject;
position.x = psSimpObj->pos.x - player.p.x;
position.z = -(psSimpObj->pos.y - player.p.z);
position.y = psSimpObj->pos.z;
z = pie_RotateProject(&position,&pixel);
if (z > 0)
{
//particle use the image radius
radius = pImd->radius;
radius *= SCALE_DEPTH;
radius /= z;
if ((pixel.x + radius < CLIP_LEFT) || (pixel.x - radius > CLIP_RIGHT)
|| (pixel.y + radius < CLIP_TOP) || (pixel.y - radius > CLIP_BOTTOM))
{
z = -1;
}
}
}
break;
case RENDER_STRUCTURE://not depth sorted
psSimpObj = (SIMPLE_OBJECT*) pObject;
position.x = psSimpObj->pos.x - player.p.x;
position.z = -(psSimpObj->pos.y - player.p.z);
//if((objectType == RENDER_STRUCTURE) && (((STRUCTURE*)pObject)->
// pStructureType->type >= REF_DEFENSE) &&
// (((STRUCTURE*)pObject)->pStructureType->type<=REF_TOWER4))
if((objectType == RENDER_STRUCTURE) &&
((((STRUCTURE*)pObject)->pStructureType->type == REF_DEFENSE) ||
(((STRUCTURE*)pObject)->pStructureType->type == REF_WALL) ||
(((STRUCTURE*)pObject)->pStructureType->type == REF_WALLCORNER)))
{
position.y = psSimpObj->pos.z + 64;
radius = ((STRUCTURE*)pObject)->sDisplay.imd->radius;//walls guntowers and tank traps clip tightly
}
else
{
position.y = psSimpObj->pos.z;
radius = (((STRUCTURE*)pObject)->sDisplay.imd->radius);
}
z = pie_RotateProject(&position,&pixel);
if (z > 0)
{
//particle use the image radius
radius *= SCALE_DEPTH;
radius /= z;
if ((pixel.x + radius < CLIP_LEFT) || (pixel.x - radius > CLIP_RIGHT)
|| (pixel.y + radius < CLIP_TOP) || (pixel.y - radius > CLIP_BOTTOM))
{
z = -1;
}
}
break;
case RENDER_FEATURE://not depth sorted
psSimpObj = (SIMPLE_OBJECT*) pObject;
position.x = psSimpObj->pos.x - player.p.x;
position.z = -(psSimpObj->pos.y - player.p.z);
position.y = psSimpObj->pos.z+2;
z = pie_RotateProject(&position,&pixel);
if (z > 0)
{
//particle use the image radius
radius = ((FEATURE*)pObject)->sDisplay.imd->radius;
radius *= SCALE_DEPTH;
radius /= z;
if ((pixel.x + radius < CLIP_LEFT) || (pixel.x - radius > CLIP_RIGHT)
|| (pixel.y + radius < CLIP_TOP) || (pixel.y - radius > CLIP_BOTTOM))
{
z = -1;
}
}
break;
case RENDER_ANIMATION://not depth sorted
psCompObj = (COMPONENT_OBJECT *) pObject;
spacetime = interpolateObjectSpacetime((SIMPLE_OBJECT *)psCompObj->psParent, graphicsTime);
position.x = spacetime.pos.x - player.p.x;
position.z = -(spacetime.pos.y - player.p.z);
position.y = spacetime.pos.z;
/* object offset translation */
position.x += psCompObj->psShape->ocen.x;
position.y += psCompObj->psShape->ocen.z;
position.z -= psCompObj->psShape->ocen.y;
pie_MatBegin();
/* object (animation) translations - ivis z and y flipped */
pie_TRANSLATE( psCompObj->position.x, psCompObj->position.z,
psCompObj->position.y );
/* object (animation) rotations */
pie_MatRotY(-psCompObj->orientation.z);
pie_MatRotZ(-psCompObj->orientation.y);
pie_MatRotX(-psCompObj->orientation.x);
z = pie_RotateProject(&position,&pixel);
pie_MatEnd();
break;
case RENDER_DROID:
case RENDER_SHADOW:
psDroid = (DROID*) pObject;
psSimpObj = (SIMPLE_OBJECT*) pObject;
position.x = psSimpObj->pos.x - player.p.x;
position.z = -(psSimpObj->pos.y - player.p.z);
position.y = psSimpObj->pos.z;
if(objectType == RENDER_SHADOW)
{
position.y+=4;
}
psBStats = asBodyStats + psDroid->asBits[COMP_BODY].nStat;
droidSize = psBStats->pIMD->radius;
z = pie_RotateProject(&position,&pixel) - (droidSize*2);
if (z > 0)
{
//particle use the image radius
radius = droidSize;
radius *= SCALE_DEPTH;
radius /= z;
if ((pixel.x + radius < CLIP_LEFT) || (pixel.x - radius > CLIP_RIGHT)
|| (pixel.y + radius < CLIP_TOP) || (pixel.y - radius > CLIP_BOTTOM))
{
z = -1;
}
}
break;
case RENDER_PROXMSG:
if (((PROXIMITY_DISPLAY *)pObject)->type == POS_PROXDATA)
{
position.x = ((VIEW_PROXIMITY *)((VIEWDATA *)((PROXIMITY_DISPLAY *)
pObject)->psMessage->pViewData)->pData)->x - player.p.x;
position.z = -(((VIEW_PROXIMITY *)((VIEWDATA *)
((PROXIMITY_DISPLAY *)pObject)->psMessage->pViewData)->pData)->y -
player.p.z);
position.y = ((VIEW_PROXIMITY *)((VIEWDATA *)((PROXIMITY_DISPLAY *)pObject)->
psMessage->pViewData)->pData)->z;
}
else if (((PROXIMITY_DISPLAY *)pObject)->type == POS_PROXOBJ)
{
position.x = ((BASE_OBJECT *)((PROXIMITY_DISPLAY *)pObject)->
psMessage->pViewData)->pos.x - player.p.x;
position.z = -(((BASE_OBJECT *)((
PROXIMITY_DISPLAY *)pObject)->psMessage->pViewData)->pos.y -
player.p.z);
position.y = ((BASE_OBJECT *)((PROXIMITY_DISPLAY *)pObject)->
psMessage->pViewData)->pos.z;
}
z = pie_RotateProject(&position,&pixel);
if (z > 0)
{
//particle use the image radius
pImd = getImdFromIndex(MI_BLIP_ENEMY);//use MI_BLIP_ENEMY as all are same radius
radius = pImd->radius;
radius *= SCALE_DEPTH;
radius /= z;
if ((pixel.x + radius < CLIP_LEFT) || (pixel.x - radius > CLIP_RIGHT)
|| (pixel.y + radius < CLIP_TOP) || (pixel.y - radius > CLIP_BOTTOM))
{
z = -1;
}
}
break;
case RENDER_EFFECT:
position.x = ((EFFECT*)pObject)->position.x - player.p.x;
position.z = -(((EFFECT*)pObject)->position.z - player.p.z);
position.y = ((EFFECT*)pObject)->position.y;
/* 16 below is HACK!!! */
z = pie_RotateProject(&position,&pixel) - 16;
if (z > 0)
{
//particle use the image radius
pImd = ((EFFECT*)pObject)->imd;
if (pImd != NULL)
{
radius = pImd->radius;
radius *= SCALE_DEPTH;
radius /= z;
if ((pixel.x + radius < CLIP_LEFT) || (pixel.x - radius > CLIP_RIGHT)
|| (pixel.y + radius < CLIP_TOP) || (pixel.y - radius > CLIP_BOTTOM))
{
z = -1;
}
}
}
break;
case RENDER_DELIVPOINT:
position.x = ((FLAG_POSITION *)pObject)->coords.x - player.p.x;
position.z = -(((FLAG_POSITION*)pObject)->
coords.y - player.p.z);
position.y = ((FLAG_POSITION*)pObject)->coords.z;
z = pie_RotateProject(&position,&pixel);
if (z > 0)
{
//particle use the image radius
radius = pAssemblyPointIMDs[((FLAG_POSITION*)pObject)->factoryType][((FLAG_POSITION*)pObject)->factoryInc]->radius;
radius *= SCALE_DEPTH;
radius /= z;
if ((pixel.x + radius < CLIP_LEFT) || (pixel.x - radius > CLIP_RIGHT)
|| (pixel.y + radius < CLIP_TOP) || (pixel.y - radius > CLIP_BOTTOM))
{
z = -1;
}
}
break;
default:
break;
}
return z;
}
/* add an object to the current render list */
void bucketAddTypeToList(RENDER_TYPE objectType, void* pObject)
{
const iIMDShape* pie;
BUCKET_TAG newTag;
int32_t z = bucketCalculateZ(objectType, pObject);
if (z < 0)
{
/* Object will not be render - has been clipped! */
if(objectType == RENDER_DROID || objectType == RENDER_STRUCTURE)
{
/* Won't draw selection boxes */
((BASE_OBJECT *)pObject)->sDisplay.frameNumber = 0;
}
return;
}
switch(objectType)
{
case RENDER_EFFECT:
switch(((EFFECT*)pObject)->group)
{
case EFFECT_EXPLOSION:
case EFFECT_CONSTRUCTION:
case EFFECT_SMOKE:
case EFFECT_FIREWORK:
// Use calculated Z
break;
case EFFECT_WAYPOINT:
pie = ((EFFECT*)pObject)->imd;
z = INT32_MAX - pie->texpage;
break;
default:
z = INT32_MAX - 42;
break;
}
break;
case RENDER_DROID:
pie = BODY_IMD(((DROID*)pObject),0);
z = INT32_MAX - pie->texpage;
break;
case RENDER_STRUCTURE:
pie = ((STRUCTURE*)pObject)->sDisplay.imd;
z = INT32_MAX - pie->texpage;
break;
case RENDER_FEATURE:
pie = ((FEATURE*)pObject)->sDisplay.imd;
z = INT32_MAX - pie->texpage;
break;
case RENDER_ANIMATION:
pie = ((COMPONENT_OBJECT*)pObject)->psShape;
z = INT32_MAX - pie->texpage;
break;
case RENDER_DELIVPOINT:
pie = pAssemblyPointIMDs[((FLAG_POSITION*)pObject)->
factoryType][((FLAG_POSITION*)pObject)->factoryInc];
z = INT32_MAX - pie->texpage;
break;
case RENDER_PARTICLE:
z = 0;
break;
default:
// Use calculated Z
break;
}
//put the object data into the tag
newTag.objectType = objectType;
newTag.pObject = pObject;
newTag.actualZ = z;
//add tag to bucketArray
bucketArray.push_back(newTag);
}
/* render Objects in list */
void bucketRenderCurrentList(void)
{
std::sort(bucketArray.begin(), bucketArray.end());
for (std::vector<BUCKET_TAG>::const_iterator thisTag = bucketArray.begin(); thisTag != bucketArray.end(); ++thisTag)
{
switch(thisTag->objectType)
{
case RENDER_PARTICLE:
renderParticle((ATPART*)thisTag->pObject);
break;
case RENDER_EFFECT:
renderEffect((EFFECT*)thisTag->pObject);
break;
case RENDER_DROID:
renderDroid((DROID*)thisTag->pObject);
break;
case RENDER_SHADOW:
renderShadow((DROID*)thisTag->pObject, getImdFromIndex(MI_SHADOW));
break;
case RENDER_STRUCTURE:
renderStructure((STRUCTURE*)thisTag->pObject);
break;
case RENDER_FEATURE:
renderFeature((FEATURE*)thisTag->pObject);
break;
case RENDER_PROXMSG:
renderProximityMsg((PROXIMITY_DISPLAY*)thisTag->pObject);
break;
case RENDER_PROJECTILE:
renderProjectile((PROJECTILE*)thisTag->pObject);
break;
case RENDER_ANIMATION:
renderAnimComponent((COMPONENT_OBJECT*)thisTag->pObject);
break;
case RENDER_DELIVPOINT:
renderDeliveryPoint((FLAG_POSITION*)thisTag->pObject, false);
break;
}
}
//reset the bucket array as we go
//reset the tag array
bucketArray.clear();
}