This repository has been archived by the owner on Jul 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathCore.cpp
998 lines (711 loc) · 26.6 KB
/
Core.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
/*
* Core.cpp
* SpatialTest Project
*
* GLUT application to help visualize and test the implemented spatial structures.
*
* Created by radix on 05/04/08.
* Copyright Mykola Konyk, <mykola@konyk.org>, 2008.
*
* This code is under Microsoft Reciprocal License (Ms-RL)
* Please see http://www.opensource.org/licenses/ms-rl.html
*
* Important points about the license (from Ms-RL):
*
* [A] For any file you distribute that contains code from the software (in source code or binary format), you must provide
* recipients the source code to that file along with a copy of this license, which license will govern that file.
* You may license other files that are entirely your own work and do not contain code from the software under any terms
* you choose.
*
* [B] No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
*
* [C] If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your
* patent license from such contributor to the software ends automatically.
*
* [D] If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices
* that are present in the software.
*
* [E] If you distribute any portion of the software in source code form, you may do so only under this license by including a
* complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object
* code form, you may only do so under a license that complies with this license.
*
* [F] The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees
* or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent
* permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular
* purpose and non-infringement.
*
*/
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#elif _MSC_VER
#include <Windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <string>
#include <sstream>
#include <cstdlib>
#include "ISpatialStructure.h"
#include "ISpatialObject.h"
#include "SphereObject.h"
#include "BruteForce.h"
#include "SortAndSweep.h"
#include "UniformGrid.h"
#include "HierarchicalGrid.h"
#include "Octree.h"
#include "LooseOctree.h"
#include "Kdtree.h"
#include "Vector3.h"
//--
static SpatialTest::ISpatialStructure* g_pSpatialStruct = NULL;
static std::vector<SpatialTest::ISpatialObject*> g_vecObjects;
// [rad] Min and Max radiuses for objects
static float g_f32RadiusMin = 1.25f;
static float g_f32RadiusMax = 7.25f;
// [rad] Number of objects to generate
static int g_i32ObjectCount = 1000;
// [rad] Used to assign unique ids to objects
static int g_i32LastId = 0;
static float g_f32StartOffset = 50.0f;
// [rad] Bounding box for all objects
static SpatialTest::Vector3 g_vec3ValuesMax = SpatialTest::Vector3(100.0f, 100.0f, 100.0f);
static SpatialTest::Vector3 g_vec3ValuesMin = SpatialTest::Vector3(-100.0f, -100.0f, -100.0f);
// [rad] Number of hash buckets to use in Uniform grid and Hierarchical grid
static int g_i32HashBucketCount = 2048;
static float g_f32Frame = 0.015f;
// [rad] Screen width and height
static int g_i32ScreenWidth = 800;
static int g_i32ScreenHeight = 600;
static int g_i32LastX = 0;
static int g_i32LastY = 0;
static float g_f32RotationX = 0.0f;
static float g_f32RotationY = 0.0f;
static int g_i32FirstFrame = 1;
static int g_i32FrameCount = 0;
static int g_i32TimeBase = 0;
static float g_f32Fps = 0.0f;
static int g_i32ResetFps = 0;
static float g_f32MaxFps = 0.0f;
static int g_i32CurrentSpatial = 2;
static int g_i32DrawBox = 1;
static int g_i32DrawAxis = 0;
static int g_i32ShowCollisionCount = 1;
static int g_i32MaxCollisions = 0;
static int g_i32CurrentRebuild = 0;
static int g_i32Pause = 0;
static int g_i32OneFrame = 0;
//--
static void PrintText(const std::string& refString, int i32X, int i32Y);
static void RenderInfo();
static void RenderScene();
static void SetupRC();
static void ChangeSize(GLsizei w, GLsizei h);
static void MouseActiveMotion(int i32X, int i32Y);
static void KeyboardNormal(unsigned char u8Key, int i32X, int i32Y);
static void CreateObjects();
static void DeleteObjects();
static void CreateSpatialStructure();
static void DeleteSpatialStructure();
static void Tick();
//--
// [rad] Generate specified number of objects and place them randomly
static void CreateObjects()
{
int i32Index;
float f32Radius;
SpatialTest::Vector3 vec3Position;
SpatialTest::Vector3 vec3Direction;
SpatialTest::SphereObject* pSphere;
// [rad] Initialize random seed
srand(time(0));
for(i32Index = 0; i32Index < g_i32ObjectCount; i32Index++)
{
// [rad] Generate random radius
f32Radius = (rand() /( static_cast<float>(RAND_MAX) + 1.0f)) *
(g_f32RadiusMax - g_f32RadiusMin) + g_f32RadiusMin;
// [rad] Generate random position
vec3Position.x = (rand() /( static_cast<float>(RAND_MAX) + 1.0f)) *
(g_vec3ValuesMax.x - g_vec3ValuesMin.x - 2.0f * g_f32StartOffset) + g_vec3ValuesMin.x + g_f32StartOffset;
vec3Position.y = (rand() /( static_cast<float>(RAND_MAX) + 1.0f)) *
(g_vec3ValuesMax.y - g_vec3ValuesMin.y - 2.0f * g_f32StartOffset) + g_vec3ValuesMin.y + g_f32StartOffset;
vec3Position.z = (rand() /( static_cast<float>(RAND_MAX) + 1.0f)) *
(g_vec3ValuesMax.z - g_vec3ValuesMin.z - 2.0f * g_f32StartOffset) + g_vec3ValuesMin.z + g_f32StartOffset;
// [rad] Generate random direction
vec3Direction.x = (rand() /( static_cast<float>(RAND_MAX) + 1.0f)) *
(g_vec3ValuesMax.x - g_vec3ValuesMin.x) + g_vec3ValuesMin.x;
vec3Direction.y = (rand() /( static_cast<float>(RAND_MAX) + 1.0f)) *
(g_vec3ValuesMax.y - g_vec3ValuesMin.y) + g_vec3ValuesMin.y;
vec3Direction.z = (rand() /( static_cast<float>(RAND_MAX) + 1.0f)) *
(g_vec3ValuesMax.z - g_vec3ValuesMin.z) + g_vec3ValuesMin.z;
// [rad] Create sphere object
pSphere = new SpatialTest::SphereObject(g_i32LastId);
// [rad] Set radius, direction and position
pSphere->VSetPosition(vec3Position);
pSphere->VSetRadius(f32Radius);
pSphere->VSetDirection(vec3Direction);
// [rad] Add object
g_vecObjects.push_back(pSphere);
// [rad] Increment unique id
++g_i32LastId;
}
}
//--
static void DeleteObjects()
{
std::vector<SpatialTest::ISpatialObject*>::iterator iter_objects;
for(iter_objects = g_vecObjects.begin(); iter_objects != g_vecObjects.end(); iter_objects++)
{
delete((*iter_objects));
(*iter_objects) = NULL;
}
}
//--
static void CreateSpatialStructure()
{
switch(g_i32CurrentSpatial)
{
case 1:
delete(g_pSpatialStruct);
g_pSpatialStruct = new SpatialTest::BruteForce();
break;
case 2:
delete(g_pSpatialStruct);
g_pSpatialStruct = new SpatialTest::SortAndSweep();
break;
case 3:
delete(g_pSpatialStruct);
g_pSpatialStruct = new SpatialTest::UniformGrid(g_i32HashBucketCount);
break;
case 4:
delete(g_pSpatialStruct);
g_pSpatialStruct = new SpatialTest::HierarchicalGrid(g_i32HashBucketCount);
break;
case 5:
delete(g_pSpatialStruct);
// [rad] Construct either an Octree or an Octree which is to be rebuilt every frame
g_pSpatialStruct = new SpatialTest::Octree(SpatialTest::Vector3(0.0f, 0.0f, 0.0f), 100.0f, g_i32CurrentRebuild);
break;
case 6:
delete(g_pSpatialStruct);
// [rad] Construct either a Loose Octree or a Loose Octree which is to be rebuilt every frame
g_pSpatialStruct = new SpatialTest::LooseOctree(SpatialTest::Vector3(0.0f, 0.0f, 0.0f), 100.0f, g_i32CurrentRebuild);
break;
case 7:
delete(g_pSpatialStruct);
g_pSpatialStruct = new SpatialTest::KDTree(SpatialTest::Vector3(0.0f, 0.0f, 0.0f), 100.0f);
break;
default:
return;
}
// [rad] Populate structure
g_pSpatialStruct->VAddObjects(g_vecObjects);
// [rad] Reset collision count
g_i32MaxCollisions = 0;
// [rad] Reset fps
g_i32ResetFps = 1;
}
//--
static void DeleteSpatialStructure()
{
delete(g_pSpatialStruct);
g_pSpatialStruct = NULL;
}
//--
static void Tick()
{
float f32Radius;
int i32Collision;
SpatialTest::Vector3 vec3Position;
SpatialTest::Vector3 vec3Direction;
SpatialTest::Vector3 vec3Normal;
// [rad] Check if we are doing one frame processing
if(g_i32OneFrame && g_i32Pause)
{
g_i32Pause = 0;
}
// [rad] only do collision detection / object update if not paused
if(!g_i32Pause)
{
// [rad] For each object, check wall collisions
std::vector<SpatialTest::ISpatialObject*>::iterator iter_objects;
for(iter_objects = g_vecObjects.begin(); iter_objects != g_vecObjects.end(); iter_objects++)
{
// [rad] Reset collision info
(*iter_objects)->VCollisionOff();
// [rad] Check wall collisions
vec3Position = (*iter_objects)->VGetPosition();
vec3Direction = (*iter_objects)->VGetDirection();
f32Radius = (*iter_objects)->VGetRadius();
i32Collision = 0;
// [rad] Check if we are close enough to the wall
if(vec3Position.x - f32Radius <= g_vec3ValuesMin.x)
{
// [rad] Left wall
vec3Normal = SpatialTest::Vector3(1.0f, 0.0f, 0.0f);
vec3Direction -= 2.0f * vec3Normal * vec3Direction.Dot(vec3Normal);
(*iter_objects)->VSetDirection(vec3Direction);
i32Collision = 1;
}
if(vec3Position.x + f32Radius >= g_vec3ValuesMax.x)
{
// [rad] Right wall
vec3Normal = SpatialTest::Vector3(-1.0f, 0.0f, 0.0f);
vec3Direction -= 2.0f * vec3Normal * vec3Direction.Dot(vec3Normal);
(*iter_objects)->VSetDirection(vec3Direction);
i32Collision = 1;
}
if(vec3Position.z - f32Radius <= g_vec3ValuesMin.z)
{
// [rad] Far wall
vec3Normal = SpatialTest::Vector3(0.0f, 0.0f, 1.0f);
vec3Direction -= 2.0f * vec3Normal * vec3Direction.Dot(vec3Normal);
(*iter_objects)->VSetDirection(vec3Direction);
i32Collision = 1;
}
if(vec3Position.z + f32Radius >= g_vec3ValuesMax.z)
{
// [rad] Near wall
vec3Normal = SpatialTest::Vector3(0.0f, 0.0f, -1.0f);
vec3Direction -= 2.0f * vec3Normal * vec3Direction.Dot(vec3Normal);
(*iter_objects)->VSetDirection(vec3Direction);
i32Collision = 1;
}
if(vec3Position.y - f32Radius <= g_vec3ValuesMin.y)
{
// [rad] Bottom wall
vec3Normal = SpatialTest::Vector3(0.0f, 1.0f, 0.0f);
vec3Direction -= 2.0f * vec3Normal * vec3Direction.Dot(vec3Normal);
(*iter_objects)->VSetDirection(vec3Direction);
i32Collision = 1;
}
if(vec3Position.y + f32Radius >= g_vec3ValuesMax.y)
{
// [rad] Top wall
vec3Normal = SpatialTest::Vector3(0.0f, -1.0f, 0.0f);
vec3Direction -= 2.0f * vec3Normal * vec3Direction.Dot(vec3Normal);
(*iter_objects)->VSetDirection(vec3Direction);
i32Collision = 1;
}
if(!i32Collision)
{
// [rad] Nothing special
vec3Normal = SpatialTest::Vector3(0.0f, 0.0f, 0.0f);
vec3Direction -= 2.0f * vec3Normal * vec3Direction.Dot(vec3Normal);
(*iter_objects)->VSetDirection(vec3Direction);
}
// [rad] Update position
vec3Position += vec3Direction * g_f32Frame;
(*iter_objects)->VSetPosition(vec3Position);
// [rad] Check if this was one frame processing
if(g_i32OneFrame)
{
g_i32Pause = 1;
g_i32OneFrame = 0;
}
}
// [rad] Do collision detection
g_pSpatialStruct->VUpdate();
}
glutPostRedisplay();
}
//--
static void PrintText(const std::string& refString, int i32X, int i32Y)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, g_i32ScreenWidth, 0, g_i32ScreenHeight);
glScalef(1, -1, 1);
glTranslatef(0, -g_i32ScreenHeight, 0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(1.0f, 1.0f, 0.0f);
glRasterPos2f(i32X, i32Y);
for(int i = 0; i < refString.size(); i++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, refString[i]);
}
glColor3f(1.0f, 0.0f, 0.0f);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
//--
static void RenderInfo()
{
int i32Time;
int i32CollisionCount = 0;
std::stringstream ssSerial;
std::string sBuf;
// [rad] Upper menu
sBuf = "";
ssSerial.str("");
ssSerial << "[1] Brute Force ";
ssSerial << "[2] Sort and Sweep ";
ssSerial << "[3] Uniform Grid ";
ssSerial << "[4] Hierarchical Grid ";
ssSerial << "[5] Octree / Octree (Rebuild) ";
sBuf = ssSerial.str();
PrintText(sBuf, 20, 20);
// [rad] middle menu
sBuf = "";
ssSerial.str("");
ssSerial << "[6] Loose Octree / Loose Octree (Rebuild) ";
ssSerial << "[7] KD-Tree (SAH) ";
sBuf = ssSerial.str();
PrintText(sBuf, 20, 40);
// [rad] Lower menu
sBuf = "";
ssSerial.str("");
ssSerial << "[Q] Quit ";
ssSerial << "[B] Enable / Disable Box ";
ssSerial << "[X] Enable / Disable Axis ";
ssSerial << "[N] Show Collision Count ";
sBuf = ssSerial.str();
PrintText(sBuf, 20, 70);
// [rad] Lower menu (2nd)
sBuf = "";
ssSerial.str("");
ssSerial << "[P] Pause ";
ssSerial << "[Space] One Frame (Pause) ";
ssSerial << "[R] Reposition ";
sBuf = ssSerial.str();
PrintText(sBuf, 20, 90);
// [rad] Print spatial structure
sBuf = "";
ssSerial.str("");
ssSerial << "Spatial Structure: ";
switch(g_i32CurrentSpatial)
{
case 1:
{
ssSerial << "Brute force";
}
break;
case 2:
{
ssSerial << "Sort and Sweep";
}
break;
case 3:
{
ssSerial << "Uniform Grid";
}
break;
case 4:
{
ssSerial << "Hierarchical Grid";
}
break;
case 5:
{
if(!g_i32CurrentRebuild)
{
ssSerial << "Octree";
}
else
{
ssSerial << "Octree (Rebuild)";
}
}
break;
case 6:
{
if(!g_i32CurrentRebuild)
{
ssSerial << "Loose Octree";
}
else
{
ssSerial << "Loose Octree (Rebuild)";
}
}
break;
case 7:
{
ssSerial << "KD-Tree (SAH)";
}
break;
default:
ssSerial << "Unknown";
}
sBuf = ssSerial.str();
PrintText(sBuf, 20, 130);
// [rad] Print fps
++g_i32FrameCount;
i32Time = glutGet(GLUT_ELAPSED_TIME);
if(i32Time - g_i32TimeBase > 1000)
{
g_f32Fps = g_i32FrameCount * 1000.0f / static_cast<float>(i32Time - g_i32TimeBase);
g_i32TimeBase = i32Time;
g_i32FrameCount = 0;
if(g_i32ResetFps)
{
g_i32ResetFps = 0;
g_f32MaxFps = 0.0f;
g_f32Fps = 0.0f;
}
}
// [rad] Store max fps, if matched
if(g_f32MaxFps < g_f32Fps)
{
g_f32MaxFps = g_f32Fps;
}
sBuf = "";
ssSerial.str("");
ssSerial << "FPS: " << g_f32Fps;
sBuf = ssSerial.str();
PrintText(sBuf, 20, 150);
sBuf = "";
ssSerial.str("");
ssSerial << "Maximum FPS: " << g_f32MaxFps;
sBuf = ssSerial.str();
PrintText(sBuf, 20, 170);
// [rad] Print object count
sBuf = "";
ssSerial.str("");
ssSerial << "Number of objects: " << g_i32ObjectCount;
sBuf = ssSerial.str();
PrintText(sBuf, 20, 190);
// [rad] Calculate number of collisions
std::vector<SpatialTest::ISpatialObject*>::iterator iter_objects;
for(iter_objects = g_vecObjects.begin(); iter_objects != g_vecObjects.end(); iter_objects++)
{
if((*iter_objects)->VGetCollisionStatus())
{
++i32CollisionCount;
}
}
// [rad] Update collision count, if needed
if(g_i32MaxCollisions < i32CollisionCount)
{
g_i32MaxCollisions = i32CollisionCount;
}
// [rad] Print collision count
if(g_i32ShowCollisionCount)
{
sBuf = "";
ssSerial.str("");
ssSerial << "Number of collisions: " << (i32CollisionCount / 2);
sBuf = ssSerial.str();
PrintText(sBuf, 20, 210);
sBuf = "";
ssSerial.str("");
ssSerial << "Maximum number of collisions: " << (g_i32MaxCollisions / 2);
sBuf = ssSerial.str();
PrintText(sBuf, 20, 230);
}
}
//--
static void RenderScene()
{
// [rad] Do object update / collision detection
Tick();
glClear(GL_COLOR_BUFFER_BIT);
// [rad] Render text
RenderInfo();
// [rad] Rotate scene if necessary
glRotatef(g_f32RotationX, 1.0f, 0.0f, 0.0f);
g_f32RotationX = 0.0f;
glRotatef(g_f32RotationY, 0.0f, 1.0f, 0.0f);
g_f32RotationY = 0.0f;
// [rad] Render all objects
std::vector<SpatialTest::ISpatialObject*>::iterator iter_objects;
for(iter_objects = g_vecObjects.begin(); iter_objects != g_vecObjects.end(); iter_objects++)
{
glPushMatrix();
glTranslatef((*iter_objects)->VGetPosition().x, (*iter_objects)->VGetPosition().y,
(*iter_objects)->VGetPosition().z);
// [rad] Check if this object is colliding
if((*iter_objects)->VGetCollisionStatus())
{
glColor3f(1.0f, 0.0f, 0.0f);
}
else
{
glColor3f(1.0f, 1.0f, 1.0f);
}
// [rad] We'll use cubes to draw our spheres, since they require the
// least number of polygons
glutSolidCube((*iter_objects)->VGetRadius());
glPopMatrix();
}
// [rad] Draw box (cube) if enabled
if(g_i32DrawBox)
{
glColor3f(1.0f, 1.0f, 1.0f);
//glutWireCube(200.0f);
glEnable(GL_LINE_STIPPLE);
glLineStipple(5, 0x55555);
glBegin(GL_LINE_STRIP);
glVertex3f(-100.0f, 100.0f, -100.0f);
glVertex3f(100.0f, 100.0f, -100.0f);
glVertex3f(100.0f, 100.0f, 100.0f);
glVertex3f(-100.0f, 100.0f, 100.0f);
glVertex3f(-100.0f, 100.0f, -100.0f);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3f(-100.0f, -100.0f, -100.0f);
glVertex3f(100.0f, -100.0f, -100.0f);
glVertex3f(100.0f, -100.0f, 100.0f);
glVertex3f(-100.0f, -100.0f, 100.0f);
glVertex3f(-100.0f, -100.0f, -100.0f);
glEnd();
glBegin(GL_LINES);
glVertex3f(-100.0f, 100.0f, 100.0f);
glVertex3f(-100.0f, -100.0f, 100.0f);
glVertex3f(-100.0f, 100.0f, -100.0f);
glVertex3f(-100.0f, -100.0f, -100.0f);
glVertex3f(100.0f, 100.0f, -100.0f);
glVertex3f(100.0f, -100.0f, -100.0f);
glVertex3f(100.0f, 100.0f, 100.0f);
glVertex3f(100.0f, -100.0f, 100.0f);
glEnd();
glDisable(GL_LINE_STIPPLE);
}
// [rad] Draw axis
if(g_i32DrawAxis)
{
glColor3f(1.0f, 1.0f, 1.0f);
glEnable(GL_LINE_STIPPLE);
glLineStipple(5, 0x55555);
glBegin(GL_LINES);
glVertex3f(-150.0f, 0.0f, 0.0f);
glVertex3f(150.0f, 0.0f, 0.0f);
glVertex3f(0.0f, -150.0f, 0.0f);
glVertex3f(0.0f, 150.0f, 0.0f);
glVertex3f(0.0f, 0.0f, -150.0f);
glVertex3f(0.0f, 0.0f, 150.0f);
glEnd();
glDisable(GL_LINE_STIPPLE);
}
// [rad] Swap double buffer
glutSwapBuffers();
}
//--
static void SetupRC()
{
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
//--
static void ChangeSize(GLsizei w, GLsizei h)
{
// [rad] Store new size
g_i32ScreenWidth = w;
g_i32ScreenHeight = h;
GLfloat range = 200.0f;
if(!h)
{
h = 1;
}
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w <= h)
{
glOrtho(-range, range, -range * h / w, range * h / w, -range, range);
}
else
{
glOrtho(-range * w / h, range * w /h, -range, range, -range, range);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//--
static void KeyboardNormal(unsigned char u8Key, int i32X, int i32Y)
{
int i32Value = u8Key - 48;
if(i32Value >= 1 && i32Value <= 7)
{
if(i32Value == g_i32CurrentSpatial)
{
g_i32CurrentRebuild = !g_i32CurrentRebuild;
}
else
{
g_i32CurrentSpatial = i32Value;
g_i32CurrentRebuild = 0;
}
// [rad] Re-create spatial structure
CreateSpatialStructure();
}
else if(u8Key == 'b' || u8Key == 'B')
{
g_i32DrawBox = !g_i32DrawBox;
}
else if(u8Key == 'x' || u8Key == 'X')
{
g_i32DrawAxis = !g_i32DrawAxis;
}
else if(u8Key == 'q' || u8Key == 'Q')
{
exit(0);
}
else if(u8Key == 'n' || u8Key == 'N')
{
g_i32ShowCollisionCount = !g_i32ShowCollisionCount;
}
else if(u8Key == 'r' || u8Key == 'R')
{
ChangeSize(g_i32ScreenWidth, g_i32ScreenHeight);
}
else if(u8Key == 'p' || u8Key == 'P')
{
g_i32Pause = !g_i32Pause;
}
else if(u8Key == ' ')
{
if(!g_i32Pause)
{
g_i32Pause = 0;
}
g_i32OneFrame = 1;
}
}
//--
static void MouseActiveMotion(int i32X, int i32Y)
{
if(g_i32FirstFrame)
{
g_i32FirstFrame = 0;
}
else
{
g_f32RotationX = static_cast<float>(g_i32LastX - i32X) * 0.5f;
g_f32RotationY = static_cast<float>(g_i32LastY - i32Y) * 0.5f;
}
g_i32LastX = i32X;
g_i32LastY = i32Y;
}
//--
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(g_i32ScreenWidth, g_i32ScreenHeight);
glutCreateWindow("SpatialTest");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutMotionFunc(MouseActiveMotion);
glutKeyboardFunc(KeyboardNormal);
CreateObjects();
CreateSpatialStructure();
SetupRC();
glutMainLoop();
DeleteSpatialStructure();
DeleteObjects();
return(0);
}