-
Notifications
You must be signed in to change notification settings - Fork 0
/
CBsp.cpp
executable file
·2210 lines (1790 loc) · 56.4 KB
/
CBsp.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
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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
CBsp.cpp
Author: Tom Naughton
Description: based on AfterShock
* Aftershock 3D rendering engine
* Copyright (C) 1999 Stephen C. Taylor
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <agl.h>
#include <glut.h>
#include <stdlib.h>
#include "CTextures.h"
#include "CPakStream.h"
#include "CGLImage.h"
#include "CBsp.h"
#include "CShader.h"
#include "CResourceManager.h"
#include "CModelInstance.h"
#include "CMd3Controller.h"
#include "CMd3.h"
#include "utilities.h"
#include "cstdlib"
static int face_cmp(const void *a, const void *b);
// Viewport (window) size in pixels
#define VIEWPORT_W 400
#define VIEWPORT_H 300
#define BSPHEADERID (*(int*)"IBSP")
#define FAKKHEADERID (*(int*)"FAKK")
#define BSPVERSION 46
#define FAKKVERSION 12
#define MAX_MISC_MODEL 500 // Room for additional models
#define CLIPPING_LEAF 1
#define CLIPPING_NODE 1
#define CLIPPING_FACE 1
#define DRAW_MAPENTS 1
#define AZ_SCALE 0.3
#define EL_SCALE 0.3
#define MOVE_SPEED 400.0
#define READLUMP(lump, val, success) \
r_num##val = readlump(lump, "lump", (void**)&r_##val, sizeof(*(r_##val)), success)
#define BSP_TESTVIS(from,to) \
(*(r_visibility->data + (from)*r_visibility->rowsize + \
((to)>>3)) & (1 << ((to) & 7)))
// mapent
mapent_class_t mapent_classinit[] =
{
#include "mapent.h"
};
CBsp::CBsp(CResourceManager *inResources)
{
_resources = inResources;
g_mapent_numinst = 0;
g_mapent_nummisc = 0;
g_mapent_numclasses = 0;
g_mapent_class = 0;
g_mapent_inst = 0;
r_subdivisiontol = 5;
r_maxmeshlevel = 5;
r_drawitems=1;
r_stereo=FALSE;
r_fullscreen=FALSE;
move = 0;
strafe = 0;
fpstime = 0;
fpsframes = 0;
bsplen = 0;
bspdata = 0;
r_models = 0;
r_verts = 0;
r_planes = 0;
r_leafs = 0;
r_nodes = 0;
r_faces = 0;
r_lfaces = 0;
r_elems = 0;
r_visibility = 0;
r_meshes = 0;
r_skybox = 0;
facelist.faces = 0;
translist.faces = 0;
r_faceinc = 0;
skylist = 0;
g_mapent_class = 0;
g_mapent_inst = 0;
entities = 0;
epairs = 0;
entity_buf = 0;
}
CBsp::~CBsp()
{
mesh_free_all();
skybox_free();
mapent_freeall();
bsp_free();
render_finalize();
entity_free();
}
Boolean CBsp::init(CPakStream *bsp, renderingAttributes_t *renderingAttributes)
{
float fov_y;
_rendering = renderingAttributes;
// My GL driver (linux/TNT2) has problems with glEnableClientState(),
// but this seems to clear it up. Go figure.
glFinish();
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
g_frametime = 0;
if (!bsp_read(bsp)) goto fail;
if (!mesh_create_all()) goto fail;
if (!skybox_create()) goto fail;
if (!mapent_loadall(bsp->rootArchive())) goto fail;
_resources->parseShaders();
// Make default shaders for those that weren't found
if (!render_init()) goto fail;
find_start_pos(_rendering);
vec_point(_rendering->r_eyedir, _rendering->r_eye_az, _rendering->r_eye_el);
glEnable(GL_DITHER);
glShadeModel(GL_SMOOTH);
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
glEnable(GL_DEPTH_TEST);
glCullFace(GL_FRONT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glViewport(0, 0, VIEWPORT_W, VIEWPORT_H);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
fov_y = calc_fov(_rendering->r_eyefov, VIEWPORT_W, VIEWPORT_H);
gluPerspective(fov_y, (float)VIEWPORT_W/(float)VIEWPORT_H,
NEAR_GL_PLANE, FAR_GL_PLANE);
glMatrixMode(GL_MODELVIEW);
g_starttime = (double)glutGet(GLUT_ELAPSED_TIME) / 1000.0;
return true;
fail:
return false;
}
void CBsp::mouse_down(renderingAttributes_t *rendering, int x, int y)
{
#pragma unused (rendering)
ox = x; oy = y;
}
void CBsp::mouse_motion(renderingAttributes_t *rendering, int x, int y)
{
float el = -(y - oy) * EL_SCALE;
float az = -(x - ox) * AZ_SCALE;
rendering->r_eye_az = rendering->r_eye_az + az;
rendering->r_eye_el = rendering->r_eye_el + el;
ox = x;
oy = y;
vec_point(rendering->r_eyedir, rendering->r_eye_az, rendering->r_eye_el);
}
void CBsp::Draw(renderingAttributes_t *renderingAttributes)
{
if (CShader::_animateShaders) g_frametime = (double)glutGet(GLUT_ELAPSED_TIME) / 1000.0 - g_starttime;
_rendering = renderingAttributes;
if (_rendering->_wireframe)
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
else
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
// FIXME: color buffer clear can be optionally removed
#define CLEAR_BITS GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
r_leafcount = 0;
// Find eye cluster for PVS checks
if (!_rendering->r_lockpvs)
_rendering->r_eyecluster = find_cluster(_rendering->r_eyepos);
// if (_rendering->r_eyecluster < 0)
// return;
// Need to enable depth mask before clear
glDepthMask(GL_TRUE);
glDrawBuffer(GL_BACK_LEFT);
glClear(CLEAR_BITS);
if (r_stereo) {
glDrawBuffer(GL_BACK_RIGHT);
glClear(CLEAR_BITS);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Set up camera
if(r_stereo)
{
vec3_t up, right;
up[0]=0; up[1]=0; up[2]=1.0;
// calculate right vector for eye seperation
vec_cross(_rendering->r_eyedir, up, right);
vec_normalize(right);
vec_scale(right, _rendering->r_eyesep, right);
// draw right image
glDrawBuffer(GL_BACK_RIGHT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Set up camera
gluLookAt(_rendering->r_eyepos[0]+right[0],_rendering->r_eyepos[1]+right[1],
_rendering->r_eyepos[2]+right[2],_rendering->r_eyepos[0]+(_rendering->r_eyedir[0]*_rendering->r_focallength),
_rendering->r_eyepos[1]+(_rendering->r_eyedir[1]*_rendering->r_focallength),
_rendering->r_eyepos[2]+(_rendering->r_eyedir[2]*_rendering->r_focallength),
0.0, 0.0, 1.0);
render_objects();
// draw left image
glDrawBuffer(GL_BACK_LEFT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(_rendering->r_eyepos[0]-right[0],_rendering->r_eyepos[1]-right[1],
_rendering->r_eyepos[2]-right[2],_rendering->r_eyepos[0]+(_rendering->r_eyedir[0]*_rendering->r_focallength),
_rendering->r_eyepos[1]+(_rendering->r_eyedir[1]*_rendering->r_focallength),
_rendering->r_eyepos[2]+(_rendering->r_eyedir[2]*_rendering->r_focallength),
0.0, 0.0, 1.0);
render_objects();
} else {
gluLookAt(_rendering->r_eyepos[0], _rendering->r_eyepos[1], _rendering->r_eyepos[2],
_rendering->r_eyepos[0]+_rendering->r_eyedir[0], _rendering->r_eyepos[1]+_rendering->r_eyedir[1],
_rendering->r_eyepos[2]+_rendering->r_eyedir[2], 0.0, 0.0, 1.0);
render_objects();
}
#if 0
// Enable for speeds reporting (like r_speeds 1)
printf("faces: %d, leafs: %d\n", facelist.numfaces + translist.numfaces,
r_leafcount);
#endif
}
void CBsp::move_eye(renderingAttributes_t *rendering, double intervaltime, int move, int strafe)
{
vec3_t delta;
if (move)
{
vec_copy(rendering->r_eyedir, delta);
vec_scale(delta, move * MOVE_SPEED * intervaltime, delta);
vec_add(rendering->r_eyepos, delta, rendering->r_eyepos);
}
if (strafe)
{
float norm = sqrt(rendering->r_eyedir[0]*rendering->r_eyedir[0] + rendering->r_eyedir[1]*rendering->r_eyedir[1]);
delta[0] = rendering->r_eyedir[1] / norm;
delta[1] = -rendering->r_eyedir[0] / norm;
delta[2] = 0.0f;
vec_scale(delta, strafe * MOVE_SPEED * intervaltime, delta);
vec_add(rendering->r_eyepos, delta, rendering->r_eyepos);
}
}
void CBsp::reshape(renderingAttributes_t *rendering, int width, int height)
{
float fov_y;
_width = width;
_height = height;
fov_y = calc_fov(rendering->r_eyefov, _width, _height);
gluPerspective(fov_y, (float)_width/(float)_height,
NEAR_GL_PLANE, FAR_GL_PLANE);
}
Boolean CBsp::bsp_read(CPakStream *inItem)
{
UInt8 *lightmapdata = nil;
bsplen = inItem->getSize();
bspdata = (Byte *) inItem->getData("bspdata");
if (!bspdata) goto fail;
dprintf("bsp size %dK\n", bsplen/1024);
bspheader = (struct header*)bspdata;
bspheader->ver = swapLong(bspheader->ver);
// Format seems to be different for FAKK
if (bspheader->id != BSPHEADERID /* && bspheader->id != FAKKHEADERID */) {
dprintf("Not a bsp file\n");
goto fail;
}
if (bspheader->ver != BSPVERSION /* && bspheader->ver != FAKKVERSION */) {
dprintf("Bad bsp file version\n");
goto fail;
}
for (int i=0; i<NUM_LUMPS; i++) {
bspheader->lump[i].fileofs = swapLong(bspheader->lump[i].fileofs);
bspheader->lump[i].filelen = swapLong(bspheader->lump[i].filelen);
}
r_numshaders = bspheader->lump[SHADERREFS].filelen / sizeof(shaderref_t);
Boolean success;
READLUMP(PLANES, planes, success);
if (!success) goto fail;
READLUMP(NODES, nodes, success);
if (!success) goto fail;
READLUMP(LEAFS, leafs, success);
if (!success) goto fail;
READLUMP(LFACES, lfaces, success);
if (!success) goto fail;
READLUMP(MODELS, models, success);
if (!success) goto fail;
READLUMP(VERTS, verts, success);
if (!success) goto fail;
READLUMP(ELEMS, elems, success);
if (!success) goto fail;
READLUMP(FACES, faces, success);
if (!success) goto fail;
(void)readlump(VISIBILITY, "VISIBILITY", (void**)&r_visibility, 1, success);
if (!success) goto fail;
// Now byte-swap the lumps if we're on a big-endian machine...
swaplump(PLANES, r_planes);
swaplump(NODES, r_nodes);
swaplump(LEAFS, r_leafs);
swaplump(LFACES, r_lfaces);
swaplump(MODELS, r_models);
swaplump(VERTS, r_verts);
swaplump(ELEMS, r_elems);
swaplump(FACES, r_faces);
r_visibility->numclusters = swapLong(r_visibility->numclusters);
r_visibility->rowsize = swapLong(r_visibility->rowsize);
entity_parse(bspheader->lump[ENTITIES].filelen, (char*)(bspdata + bspheader->lump[ENTITIES].fileofs));
if (!_resources->initShaders(r_numshaders, 200, (shaderref_t*) (bspdata + bspheader->lump[SHADERREFS].fileofs)) == noErr)
goto fail;
r_lightmapsize = readlump(LIGHTMAPS, "LIGHTMAPS", (void**)&lightmapdata, 1, success);
if (!success) goto fail;
if (!_resources->initLightMaps(lightmapdata, r_lightmapsize) == noErr) goto fail;
CMemoryTracker::safeFree(lightmapdata);
CMemoryTracker::safeFree(bspdata);
bsp_list();
return true;
fail:
if (lightmapdata)
CMemoryTracker::safeFree(lightmapdata);
if (bspdata)
CMemoryTracker::safeFree(bspdata);
return false;
}
void CBsp::bsp_list(void)
{
dprintf("Contents of BSP file:\n\n");
dprintf("num entities %d\n", g_numentities);
dprintf("num models %d\n", r_nummodels);
dprintf("num shaders %d\n", r_numshaders);
dprintf("num planes %d\n", r_numplanes);
dprintf("num verts %d\n", r_numverts);
dprintf("num vertex elems %d\n", r_numelems);
dprintf("num leafs %d\n", r_numleafs);
dprintf("num nodes %d\n", r_numnodes);
dprintf("num faces %d\n", r_numfaces);
dprintf("num lfaces %d\n", r_numlfaces);
dprintf("vis. clusters %d\n", r_visibility->numclusters);
}
void CBsp::bsp_free(void)
{
CMemoryTracker::safeFree(r_models);
CMemoryTracker::safeFree(r_verts);
CMemoryTracker::safeFree(r_planes);
CMemoryTracker::safeFree(r_leafs);
CMemoryTracker::safeFree(r_nodes);
CMemoryTracker::safeFree(r_faces);
CMemoryTracker::safeFree(r_lfaces);
CMemoryTracker::safeFree(r_elems);
CMemoryTracker::safeFree(r_visibility);
}
int CBsp::readlump(int lump, const char *name, void** mem, size_t elem, Boolean &success)
{
unsigned long len = bspheader->lump[lump].filelen;
int num = len / elem;
dprintf("lump size %dK\n", len/1024);
*mem = CMemoryTracker::safeAlloc((unsigned long)1, (unsigned long)len, (char*)name);
if (!*mem) goto fail;
memcpy(*mem, bspdata + bspheader->lump[lump].fileofs, len);
success = true;
return num;
fail:
success = false;
return 0;
}
void CBsp::swaplump(int lump, void *mem)
{
if (!mem) return;
int i, len=bspheader->lump[lump].filelen;
for (i=0; i<len>>2; i++) {
unsigned *ptr = (unsigned *)mem;
ptr[i] = swapLong(ptr[i]);
}
}
#pragma mark -
// mesh
#define LEVEL_WIDTH(lvl) ((1 << (lvl+1)) + 1)
Boolean CBsp::mesh_create_all(void)
{
int i;
// Count meshes
for (r_nummeshes=0; r_nummeshes < r_numfaces; r_nummeshes++)
if (r_faces[r_nummeshes].facetype != FACETYPE_MESH)
break;
r_meshes = (mesh_t*)CMemoryTracker::safeAlloc(r_nummeshes, sizeof(mesh_t), "r_meshes");
if (!r_meshes) goto fail;
for (i=0; i < r_nummeshes; i++) {
r_meshes[i].points = 0;
r_meshes[i].elems = 0;
}
for (i=0; i < r_nummeshes; i++) {
if (!mesh_create(&r_faces[i], &r_meshes[i])) goto fail;
}
return true;
fail:
return false;
}
void CBsp::mesh_free_all(void)
{
int i;
if (r_meshes) {
for (i=0; i < r_nummeshes; i++) {
CMemoryTracker::safeFree(r_meshes[i].points);
// tex_st and lm_st are part of points: don't free
CMemoryTracker::safeFree(r_meshes[i].elems);
}
CMemoryTracker::safeFree(r_meshes);
}
}
int CBsp::mesh_find_level(vec3_t *v)
{
int level;
vec3_t a, b, dist;
// Subdivide on the left until tolerance is reached
for (level=0; level < r_maxmeshlevel-1; level++)
{
// Subdivide on the left
vec_avg(v[0], v[1], a);
vec_avg(v[1], v[2], b);
vec_avg(a, b, v[2]);
// Find distance moved
vec_sub(v[2], v[1], dist);
// Check for tolerance
if (vec_dot(dist, dist) < r_subdivisiontol * r_subdivisiontol)
break;
// Insert new middle vertex
vec_copy(a, v[1]);
}
return level;
}
void CBsp::mesh_find_size(int *numcp, vec3_t *cp, int *size)
{
int u, v, found, level;
float *a, *b;
vec3_t test[3];
// Find non-coincident pairs in u direction
found = 0;
for (v=0; v < numcp[1]; v++)
{
for (u=0; u < numcp[0]-1; u += 2)
{
a = cp[v * numcp[0] + u];
b = cp[v * numcp[0] + u + 2];
if (!vec_cmp(a,b))
{
found = 1;
break;
}
}
if (found) break;
}
if (!found) dprintf("Bad mesh control points\n");
// Find subdivision level in u
vec_copy(a, test[0]);
vec_copy((a+3), test[1]);
vec_copy(b, test[2]);
level = mesh_find_level(test);
size[0] = (LEVEL_WIDTH(level) - 1) * ((numcp[0]-1) / 2) + 1;
// Find non-coincident pairs in v direction
found = 0;
for (u=0; u < numcp[0]; u++)
{
for (v=0; v < numcp[1]-1; v += 2)
{
a = cp[v * numcp[0] + u];
b = cp[(v + 2) * numcp[0] + u];
if (!vec_cmp(a,b))
{
found = 1;
break;
}
}
if (found) break;
}
if (!found) dprintf("Bad mesh control points\n");
// Find subdivision level in v
vec_copy(a, test[0]);
vec_copy((a+numcp[0]*3), test[1]);
vec_copy(b, test[2]);
level = mesh_find_level(test);
size[1] = (LEVEL_WIDTH(level) - 1)* ((numcp[1]-1) / 2) + 1;
}
void CBsp::mesh_fill_curve_3(int numcp, int size, int stride, vec3_t *p)
{
int step, halfstep, i, mid;
vec3_t a, b;
step = (size-1) / (numcp-1);
while (step > 0)
{
halfstep = step / 2;
for (i=0; i < size-1; i += step*2)
{
mid = (i+step)*stride;
vec_avg(p[i*stride], p[mid], a);
vec_avg(p[mid], p[(i+step*2)*stride], b);
vec_avg(a, b, p[mid]);
if (halfstep > 0)
{
vec_copy(a, p[(i+halfstep)*stride]);
vec_copy(b, p[(i+3*halfstep)*stride]);
}
}
step /= 2;
}
}
void CBsp::mesh_fill_curve_2(int numcp, int size, int stride, vec2_t *p)
{
int step, halfstep, i, mid;
vec2_t a, b;
step = (size-1) / (numcp-1);
while (step > 0)
{
halfstep = step / 2;
for (i=0; i < size-1; i += step*2)
{
mid = (i+step)*stride;
vec2_avg(p[i*stride], p[mid], a);
vec2_avg(p[mid], p[(i+step*2)*stride], b);
vec2_avg(a, b, p[mid]);
if (halfstep > 0)
{
vec2_copy(a, p[(i+halfstep)*stride]);
vec2_copy(b, p[(i+3*halfstep)*stride]);
}
}
step /= 2;
}
}
void CBsp::mesh_fill_curve_c(int numcp, int size, int stride, colour_t *p)
{
int step, halfstep, i, mid;
colour_t a, b;
step = (size-1) / (numcp-1);
while (step > 0)
{
halfstep = step / 2;
for (i=0; i < size-1; i += step*2)
{
mid = (i+step)*stride;
colour_avg(p[i*stride], p[mid], a);
colour_avg(p[mid], p[(i+step*2)*stride], b);
colour_avg(a, b, p[mid]);
if (halfstep > 0)
{
colour_copy(a, p[(i+halfstep)*stride]);
colour_copy(b, p[(i+3*halfstep)*stride]);
}
}
step /= 2;
}
}
void CBsp::mesh_fill_patch_3(int *numcp, int *size, vec3_t *p)
{
int step, u, v;
// Fill in control points in v direction
step = (size[0]-1) / (numcp[0]-1);
for (u = 0; u < size[0]; u += step)
{
mesh_fill_curve_3(numcp[1], size[1], size[0], p + u);
}
// Fill in the rest in the u direction
for (v = 0; v < size[1]; v++)
{
mesh_fill_curve_3(numcp[0], size[0], 1, p + v * size[0]);
}
}
void CBsp::mesh_fill_patch_2(int *numcp, int *size, vec2_t *p)
{
int step, u, v;
// Fill in control points in v direction
step = (size[0]-1) / (numcp[0]-1);
for (u = 0; u < size[0]; u += step)
{
mesh_fill_curve_2(numcp[1], size[1], size[0], p + u);
}
// Fill in the rest in the u direction
for (v = 0; v < size[1]; v++)
{
mesh_fill_curve_2(numcp[0], size[0], 1, p + v * size[0]);
}
}
void CBsp::mesh_fill_patch_c(int *numcp, int *size, colour_t *p)
{
int step, u, v;
// Fill in control points in v direction
step = (size[0]-1) / (numcp[0]-1);
for (u = 0; u < size[0]; u += step)
{
mesh_fill_curve_c(numcp[1], size[1], size[0], p + u);
}
// Fill in the rest in the u direction
for (v = 0; v < size[1]; v++)
{
mesh_fill_curve_c(numcp[0], size[0], 1, p + v * size[0]);
}
}
Boolean CBsp::mesh_create(face_t *face, mesh_t *mesh)
{
int step[2], size[2], len, i, u, v, p;
vec3_t *cp;
vertex_t *vert;
cp = (vec3_t*)CMemoryTracker::safeAlloc(face->numverts, sizeof(vec3_t), "mesh vertex indices");
if (!cp) goto fail;
vert = &r_verts[face->firstvert];
for (i=0; i < face->numverts; i++)
{
vec_copy(vert->v_point, cp[i]);
vert++;
}
// Find the degree of subdivision in the u and v directions
mesh_find_size(face->mesh_cp, cp, size);
CMemoryTracker::safeFree(cp);
// Allocate space for mesh
len = size[0] * size[1];
mesh->size[0] = size[0];
mesh->size[1] = size[1];
mesh->points = (vec3_t*)CMemoryTracker::safeAlloc(len, (sizeof(vec3_t) + 2 * sizeof(texcoord_t) + sizeof(colour_t)), "mesh vertices");
if (!mesh->points) goto fail;
mesh->colour = (colour_t*)(mesh->points + len);
mesh->tex_st = (texcoord_t*)(mesh->colour + len);
mesh->lm_st = mesh->tex_st + len;
// Fill in sparse mesh control points
step[0] = (size[0]-1) / (face->mesh_cp[0]-1);
step[1] = (size[1]-1) / (face->mesh_cp[1]-1);
vert = &r_verts[face->firstvert];
for (v = 0; v < size[1]; v += step[1])
{
for (u = 0; u < size[0]; u += step[0])
{
p = v * size[0] + u;
vec_copy(vert->v_point, mesh->points[p]);
colour_copy(vert->colour, mesh->colour[p]);
vec2_copy(vert->tex_st, mesh->tex_st[p]);
vec2_copy(vert->lm_st, mesh->lm_st[p]);
vert++;
}
}
// Fill in each mesh
mesh_fill_patch_3(face->mesh_cp, size, mesh->points);
mesh_fill_patch_c(face->mesh_cp, size, mesh->colour);
mesh_fill_patch_2(face->mesh_cp, size, (vec2_t*)mesh->tex_st);
mesh_fill_patch_2(face->mesh_cp, size, (vec2_t*)mesh->lm_st);
// Allocate and fill element table
mesh->numelems = (size[0]-1) * (size[1]-1) * 6;
mesh->elems = (UInt32*)CMemoryTracker::safeAlloc(mesh->numelems, sizeof(UInt32), "mesh->elems");
if (!mesh->elems) goto fail;
i = 0;
for (v = 0; v < size[1]-1; ++v)
{
for (u = 0; u < size[0]-1; ++u)
{
mesh->elems[i++] = v * size[0] + u;
mesh->elems[i++] = (v+1) * size[0] + u;
mesh->elems[i++] = v * size[0] + u + 1;
mesh->elems[i++] = v * size[0] + u + 1;
mesh->elems[i++] = (v+1) * size[0] + u;
mesh->elems[i++] = (v+1) * size[0] + u + 1;
}
}
return true;
fail:
return false;
}
#pragma mark -
// skybox
#define SIDE_SIZE 9
#define POINTS_LEN (SIDE_SIZE*SIDE_SIZE)
#define ELEM_LEN ((SIDE_SIZE-1)*(SIDE_SIZE-1)*6)
#define SPHERE_RAD 10.0
#define EYE_RAD 9.0
#define SCALE_S 4.0 // Arbitrary (?) texture scaling factors
#define SCALE_T 4.0
Boolean CBsp::skybox_create(void)
{
int i;
// Alloc space for skybox verts, etc.
r_skybox = (skybox_t*)CMemoryTracker::safeAlloc(1, sizeof(skybox_t), "r_skybox");
if (!r_skybox) goto fail;
r_skybox->points[0] = 0;
r_skybox->tex_st[0] = 0;
r_skybox->elems = 0;
r_skybox->points[0] = (vec3_t*)CMemoryTracker::safeAlloc(5 * POINTS_LEN, sizeof(vec3_t), "r_skybox->points");
if (!r_skybox->points[0]) goto fail;
r_skybox->tex_st[0] = (texcoord_t*)CMemoryTracker::safeAlloc(5 * POINTS_LEN, sizeof(texcoord_t), "r_skybox->tex_st");
if (!r_skybox->tex_st[0]) goto fail;
r_skybox->elems = (UInt32*)CMemoryTracker::safeAlloc(ELEM_LEN, sizeof(UInt32), "r_skybox->elems");
if (!r_skybox->elems) goto fail;
r_skybox->numpoints = POINTS_LEN;
r_skybox->numelems = ELEM_LEN;
for (i=1; i < 5; i++)
{
r_skybox->points[i] = r_skybox->points[i-1] + POINTS_LEN;
r_skybox->tex_st[i] = r_skybox->tex_st[i-1] + POINTS_LEN;
}
gen_box();
gen_elems();
return true;
fail:
return false;
}
void CBsp::skybox_free(void)
{
if (r_skybox) {
CMemoryTracker::safeFree(r_skybox->points[0]);
CMemoryTracker::safeFree(r_skybox->tex_st[0]);
CMemoryTracker::safeFree(r_skybox->elems);
CMemoryTracker::safeFree(r_skybox);
}
}
void CBsp::gen_elems(void)
{
int u, v;
UInt32 *e;
// Box elems in tristrip order
e = r_skybox->elems;
for (v = 0; v < SIDE_SIZE-1; ++v)
{
for (u = 0; u < SIDE_SIZE-1; ++u)
{
*e++ = v * SIDE_SIZE + u;
*e++ = (v+1) * SIDE_SIZE + u;
*e++ = v * SIDE_SIZE + u + 1;
*e++ = v * SIDE_SIZE + u + 1;
*e++ = (v+1) * SIDE_SIZE + u;
*e++ = (v+1) * SIDE_SIZE + u + 1;
}
}
}
void CBsp::gen_box(void)
{
vec3_t orig, drow, dcol;
float size = 1.0f;
float step = 0.25f;
// Top
orig[0] = -size;
orig[1] = size;
orig[2] = size;
drow[0] = 0.0;
drow[1] = -step;
drow[2] = 0.0;
dcol[0] = step;
dcol[1] = 0.0;
dcol[2] = 0.0;
gen_box_side(SKYBOX_TOP, orig, drow, dcol);
// Front
orig[0] = size;
orig[1] = size;
orig[2] = size;
drow[0] = 0.0;
drow[1] = 0.0;
drow[2] = -step;
dcol[0] = -step;
dcol[1] = 0.0;
dcol[2] = 0.0;
gen_box_side(SKYBOX_FRONT, orig, drow, dcol);
// Right
orig[0] = size;
orig[1] = -size;
orig[2] = size;
drow[0] = 0.0;
drow[1] = 0.0;
drow[2] = -step;
dcol[0] = 0.0;
dcol[1] = step;
dcol[2] = 0.0;
gen_box_side(SKYBOX_RIGHT, orig, drow, dcol);
// Back
orig[0] = -size;
orig[1] = -size;
orig[2] = size;
drow[0] = 0.0;
drow[1] = 0.0;
drow[2] = -step;
dcol[0] = step;
dcol[1] = 0.0;
dcol[2] = 0.0;
gen_box_side(SKYBOX_BACK, orig, drow, dcol);
// Left
orig[0] = -size;
orig[1] = size;
orig[2] = size;
drow[0] = 0.0;
drow[1] = 0.0;
drow[2] = -step;
dcol[0] = 0.0;
dcol[1] = -step;
dcol[2] = 0.0;
gen_box_side(SKYBOX_LEFT, orig, drow, dcol);
}
void CBsp::gen_box_side(int side, vec3_t orig, vec3_t drow, vec3_t dcol)
{
vec3_t pos, w, row, *v;
texcoord_t *tc;
float p;
int r, c;
float d, b, t;
// * I don't know exactly what Q3A does for skybox texturing, but this is
// * at least fairly close. We tile the texture onto the inside of
// * a large sphere, and put the camera near the top of the sphere.
// * We place the box around the camera, and cast rays through the
// * box verts to the sphere to find the texture coordinates.
d = EYE_RAD; // Sphere center to camera distance
b = SPHERE_RAD; // Sphere radius
v = &r_skybox->points[side][0];
tc = &r_skybox->tex_st[side][0];
vec_copy(orig, row);
for (r = 0; r < SIDE_SIZE; ++r)
{
vec_copy(row, pos);
for (c = 0; c < SIDE_SIZE; ++c)
{
// pos points from eye to vertex on box
vec_copy(pos, (*v));
vec_copy(pos, w);
// Normalize pos -> w
p = sqrt(vec_dot(w, w));
w[0] /= p;
w[1] /= p;
w[2] /= p;