map.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include <limits>
  2. #include "map.h"
  3. #include "Mesh.h"
  4. #include "Scanner.h"
  5. #include "Parser.h"
  6. #include "Resource.h"
  7. #include "Camera.h"
  8. /*
  9. =======================================================================================================================================
  10. CreateRoot =
  11. =======================================================================================================================================
  12. */
  13. void octree_t::CreateRoot(const Vec<Mesh*>& meshes)
  14. {
  15. DEBUG_ERR(root); // root should be NULL
  16. /// get the root's aabb size
  17. Vec3 min(numeric_limits<float>::max()), max(numeric_limits<float>::min());
  18. for(uint m=0; m<meshes.size(); m++)
  19. {
  20. Mesh* cmesh = meshes[m];
  21. for(uint v=0; v<cmesh->vertCoords.size(); v++)
  22. {
  23. const Vec3& vertCoords = cmesh->vertCoords[v];
  24. for(int i=0; i<3; i++)
  25. {
  26. if(vertCoords[i] > max[i])
  27. max[i] = vertCoords[i];
  28. else if(vertCoords[i] < min[i])
  29. min[i] = vertCoords[i];
  30. } // end for 3 times
  31. } // end for all mesh verts
  32. } // end for all meshes
  33. /// create a new node
  34. node_t* node = new node_t;
  35. node->bounding_box.min = min;
  36. node->bounding_box.max = max;
  37. /// create the face and vert ids
  38. DEBUG_ERR(node->face_ids.size() != 0 || node->vertIds.size() != 0 || node->meshes.size() != 0); // vectors not empty. wrong node init
  39. node->face_ids.resize(meshes.size());
  40. node->vertIds.resize(meshes.size());
  41. node->meshes.resize(meshes.size());
  42. for(uint m=0; m<meshes.size(); m++)
  43. {
  44. Mesh* cmesh = meshes[m];
  45. // first set the mesh
  46. node->meshes[m] = cmesh;
  47. // then set the face_ids
  48. node->face_ids[m].resize(cmesh->tris.size());
  49. for(uint f=0; f<cmesh->tris.size(); f++)
  50. node->face_ids[m][f] = f; // simple as taking a shit
  51. // and last the verts
  52. }
  53. /// set root
  54. root = node;
  55. }
  56. /*
  57. =======================================================================================================================================
  58. GetFacesNum =
  59. =======================================================================================================================================
  60. */
  61. uint octree_t::node_t::GetFacesNum() const
  62. {
  63. int count = 0;
  64. for(uint i=0; i<meshes.size(); i++)
  65. {
  66. count += meshes[i]->tris.size();
  67. }
  68. return count;
  69. }
  70. /*
  71. =======================================================================================================================================
  72. IsSubdivHeuristicMet =
  73. returns true when the used difined heuristic is met that sais that we can subdivide the node. Long story short it returns true when =
  74. we can subdivide the node further =
  75. =======================================================================================================================================
  76. */
  77. bool octree_t::IsSubdivHeuristicMet(node_t* node) const
  78. {
  79. if(node->GetFacesNum() < 100) return false;
  80. return true;
  81. }
  82. /*
  83. =======================================================================================================================================
  84. SubdivideNode =
  85. subdivides the node and creates max 8 children and then subdivides the children =
  86. =======================================================================================================================================
  87. */
  88. void octree_t::SubdivideNode(node_t* node)
  89. {
  90. if(!IsSubdivHeuristicMet(node)) return;
  91. // subdivide the children
  92. for(int i=0; i<8; i++)
  93. {
  94. if(node->childs[i] == NULL) continue;
  95. SubdivideNode(node->childs[i]);
  96. }
  97. }
  98. /*
  99. =======================================================================================================================================
  100. CreateTree =
  101. =======================================================================================================================================
  102. */
  103. void octree_t::CreateTree(const Vec<Mesh*>& meshes)
  104. {
  105. CreateRoot(meshes);
  106. SubdivideNode(root);
  107. }
  108. /*
  109. =======================================================================================================================================
  110. CheckNodeAgainstFrustum =
  111. the func checks the node and returns if its inside the cameras fruntum. It returns 0 if the cube is not inside, 1 if partialy =
  112. inside and 2 if totaly inside =
  113. =======================================================================================================================================
  114. */
  115. uint octree_t::CheckNodeAgainstFrustum(node_t* node, const Camera& cam) const
  116. {
  117. int points_outside_frustum_num = 0;
  118. const aabb_t& box = node->bounding_box;
  119. Vec3 box_points[] = { box.max, Vec3(box.min.x, box.max.y, box.max.z), Vec3(box.min.x, box.min.y, box.max.z), Vec3(box.max.x, box.min.y, box.max.z),
  120. box.min, Vec3(box.min.x, box.max.y, box.min.z), Vec3(box.min.x, box.min.y, box.min.z), Vec3(box.max.x, box.min.y, box.min.z), };
  121. for(int i=0; i<8; i++)
  122. {
  123. for(int j=0; j<6; j++)
  124. {
  125. const plane_t& plane = cam.wspaceFrustumPlanes[j];
  126. if(plane.Test(box_points[i]) < 0.0)
  127. {
  128. ++points_outside_frustum_num;
  129. continue;
  130. }
  131. }
  132. }
  133. if(points_outside_frustum_num == 8) return 0;
  134. if(points_outside_frustum_num < 8) return 1;
  135. return 2;
  136. }
  137. /*
  138. =======================================================================================================================================
  139. map =
  140. =======================================================================================================================================
  141. */
  142. /*
  143. =======================================================================================================================================
  144. load =
  145. =======================================================================================================================================
  146. */
  147. bool map_t::load(const char* filename)
  148. {
  149. DEBUG_ERR(meshes.size() != 0); // meshes vector should be empty
  150. Scanner scanner;
  151. const Scanner::Token* token;
  152. if(!scanner.loadFile(filename)) return false;
  153. do
  154. {
  155. token = &scanner.getNextToken();
  156. // strings is what we want in this case... please let it be G-Strings
  157. if(token->getCode() == Scanner::TC_STRING)
  158. {
  159. Mesh* mesh = Rsrc::meshes.load(token->getValue().getString());
  160. if(!mesh) return false;
  161. meshes.push_back(mesh);
  162. }
  163. // end of file
  164. else if(token->getCode() == Scanner::TC_EOF)
  165. {
  166. break;
  167. }
  168. // other crap
  169. else
  170. {
  171. PARSE_ERR_UNEXPECTED();
  172. return false;
  173. }
  174. }while(true);
  175. return true;
  176. }