StaticModel.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Batch.h"
  25. #include "Camera.h"
  26. #include "Context.h"
  27. #include "Geometry.h"
  28. #include "Log.h"
  29. #include "Material.h"
  30. #include "Model.h"
  31. #include "OcclusionBuffer.h"
  32. #include "OctreeQuery.h"
  33. #include "Profiler.h"
  34. #include "ResourceCache.h"
  35. #include "ResourceEvents.h"
  36. #include "StaticModel.h"
  37. #include "XMLElement.h"
  38. #include "DebugNew.h"
  39. OBJECTTYPESTATIC(StaticModel);
  40. StaticModel::StaticModel(Context* context) :
  41. Drawable(context),
  42. softwareLodLevel_(M_MAX_UNSIGNED)
  43. {
  44. drawableFlags_ = DRAWABLE_GEOMETRY;
  45. }
  46. StaticModel::~StaticModel()
  47. {
  48. }
  49. void StaticModel::RegisterObject(Context* context)
  50. {
  51. context->RegisterFactory<StaticModel>();
  52. context->CopyBaseAttributes<Drawable, StaticModel>();
  53. ACCESSOR_ATTRIBUTE(StaticModel, VAR_RESOURCEREF, "Model", GetModelAttr, SetModelAttr, ResourceRef, ResourceRef(Model::GetTypeStatic()), AM_DEFAULT);
  54. ACCESSOR_ATTRIBUTE(StaticModel, VAR_RESOURCEREFLIST, "Materials", GetMaterialsAttr, SetMaterialsAttr, ResourceRefList, ResourceRefList(Material::GetTypeStatic()), AM_DEFAULT);
  55. ATTRIBUTE(StaticModel, VAR_INT, "Raycast/Occlusion LOD Level", softwareLodLevel_, M_MAX_UNSIGNED, AM_DEFAULT);
  56. }
  57. void StaticModel::ProcessRayQuery(RayOctreeQuery& query, float initialDistance)
  58. {
  59. PROFILE(RaycastStaticModel);
  60. RayQueryLevel level = query.level_;
  61. switch (level)
  62. {
  63. case RAY_AABB_NOSUBOBJECTS:
  64. case RAY_AABB:
  65. {
  66. RayQueryResult result;
  67. result.drawable_ = this;
  68. result.node_ = GetNode();
  69. result.distance_ = initialDistance;
  70. query.result_.Push(result);
  71. }
  72. break;
  73. case RAY_OBB:
  74. {
  75. Matrix3x4 inverse(GetWorldTransform().Inverse());
  76. Ray localRay(inverse * query.ray_.origin_, inverse * Vector4(query.ray_.direction_, 0.0f));
  77. float distance = boundingBox_.Distance(localRay);
  78. if (distance < query.maxDistance_)
  79. {
  80. RayQueryResult result;
  81. result.drawable_ = this;
  82. result.node_ = GetNode();
  83. result.distance_ = distance;
  84. query.result_.Push(result);
  85. }
  86. }
  87. break;
  88. case RAY_TRIANGLE:
  89. {
  90. // Do a pretest using the OBB
  91. Matrix3x4 inverse(GetWorldTransform().Inverse());
  92. Ray localRay(inverse * query.ray_.origin_, inverse * Vector4(query.ray_.direction_, 0.0f));
  93. float distance = boundingBox_.Distance(localRay);
  94. if (distance < query.maxDistance_)
  95. {
  96. // Then the actual test using triangle geometry
  97. for (unsigned i = 0; i < geometries_.Size(); ++i)
  98. {
  99. unsigned lodLevel;
  100. // Check whether to use same LOD as visible, or a specific LOD
  101. if (softwareLodLevel_ == M_MAX_UNSIGNED)
  102. lodLevel = lodLevels_[i];
  103. else
  104. lodLevel = Clamp(softwareLodLevel_, 0, geometries_[i].Size());
  105. Geometry* geom = geometries_[i][lodLevel];
  106. if (geom)
  107. {
  108. distance = geom->GetDistance(localRay);
  109. if (distance < query.maxDistance_)
  110. {
  111. RayQueryResult result;
  112. result.drawable_ = this;
  113. result.node_ = GetNode();
  114. result.distance_ = distance;
  115. query.result_.Push(result);
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. }
  122. break;
  123. }
  124. }
  125. void StaticModel::UpdateGeometry(const FrameInfo& frame)
  126. {
  127. if (lodLevelsDirty_)
  128. CalculateLodLevels();
  129. }
  130. unsigned StaticModel::GetNumBatches()
  131. {
  132. return geometries_.Size();
  133. }
  134. void StaticModel::GetBatch(const FrameInfo& frame, unsigned batchIndex, Batch& batch)
  135. {
  136. batch.geometry_ = geometries_[batchIndex][lodLevels_[batchIndex]];
  137. batch.worldTransform_ = &GetWorldTransform();
  138. batch.material_ = materials_[batchIndex];
  139. }
  140. bool StaticModel::DrawOcclusion(OcclusionBuffer* buffer)
  141. {
  142. bool success = true;
  143. for (unsigned i = 0; i < geometries_.Size(); ++i)
  144. {
  145. unsigned lodLevel;
  146. // Check whether to use same LOD as visible, or a specific LOD
  147. if (softwareLodLevel_ == M_MAX_UNSIGNED)
  148. lodLevel = lodLevels_[i];
  149. else
  150. lodLevel = Clamp(softwareLodLevel_, 0, geometries_[i].Size());
  151. Geometry* geom = geometries_[i][lodLevel];
  152. if (!geom)
  153. continue;
  154. // Check that the material is suitable for occlusion (default material always is)
  155. // and set culling mode
  156. Material* mat = materials_[i];
  157. if (mat)
  158. {
  159. if (!mat->GetOcclusion())
  160. continue;
  161. buffer->SetCullMode(mat->GetCullMode());
  162. }
  163. else
  164. buffer->SetCullMode(CULL_CCW);
  165. const unsigned char* vertexData;
  166. unsigned vertexSize;
  167. const unsigned char* indexData;
  168. unsigned indexSize;
  169. geom->GetRawData(vertexData, vertexSize, indexData, indexSize);
  170. // Check for valid geometry data
  171. if (!vertexData || !indexData)
  172. continue;
  173. unsigned indexStart = geom->GetIndexStart();
  174. unsigned indexCount = geom->GetIndexCount();
  175. // Draw and check for running out of triangles
  176. if (!buffer->Draw(GetWorldTransform(), vertexData, vertexSize, indexData, indexSize, indexStart, indexCount))
  177. success = false;
  178. if (!success)
  179. break;
  180. }
  181. return success;
  182. }
  183. void StaticModel::SetModel(Model* model)
  184. {
  185. if (!model || model == model_)
  186. return;
  187. // Unsubscribe from the reload event of previous model (if any), then subscribe to the new
  188. if (model_)
  189. UnsubscribeFromEvent(model_, E_RELOADFINISHED);
  190. if (model)
  191. SubscribeToEvent(model, E_RELOADFINISHED, HANDLER(StaticModel, HandleModelReloadFinished));
  192. model_ = model;
  193. // Copy the subgeometry & LOD level structure
  194. SetNumGeometries(model->GetNumGeometries());
  195. const Vector<Vector<SharedPtr<Geometry> > >& geometries = model->GetGeometries();
  196. for (unsigned i = 0; i < geometries.Size(); ++i)
  197. geometries_[i] = geometries[i];
  198. SetBoundingBox(model->GetBoundingBox());
  199. ResetLodLevels();
  200. }
  201. void StaticModel::SetMaterial(Material* material)
  202. {
  203. for (unsigned i = 0; i < materials_.Size(); ++i)
  204. materials_[i] = material;
  205. }
  206. bool StaticModel::SetMaterial(unsigned index, Material* material)
  207. {
  208. if (index >= materials_.Size())
  209. {
  210. LOGERROR("Material index out of bounds");
  211. return false;
  212. }
  213. materials_[index] = material;
  214. return true;
  215. }
  216. void StaticModel::SetSoftwareLodLevel(unsigned level)
  217. {
  218. softwareLodLevel_ = level;
  219. }
  220. Material* StaticModel::GetMaterial(unsigned index) const
  221. {
  222. return index < materials_.Size() ? materials_[index] : (Material*)0;
  223. }
  224. void StaticModel::SetBoundingBox(const BoundingBox& box)
  225. {
  226. boundingBox_ = box;
  227. OnMarkedDirty(node_);
  228. }
  229. void StaticModel::SetNumGeometries(unsigned num)
  230. {
  231. geometries_.Resize(num);
  232. materials_.Resize(num);
  233. ResetLodLevels();
  234. }
  235. void StaticModel::SetModelAttr(ResourceRef value)
  236. {
  237. ResourceCache* cache = GetSubsystem<ResourceCache>();
  238. SetModel(cache->GetResource<Model>(value.id_));
  239. }
  240. void StaticModel::SetMaterialsAttr(ResourceRefList value)
  241. {
  242. ResourceCache* cache = GetSubsystem<ResourceCache>();
  243. for (unsigned i = 0; i < value.ids_.Size(); ++i)
  244. SetMaterial(i, cache->GetResource<Material>(value.ids_[i]));
  245. }
  246. ResourceRef StaticModel::GetModelAttr() const
  247. {
  248. return GetResourceRef(model_, Model::GetTypeStatic());
  249. }
  250. ResourceRefList StaticModel::GetMaterialsAttr() const
  251. {
  252. return GetResourceRefList(materials_);
  253. }
  254. void StaticModel::OnWorldBoundingBoxUpdate()
  255. {
  256. worldBoundingBox_ = boundingBox_.Transformed(GetWorldTransform());
  257. }
  258. void StaticModel::ResetLodLevels()
  259. {
  260. // Ensure that each subgeometry has at least one LOD level, and reset the current LOD level
  261. lodLevels_.Resize(geometries_.Size());
  262. for (unsigned i = 0; i < geometries_.Size(); ++i)
  263. {
  264. if (!geometries_[i].Size())
  265. geometries_[i].Resize(1);
  266. lodLevels_[i] = 0;
  267. }
  268. // Find out the real LOD levels on next geometry update
  269. lodLevelsDirty_ = true;
  270. }
  271. void StaticModel::CalculateLodLevels()
  272. {
  273. for (unsigned i = 0; i < geometries_.Size(); ++i)
  274. {
  275. unsigned j;
  276. for (j = 1; j < geometries_[i].Size(); ++j)
  277. {
  278. if (geometries_[i][j] && lodDistance_ <= geometries_[i][j]->GetLodDistance())
  279. break;
  280. }
  281. lodLevels_[i] = j - 1;
  282. }
  283. lodLevelsDirty_ = false;
  284. }
  285. void StaticModel::HandleModelReloadFinished(StringHash eventType, VariantMap& eventData)
  286. {
  287. Model* currentModel = model_;
  288. model_ = 0; // Set null to allow to be re-set
  289. SetModel(currentModel);
  290. }