StaticModel.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 "Camera.h"
  25. #include "Geometry.h"
  26. #include "Log.h"
  27. #include "Material.h"
  28. #include "Model.h"
  29. #include "OcclusionBuffer.h"
  30. #include "OctreeQuery.h"
  31. #include "Profiler.h"
  32. #include "ReplicationUtils.h"
  33. #include "ResourceCache.h"
  34. #include "StaticModel.h"
  35. #include "XMLElement.h"
  36. #include "DebugNew.h"
  37. StaticModel::StaticModel(Octant* octant, const std::string& name) :
  38. GeometryNode(NODE_STATICMODEL, octant, name)
  39. {
  40. }
  41. StaticModel::StaticModel(unsigned flags, Octant* octant, const std::string& name) :
  42. GeometryNode(flags, octant, name)
  43. {
  44. }
  45. StaticModel::~StaticModel()
  46. {
  47. }
  48. void StaticModel::save(Serializer& dest)
  49. {
  50. // Write GeometryNode properties
  51. GeometryNode::save(dest);
  52. // Write StaticModel properties
  53. dest.writeStringHash(getResourceHash(mModel));
  54. dest.writeVLE(mMaterials.size());
  55. for (unsigned i = 0; i < mMaterials.size(); ++i)
  56. dest.writeStringHash(getResourceHash(mMaterials[i]));
  57. }
  58. void StaticModel::load(Deserializer& source, ResourceCache* cache)
  59. {
  60. // Read GeometryNode properties
  61. GeometryNode::load(source, cache);
  62. // Read StaticModel properties
  63. setModel(cache->getResource<Model>(source.readStringHash()));
  64. unsigned numMaterials = source.readVLE();
  65. for (unsigned i = 0; i < numMaterials; ++i)
  66. setMaterial(i, cache->getResource<Material>(source.readStringHash()));
  67. }
  68. void StaticModel::saveXML(XMLElement& dest)
  69. {
  70. // Write GeometryNode properties
  71. GeometryNode::saveXML(dest);
  72. // Write StaticModel properties
  73. XMLElement modelElem = dest.createChildElement("model");
  74. modelElem.setString("name", getResourceName(mModel));
  75. for (unsigned i = 0; i < mMaterials.size(); ++i)
  76. {
  77. XMLElement materialElem = dest.createChildElement("material");
  78. materialElem.setInt("index", i);
  79. materialElem.setString("name", getResourceName(mMaterials[i]));
  80. }
  81. }
  82. void StaticModel::loadXML(const XMLElement& source, ResourceCache* cache)
  83. {
  84. // Read GeometryNode properties
  85. GeometryNode::loadXML(source, cache);
  86. // Read StaticModel properties
  87. XMLElement modelElem = source.getChildElement("model");
  88. setModel(cache->getResource<Model>(modelElem.getString("name")));
  89. XMLElement materialElem = source.getChildElement("material");
  90. while (materialElem)
  91. {
  92. unsigned index = materialElem.getInt("index");
  93. setMaterial(index, cache->getResource<Material>(materialElem.getString("name")));
  94. materialElem = materialElem.getNextElement("material");
  95. }
  96. }
  97. bool StaticModel::writeNetUpdate(Serializer& dest, Serializer& destRevision, Deserializer& baseRevision, const NetUpdateInfo& info)
  98. {
  99. // Write GeometryNode properties and see if there were any changes
  100. bool prevBits = GeometryNode::writeNetUpdate(dest, destRevision, baseRevision, info);
  101. // Build bitmask of changed properties
  102. unsigned char bits = 0;
  103. checkStringHash(getResourceHash(mModel), baseRevision, bits, 1);
  104. unsigned numBaseMaterials = baseRevision.getSize() ? baseRevision.readVLE() : 0;
  105. if (mMaterials.size() != numBaseMaterials)
  106. bits |= 2;
  107. for (unsigned i = 0; i < numBaseMaterials; ++i)
  108. {
  109. if (i < mMaterials.size())
  110. checkStringHash(getResourceHash(mMaterials[i]), baseRevision, bits, 2);
  111. else
  112. baseRevision.readStringHash();
  113. }
  114. // Update replication state fully, and network stream by delta
  115. dest.writeUByte(bits);
  116. writeStringHashDelta(getResourceHash(mModel), dest, destRevision, bits & 1);
  117. writeVLEDelta(mMaterials.size(), dest, destRevision, bits & 2);
  118. for (unsigned i = 0; i < mMaterials.size(); ++i)
  119. writeStringHashDelta(getResourceHash(mMaterials[i]), dest, destRevision, bits & 2);
  120. return prevBits || (bits != 0);
  121. }
  122. void StaticModel::readNetUpdate(Deserializer& source, ResourceCache* cache, const NetUpdateInfo& info)
  123. {
  124. // Read GeometryNode properties
  125. GeometryNode::readNetUpdate(source, cache, info);
  126. unsigned char bits = source.readUByte();
  127. if (bits & 1)
  128. setModel(cache->getResource<Model>(source.readStringHash()));
  129. if (bits & 2)
  130. {
  131. unsigned numMaterials = source.readVLE();
  132. for (unsigned i = 0; i < numMaterials; ++i)
  133. setMaterial(i, cache->getResource<Material>(source.readStringHash()));
  134. }
  135. }
  136. void StaticModel::getResourceRefs(std::vector<Resource*>& dest)
  137. {
  138. if (mModel)
  139. dest.push_back(mModel);
  140. for (unsigned i = 0; i < mMaterials.size(); ++i)
  141. {
  142. if (mMaterials[i])
  143. dest.push_back(mMaterials[i]);
  144. }
  145. }
  146. void StaticModel::processRayQuery(RayOctreeQuery& query, float initialDistance)
  147. {
  148. PROFILE(StaticModel_Raycast);
  149. RayQueryLevel level = query.mLevel;
  150. switch (level)
  151. {
  152. case RAY_AABB_NOSUBOBJECTS:
  153. case RAY_AABB:
  154. {
  155. RayQueryResult result;
  156. result.mNode = this;
  157. result.mDistance = initialDistance;
  158. query.mResult.push_back(result);
  159. }
  160. break;
  161. case RAY_OBB:
  162. {
  163. Matrix4x3 inverse = getWorldTransform().getInverse();
  164. Ray localRay(inverse * query.mRay.mOrigin, inverse * Vector4(query.mRay.mDirection, 0.0f));
  165. float distance = mBoundingBox.getDistance(localRay);
  166. if (distance < query.mMaxDistance)
  167. {
  168. RayQueryResult result;
  169. result.mNode = this;
  170. result.mDistance = distance;
  171. query.mResult.push_back(result);
  172. }
  173. }
  174. break;
  175. case RAY_TRIANGLE:
  176. {
  177. // Do a pretest using the OBB
  178. Matrix4x3 inverse = getWorldTransform().getInverse();
  179. Ray localRay(inverse * query.mRay.mOrigin, inverse * Vector4(query.mRay.mDirection, 0.0f));
  180. float distance = mBoundingBox.getDistance(localRay);
  181. if (distance < query.mMaxDistance)
  182. {
  183. // Then the actual test using triangle geometry
  184. for (unsigned i = 0; i < mGeometries.size(); ++i)
  185. {
  186. unsigned lodLevel = mModel->getRaycastLodLevel();
  187. if (lodLevel >= mGeometries[i].size())
  188. lodLevel = mLodLevels[i];
  189. Geometry* geom = mGeometries[i][lodLevel];
  190. if (geom)
  191. {
  192. distance = geom->getDistance(localRay);
  193. if (distance < query.mMaxDistance)
  194. {
  195. RayQueryResult result;
  196. result.mNode = this;
  197. result.mDistance = distance;
  198. query.mResult.push_back(result);
  199. break;
  200. }
  201. }
  202. }
  203. }
  204. }
  205. break;
  206. }
  207. }
  208. void StaticModel::updateGeometry(const FrameInfo& frame, Renderer* renderer)
  209. {
  210. if (mLodLevelsDirty)
  211. calculateLodLevels();
  212. }
  213. unsigned StaticModel::getNumBatches()
  214. {
  215. return mGeometries.size();
  216. }
  217. Geometry* StaticModel::getBatchGeometry(unsigned batchIndex)
  218. {
  219. return mGeometries[batchIndex][mLodLevels[batchIndex]];
  220. }
  221. Material* StaticModel::getBatchMaterial(unsigned batchIndex)
  222. {
  223. return mMaterials[batchIndex];
  224. }
  225. bool StaticModel::drawOcclusion(OcclusionBuffer* buffer)
  226. {
  227. bool success = true;
  228. for (unsigned i = 0; i < mGeometries.size(); ++i)
  229. {
  230. // Use designated LOD level for occlusion, or if out of range, same as visible
  231. unsigned lodLevel = mModel->getOcclusionLodLevel();
  232. if (lodLevel >= mGeometries[i].size())
  233. lodLevel = mLodLevels[i];
  234. Geometry* geom = mGeometries[i][lodLevel];
  235. if (!geom)
  236. continue;
  237. // Check that the material is suitable for occlusion (default material always is)
  238. // and set culling mode
  239. Material* mat = mMaterials[i];
  240. if (mat)
  241. {
  242. if (!mat->getOcclusion())
  243. continue;
  244. buffer->setCullMode(mat->getOcclusionCullMode());
  245. }
  246. else
  247. buffer->setCullMode(CULL_CCW);
  248. const unsigned char* vertexData;
  249. unsigned vertexSize;
  250. const unsigned char* indexData;
  251. unsigned indexSize;
  252. geom->lockRawData(vertexData, vertexSize, indexData, indexSize);
  253. // Check for valid geometry data
  254. if ((!vertexData) || (!indexData))
  255. continue;
  256. unsigned indexStart = geom->getIndexStart();
  257. unsigned indexCount = geom->getIndexCount();
  258. // Draw and check for running out of triangles
  259. if (!buffer->draw(getWorldTransform(), vertexData, vertexSize, indexData, indexSize, indexStart, indexCount))
  260. success = false;
  261. geom->unlockRawData();
  262. if (!success)
  263. break;
  264. }
  265. return success;
  266. }
  267. void StaticModel::setModel(Model* model)
  268. {
  269. if (model == mModel)
  270. return;
  271. PROFILE(StaticModel_SetModel);
  272. if (!model)
  273. return;
  274. mModel = model;
  275. // Copy the subgeometry & LOD level structure
  276. setNumGeometries(model->getNumGeometries());
  277. const std::vector<std::vector<SharedPtr<Geometry> > >& geometries = model->getGeometries();
  278. for (unsigned i = 0; i < geometries.size(); ++i)
  279. mGeometries[i] = geometries[i];
  280. setBoundingBox(model->getBoundingBox());
  281. resetLodLevels();
  282. }
  283. void StaticModel::setMaterial(Material* material)
  284. {
  285. for (unsigned i = 0; i < mMaterials.size(); ++i)
  286. mMaterials[i] = material;
  287. }
  288. bool StaticModel::setMaterial(unsigned index, Material* material)
  289. {
  290. if (index >= mMaterials.size())
  291. {
  292. LOGERROR("Illegal material index");
  293. return false;
  294. }
  295. mMaterials[index] = material;
  296. return true;
  297. }
  298. Material* StaticModel::getMaterial(unsigned index) const
  299. {
  300. if (index >= mMaterials.size())
  301. return 0;
  302. return mMaterials[index];
  303. }
  304. void StaticModel::setBoundingBox(const BoundingBox& box)
  305. {
  306. mBoundingBox = box;
  307. VolumeNode::onMarkedDirty();
  308. }
  309. void StaticModel::setNumGeometries(unsigned num)
  310. {
  311. mGeometries.resize(num);
  312. mMaterials.resize(num);
  313. resetLodLevels();
  314. }
  315. void StaticModel::onWorldBoundingBoxUpdate(BoundingBox& worldBoundingBox)
  316. {
  317. worldBoundingBox = mBoundingBox.getTransformed(getWorldTransform());
  318. }
  319. void StaticModel::resetLodLevels()
  320. {
  321. // Ensure that each subgeometry has at least one LOD level, and reset the current LOD level
  322. mLodLevels.resize(mGeometries.size());
  323. for (unsigned i = 0; i < mGeometries.size(); ++i)
  324. {
  325. if (!mGeometries[i].size())
  326. mGeometries[i].resize(1);
  327. mLodLevels[i] = 0;
  328. }
  329. // Find out the real LOD levels on next geometry update
  330. mLodLevelsDirty = true;
  331. }
  332. void StaticModel::calculateLodLevels()
  333. {
  334. for (unsigned i = 0; i < mGeometries.size(); ++i)
  335. {
  336. unsigned j;
  337. for (j = 1; j < mGeometries[i].size(); ++j)
  338. {
  339. if ((mGeometries[i][j]) && (mLodDistance <= mGeometries[i][j]->getLodDistance()))
  340. break;
  341. }
  342. mLodLevels[i] = j - 1;
  343. }
  344. mLodLevelsDirty = false;
  345. }