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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#include "stdafx.h"
#include "hammer.h"
#include "StockSolids.h"
#include "hammer_mathlib.h"
#include "MapSolid.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
#pragma warning(disable:4244)
//Vector pmPoints[64];
StockSolid::StockSolid(int nFields)
{
AllocateDataFields(nFields);
cofs.Init();
}
StockSolid::~StockSolid()
{
if ( pFields )
{
delete[] pFields;
pFields = NULL;
}
}
void StockSolid::AllocateDataFields(int nFields_)
{
pFields = new STSDATAFIELD[nFields_];
Assert(pFields);
iMaxFields = nFields_;
this->nFields = 0; // none yet
}
void StockSolid::Serialize(std::fstream& file, BOOL bIsStoring)
{
}
int StockSolid::GetFieldCount() const
{
return nFields;
}
void StockSolid::SetFieldData(int iIndex, int iData)
{
Assert(iIndex < nFields);
STSDATAFIELD& field = pFields[iIndex];
field.iValue = iData;
if(field.flags & DFFLAG_RANGED)
{
Assert(!(iData < field.iRangeLower || iData > field.iRangeUpper));
}
}
int StockSolid::GetFieldData(int iIndex, int *piData) const
{
Assert(iIndex < nFields);
STSDATAFIELD& field = pFields[iIndex];
if(piData)
piData[0] = field.iValue;
return field.iValue;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void StockSolid::SetOrigin(const Vector &o)
{
origin = o;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void StockSolid::SetCenterOffset(const Vector &ofs)
{
cofs = ofs;
}
void StockSolid::AddDataField(STSDF_TYPE type, const char *pszName, int iRangeLower, int iRangeUpper)
{
Assert(nFields < iMaxFields);
STSDATAFIELD& field = pFields[nFields++];
field.type = type;
field.flags = 0;
strcpy(field.szName, pszName);
if(iRangeLower != -1)
{
field.flags |= DFFLAG_RANGED;
field.iRangeLower = iRangeLower;
field.iRangeUpper = iRangeUpper;
}
}
// ----------------------------------------------------------------------------
// StockBlock()
// ----------------------------------------------------------------------------
StockBlock::StockBlock() :
StockSolid(3)
{
AddDataField(DFTYPE_INTEGER, "Width (X)");
AddDataField(DFTYPE_INTEGER, "Depth (Y)");
AddDataField(DFTYPE_INTEGER, "Height (Z)");
}
void StockBlock::SetFromBox(BoundBox *pBox)
{
// round floats before converting to integers
SetFieldData(fieldWidth, (pBox->bmaxs[0] - pBox->bmins[0])+0.5f );
SetFieldData(fieldDepth, (pBox->bmaxs[1] - pBox->bmins[1])+0.5f );
SetFieldData(fieldHeight, (pBox->bmaxs[2] - pBox->bmins[2])+0.5f );
Vector o;
pBox->GetBoundsCenter(o);
SetOrigin(o);
}
void StockBlock::CreateMapSolid(CMapSolid *pSolid, TextureAlignment_t eAlignment)
{
CMapFace Face;
float fDepth = float(GetFieldData(fieldDepth))/2;
float fWidth = float(GetFieldData(fieldWidth))/2;
float fHeight = float(GetFieldData(fieldHeight))/2;
// create box
Vector bmins, bmaxs;
bmins[0] = origin[0] - fWidth + cofs[0];
bmins[1] = origin[1] - fDepth + cofs[1];
bmins[2] = origin[2] - fHeight + cofs[2];
bmaxs[0] = origin[0] + fWidth + cofs[0];
bmaxs[1] = origin[1] + fDepth + cofs[1];
bmaxs[2] = origin[2] + fHeight + cofs[2];
Vector Points[4];
// x planes - top first
Points[0][0] = bmins[0];
Points[0][1] = bmaxs[1];
Points[0][2] = bmaxs[2];
Points[1][0] = bmaxs[0];
Points[1][1] = bmaxs[1];
Points[1][2] = bmaxs[2];
Points[2][0] = bmaxs[0];
Points[2][1] = bmins[1];
Points[2][2] = bmaxs[2];
Points[3][0] = bmins[0];
Points[3][1] = bmins[1];
Points[3][2] = bmaxs[2];
Face.CreateFace(Points, 4, pSolid->IsCordonBrush());
pSolid->AddFace(&Face);
// top - modify heights
for(int i = 0; i < 4; i++)
{
Points[i][2] = bmins[2];
}
Face.CreateFace(Points, -4, pSolid->IsCordonBrush());
pSolid->AddFace(&Face);
// y planes - left
Points[0][0] = bmins[0];
Points[0][1] = bmaxs[1];
Points[0][2] = bmaxs[2];
Points[1][0] = bmins[0];
Points[1][1] = bmins[1];
Points[1][2] = bmaxs[2];
Points[2][0] = bmins[0];
Points[2][1] = bmins[1];
Points[2][2] = bmins[2];
Points[3][0] = bmins[0];
Points[3][1] = bmaxs[1];
Points[3][2] = bmins[2];
Face.CreateFace(Points, 4, pSolid->IsCordonBrush());
pSolid->AddFace(&Face);
// right - modify xloc
for(int i = 0; i < 4; i++)
{
Points[i][0] = bmaxs[0];
}
Face.CreateFace(Points, -4, pSolid->IsCordonBrush());
pSolid->AddFace(&Face);
// x planes - farthest
Points[0][0] = bmaxs[0];
Points[0][1] = bmaxs[1];
Points[0][2] = bmaxs[2];
Points[1][0] = bmins[0];
Points[1][1] = bmaxs[1];
Points[1][2] = bmaxs[2];
Points[2][0] = bmins[0];
Points[2][1] = bmaxs[1];
Points[2][2] = bmins[2];
Points[3][0] = bmaxs[0];
Points[3][1] = bmaxs[1];
Points[3][2] = bmins[2];
Face.CreateFace(Points, 4, pSolid->IsCordonBrush());
pSolid->AddFace(&Face);
// nearest - modify yloc
for(int i = 0; i < 4; i++)
{
Points[i][1] = bmins[1];
}
Face.CreateFace(Points, -4, pSolid->IsCordonBrush());
pSolid->AddFace(&Face);
pSolid->CalcBounds();
pSolid->InitializeTextureAxes(eAlignment, INIT_TEXTURE_ALL | INIT_TEXTURE_FORCE);
}
// ----------------------------------------------------------------------------
// StockWedge()
// ----------------------------------------------------------------------------
StockWedge::StockWedge() :
StockSolid(3)
{
AddDataField(DFTYPE_INTEGER, "Width (X)");
AddDataField(DFTYPE_INTEGER, "Depth (Y)");
AddDataField(DFTYPE_INTEGER, "Height (Z)");
}
void StockWedge::SetFromBox(BoundBox *pBox)
{
SetFieldData(fieldWidth, pBox->bmaxs[0] - pBox->bmins[0]);
SetFieldData(fieldDepth, pBox->bmaxs[1] - pBox->bmins[1]);
SetFieldData(fieldHeight, pBox->bmaxs[2] - pBox->bmins[2]);
Vector o;
pBox->GetBoundsCenter(o);
SetOrigin(o);
}
void StockWedge::CreateMapSolid(CMapSolid *pSolid, TextureAlignment_t eTextureAlignment)
{
CMapFace Face;
float fDepth = float(GetFieldData(fieldDepth))/2;
float fWidth = float(GetFieldData(fieldWidth))/2;
float fHeight = float(GetFieldData(fieldHeight))/2;
Vector Points[4];
// x planes - top
Points[0][0] = origin[0] + fWidth;
Points[0][1] = origin[1] + fDepth;
Points[0][2] = origin[2] + fHeight;
Points[1][0] = origin[0] + fWidth;
Points[1][1] = origin[1] - fDepth;
Points[1][2] = origin[2] + fHeight;
Points[2][0] = origin[0] - fWidth;
Points[2][1] = origin[1] - fDepth;
Points[2][2] = origin[2] + fHeight;
Face.CreateFace(Points, 3);
pSolid->AddFace(&Face);
// bottom
for (int i = 0; i < 3; i++)
{
Points[i][2] = origin[2] - fHeight;
}
Face.CreateFace(Points, -3);
pSolid->AddFace(&Face);
// left (slant)
Points[0][0] = origin[0] + fWidth;
Points[0][1] = origin[1] + fDepth;
Points[0][2] = origin[2] - fHeight;
Points[1][0] = origin[0] + fWidth;
Points[1][1] = origin[1] + fDepth;
Points[1][2] = origin[2] + fHeight;
Points[2][0] = origin[0] - fWidth;
Points[2][1] = origin[1] - fDepth;
Points[2][2] = origin[2] + fHeight;
Points[3][0] = origin[0] - fWidth;
Points[3][1] = origin[1] - fDepth;
Points[3][2] = origin[2] - fHeight;
Face.CreateFace(Points, 4);
pSolid->AddFace(&Face);
// south
Points[0][0] = origin[0] + fWidth;
Points[0][1] = origin[1] - fDepth;
Points[0][2] = origin[2] + fHeight;
Points[1][0] = origin[0] + fWidth;
Points[1][1] = origin[1] - fDepth;
Points[1][2] = origin[2] - fHeight;
Points[2][0] = origin[0] - fWidth;
Points[2][1] = origin[1] - fDepth;
Points[2][2] = origin[2] - fHeight;
Points[3][0] = origin[0] - fWidth;
Points[3][1] = origin[1] - fDepth;
Points[3][2] = origin[2] + fHeight;
Face.CreateFace(Points, 4);
pSolid->AddFace(&Face);
// right
Points[0][0] = origin[0] + fWidth;
Points[0][1] = origin[1] + fDepth;
Points[0][2] = origin[2] + fHeight;
Points[1][0] = origin[0] + fWidth;
Points[1][1] = origin[1] + fDepth;
Points[1][2] = origin[2] - fHeight;
Points[2][0] = origin[0] + fWidth;
Points[2][1] = origin[1] - fDepth;
Points[2][2] = origin[2] - fHeight;
Points[3][0] = origin[0] + fWidth;
Points[3][1] = origin[1] - fDepth;
Points[3][2] = origin[2] + fHeight;
Face.CreateFace(Points, 4);
pSolid->AddFace(&Face);
pSolid->CalcBounds();
pSolid->InitializeTextureAxes(eTextureAlignment, INIT_TEXTURE_ALL | INIT_TEXTURE_FORCE);
}
// ----------------------------------------------------------------------------
// StockCylinder()
// ----------------------------------------------------------------------------
StockCylinder::StockCylinder()
: StockSolid(4)
{
AddDataField(DFTYPE_INTEGER, "Width (X)");
AddDataField(DFTYPE_INTEGER, "Depth (Y)");
AddDataField(DFTYPE_INTEGER, "Height (Z)");
AddDataField(DFTYPE_INTEGER, "Number of Sides");
SetFieldData(fieldSideCount, 8);
}
void StockCylinder::SetFromBox(BoundBox *pBox)
{
SetFieldData(fieldWidth, pBox->bmaxs[0] - pBox->bmins[0]);
SetFieldData(fieldDepth, pBox->bmaxs[1] - pBox->bmins[1]);
SetFieldData(fieldHeight, pBox->bmaxs[2] - pBox->bmins[2]);
Vector o;
pBox->GetBoundsCenter(o);
SetOrigin(o);
}
void StockCylinder::CreateMapSolid(CMapSolid *pSolid, TextureAlignment_t eTextureAlignment)
{
CMapFace Face;
float fDepth = float(GetFieldData(fieldDepth))/2;
float fWidth = float(GetFieldData(fieldWidth))/2;
float fHeight = float(GetFieldData(fieldHeight))/2;
int nSides = GetFieldData(fieldSideCount);
Vector pmPoints[64];
polyMake(origin[0] - fWidth, origin[1] - fDepth, origin[0] + fWidth, origin[1] + fDepth, nSides, 0, pmPoints );
// face 0 - top face
for(int i = 0; i < nSides+1; i++)
{
pmPoints[i][2] = origin[2] - fHeight;
}
Face.CreateFace( pmPoints, -nSides);
pSolid->AddFace(&Face);
// bottom face
for(int i = 0; i < nSides+1; i++)
{
pmPoints[i][2] = origin[2] + fHeight;
}
Face.CreateFace( pmPoints, nSides);
pSolid->AddFace(&Face);
// other sides
Vector Points[4];
for(int i = 0; i < nSides; i++)
{
Points[0][0] = pmPoints[i][0];
Points[0][1] = pmPoints[i][1];
Points[0][2] = origin[2] - fHeight;
Points[1][0] = pmPoints[i+1][0];
Points[1][1] = pmPoints[i+1][1];
Points[1][2] = origin[2] - fHeight;
Points[2][0] = pmPoints[i+1][0];
Points[2][1] = pmPoints[i+1][1];
Points[2][2] = origin[2] + fHeight;
Points[3][0] = pmPoints[i][0];
Points[3][1] = pmPoints[i][1];
Points[3][2] = origin[2] + fHeight;
Face.CreateFace(Points, 4);
Face.texture.smooth = 1.f;
pSolid->AddFace(&Face);
}
pSolid->CalcBounds();
pSolid->InitializeTextureAxes(eTextureAlignment, INIT_TEXTURE_ALL | INIT_TEXTURE_FORCE);
}
// ----------------------------------------------------------------------------
// StockSpike()
// ----------------------------------------------------------------------------
StockSpike::StockSpike()
: StockSolid(4)
{
AddDataField(DFTYPE_INTEGER, "Width (X)");
AddDataField(DFTYPE_INTEGER, "Depth (Y)");
AddDataField(DFTYPE_INTEGER, "Height (Z)");
AddDataField(DFTYPE_INTEGER, "Number of Sides");
SetFieldData(fieldSideCount, 8);
}
void StockSpike::SetFromBox(BoundBox *pBox)
{
SetFieldData(fieldWidth, pBox->bmaxs[0] - pBox->bmins[0]);
SetFieldData(fieldDepth, pBox->bmaxs[1] - pBox->bmins[1]);
SetFieldData(fieldHeight, pBox->bmaxs[2] - pBox->bmins[2]);
Vector o;
pBox->GetBoundsCenter(o);
SetOrigin(o);
}
void StockSpike::CreateMapSolid(CMapSolid *pSolid, TextureAlignment_t eTextureAlignment)
{
float fDepth = float(GetFieldData(fieldDepth))/2;
float fWidth = float(GetFieldData(fieldWidth))/2;
float fHeight = float(GetFieldData(fieldHeight))/2;
int nSides = GetFieldData(fieldSideCount);
CMapFace NewFace;
// create bottom poly
Vector pmPoints[64];
polyMake(origin[0] - fWidth, origin[1] - fDepth, origin[0] + fWidth, origin[1] + fDepth, nSides, 0, pmPoints);
// bottom face
for(int i = 0; i < nSides+1; i++)
{
// YWB rounding???
pmPoints[i][2] = V_rint(origin[2] - fHeight);
}
NewFace.CreateFace(pmPoints, -nSides);
pSolid->AddFace(&NewFace);
// other sides
Vector Points[3];
// get centerpoint
Points[0][0] = origin[0];
Points[0][1] = origin[1];
// YWB rounding???
Points[0][2] = V_rint(origin[2] + fHeight);
for(int i = 0; i < nSides; i++)
{
Points[1][0] = pmPoints[i][0];
Points[1][1] = pmPoints[i][1];
Points[1][2] = pmPoints[i][2];
Points[2][0] = pmPoints[i+1][0];
Points[2][1] = pmPoints[i+1][1];
Points[2][2] = pmPoints[i+1][2];
NewFace.CreateFace(Points, 3);
pSolid->AddFace(&NewFace);
}
pSolid->CalcBounds();
pSolid->InitializeTextureAxes(eTextureAlignment, INIT_TEXTURE_ALL | INIT_TEXTURE_FORCE);
}
StockSphere::StockSphere()
: StockSolid(4)
{
AddDataField(DFTYPE_INTEGER, "Width (X)");
AddDataField(DFTYPE_INTEGER, "Depth (Y)");
AddDataField(DFTYPE_INTEGER, "Height (Z)");
AddDataField(DFTYPE_INTEGER, "Subdivisions");
SetFieldData(fieldSideCount, 8);
}
void StockSphere::SetFromBox(BoundBox *pBox)
{
SetFieldData(fieldWidth, pBox->bmaxs[0] - pBox->bmins[0]);
SetFieldData(fieldDepth, pBox->bmaxs[1] - pBox->bmins[1]);
SetFieldData(fieldHeight, pBox->bmaxs[2] - pBox->bmins[2]);
Vector o;
pBox->GetBoundsCenter(o);
SetOrigin(o);
}
//-----------------------------------------------------------------------------
// Purpose: Builds a tesselated sphere.
// Input : pSolid - Pointer to a solid that will become a sphere.
//-----------------------------------------------------------------------------
void StockSphere::CreateMapSolid(CMapSolid *pSolid, TextureAlignment_t eTextureAlignment)
{
CMapFace Face;
float fDepth = (float)GetFieldData(fieldDepth) / 2;
float fWidth = (float)GetFieldData(fieldWidth) / 2;
float fHeight = (float)GetFieldData(fieldHeight) / 2;
int nSides = GetFieldData(fieldSideCount);
float fAngle = 0;
float fAngleStep = 180.0 / nSides;
//
// Build the sphere by building slices at constant angular intervals.
//
// Each slice is a ring of four-sided faces, except for the top and bottom slices,
// which are flattened cones.
//
// Unrolled, a sphere made with 5 'sides' has 25 faces and looks like this:
//
// /\ /\ /\ /\ /\
// / 0\/ 1\/ 2\/ 3\/ 4\
// | 5| 6| 7| 8| 9|
// | 10| 11| 12| 13| 14|
// | 15| 16| 17| 18| 19|
// \20/\21/\22/\23/\24/
// \/ \/ \/ \/ \/
//
for (int nSlice = 0; nSlice < nSides; nSlice++)
{
float fAngle1 = fAngle + fAngleStep;
//
// Make the upper polygon.
//
Vector TopPoints[64];
float fUpperWidth = fWidth * sin(DEG2RAD(fAngle));
float fUpperDepth = fDepth * sin(DEG2RAD(fAngle));
polyMake(origin[0] - fUpperWidth, origin[1] - fUpperDepth, origin[0] + fUpperWidth, origin[1] + fUpperDepth, nSides, 0, TopPoints);
//
// Make the lower polygon.
//
Vector BottomPoints[64];
float fLowerWidth = fWidth * sin(DEG2RAD(fAngle1));
float fLowerDepth = fDepth * sin(DEG2RAD(fAngle1));
polyMake(origin[0] - fLowerWidth, origin[1] - fLowerDepth, origin[0] + fLowerWidth, origin[1] + fLowerDepth, nSides, 0, BottomPoints);
//
// Build the faces that connect the upper and lower polygons.
//
Vector Points[4];
float fUpperHeight = origin[2] + fHeight * cos(DEG2RAD(fAngle));
float fLowerHeight = origin[2] + fHeight * cos(DEG2RAD(fAngle1));
for (int i = 0; i < nSides; i++)
{
if (nSlice != 0)
{
Points[0][0] = TopPoints[i + 1][0];
Points[0][1] = TopPoints[i + 1][1];
Points[0][2] = fUpperHeight;
}
Points[1][0] = TopPoints[i][0];
Points[1][1] = TopPoints[i][1];
Points[1][2] = fUpperHeight;
Points[2][0] = BottomPoints[i][0];
Points[2][1] = BottomPoints[i][1];
Points[2][2] = fLowerHeight;
if (nSlice != nSides - 1)
{
Points[3][0] = BottomPoints[i + 1][0];
Points[3][1] = BottomPoints[i + 1][1];
Points[3][2] = fLowerHeight;
}
//
// Top and bottom are cones, not rings, so remove one vertex per face.
//
if (nSlice == 0)
{
Face.CreateFace(&Points[1], 3);
}
else if (nSlice == nSides - 1)
{
Face.CreateFace(Points, 3);
}
else
{
Face.CreateFace(Points, 4);
}
Face.texture.smooth = 1.f;
pSolid->AddFace(&Face);
}
fAngle += fAngleStep;
}
pSolid->CalcBounds();
pSolid->InitializeTextureAxes(eTextureAlignment, INIT_TEXTURE_ALL | INIT_TEXTURE_FORCE);
}
|