fOctreeRenderC.cpp 6.8 KB

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