StaticModel.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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::processRayQuery(RayOctreeQuery& query, float initialDistance)
  137. {
  138. PROFILE(StaticModel_Raycast);
  139. RayQueryLevel level = query.mLevel;
  140. switch (level)
  141. {
  142. case RAY_AABB_NOSUBOBJECTS:
  143. case RAY_AABB:
  144. {
  145. RayQueryResult result;
  146. result.mNode = this;
  147. result.mDistance = initialDistance;
  148. query.mResult.push_back(result);
  149. }
  150. break;
  151. case RAY_OBB:
  152. {
  153. Matrix4x3 inverse = getWorldTransform().getInverse();
  154. Ray localRay(inverse * query.mRay.mOrigin, inverse * Vector4(query.mRay.mDirection, 0.0f));
  155. float distance = mBoundingBox.getDistance(localRay);
  156. if (distance < query.mMaxDistance)
  157. {
  158. RayQueryResult result;
  159. result.mNode = this;
  160. result.mDistance = distance;
  161. query.mResult.push_back(result);
  162. }
  163. }
  164. break;
  165. case RAY_TRIANGLE:
  166. {
  167. // Do a pretest using the OBB
  168. Matrix4x3 inverse = getWorldTransform().getInverse();
  169. Ray localRay(inverse * query.mRay.mOrigin, inverse * Vector4(query.mRay.mDirection, 0.0f));
  170. float distance = mBoundingBox.getDistance(localRay);
  171. if (distance < query.mMaxDistance)
  172. {
  173. // Then the actual test using triangle geometry
  174. for (unsigned i = 0; i < mGeometries.size(); ++i)
  175. {
  176. unsigned lodLevel = mModel->getRaycastLodLevel();
  177. if (lodLevel >= mGeometries[i].size())
  178. lodLevel = mLodLevels[i];
  179. Geometry* geom = mGeometries[i][lodLevel];
  180. if (geom)
  181. {
  182. distance = geom->getDistance(localRay);
  183. if (distance < query.mMaxDistance)
  184. {
  185. RayQueryResult result;
  186. result.mNode = this;
  187. result.mDistance = distance;
  188. query.mResult.push_back(result);
  189. break;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. break;
  196. }
  197. }
  198. void StaticModel::updateGeometry(const FrameInfo& frame, Renderer* renderer)
  199. {
  200. if (mLodLevelsDirty)
  201. calculateLodLevels();
  202. }
  203. unsigned StaticModel::getNumBatches()
  204. {
  205. return mGeometries.size();
  206. }
  207. Geometry* StaticModel::getBatchGeometry(unsigned batchIndex)
  208. {
  209. return mGeometries[batchIndex][mLodLevels[batchIndex]];
  210. }
  211. Material* StaticModel::getBatchMaterial(unsigned batchIndex)
  212. {
  213. return mMaterials[batchIndex];
  214. }
  215. bool StaticModel::drawOcclusion(OcclusionBuffer* buffer)
  216. {
  217. bool success = true;
  218. for (unsigned i = 0; i < mGeometries.size(); ++i)
  219. {
  220. // Use designated LOD level for occlusion, or if out of range, same as visible
  221. unsigned lodLevel = mModel->getOcclusionLodLevel();
  222. if (lodLevel >= mGeometries[i].size())
  223. lodLevel = mLodLevels[i];
  224. Geometry* geom = mGeometries[i][lodLevel];
  225. if (!geom)
  226. continue;
  227. // Check that the material is suitable for occlusion (default material always is)
  228. // and set culling mode
  229. Material* mat = mMaterials[i];
  230. if (mat)
  231. {
  232. if (!mat->getOcclusion())
  233. continue;
  234. buffer->setCullMode(mat->getOcclusionCullMode());
  235. }
  236. else
  237. buffer->setCullMode(CULL_CCW);
  238. const unsigned char* vertexData;
  239. unsigned vertexSize;
  240. const unsigned char* indexData;
  241. unsigned indexSize;
  242. geom->lockRawData(vertexData, vertexSize, indexData, indexSize);
  243. // Check for valid geometry data
  244. if ((!vertexData) || (!indexData))
  245. continue;
  246. unsigned indexStart = geom->getIndexStart();
  247. unsigned indexCount = geom->getIndexCount();
  248. // Draw and check for running out of triangles
  249. if (!buffer->draw(getWorldTransform(), vertexData, vertexSize, indexData, indexSize, indexStart, indexCount))
  250. success = false;
  251. geom->unlockRawData();
  252. if (!success)
  253. break;
  254. }
  255. return success;
  256. }
  257. bool StaticModel::setModel(Model* model)
  258. {
  259. PROFILE(StaticModel_SetModel);
  260. if (!model)
  261. {
  262. LOGERROR("Null model for StaticModel");
  263. return false;
  264. }
  265. if (model == mModel)
  266. return true;
  267. mModel = model;
  268. // Copy the subgeometry & LOD level structure
  269. setNumGeometries(model->getNumGeometries());
  270. const std::vector<std::vector<SharedPtr<Geometry> > >& geometries = model->getGeometries();
  271. for (unsigned i = 0; i < geometries.size(); ++i)
  272. mGeometries[i] = geometries[i];
  273. setBoundingBox(model->getBoundingBox());
  274. resetLodLevels();
  275. return true;
  276. }
  277. void StaticModel::setMaterial(Material* material)
  278. {
  279. for (unsigned i = 0; i < mMaterials.size(); ++i)
  280. mMaterials[i] = material;
  281. }
  282. bool StaticModel::setMaterial(unsigned index, Material* material)
  283. {
  284. if (index >= mMaterials.size())
  285. {
  286. LOGERROR("Illegal material index");
  287. return false;
  288. }
  289. mMaterials[index] = material;
  290. return true;
  291. }
  292. Material* StaticModel::getMaterial(unsigned index) const
  293. {
  294. if (index >= mMaterials.size())
  295. return 0;
  296. return mMaterials[index];
  297. }
  298. void StaticModel::setBoundingBox(const BoundingBox& box)
  299. {
  300. mBoundingBox = box;
  301. VolumeNode::onMarkedDirty();
  302. }
  303. void StaticModel::setNumGeometries(unsigned num)
  304. {
  305. mGeometries.resize(num);
  306. mMaterials.resize(num);
  307. resetLodLevels();
  308. }
  309. void StaticModel::onWorldBoundingBoxUpdate(BoundingBox& worldBoundingBox)
  310. {
  311. worldBoundingBox = mBoundingBox.getTransformed(getWorldTransform());
  312. }
  313. void StaticModel::resetLodLevels()
  314. {
  315. // Ensure that each subgeometry has at least one LOD level, and reset the current LOD level
  316. mLodLevels.resize(mGeometries.size());
  317. for (unsigned i = 0; i < mGeometries.size(); ++i)
  318. {
  319. if (!mGeometries[i].size())
  320. mGeometries[i].resize(1);
  321. mLodLevels[i] = 0;
  322. }
  323. // Find out the real LOD levels on next geometry update
  324. mLodLevelsDirty = true;
  325. }
  326. void StaticModel::calculateLodLevels()
  327. {
  328. for (unsigned i = 0; i < mGeometries.size(); ++i)
  329. {
  330. unsigned j;
  331. for (j = 1; j < mGeometries[i].size(); ++j)
  332. {
  333. if ((mGeometries[i][j]) && (mLodDistance <= mGeometries[i][j]->getLodDistance()))
  334. break;
  335. }
  336. mLodLevels[i] = j - 1;
  337. }
  338. mLodLevelsDirty = false;
  339. }