forked from scp-fs2open/PCS2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSPDataStructs.cpp
649 lines (467 loc) · 14.4 KB
/
BSPDataStructs.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
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
//****************************************************************************
// BSPDataStructs.cpp
// Author: Derek Meek
// Code for member functions;
//****************************************************************************
/*
* $Logfile: /BSPDataStructs.cpp $
* $Revision: 1.7 $
* $Date: 2008/09/17 19:34:00 $
* $Author: kazan $
*
* $Log: BSPDataStructs.cpp,v $
* Revision 1.7 2008/09/17 19:34:00 kazan
* little cleanup
*
* Revision 1.6 2008/02/09 22:32:57 kazan
* bye bye kaz_vector
*
* Revision 1.5 2008/01/31 17:02:23 kazan
* Add error checking to BSP tree packing, this will slow packing somewhat, but will prevent crashes.
* Correct error in calculating tree size.
*
* Revision 1.4 2008/01/10 18:28:09 bobboau
* *** empty log message ***
*
* Revision 1.3 2005/09/19 21:47:12 kazan
* Linux compat changes
*
* Revision 1.2 2005/08/23 22:36:42 kazan
* s/std::{vector:string}/kaz_{vector:string}
* renderer motion works
* location PMF::LoadFromPOF bottleneck
*
* Revision 1.1.1.1 2004/12/31 06:19:41 kazan
* no message
*
*
*
*/
#include "BSPDataStructs.h"
#include <memory.h>
#include <cmath>
//********************************************************************************************************************
int BSP_BlockHeader::Read(char *buffer) // Binary mode!
{
memcpy(&id, buffer, sizeof(int));
buffer += sizeof(int);
memcpy(&size, buffer, sizeof(int));
buffer += sizeof(int);
return MySize();
}
int BSP_BlockHeader::Write(char *buffer)
{
memcpy(buffer, &id, sizeof(int));
buffer += sizeof(int);
memcpy(buffer, &size, sizeof(int));
buffer += sizeof(int);
return 8;
}
//********************************************************************************************************************
int BSP_DefPoints::Read(char *buffer, BSP_BlockHeader hdr)
{
char *temp = buffer;
head = hdr;
memcpy(&n_verts, buffer, sizeof(int));
buffer += sizeof(int);
memcpy(&n_norms, buffer,sizeof(int));
buffer += sizeof(int);
memcpy(&offset, buffer,sizeof(int));
buffer += sizeof(int);
norm_counts.resize(n_verts);
memcpy(&norm_counts.front(), buffer, n_verts);
buffer += n_verts;
// ok this is based off my GUESS .. i hope i guessed correctly
vertex_data.resize(n_verts);
//realign the buffer
buffer = temp;
buffer += (offset-8);
// buffer now in alignment to read the vertex data;
for (int i = 0; i < n_verts; i++)
{
memcpy(&vertex_data[i].vertex, buffer, sizeof(vector3d));
buffer += sizeof(vector3d);
if (int(norm_counts[i]) > 0)
{
vertex_data[i].norms.resize(norm_counts[i]);
memcpy(&vertex_data[i].norms.front(), buffer, sizeof(vector3d) * int(norm_counts[i]));
size_t offset = normals.size();
normals.resize(normals.size() + norm_counts[i]);
memcpy(&normals.front() + offset, buffer, sizeof(vector3d)* int(norm_counts[i]));
buffer += (sizeof(vector3d) * norm_counts[i]);
}
else
vertex_data[i].norms.clear();
}
//vertex_data
return MySize();
}
int BSP_DefPoints::Write(char *buffer)
{
int Collector = 0;
char *tbuff = buffer;
head.size = MySize();
tbuff += head.Write(tbuff); //size of the header;
memcpy(tbuff, &n_verts, sizeof(int));
tbuff += sizeof(int);
memcpy(tbuff, &n_norms, sizeof(int));
tbuff += sizeof(int);
memcpy(tbuff, &offset, sizeof(int));
tbuff += sizeof(int);
memcpy(tbuff, &norm_counts.front(), n_verts);
tbuff += n_verts;
// ok this is based off my GUESS .. i hope i guessed correctly
for (int i = 0; i < n_verts; i++)
{
memcpy(tbuff, &vertex_data[i].vertex, sizeof(vector3d));
tbuff += sizeof(vector3d);
if (norm_counts[i])
{
memcpy(tbuff, &vertex_data[i].norms.front(), sizeof(vector3d) * ((int) (unsigned char)norm_counts[i]));
tbuff += (sizeof(vector3d) * norm_counts[i]);
Collector += int(norm_counts[i]);
}
}
//vertex_data
return (unsigned int)(tbuff-buffer);
}
int BSP_DefPoints::MySize()
{
/*
struct BSP_DefPoints // 1 - Defines Verticies
{
BSP_BlockHeader head; //8 |8
int n_verts; //4 |12
int n_norms; //4 |16
int offset; //4 |20 |from start of chunk to vertex data
unsigned char *norm_counts; //n_norms |20+n_norms |n_verts in size;
vertdata *vertex_data; //Equation |20+n_norms+Equation
};*/
int Collector = 0;
for (int i = 0; i < n_verts; i++)
{
Collector += (int) ((unsigned char)norm_counts[i]);
}
return (20 + n_verts + (sizeof(vector3d) * (Collector + n_verts)));
}
//********************************************************************************************************************
int Flat_vertex::Read(char *buffer)
{
memcpy(&vertnum, buffer, sizeof(short));
buffer += sizeof(short);
memcpy(&normnum, buffer, sizeof(short));
buffer += sizeof(short);
return MySize();
}
int Flat_vertex::Write(char *buffer)
{
memcpy(buffer, &vertnum, sizeof(short));
buffer += sizeof(short);
memcpy(buffer, &normnum, sizeof(short));
buffer += sizeof(short);
return 4;
}
//********************************************************************************************************************
int BSP_FlatPoly::Read(char *buffer, BSP_BlockHeader hdr)
{
head = hdr;
memcpy(&normal, buffer, sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(¢er, buffer, sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(&radius, buffer, sizeof(float));
buffer += sizeof(float);
memcpy(&nverts, buffer, sizeof(int));
buffer += sizeof(int);
memcpy(&red, buffer, sizeof(byte));
buffer += sizeof(byte);
memcpy(&green, buffer, sizeof(byte));
buffer += sizeof(byte);
memcpy(&blue, buffer, sizeof(byte));
buffer += sizeof(byte);
memcpy(&pad, buffer, sizeof(byte));
buffer += sizeof(byte);
verts.resize(nverts);
for (int i = 0; i < nverts; i++)
{
verts[i].Read(buffer);
buffer += 4; // sizeof short*2 - which is the size of a Flat_vertex
}
return MySize();
}
vector3d BSP_FlatPoly::MyCenter(std::vector<vector3d> Verts)
{
float TotalArea=0, triarea;
vector3d Centroid = MakeVector(0,0,0), midpoint;
for (size_t i = 0; i < Verts.size(); i++)
{
midpoint = Verts[i] + Verts[i+1] + Verts[i+2];
midpoint = midpoint/3;
// Area of Triangle defined by P1, P2, P3 = vector3d(crossProduct(p2-p1, p3-p1)).magnitude()/2
triarea = Magnitude(CrossProduct(Verts[i+1]-Verts[i],
Verts[i+2]-Verts[i])); // this needs to be area * 2
midpoint = triarea*midpoint;
TotalArea += triarea;
Centroid += midpoint;
}
Centroid = float(1.0 / TotalArea) * Centroid;
return Centroid;
}
float BSP_FlatPoly::MyRadius(vector3d center, std::vector<vector3d> Verts)
{
float RetVal=0;
vector3d max;
max.x = Abs(Verts[0].x);
max.y = Abs(Verts[0].y);
max.z = Abs(Verts[0].z);
for (int i = 0; i < nverts; i++)
{
if (Abs(Verts[i].x) > max.x)
max.x = Abs(Verts[i].x);
if (Abs(Verts[i].y) > max.y)
max.y = Abs(Verts[i].y);
if (Abs(Verts[i].z) > max.z)
max.z = Abs(Verts[i].z);
}
RetVal = ((max.x-Abs(center.x)) * (max.x-Abs(center.x))) +
((max.y-Abs(center.y)) * (max.y-Abs(center.y))) +
((max.z-Abs(center.z)) * (max.z-Abs(center.z)));
RetVal = float(sqrt(RetVal));
return RetVal;
}
int BSP_FlatPoly::Write(char *buffer)
{
buffer += head.Write(buffer);
memcpy(buffer, &normal, sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(buffer, ¢er, sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(buffer, &radius, sizeof(float));
buffer += sizeof(float);
memcpy(buffer, &nverts, sizeof(int));
buffer += sizeof(int);
memcpy(buffer, &red, sizeof(byte));
buffer += sizeof(byte);
memcpy(buffer, &green, sizeof(byte));
buffer += sizeof(byte);
memcpy(buffer, &blue, sizeof(byte));
buffer += sizeof(byte);
memcpy(buffer, &pad, sizeof(byte));
buffer += sizeof(byte);
for (int i = 0; i < nverts; i++)
{
buffer += verts[i].Write(buffer);
}
return (head.MySize() + (4 /*sizeof Flat_vertex*/ * nverts)
+ (sizeof(byte) * 4) + sizeof(int) + sizeof(float) + (sizeof(vector3d) * 2));
}
//********************************************************************************************************************
int Tmap_vertex::Read(char *buffer)
{
memcpy(&vertnum, buffer, sizeof(short));
buffer += sizeof(short);
memcpy(&normnum, buffer, sizeof(short));
buffer += sizeof(short);
memcpy(&u, buffer, sizeof(float));
buffer += sizeof(float);
memcpy(&v, buffer, sizeof(float));
buffer += sizeof(float);
return MySize();
}
int Tmap_vertex::Write(char *buffer)
{
memcpy(buffer, &vertnum, sizeof(short));
buffer += sizeof(short);
memcpy(buffer, &normnum, sizeof(short));
buffer += sizeof(short);
memcpy(buffer, &u, sizeof(float));
buffer += sizeof(float);
memcpy(buffer, &v, sizeof(float));
buffer += sizeof(float);
return ((sizeof(short) * 2) + (sizeof(float) * 2));
}
//********************************************************************************************************************
int BSP_TmapPoly::Read(char *buffer, BSP_BlockHeader hdr)
{
head = hdr;
memcpy(&normal, buffer, sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(¢er, buffer, sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(&radius, buffer, sizeof(float));
buffer += sizeof(float);
memcpy(&nverts, buffer, sizeof(int));
buffer += sizeof(int);
memcpy(&tmap_num, buffer, sizeof(int));
buffer += sizeof(int);
verts.resize(nverts);
for (int i =0; i < nverts; i++)
{
verts[i].Read(buffer);
buffer += 12; //12 = sizeof(Tmap_vertex)
}
return MySize();
}
vector3d BSP_TmapPoly::MyCenter(std::vector<vector3d> Verts)
{
float TotalArea=0, triarea;
vector3d Centroid = MakeVector(0,0,0), midpoint;
for (int i = 0; i < nverts-2; i++)
{
midpoint = Verts[i] + Verts[i+1] + Verts[i+2];
midpoint = midpoint/3;
// Area of Triangle defined by P1, P2, P3 = vector3d(crossProduct(p2-p1, p3-p1)).magnitude()/2
triarea = Magnitude(CrossProduct(Verts[i+1]-Verts[i],
Verts[i+2]-Verts[i])); // this needs to be area * 2
midpoint = triarea*midpoint;
TotalArea += triarea;
Centroid += midpoint;
}
Centroid = float(1.0 / TotalArea) * Centroid;
return Centroid;
}
float BSP_TmapPoly::MyRadius(vector3d center, std::vector<vector3d> Verts)
{
float RetVal=0;
vector3d max;
max.x = Abs(Verts[0].x);
max.y = Abs(Verts[0].y);
max.z = Abs(Verts[0].z);
for (int i = 0; i < nverts; i++)
{
if (Abs(Verts[i].x) > max.x)
max.x = Abs(Verts[i].x);
if (Abs(Verts[i].y) > max.y)
max.y = Abs(Verts[i].y);
if (Abs(Verts[i].z) > max.z)
max.z = Abs(Verts[i].z);
}
RetVal = ((max.x-Abs(center.x)) * (max.x-Abs(center.x))) +
((max.y-Abs(center.y)) * (max.y-Abs(center.y))) +
((max.z-Abs(center.z)) * (max.z-Abs(center.z)));
RetVal = float(sqrt(RetVal));
return RetVal;
}
int BSP_TmapPoly::Write(char *buffer)
{
buffer += head.Write(buffer);
memcpy(buffer, &normal, sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(buffer, ¢er, sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(buffer, &radius, sizeof(float));
buffer += sizeof(float);
memcpy(buffer, &nverts, sizeof(int));
buffer += sizeof(int);
memcpy(buffer, &tmap_num, sizeof(int));
buffer += sizeof(int);
for (int i =0; i < nverts; i++)
{
buffer += verts[i].Write(buffer); //12 = sizeof(Tmap_vertex)
}
return ((12 * nverts) + (sizeof(int) * 2) + sizeof(float) + (sizeof(vector3d) * 2));
}
//********************************************************************************************************************
int BSP_SortNorm::Read(char *buffer, BSP_BlockHeader hdr)
{
head = hdr;
memcpy(&plane_normal, buffer, sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(&plane_point, buffer, sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(&reserved, buffer, sizeof(int));
buffer += sizeof(int);
memcpy(&front_offset, buffer, sizeof(int));
buffer += sizeof(int);
memcpy(&back_offset, buffer, sizeof(int));
buffer += sizeof(int);
memcpy(&prelist_offset, buffer, sizeof(int));
buffer += sizeof(int);
memcpy(&postlist_offset, buffer, sizeof(int));
buffer += sizeof(int);
memcpy(&online_offset, buffer, sizeof(int));
buffer += sizeof(int);
memcpy(&min_bounding_box_point, buffer, sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(&max_bounding_box_point, buffer, sizeof(vector3d));
buffer += sizeof(vector3d);
return MySize();
}
int BSP_SortNorm::Write(char *buffer)
{
buffer += head.Write(buffer); // i was getting a all kinds of problems because i forgot to write this
memcpy(buffer, &plane_normal, sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(buffer, &plane_point,sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(buffer, &reserved, sizeof(int));
buffer += sizeof(int);
memcpy(buffer, &front_offset, sizeof(int));
buffer += sizeof(int);
memcpy(buffer, &back_offset, sizeof(int));
buffer += sizeof(int);
memcpy(buffer, &prelist_offset, sizeof(int));
buffer += sizeof(int);
memcpy(buffer, &postlist_offset,sizeof(int));
buffer += sizeof(int);
memcpy(buffer, &online_offset, sizeof(int));
buffer += sizeof(int);
memcpy(buffer, &min_bounding_box_point, sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(buffer, &max_bounding_box_point, sizeof(vector3d));
buffer += sizeof(vector3d);
return 72;
}
//********************************************************************************************************************
int BSP_BoundBox::Read(char *buffer, BSP_BlockHeader hdr)
{
head = hdr;
memcpy(&min_point, buffer, sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(&max_point, buffer, sizeof(vector3d));
buffer += sizeof(vector3d);
return MySize();
}
int BSP_BoundBox::Write(char *buffer)
{
buffer += head.Write(buffer);
memcpy(buffer, &min_point, sizeof(vector3d));
buffer += sizeof(vector3d);
memcpy(buffer, &max_point, sizeof(vector3d));
buffer += sizeof(vector3d);
return 30;
}
//********************************************************************************************************************
bool operator==(BSP_TmapPoly &a, BSP_TmapPoly &b)
{
bool Ret = ((a.center == b.center) && (a.radius == b.radius) && (a.normal == b.normal) && (a.nverts == b.nverts));
if (Ret)
{
for (int i = 0; i < a.nverts; i++)
{
if (!(a.verts[i].normnum == b.verts[i].normnum &&
a.verts[i].vertnum == b.verts[i].vertnum &&
a.verts[i].u == b.verts[i].u &&
a.verts[i].v == b.verts[i].v))
return false;
}
}
else return false;
return true;
}
bool operator==(BSP_FlatPoly &a, BSP_FlatPoly &b)
{
bool Ret = ((a.center == b.center) && (a.radius == b.radius) && (a.normal == b.normal) && (a.nverts == b.nverts));
if (Ret)
{
for (int i = 0; i < a.nverts; i++)
{
if (!(a.verts[i].normnum == b.verts[i].normnum &&
a.verts[i].vertnum == b.verts[i].vertnum))
return false;
}
}
else
return false;
return true;
}