-
Notifications
You must be signed in to change notification settings - Fork 96
/
3DWorld.h
executable file
·1438 lines (1228 loc) · 58.4 KB
/
3DWorld.h
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
// 3D World - OpenGL CS184 Computer Graphics Project
// by Frank Gennari
// 3/10/02
#pragma once
// timer_t is used in types.h on linux, and also in 3DWorld, so we have to typedef it as something else for these includes
#define timer_t stdlib_timer_t
#include "globals.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "gl_includes.h"
#include "rand_gen.h"
// STL include (others come from rand_gen.h)
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#undef timer_t
using std::vector;
using std::deque;
using std::set;
using std::map;
using std::swap;
using std::pair;
using std::make_pair;
using std::cout;
using std::endl;
using std::min;
using std::max;
#ifndef PI
#define PI 3.141592654f
#endif
int const CAMERA_ID = -1;
int const NO_SOURCE = -2;
unsigned const INIT_CCELL_SIZE = 16;
float const LARGE_OBJ_RAD = 0.01;
float const TOLERANCE = 1.0E-12;
float const ABSOLUTE_ZERO = -273; // in degrees C
float const MAX_SPLASH_DEPTH = 0.1;
float const WATER_INDEX_REFRACT = 1.333;
float const ICE_INDEX_REFRACT = 1.309;
float const WATER_COL_ATTEN = 0.6;
float const MESH_BOT_QUAD_DZ = 0.050; // measured from zbottom
float const MIN_WATER_DZ = 0.045;
float const MESH_LOWEST_DZ = 0.040;
unsigned const TICKS_PER_SECOND = 40;
float const SMALL_NUMBER = 0.001;
float const MIN_POLY_THICK = 0.001;
unsigned const MAX_CHARS = 256;
float const LIGHT_ROT_AMT = 0.05;
float const WIND_ADJUST = 0.2;
float const DEF_TIMESTEP = 0.007;
int const TIMESCALE = 1; // (integer) larger makes surface movement faster
int const MAX_I_TIMESCALE = 8; // (integer) larger makes surface movement slower (max inverse of TIMESCALE)
float const GRAVITY = 300.0;
float const CLOUD_CEILING0 = 1.5;
int const LITNING_TIME = 50; // in ticks
float const MEL_EVAP_RATIO = 0.75;
float const LITNING_LINEAR_I = 1.0;
float const STICK_THRESHOLD = 1.0;
float const COLL_DAMAGE = 0.75;
float const WATER_DAMAGE = 5.0;
float const WATER_FRAME_DAM = 1.0;
float const TEMP_INCREMENT = 10.0;
float const RAIN_MIN_TEMP = 2.0; // degrees C
float const SNOW_MAX_TEMP = -2.0;
float const WATER_MAX_TEMP = 100.0; // boiling point
float const DEF_TEMPERATURE = 20.0;
float const SNOW_ACC = 10.0;
float const W_FREEZE_POINT = -0.1;
float const WATER_DENSITY = 1.0;
float const SPLASH_BASE_SZ = 0.01;
float const DEF_AMBIENT = 0.5;
float const DEF_DIFFUSE = 0.9;
float const RAIN_TAIL_MIN_V = -1.0;
float const WATER_ALPHA = 0.75;
float const ICE_ALPHA = 0.88;
float const SPLASH_ALPHA = 1.0;
float const SNOW_ALPHA = 1.0;
int const N_RAND_GAUSS = 10;
int const N_STAR_POINTS = 5;
int const N_CYL_SIDES = 32;
int const N_SPHERE_DIV = 32;
int const N_COLL_POLY_PTS = 4;
int const MAX_SPLASH_DROP = 100;
int const SMILEY_NCHUNKS = 25;
int const NUM_CHUNK_BLOCKS = 4;
float const GROUND_SPEED = 0.0175;
float const SIDESTEP_SPEED = 0.8;
float const BACKWARD_SPEED = 0.85;
float const DEF_NEAR_CLIP = 0.01;
float const DEF_FAR_CLIP = 100.0;
float const FAR_DISTANCE = 100.0;
float const PERSP_ANGLE = 60.0;
float const ZOOM_FACTOR = 5.0;
float const UNIV_NCLIP_SCALE = 0.02;
float const MIN_SHADOW_ALPHA = 0.5;
float const GET_OCC_EXPAND = 0.02;
float const DEF_Z_BIAS = 0.0005;
unsigned const CELL_Z_DIVS = 8;
unsigned const SMALL_NDIV = 8;
int const FAST_VISIBILITY_CALC = 1;
float const TWO_PI = 2.0*PI;
float const PI_TWO = PI/2.0;
float const PI_INV = 1.0/PI;
float const SQRT2 = sqrt(2.0);
float const SQRT3 = sqrt(3.0);
float const TWO_SQRT2 = 2.0*SQRT2;
float const SQRTOFTWOINV = 1.0/SQRT2;
float const TO_DEG = 180.0/PI;
float const TO_RADIANS = PI/180.0;
float const SQRT_ZOOMF = sqrt(ZOOM_FACTOR);
float const SQRT_ZOOMF_INV = 1.0/SQRT_ZOOMF;
unsigned const MAX_SHADER_LIGHTS = 8;
unsigned const quad_to_tris_ixs [6] = {0,1,2, 0,2,3};
unsigned const cube_dim_table[2][3] = {{1, 2, 0}, {2, 0, 1}};
#define CLIP_TO_01(x) max( 0.0f, min(1.0f, (x)))
#define CLIP_TO_pm1(x) max(-1.0f, min(1.0f, (x)))
#define BITSHIFT_CEIL(num, bs) (((num-1) >> bs) + 1)
#define UNROLL_2X(expr) {{unsigned const i_(0); expr} {unsigned const i_(1); expr}}
#define UNROLL_3X(expr) {UNROLL_2X(expr) {unsigned const i_(2); expr}}
#define UNROLL_4X(expr) {UNROLL_3X(expr) {unsigned const i_(3); expr}}
template<typename T> inline void min_eq(T &A, T const B) {A = min(A, B);}
template<typename T> inline void max_eq(T &A, T const B) {A = max(A, B);}
enum {CAM_FILT_DAMAGE=0, CAM_FILT_FOG, CAM_FILT_BURN, CAMERA_FILT_BKG, CAM_FILT_UWATER, CAM_FILT_TELEPORT, CAM_FILT_FROZEN, CAM_FILT_END};
enum {FG_PROJECTION=0, FG_MODELVIEW};
enum {GAME_MODE_NONE=0, GAME_MODE_FPS, GAME_MODE_DODGEBALL};
unsigned const GAME_MODE_BUILDINGS = 2; // same as GAME_MODE_DODGEBALL
template<typename T> struct point2d { // size = 8
T x, y;
point2d() : x(0.0), y(0.0) {}
point2d(T x_, T y_) : x(x_), y(y_) {}
point2d(point2d const &a, point2d const &b) : x(a.x - b.x), y(a.y - b.y) {}
void assign(T x_, T y_) {x = x_; y = y_;}
bool operator==(point2d const &p) const {return (x == p.x && y == p.y);}
bool operator!=(point2d const &p) const {return (x != p.x || y != p.y);}
T mag_sq() const {return (x*x + y*y);}
T mag() const {return sqrt(mag_sq());}
T cp_mag(point2d const &p) const {return (x*p.y - y*p.x);}
T get_max_val() const {return max(x, y);}
T get_min_val() const {return min(x, y);}
void operator+=(point2d const &p) {x += p.x; y += p.y;}
void operator-=(point2d const &p) {x -= p.x; y -= p.y;}
void operator*=(point2d const &p) {x *= p.x; y *= p.y;} // component multiply
void operator+=(T const &v) {x += v; y += v;}
void operator-=(T const &v) {x -= v; y -= v;}
void operator*=(T m) {x *= m; y *= m;}
point2d operator+(point2d const &p) const {return point2d((x+p.x), (y+p.y));}
point2d operator-(point2d const &p) const {return point2d((x-p.x), (y-p.y));}
point2d operator+(T const &v) const {return point2d((x+v), (y+v));}
point2d operator-(T const &v) const {return point2d((x-v), (y-v));}
point2d operator*(T const val) const {return point2d(x*val, y*val);}
point2d operator*(point2d const &p) const {return point2d(x*p.x, y*p.y);} // component multiply
point2d operator-() const {return point2d(-x, -y);}
const T &operator[](unsigned i) const {
switch(i) {
case 0: return x;
case 1: return y;
default: assert(0);
}
return x; // never gets here
}
T &operator[](unsigned i) {
switch(i) {
case 0: return x;
case 1: return y;
default: assert(0);
}
return x; // never gets here
}
void negate() {x = -x; y = -y;}
void normalize() {
T const d(mag());
if (d >= TOLERANCE) {x /= d; y /= d;}
}
point2d get_norm() const {
T const vmag(mag());
return ((vmag < TOLERANCE) ? *this : point2d(x/vmag, y/vmag));
}
};
typedef point2d<float> vector2d;
template<typename T> struct pointT { // size = 12 (float), 24(double)
typedef T value_type;
T x, y, z;
pointT() : x(0.0), y(0.0), z(0.0) {}
//pointT(T v) : x(v), y(v), z(v) {} // unsafe?
pointT(T x_, T y_, T z_) : x(x_), y(y_), z(z_) {}
pointT(pointT const &p1, pointT const &p2) : x(p1.x-p2.x), y(p1.y-p2.y), z(p1.z-p2.z) {} // take the difference (vector)
template<typename S> pointT(pointT<S> const &p) : x(p.x), y(p.y), z(p.z) {}
std::string str() const {std::ostringstream oss; oss << x << ", " << y << ", " << z; return oss.str();}
std::string raw_str() const {std::ostringstream oss; oss << x << " " << y << " " << z; return oss.str();}
template<typename S> void operator=(pointT<S> const &p) {x = p.x; y = p.y; z = p.z;}
bool operator==(const pointT &p) const {return (p.x == x && p.y == y && p.z == z);}
bool operator!=(const pointT &p) const {return !operator==(p);}
const T &operator[](unsigned i) const {
switch(i) {
case 0: return x;
case 1: return y;
case 2: return z;
default: assert(0);
}
return x; // never gets here
}
T &operator[](unsigned i) {
switch(i) {
case 0: return x;
case 1: return y;
case 2: return z;
default: assert(0);
}
return x; // never gets here
}
void assign(T x_, T y_, T z_) {x = x_; y = y_; z = z_;}
void operator+=(pointT const &p) {x += p.x; y += p.y; z += p.z;}
void operator-=(pointT const &p) {x -= p.x; y -= p.y; z -= p.z;}
void operator*=(pointT const &p) {x *= p.x; y *= p.y; z *= p.z;} // component multiply
void operator+=(T const &v) {x += v; y += v; z += v;}
void operator-=(T const &v) {x -= v; y -= v; z -= v;}
void operator*=(T m) {x *= m; y *= m; z *= m;}
void operator/=(T d) {
assert(d != 0.0);
T const m(1.0/d);
x *= m; y *= m; z *= m;
}
void normalize() {
T const d(mag());
if (d >= TOLERANCE) {
T const m(1.0/d);
x *= m; y *= m; z *= m;
}
}
bool normalize_test() {
T const d(mag());
if (d < TOLERANCE) return 0;
T const m(1.0/d);
x *= m; y *= m; z *= m;
return 1;
}
void negate() {x = -x; y = -y; z = -z;}
void invert() {
if (x == 0.0) {x = TOLERANCE;}
if (y == 0.0) {y = TOLERANCE;}
if (z == 0.0) {z = TOLERANCE;}
x = 1.0/x; y = 1.0/y; z = 1.0/z;
}
pointT inverse() const {return pointT(1.0/x, 1.0/y, 1.0/z);} // no divide by zero check
pointT get_norm() const {
T const vmag(mag());
return ((vmag < TOLERANCE) ? *this : pointT(x/vmag, y/vmag, z/vmag));
}
void set_max_mag(T vmax) {
T const vmag(mag());
if (vmag > TOLERANCE && vmag > vmax) operator*=(vmax/vmag);
}
T sum() const {return (x + y + z);}
pointT operator+(pointT const &p) const {return pointT((x+p.x), (y+p.y), (z+p.z));}
pointT operator-(pointT const &p) const {return pointT((x-p.x), (y-p.y), (z-p.z));}
pointT operator+(T const &v) const {return pointT((x+v), (y+v), (z+v));}
pointT operator-(T const &v) const {return pointT((x-v), (y-v), (z-v));}
pointT operator*(T const val) const {return pointT(x*val, y*val, z*val);}
pointT operator*(pointT const &p) const {return pointT(x*p.x, y*p.y, z*p.z);} // component multiply
pointT operator/(pointT const &p) const {return pointT(x/p.x, y/p.y, z/p.z);} // component division
pointT operator-() const {return pointT(-x, -y, -z);}
pointT operator/(T const val) const {
assert(val != 0.0);
T const val_inv(1.0/val);
return pointT(x*val_inv, y*val_inv, z*val_inv);
}
float dot (pointT const &v) const {return (x*v.x + y*v.y + z*v.z);}
pointT cross(pointT const &v) const {return pointT((y*v.z - z*v.y), (z*v.x - x*v.z), (x*v.y - y*v.x));}
pointT min (pointT const &v) const {return pointT(std::min(x, v.x), std::min(y, v.y), std::min(z, v.z));}
pointT max (pointT const &v) const {return pointT(std::max(x, v.x), std::max(y, v.y), std::max(z, v.z));}
T mag_sq() const {return (x*x + y*y + z*z);}
T mag() const {return sqrt(mag_sq());}
T xy_mag_sq() const {return (x*x + y*y);}
T xy_mag() const {return sqrt(xy_mag_sq());}
T get_min_val() const {return std::min(x, std::min(y, z));}
T get_max_val() const {return std::max(x, std::max(y, z));}
bool is_nonzero() const {return (x != 0.0 || y != 0.0 || z != 0.0);}
bool operator<(pointT const &p) const { // needed for maps and stuff
if (z > p.z) return 1;
if (z < p.z) return 0; // greater than operation?
if (y < p.y) return 1;
if (y > p.y) return 0;
return (x < p.x);
}
};
// premultiply a pointT by a scalar
template<typename S, typename T> pointT<T> inline operator*(S const v, pointT<T> const &p) {return pointT<T>(v*p.x, v*p.y, v*p.z);}
typedef pointT<float> point;
typedef pointT<float> vector3d;
typedef pointT<double> point_d;
typedef pointT<double> vector3d_d;
typedef point_d upos_point_type;
// constants
point const all_zeros(0, 0, 0);
vector3d const plus_x(1, 0, 0);
vector3d const plus_y(0, 1, 0);
vector3d const plus_z(0, 0, 1);
vector3d const zero_vector(0, 0, 0);
vector3d const all_ones(1, 1, 1);
template<typename T> uint32_t jenkins_one_at_a_time_hash(const T* key, size_t length) { // T is an unsigned integer type
size_t i = 0;
uint32_t hash = 0;
while (i != length) {hash += key[i++]; hash += hash << 10; hash ^= hash >> 6;}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}
template<typename T> struct hash_by_bytes { // should work with all packed vertex types
uint32_t operator()(T const &v) const {return jenkins_one_at_a_time_hash((const uint8_t*)&v, sizeof(T));} // slower but better quality hash
//uint32_t operator()(T const &v) const {return jenkins_one_at_a_time_hash((const uint32_t*)&v, sizeof(T)>>2);} // faster but lower quality hash
};
inline unsigned hash_point(point const &p) {return hash_by_bytes<point>()(p);}
inline void hash_mix_point(point const &p, unsigned &hv) {
hv += hash_point(p);
hv += hv << 10;
hv ^= hv >> 6;
}
template<typename T> unsigned hash_vect_as_int(vector<T> const &v) {
assert((sizeof(T) % sizeof(int)) == 0); // must be a multiple of 4 bytes
return jenkins_one_at_a_time_hash((int const*)v.data(), sizeof(T)*v.size()/sizeof(int));
}
struct vector4d : public vector3d { // size = 16
float w;
vector4d() : w(0.0) {}
vector4d(float x_, float y_, float z_, float w_) : vector3d(x_, y_, z_), w(w_) {}
vector4d(vector3d const &v, float w_) : vector3d(v), w(w_) {}
void assign(float x_, float y_, float z_, float w_) {x = x_; y = y_; z = z_; w = w_;}
std::string str() const {std::ostringstream oss; oss << x << ", " << y << ", " << z << ", " << w; return oss.str();}
vector4d operator+ (vector4d const &p) const {return vector4d((x+p.x), (y+p.y), (z+p.z), (w+p.w));}
vector4d operator- (vector4d const &p) const {return vector4d((x-p.x), (y-p.y), (z-p.z), (w-p.w));}
void operator+=(vector4d const &p) {x += p.x; y += p.y; z += p.z; w += p.w;}
void operator-=(vector4d const &p) {x -= p.x; y -= p.y; z -= p.z; w -= p.w;}
vector4d operator-() const {return vector4d(-x, -y, -z, -w);}
bool operator==(vector4d const &v) const {return (v.x == x && v.y == y && v.z == z && v.w == w);}
bool operator!=(vector4d const &v) const {return !operator==(v);}
bool operator< (vector4d const &p) const {return ((w == p.w) ? vector3d::operator<(p) : (w < p.w));}
};
struct sphere_t {
point pos;
float radius;
sphere_t(point const &p=all_zeros, float r=0.0) : pos(p), radius(r) {}
bool operator==(sphere_t const &s) const {return (pos == s.pos && radius == s.radius);}
bool operator!=(sphere_t const &s) const {return (pos != s.pos || radius != s.radius);}
point const &get_pos() const {return pos;}
float get_radius() const {return radius;}
float get_volume() const {return (4.0/3.0)*PI*radius*radius*radius;}
float get_surf_area() const {return 4.0*PI*radius*radius;}
bool contains_point(point const &p) const;
};
struct cube_t { // size = 24; Note: AABB, not actually a cube
float d[3][2]; // {x,y,z},{min,max}
cube_t() {set_to_zeros();}
cube_t(float x1_, float x2_, float y1_, float y2_, float z1_, float z2_) {
x1() = x1_; x2() = x2_; y1() = y1_; y2() = y2_; z1() = z1_; z2() = z2_;
}
cube_t(point const &p1, point const &p2) {UNROLL_3X(d[i_][0] = min(p1[i_], p2[i_]); d[i_][1] = max(p1[i_], p2[i_]);)}
cube_t(point const &pt) {set_from_point(pt);}
cube_t(point const *const pts, unsigned npts) {set_from_points(pts, npts);}
void set_to_zeros() {set_from_point(all_zeros);}
void copy_from(cube_t const &c) {
UNROLL_3X(d[i_][0] = c.d[i_][0]; d[i_][1] = c.d[i_][1];)
}
void set_from_point(point const &pt) {UNROLL_3X(d[i_][0] = d[i_][1] = pt[i_];)}
void set_from_sphere(point const &pt, float radius) {
UNROLL_3X(d[i_][0] = pt[i_]-radius; d[i_][1] = pt[i_]+radius;)
}
void set_from_sphere(sphere_t const &s) {set_from_sphere(s.pos, s.radius);}
bool operator==(cube_t const &c) const {
UNROLL_3X(if (d[i_][0] != c.d[i_][0]) return 0;)
UNROLL_3X(if (d[i_][1] != c.d[i_][1]) return 0;)
return 1;
}
bool operator<(cube_t const &c) const { // for use in sorting/uniquing
UNROLL_3X(if (d[i_][0] != c.d[i_][0]) return (d[i_][0] < c.d[i_][0]);)
UNROLL_3X(if (d[i_][1] != c.d[i_][1]) return (d[i_][1] < c.d[i_][1]);)
return 0;
}
float x1() const {return d[0][0];}
float x2() const {return d[0][1];}
float y1() const {return d[1][0];}
float y2() const {return d[1][1];}
float z1() const {return d[2][0];}
float z2() const {return d[2][1];}
float &x1() {return d[0][0];}
float &x2() {return d[0][1];}
float &y1() {return d[1][0];}
float &y2() {return d[1][1];}
float &z1() {return d[2][0];}
float &z2() {return d[2][1];}
float xc() const {return 0.5f*(x1() + x2());}
float yc() const {return 0.5f*(y1() + y2());}
float zc() const {return 0.5f*(z1() + z2());}
float dx() const {return (x2() - x1());}
float dy() const {return (y2() - y1());}
float dz() const {return (z2() - z1());}
bool operator!=(cube_t const &c) const {return !operator==(c);}
cube_t operator+ (vector3d const &p) const {cube_t c(*this); c += p; return c;}
cube_t operator- (vector3d const &p) const {cube_t c(*this); c -= p; return c;}
cube_t operator* (vector3d const &p) const {cube_t c(*this); c *= p; return c;}
cube_t operator* (float scale ) const {cube_t c(*this); c *= scale; return c;}
void operator+=(vector3d const &p) {translate( p);}
void operator-=(vector3d const &p) {translate(-p);}
void operator*=(vector3d const &p) {UNROLL_3X(d[i_][0] *= p[i_]; d[i_][1] *= p[i_];)}
void operator*=(float scale ) {UNROLL_3X(d[i_][0] *= scale; d[i_][1] *= scale;)}
void translate(point const &p) {UNROLL_3X(d[i_][0] += p[i_]; d[i_][1] += p[i_];)}
void translate_dim(unsigned dim, float v) {assert(dim < 3); d[dim][0] += v; d[dim][1] += v;}
void swap_dims(unsigned d1, unsigned d2) {assert(d1 < 3 && d2 < 3); swap(d[d1][0], d[d2][0]); swap(d[d1][1], d[d2][1]);}
void set_from_points(point const *const pts, unsigned npts);
void set_from_points(vector<point> const &pts) {set_from_points(pts.data(), pts.size());}
std::string str() const;
std::string raw_str() const;
bool is_near_zero_area() const;
bool is_all_zeros() const {return (x1() == 0 && x2() == 0 && y1() == 0 && y2() == 0 && z1() == 0 && z2() == 0);}
void union_with_pt(point const &pt) {
UNROLL_3X(min_eq(d[i_][0], pt[i_]); max_eq(d[i_][1], pt[i_]);)
}
void assign_or_union_with_pt(point const &pt) {
if (is_all_zeros()) {set_from_point(pt);} else {union_with_pt(pt);} // Note: won't work if pt == (0,0,0)
}
void assign_or_union_with_sphere(point const &pt, float radius) {
if (is_zero_area()) {set_from_sphere(pt, radius);} else {union_with_sphere(pt, radius);} // Note: won't work if pt == (0,0,0)
}
void union_with_sphere(point const &pt, float radius) {
UNROLL_3X(min_eq(d[i_][0], pt[i_]-radius); max_eq(d[i_][1], pt[i_]+radius);)
}
void union_with_sphere(sphere_t const &s) {union_with_sphere(s.pos, s.radius);}
void union_with_cube(cube_t const &c) {
UNROLL_3X(min_eq(d[i_][0], c.d[i_][0]); max_eq(d[i_][1], c.d[i_][1]);)
}
void union_with_cube_xy(cube_t const &c) {
UNROLL_2X(min_eq(d[i_][0], c.d[i_][0]); max_eq(d[i_][1], c.d[i_][1]);)
}
void assign_or_union_with_cube(cube_t const &c) {
if (c.is_zero_area()) return;
if (is_zero_area()) {copy_from(c);} else {union_with_cube(c);}
}
void intersect_with_cube(cube_t const &c) { // Note: cube and *this must overlap
UNROLL_3X(max_eq(d[i_][0], c.d[i_][0]); min_eq(d[i_][1], c.d[i_][1]);)
}
void intersect_with_cube_xy(cube_t const &c) { // Note: cube and *this must overlap
UNROLL_2X(max_eq(d[i_][0], c.d[i_][0]); min_eq(d[i_][1], c.d[i_][1]);)
}
void normalize() {UNROLL_3X(if (d[i_][1] < d[i_][0]) swap(d[i_][0], d[i_][1]);)}
bool is_zero_area() const {
UNROLL_3X(if (d[i_][0] == d[i_][1]) return 1;)
return 0;
}
bool is_normalized() const {
UNROLL_3X(if (d[i_][0] > d[i_][1]) return 0;)
return 1;
}
bool is_strictly_normalized() const {
UNROLL_3X(if (d[i_][0] >= d[i_][1]) return 0;)
return 1;
}
bool intersects(const cube_t &cube) const { // includes adjacency
UNROLL_3X(if (cube.d[i_][1] < d[i_][0] || cube.d[i_][0] > d[i_][1]) return 0;)
return 1;
}
bool intersects_no_adj(const cube_t &cube) const { // excludes adjacency
UNROLL_3X(if (cube.d[i_][1] <= d[i_][0] || cube.d[i_][0] >= d[i_][1]) return 0;)
return 1;
}
bool intersects_xy(const cube_t &cube) const {
UNROLL_2X(if (cube.d[i_][1] < d[i_][0] || cube.d[i_][0] > d[i_][1]) return 0;)
return 1;
}
bool intersects_xy_no_adj(const cube_t &cube) const { // excludes adjacency
UNROLL_2X(if (cube.d[i_][1] <= d[i_][0] || cube.d[i_][0] >= d[i_][1]) return 0;)
return 1;
}
bool intersects(const cube_t &cube, float toler) const { // Note: toler > 0 makes adjacent cubes *not* intersect
UNROLL_3X(if (cube.d[i_][1] < (d[i_][0] + toler) || cube.d[i_][0] > (d[i_][1] - toler)) return 0;)
return 1;
}
bool contains_cube(const cube_t &cube) const {
UNROLL_3X(if (cube.d[i_][0] < d[i_][0] || cube.d[i_][1] > d[i_][1]) return 0;)
return 1;
}
bool contains_cube_xy(const cube_t &cube) const {
UNROLL_2X(if (cube.d[i_][0] < d[i_][0] || cube.d[i_][1] > d[i_][1]) return 0;)
return 1;
}
bool contains_cube_xy_exp(const cube_t &cube, float exp) const {
UNROLL_2X(if (cube.d[i_][0]-exp < d[i_][0] || cube.d[i_][1]+exp > d[i_][1]) return 0;)
return 1;
}
bool contains_cube_xy_no_adj(const cube_t &cube) const {
UNROLL_2X(if (cube.d[i_][0] <= d[i_][0] || cube.d[i_][1] >= d[i_][1]) return 0;)
return 1;
}
bool contains_cube_xy_overlaps_z(const cube_t &cube) const { // no adj in Z
return (contains_cube_xy(cube) && cube.z1() < z2() && cube.z2() > z1());
}
bool contains_pt(point const &pt) const { // includes points on the edge
UNROLL_3X(if (pt[i_] < d[i_][0] || pt[i_] > d[i_][1]) return 0;)
return 1;
}
bool contains_pt_xy (point const &pt) const {return (pt.x > x1() && pt.x < x2() && pt.y > y1() && pt.y < y2());} // excludes points on the edge
bool contains_pt_xy_inc_low_edge(point const &pt) const {return (pt.x >= x1() && pt.x < x2() && pt.y >= y1() && pt.y < y2());} // includes points on the lower edges
bool contains_pt_xy_inclusive (point const &pt) const {return (pt.x >= x1() && pt.x <= x2() && pt.y >= y1() && pt.y <= y2());} // includes points on the edge
bool contains_pt_xy_exp (point const &pt, float exp) const {return (pt.x > x1()-exp && pt.x < x2()+exp && pt.y > y1()-exp && pt.y < y2()+exp);}
bool contains_pt_exp (point const &pt, float exp) const {return (pt.x > x1()-exp && pt.x < x2()+exp && pt.y > y1()-exp && pt.y < y2()+exp && pt.z > z1()-exp && pt.z < z2()+exp);}
bool contains_pt_exp_xy_only (point const &pt, float exp) const {return (pt.x > x1()-exp && pt.x < x2()+exp && pt.y > y1()-exp && pt.y < y2()+exp && pt.z > z1() && pt.z < z2());}
bool quick_intersect_test(const cube_t &cube) const {
UNROLL_3X(if (cube.d[i_][0] >= d[i_][1] || cube.d[i_][1] <= d[i_][0]) return 0;)
return 1;
}
bool line_intersects(point const &p1, point const &p2) const;
void clamp_pt (point &pt) const {UNROLL_3X(pt[i_] = min(d[i_][1], max(d[i_][0], pt[i_]));)}
void clamp_pt_xy(point &pt) const {UNROLL_2X(pt[i_] = min(d[i_][1], max(d[i_][0], pt[i_]));)}
float get_volume() const {return fabs(x2() - x1())*fabs(y2() - y1())*fabs(z2() - z1());}
float get_area () const {return 2.0f*(fabs(x2() - x1())*fabs(y2() - y1()) + fabs(y2() - y1())*fabs(z2() - z1()) + fabs(z2() - z1())*fabs(x2() - x1()));}
float get_area_xy()const {return dx()*dy();}
float max_len () const {return max((x2() - x1()), max((y2() - y1()), (z2() - z1())));}
float min_len () const {return min((x2() - x1()), min((y2() - y1()), (z2() - z1())));}
float second_largest_len() const {
return min(max((x2() - x1()), (y2() - y1())), min(max((y2() - y1()), (z2() - z1())), max((z2() - z1()), (x2() - x1()))));
}
point get_cube_center() const {
return point(0.5f*(x1()+x2()), 0.5f*(y1()+y2()), 0.5f*(z1()+z2()));
}
float get_bsphere_radius() const {
return 0.5f*sqrt((x2()-x1())*(x2()-x1()) + (y2()-y1())*(y2()-y1()) + (z2()-z1())*(z2()-z1()));
}
float get_xy_bsphere_radius() const {
return 0.5f*sqrt((x2()-x1())*(x2()-x1()) + (y2()-y1())*(y2()-y1()));
}
sphere_t get_bsphere() const {return sphere_t(get_cube_center(), get_bsphere_radius());}
sphere_t get_bcylin () const {return sphere_t(get_cube_center(), get_xy_bsphere_radius());}
point get_llc() const {return point(x1(), y1(), z1());}
point get_urc() const {return point(x2(), y2(), z2());}
vector3d get_size() const {return vector3d((x2()-x1()), (y2()-y1()), (z2()-z1()));}
float get_center_dim(unsigned dim) const {assert(dim < 3); return 0.5f*(d[dim][0] + d[dim][1]);}
float get_sz_dim (unsigned dim) const {assert(dim < 3); return (d[dim][1] - d[dim][0]);}
void expand_by(float val) {UNROLL_3X(d[i_][0] -= val; d[i_][1] += val;)}
void expand_by(float x, float y, float z) {expand_by(vector3d(x, y, z));}
void expand_by(vector3d const &val) {UNROLL_3X(d[i_][0] -= val[i_]; d[i_][1] += val[i_];)}
void expand_by_xy(float val) {UNROLL_2X(d[i_][0] -= val; d[i_][1] += val;)}
void expand_by_xy(vector3d const &val) {UNROLL_2X(d[i_][0] -= val[i_]; d[i_][1] += val[i_];)}
void expand_in_dim(unsigned dim, float val) {assert(dim < 3); d[dim][0] -= val; d[dim][1] += val;}
void expand_in_x(float val) {x1() -= val; x2() += val;}
void expand_in_y(float val) {y1() -= val; y2() += val;}
void expand_in_z(float val) {z1() -= val; z2() += val;}
unsigned get_split_dim(float &max_sz, float &sval, unsigned skip_dims) const;
bool cube_intersection(const cube_t &cube, cube_t &res) const;
float get_overlap_volume(const cube_t &cube) const;
vector3d closest_side_dir(point const &pos, unsigned skip_dims=0) const;
bool closest_dist_less_than(point const &pos, float dist) const;
bool closest_dist_xy_less_than(point const &pos, float dist) const;
point closest_pt(point const &pos) const { // closest point inside this cube
point pt(pos);
clamp_pt(pt);
return pt;
}
float get_max_extent() const { // from (0,0,0)
float mextent(0.0);
UNROLL_3X(mextent = max(mextent, max(-d[i_][0], d[i_][1]));)
return mextent;
}
float get_max_dim_sz() const {return std::max(dz(), std::max(dx(), dy()));}
float furthest_dist_to_pt(point const &pos) const {
vector3d dmax;
UNROLL_3X(dmax[i_] = max((pos[i_] - d[i_][0]), (d[i_][1] - pos[i_]));)
return dmax.mag();
}
int closest_face(point const &pos) const;
bool cube_merge(cube_t const &cube);
void get_points(point pts[8]) const;
};
typedef vector<cube_t> vect_cube_t;
cube_t const all_zeros_cube(0,0,0,0,0,0);
struct cube_with_ix_t : public cube_t {
unsigned ix;
cube_with_ix_t(unsigned ix_=0) : ix(ix_) {}
cube_with_ix_t(cube_t const &c, unsigned ix_=0) : cube_t(c), ix(ix_) {}
};
typedef vector<cube_with_ix_t> vect_cube_with_ix_t;
vector3d get_poly_norm(point const *const points, bool normalize=1);
struct tquad_t { // size = 52
point pts[4];
unsigned npts;
tquad_t(unsigned npts_=0) : npts(npts_) {}
bool is_valid() const;
void update_bcube(cube_t &c) const;
cube_t get_bcube() const;
vector3d get_norm(bool normalize=1) const {return get_poly_norm(pts, normalize);}
point const &operator[](unsigned i) const {return pts[i];}
point &operator[](unsigned i) {return pts[i];}
};
struct line_3dw {
point p1, p2;
line_3dw() : p1(all_zeros), p2(all_zeros) {}
line_3dw(point const &p1_, point const &p2_) : p1(p1_), p2(p2_) {assert(p1 != p2);}
vector3d get_norm_dir_vect() const {return (p2 - p1).get_norm();}
float get_length() const {return (p1 - p2).mag();}
void translate(point const &p) {p1 += p; p2 += p;}
};
struct vector_point_norm {
vector<point> p;
vector<vector3d> n;
};
struct pos_dir_up { // defines a view frustum
point pos;
vector3d dir, upv, upv_, cp;
float angle, tterm, sterm, x_sterm, behind_sphere_mult, near_, far_;
double A; // aspect ratio x/y
bool valid;
pos_dir_up(void) : angle(0.0f), tterm(0.0f), sterm(0.0f), x_sterm(0.0f), behind_sphere_mult(0.0f), near_(0.0f), far_(0.0f), A(0.0), valid(0) {}
pos_dir_up(point const &p, vector3d const &d, vector3d const &u, float angle_, float n, float f, float a=0.0, bool no_zoom=0);
void orthogonalize_up_dir();
bool point_visible_test(point const &pos_) const;
bool line_visible_test(point const &p1, point const &p2) const;
bool sphere_visible_test(point const &pos_, float radius) const;
bool sphere_visible_test_no_inside_test(point const &pos_, float radius) const;
bool sphere_completely_visible_test(point const &pos_, float radius) const {return sphere_visible_test(pos_, -radius);}
template<unsigned N> bool pt_set_visible(point const *const pts) const;
bool cube_visible(cube_t const &c) const;
bool cube_completely_visible(cube_t const &c) const;
bool cube_visible_likely(cube_t const &c) const {return (!valid || point_visible_test(c.get_cube_center()) || cube_visible(c));}
bool cube_visible_for_light_cone(cube_t const &c) const;
bool projected_cube_visible(cube_t const &cube, point const &proj_pt) const;
bool sphere_and_cube_visible_test(point const &pos_, float radius, cube_t const &cube) const;
void get_frustum_corners(point pts[8]) const;
point get_frustum_center() const;
void draw_frustum() const;
void translate(vector3d const &tv) {pos += tv;}
void scale(float s) {pos *= s; near_ *= s; far_ *= s;}
void rotate(vector3d const &axis, float angle);
void apply_z_mirror(float zval) {apply_dim_mirror(2, zval);}
void apply_dim_mirror(unsigned dim, float val);
};
struct cylinder_3dw : public line_3dw { // size = 32
float r1, r2;
cylinder_3dw() : r1(0.0), r2(0.0) {}
cylinder_3dw(point const &p1_, point const &p2_, float r1_, float r2_) : line_3dw(p1_, p2_), r1(r1_), r2(r2_) {}
void calc_bcube(cube_t &bcube) const;
float get_volume() const {return PI*(r1*r1 + r1*r2 + r2*r2)*get_length()/3.0f;}
float get_surface_area() const;
point get_center() const {return 0.5f*(p1 + p2);}
float get_avg_radius() const {return 0.5f*(r1 + r2);}
float get_bounding_radius() const;
};
struct colorRGB { // size = 12
float R, G, B;
colorRGB() : R(0.0f), G(0.0f), B(0.0f) {}
colorRGB(float r, float g, float b) : R(r), G(g), B(b) {}
void assign(float r, float g, float b) {R = r; G = g; B = b;}
void set_to_val(float val) {R = G = B = val;}
bool operator==(const colorRGB &c) const {return (c.R == R && c.G == G && c.B == B);}
bool operator!=(const colorRGB &c) const {return !operator==(c);}
const float &operator[](unsigned i) const {
switch(i) {
case 0: return R;
case 1: return G;
case 2: return B;
default: assert(0);
}
return R; // never gets here
}
float &operator[](unsigned i) {
switch(i) {
case 0: return R;
case 1: return G;
case 2: return B;
default: assert(0);
}
return R; // never gets here
}
bool operator<(const colorRGB &c) const { // greater than operation?
if (R < c.R) return 1;
if (R > c.R) return 0;
if (G < c.G) return 1;
if (G > c.G) return 0;
return (B < c.B);
}
colorRGB operator+ (colorRGB const &c) const {return colorRGB(R+c.R, G+c.G, B+c.B);}
void operator+=(colorRGB const &c) {R += c.R; G += c.G; B += c.B;}
colorRGB operator*(float val) const {return colorRGB(R*val, G*val, B*val);}
void operator*=(float val) {R *= val; G *= val; B *= val;}
colorRGB modulate_with(colorRGB const &c) const {return colorRGB(R*c.R, G*c.G, B*c.B);}
void set_valid_color() {
R = CLIP_TO_01(R);
G = CLIP_TO_01(G);
B = CLIP_TO_01(B);
}
void from_normal(vector3d const &n) { // for normal maps
R = 0.5*(n.x + 1.0);
G = 0.5*(n.y + 1.0);
B = 0.5*(n.z + 1.0);
}
void to_normal(vector3d &n) const { // for normal maps
n.x = 2.0*R - 1.0;
n.y = 2.0*G - 1.0;
n.z = 2.0*B - 1.0;
}
void normalize_to_max_comp() {
float const max_comp(get_max_component());
if (max_comp > TOLERANCE) {R /= max_comp; G /= max_comp; B /= max_comp;}
}
std::string str() const {std::ostringstream oss; oss << "R: " << R << ", G: " << G << ", B: " << B; return oss.str();}
std::string raw_str() const {std::ostringstream oss; oss << R << " " << G << " " << B; return oss.str();}
float get_luminance() const {return (R + G + B)/3.0f;}
float get_weighted_luminance() const {return (0.2126*R + 0.7152*G + 0.0722*B);} // see https://www.w3.org/WAI/GL/wiki/Relative_luminance
float get_max_component() const {return max(R, max(G, B));}
void set_for_cur_shader() const;
};
struct colorRGBA : public colorRGB { // size = 16
union {float A; float alpha;}; // A and alpha are both valid components
colorRGBA() : alpha(1.0) {}
colorRGBA(float r, float g, float b, float a=1.0) : colorRGB(r, g, b), A(a) {}
colorRGBA(colorRGB const &color, float a=1.0) : colorRGB(color), A(a) {}
void assign(float r, float g, float b, float a=1.0) {R = r; G = g; B = b; A = a;}
bool operator==(const colorRGBA &c) const {return (c.R == R && c.G == G && c.B == B && c.A == A);}
bool operator!=(const colorRGBA &c) const {return !operator==(c);}
const float &operator[](unsigned i) const {
switch(i) {
case 0: return R;
case 1: return G;
case 2: return B;
case 3: return A;
default: assert(0);
}
return R; // never gets here
}
float &operator[](unsigned i) {
switch(i) {
case 0: return R;
case 1: return G;
case 2: return B;
case 3: return A;
default: assert(0);
}
return R; // never gets here
}
bool operator<(const colorRGBA &c) const { // greater than operation?
if (A > c.A) return 1; // note: alpha less than so that low alpha colors are last
if (A < c.A) return 0;
return colorRGB::operator<(c);
}
colorRGBA operator* (float val) const {return colorRGBA(R*val, G*val, B*val, A);}
colorRGBA operator/ (float val) const {return colorRGBA(R/val, G/val, B/val, A);}
colorRGBA operator+ (colorRGBA const &c) const {return colorRGBA(R+c.R, G+c.G, B+c.B, A+c.A);}
void operator+=(colorRGBA const &c) {R += c.R; G += c.G; B += c.B; A += c.A;}
colorRGBA modulate_with(colorRGBA const &c) const {return colorRGBA(R*c.R, G*c.G, B*c.B, A*c.A);}
void set_valid_color() {
colorRGB::set_valid_color();
A = CLIP_TO_01(A);
}
void normalize_to_alpha_1() {
if (A == 1.0) return;
R *= A; G *= A; B *= A;
A = 1.0;
}
bool within_thresh_of_rgb(float thresh, colorRGBA const &c) const { // no alpha check
return ((fabs(R-c.R) + fabs(G-c.G) + fabs(B-c.B)) < thresh);
}
bool within_thresh_of_rgba(float thresh, colorRGBA const &c) const { // no alpha check
return ((fabs(R-c.R) + fabs(G-c.G) + fabs(B-c.B) + fabs(A-c.A)) < thresh);
}
bool is_valid() const {return (R >= 0 && G >= 0 && B >= 0 && A >= 0 && R <= 1 && G <= 1 && B <= 1 && A <= 1);}
std::string str() const {std::ostringstream oss; oss << "R: " << R << ", G: " << G << ", B: " << B << ", A: " << A; return oss.str();}
std::string raw_str() const {std::ostringstream oss; oss << R << " " << G << " " << B << " " << A; return oss.str();}
void set_for_cur_shader() const;
};
struct tex_range_t {
float x1, y1, x2, y2;
bool clip_quad, swap_xy;
tex_range_t() : x1(0.0), y1(0.0), x2(1.0), y2(1.0), clip_quad(0), swap_xy(0) {}
tex_range_t(float x1_, float y1_, float x2_, float y2_, bool clip_quad_=0, bool swap_xy_=0) : x1(x1_), y1(y1_), x2(x2_), y2(y2_), clip_quad(clip_quad_), swap_xy(swap_xy_) {}
void mirror_x() {swap(x1, x2);}
void mirror_y() {swap(y1, y2);}
static tex_range_t from_atlas(unsigned xv, unsigned yv, unsigned nx, unsigned ny) {
assert(nx > 0 && ny > 0 && xv < nx && yv < ny);
return tex_range_t(xv/float(nx), yv/float(ny), (xv+1)/float(nx), (yv+1)/float(ny));
}
};
#include "vertex_types.h" // must be included here
bool bind_temp_vbo_from_verts(void const *const verts, unsigned count, unsigned vert_size, void const *&vbo_ptr_offset);
void unbind_temp_vbo();
template< typename T> void set_ptr_state(T const *const verts, unsigned count, unsigned start_ix=0, bool set_array_client_state=1) {
void const *ptr_offset = NULL;
if (verts && !bind_temp_vbo_from_verts(verts+start_ix, count, sizeof(T), ptr_offset)) {ptr_offset = verts + start_ix;}
T::set_vbo_arrays(set_array_client_state, ptr_offset);
}
template <typename T> void unset_ptr_state(T const *const verts) {
T::unset_attrs();
if (verts) {unbind_temp_vbo();}
}
extern unsigned num_frame_draw_calls;
template <typename T> void draw_verts(T const *const verts, unsigned count, int gl_type, unsigned start_ix=0, bool set_array_client_state=1) {
assert(count > 0);
set_ptr_state(verts, count, start_ix, set_array_client_state);
glDrawArrays(gl_type, start_ix, count);
++num_frame_draw_calls;
unset_ptr_state(verts);
}
template <typename T> void draw_verts(vector<T> const &verts, int gl_type, unsigned start_ix=0, bool set_array_client_state=1) {
if (!verts.empty()) {draw_verts(&verts.front(), verts.size(), gl_type, start_ix, set_array_client_state);}
}
template <typename T> void draw_and_clear_verts(vector<T> &verts, int gl_type) {
draw_verts(verts, gl_type);
verts.resize(0); // clear()?
}
void draw_quads_as_tris(unsigned num_quad_verts, unsigned start_quad_vert=0, unsigned num_instances=1);
bool bind_quads_as_tris_ivbo(unsigned num_quad_verts);
void convert_quad_ixs_to_tri_ixs(vector<unsigned> const &qixs, vector<unsigned> &tixs);
template <typename T> void draw_quad_verts_as_tris(T const *const verts, unsigned count, unsigned start_ix=0, unsigned num_instances=1, bool set_array_client_state=1) {
assert(count > 0);
set_ptr_state(verts, count, start_ix, set_array_client_state);
draw_quads_as_tris(count, start_ix, num_instances);
unset_ptr_state(verts);
}
template <typename T> void draw_quad_verts_as_tris(vector<T> const &verts, unsigned start_ix=0, unsigned num_instances=1, bool set_array_client_state=1) {
if (!verts.empty()) {draw_quad_verts_as_tris(&verts.front(), verts.size(), start_ix, num_instances, set_array_client_state);}
}
template <typename T> void draw_quad_verts_as_tris_and_clear(vector<T> &verts) {
draw_quad_verts_as_tris(verts); verts.clear();
}
extern bool use_core_context;
template<typename T> void draw_vect_quads(T const &verts) {
if (use_core_context) {draw_quad_verts_as_tris(verts);} else {draw_verts(verts, GL_QUADS);}
}
template <typename T> void translate_verts(vector<T> &verts, vector3d const &xlate) {
for (auto i = verts.begin(); i != verts.end(); ++i) {i->v += xlate;}
}
template <typename T> void scale_verts(vector<T> &verts, vector3d const &scale) {
for (auto i = verts.begin(); i != verts.end(); ++i) {i->v *= scale;}
}
template <typename T> void tri_strip_push(vector<T> &v) {
assert(v.size() >= 3);
v.push_back(v[v.size()-2]);
v.push_back(v[v.size()-2]);
}
class draw_call_counter {
std::string name;
unsigned start_num_draw_calls;
public:
draw_call_counter(std::string const &name_) : name(name_), start_num_draw_calls(num_frame_draw_calls) {}
~draw_call_counter() {std::cout << name << ": " << (num_frame_draw_calls - start_num_draw_calls) << std::endl;}
};
template<typename T> struct triangle_t {
T pts[3];
triangle_t() {}
triangle_t(T const &p1, T const &p2, T const &p3) {pts[0] = p1; pts[1] = p2; pts[2] = p3;}
triangle_t(T const *const p) {pts[0] = p[0]; pts[1] = p[1]; pts[2] = p[2];}
vector3d get_normal(bool normalize=1) const {return get_poly_norm(pts, normalize);}
void operator+=(T const &p) {pts[0] += p; pts[1] += p; pts[2] += p;}
cube_t get_bbox(vector<T> const &p) const { // Note: only works on some types of triangles
cube_t bbox(pts[0], pts[0]);
bbox.union_with_pt(pts[1]);
bbox.union_with_pt(pts[2]);
return bbox;
}
};
typedef triangle_t<point> triangle;
typedef triangle_t<point_d> triangle_d; // unused
typedef triangle_t<vert_norm_tc> triangle_vntc;