2
0

Unit1.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. //#include <gl\gl.h>
  5. #pragma hdrstop
  6. #include "Unit1.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma link "GLBaseClasses"
  10. #pragma link "GLCadencer"
  11. #pragma link "GLCoordinates"
  12. #pragma link "GLMaterial"
  13. #pragma link "GLObjects"
  14. #pragma link "GLScene"
  15. #pragma link "GLSimpleNavigation"
  16. #pragma link "GLWin32Viewer"
  17. #pragma link "GLCrossPlatform"
  18. #pragma resource "*.dfm"
  19. TForm1 *Form1;
  20. const cBOX_SIZE = 14.2;
  21. //---------------------------------------------------------------------------
  22. __fastcall TForm1::TForm1(TComponent* Owner)
  23. : TForm(Owner)
  24. {
  25. }
  26. //---------------------------------------------------------------------------
  27. void __fastcall TForm1::FormCreate(TObject *Sender)
  28. {
  29. int i;
  30. Randomize;
  31. Octree = new TOctreeSpacePartition;
  32. Octree->SetSize(AffineVectorMake(-15, -15, -15), AffineVectorMake(15, 15, 15));
  33. Octree->MaxTreeDepth = 6;
  34. Octree->LeafThreshold = 10;
  35. TrackBar_LeafThresholdChange(NULL);
  36. for (i = 0; i < 300; i ++)
  37. CreateBox();
  38. VerifySpacialMisc();
  39. }
  40. // class TGLSpacePartitionLeaf
  41. __fastcall TGLSpacePartitionLeaf::CreateGLOwned(TBaseSpacePartition *SpacePartition, TGLBaseSceneObject *aGLBaseSceneObject)
  42. {
  43. GLBaseSceneObject = aGLBaseSceneObject;
  44. // Set them all off in the same direction
  45. Direction.X = rand();
  46. Direction.Y = rand();
  47. Direction.Z = rand();
  48. NormalizeVector(Direction);
  49. // inherited CreateOwned(SpacePartition);
  50. }
  51. void __fastcall TGLSpacePartitionLeaf::UpdateCachedAABBAndBSphere()
  52. {
  53. FCachedAABB = GLBaseSceneObject->AxisAlignedBoundingBox();
  54. FCachedAABB.Min = GLBaseSceneObject->LocalToAbsolute(FCachedAABB.Min);
  55. FCachedAABB.Max = GLBaseSceneObject->LocalToAbsolute(FCachedAABB.Max);
  56. FCachedBSphere.Radius = GLBaseSceneObject->BoundingSphereRadius();
  57. FCachedBSphere.Center = GLBaseSceneObject->Position->AsAffineVector;
  58. }
  59. //---------------------------------------------------------------------------
  60. TAffineVector RandomPos()
  61. {
  62. int c1 = 10;
  63. TAffineVector result;
  64. //MakeVector(result, rand()*c1-c1/2, rand()*c1-c1/2, rand()*c1-c1/2);
  65. MakeVector(result, rand(), rand(), rand());
  66. return result;
  67. };
  68. float RandomSize()
  69. {
  70. return 0.1+rand()*0.5;
  71. };
  72. void __fastcall TForm1::CreateBox()
  73. {
  74. TGLCube *GLCube;
  75. TGLSpacePartitionLeaf * GLSpacePartitionLeaf;
  76. GLCube = (TGLCube *)GLScene1->Objects->AddNewChild(__classid(TGLCube));
  77. GLCube->Position->AsAffineVector = RandomPos();
  78. GLCube->CubeWidth = RandomSize();
  79. GLCube->CubeHeight = RandomSize();
  80. GLCube->CubeDepth = RandomSize();
  81. GLSpacePartitionLeaf = new TGLSpacePartitionLeaf;
  82. GLSpacePartitionLeaf->CreateGLOwned(Octree, GLCube); //C++
  83. }
  84. //---------------------------------------------------------------------------
  85. void __fastcall TForm1::TrackBar_LeafThresholdChange(TObject *Sender)
  86. {
  87. Label3->Caption = Format("Leaf Threshold : %d",
  88. ARRAYOFCONST ((TrackBar_LeafThreshold->Position)));
  89. Octree->LeafThreshold = TrackBar_LeafThreshold->Position;
  90. }
  91. //---------------------------------------------------------------------------
  92. float TestMove(float pos, float dir, const double deltaTime)
  93. {
  94. const cSPEED = 2;
  95. {
  96. if (abs(pos+dir*deltaTime*cSPEED)>=cBOX_SIZE)
  97. dir = -dir;
  98. return (pos + dir * deltaTime*cSPEED);
  99. }
  100. }
  101. void __fastcall TForm1::RenderAABB(TAABB* AABB, float w, float r, float g, float b)
  102. {
  103. /*
  104. rci.GLStates->LineWidth = w;
  105. glColor3f(r,g,b);
  106. glBegin(GL_LINE_STRIP);
  107. glVertex3f(AABB.min.X,AABB.min.Y, AABB.min.Z);
  108. glVertex3f(AABB.min.X,AABB.max.Y, AABB.min.Z);
  109. glVertex3f(AABB.max.X,AABB.max.Y, AABB.min.Z);
  110. glVertex3f(AABB.max.X,AABB.min.Y, AABB.min.Z);
  111. glVertex3f(AABB.min.X,AABB.min.Y, AABB.min.Z);
  112. glVertex3f(AABB.min.X,AABB.min.Y, AABB.max.Z);
  113. glVertex3f(AABB.min.X,AABB.max.Y, AABB.max.Z);
  114. glVertex3f(AABB.max.X,AABB.max.Y, AABB.max.Z);
  115. glVertex3f(AABB.max.X,AABB.min.Y, AABB.max.Z);
  116. glVertex3f(AABB.min.X,AABB.min.Y, AABB.max.Z);
  117. glEnd();
  118. glBegin(GL_LINES);
  119. glVertex3f(AABB.min.X,AABB.max.Y, AABB.min.Z);
  120. glVertex3f(AABB.min.X,AABB.max.Y, AABB.max.Z);
  121. glVertex3f(AABB.max.X,AABB.max.Y, AABB.min.Z);
  122. glVertex3f(AABB.max.X,AABB.max.Y, AABB.max.Z);
  123. glVertex3f(AABB.max.X,AABB.min.Y, AABB.min.Z);
  124. glVertex3f(AABB.max.X,AABB.min.Y, AABB.max.Z);
  125. glEnd();
  126. */
  127. }
  128. //---------------------------------------------------------------------------
  129. void __fastcall TForm1::RenderOctreeNode(TSectorNode* Node)
  130. {
  131. int i;
  132. TAABB* AABB;
  133. if (Node->NoChildren)
  134. {
  135. AABB = new TAABB;
  136. // AABB = Node->AABB;
  137. if (Node->RecursiveLeafCount > 0)
  138. RenderAABB(AABB, 1, 0, 0, 0);
  139. else
  140. RenderAABB(AABB, 1, 0.8, 0.8, 0.8);
  141. }
  142. else
  143. {
  144. for (i = 0; i < Node->ChildCount-1; i++)
  145. RenderOctreeNode(Node->Children[i]);
  146. }
  147. }
  148. //---------------------------------------------------------------------------
  149. void __fastcall TForm1::GLDirectOpenGL1Render(TObject *Sender, TGLRenderContextInfo &rci)
  150. {
  151. TAABB* AABB;
  152. rci.GLStates->Disable(stLighting);
  153. rci.GLStates->Enable(stColorMaterial);
  154. MakeVector(AABB->Min, -cBOX_SIZE, -cBOX_SIZE, -cBOX_SIZE);
  155. MakeVector(AABB->Max, cBOX_SIZE, cBOX_SIZE, cBOX_SIZE);
  156. RenderAABB(AABB,2, 0,0,0);
  157. RenderOctreeNode(Octree->RootNode);
  158. }
  159. //---------------------------------------------------------------------------
  160. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  161. const double newTime)
  162. {
  163. const cSPEED = 2;
  164. TAABB* AABB;
  165. TBSphere* BSphere;
  166. TGLSpacePartitionLeaf* Leaf;
  167. TGLSpacePartitionLeaf* TestLeaf;
  168. TGLCube* Cube;
  169. int i, j, CollidingLeafCount;
  170. for (i = 0; i < Octree->Leaves->Count-1; i++)
  171. {
  172. Leaf = (TGLSpacePartitionLeaf*)(Octree->Leaves->Items[i]);
  173. Cube = (TGLCube*)(Leaf->GLBaseSceneObject);
  174. Cube->Position->X = TestMove(Cube->Position->X, Leaf->GLBaseSceneObject->Direction->X, deltaTime);
  175. Cube->Position->Y = TestMove(Cube->Position->Y, Leaf->GLBaseSceneObject->Direction->Y, deltaTime);
  176. Cube->Position->Z = TestMove(Cube->Position->Z, Leaf->GLBaseSceneObject->Direction->Z, deltaTime);
  177. Leaf->Changed();
  178. }
  179. for (i = 0; i < Octree->Leaves->Count-1; i++)
  180. {
  181. Leaf = (TGLSpacePartitionLeaf*)(Octree->Leaves->Items[i]);
  182. Cube = (TGLCube*)(Leaf->GLBaseSceneObject);
  183. Cube->Material->FrontProperties->Emission->Red = 0;
  184. Cube->Material->FrontProperties->Emission->Green = 0;
  185. Cube->Material->FrontProperties->Emission->Blue = 0;
  186. }
  187. // AABB collision
  188. AABB = new TAABB();
  189. /*
  190. AABB = GLCube1->AxisAlignedBoundingBox;
  191. AABB->Max = GLCube1->LocalToAbsolute(AABB->Min);
  192. AABB->Max = GLCube1->LocalToAbsolute(AABB->Max);
  193. Octree->QueryAABB(AABB);
  194. */
  195. }
  196. //---------------------------------------------------------------------------
  197. void __fastcall TForm1::VerifySpacialMisc()
  198. {
  199. TAABB* AABBmajor;
  200. TAABB* AABBfull;
  201. TAABB* AABBoff;
  202. TAABB* AABBpartial;
  203. AABBmajor = new TAABB;
  204. MakeVector(AABBmajor->Min, 0, 0, 0);
  205. MakeVector(AABBmajor->Max, 5, 5, 5);
  206. /*
  207. MakeVector(AABBpartial->Min, 4, 4, 4);
  208. MakeVector(AABBpartial->Max, 6, 6, 6);
  209. MakeVector(AABBfull->Min, 1, 1, 1);
  210. MakeVector(AABBfull->Max, 2, 2, 2);
  211. MakeVector(AABBoff->Min, 7, 7, 7);
  212. MakeVector(AABBoff->Max, 8, 8, 8);
  213. */
  214. }
  215. //---------------------------------------------------------------------------
  216. void __fastcall TForm1::Button_ResetOctreeSizeClick(TObject *Sender)
  217. {
  218. Octree->GrowMethod = gmBestFit;
  219. Octree->UpdateStructureSize(0.05);
  220. Octree->GrowMethod = gmIncreaseToFitAll;
  221. }
  222. //---------------------------------------------------------------------------
  223. void __fastcall TForm1::FormDestroy(TObject *Sender)
  224. {
  225. Octree->Free();
  226. }
  227. //---------------------------------------------------------------------------