CustomGeometry.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Batch.h"
  24. #include "Camera.h"
  25. #include "Context.h"
  26. #include "CustomGeometry.h"
  27. #include "Geometry.h"
  28. #include "Log.h"
  29. #include "Material.h"
  30. #include "MemoryBuffer.h"
  31. #include "Node.h"
  32. #include "OcclusionBuffer.h"
  33. #include "OctreeQuery.h"
  34. #include "Profiler.h"
  35. #include "ResourceCache.h"
  36. #include "VectorBuffer.h"
  37. #include "VertexBuffer.h"
  38. #include "DebugNew.h"
  39. namespace Urho3D
  40. {
  41. extern const char* GEOMETRY_CATEGORY;
  42. CustomGeometry::CustomGeometry(Context* context) :
  43. Drawable(context, DRAWABLE_GEOMETRY),
  44. vertexBuffer_(new VertexBuffer(context)),
  45. elementMask_(MASK_POSITION),
  46. geometryIndex_(0),
  47. materialsAttr_(Material::GetTypeStatic())
  48. {
  49. vertexBuffer_->SetShadowed(true);
  50. SetNumGeometries(1);
  51. }
  52. CustomGeometry::~CustomGeometry()
  53. {
  54. }
  55. void CustomGeometry::RegisterObject(Context* context)
  56. {
  57. context->RegisterFactory<CustomGeometry>(GEOMETRY_CATEGORY);
  58. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  59. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_BUFFER, "Geometry Data", GetGeometryDataAttr, SetGeometryDataAttr, PODVector<unsigned char>, Variant::emptyBuffer, AM_FILE|AM_NOEDIT);
  60. REF_ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_RESOURCEREFLIST, "Materials", GetMaterialsAttr, SetMaterialsAttr, ResourceRefList, ResourceRefList(Material::GetTypeStatic()), AM_DEFAULT);
  61. ATTRIBUTE(CustomGeometry, VAR_BOOL, "Is Occluder", occluder_, false, AM_DEFAULT);
  62. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_BOOL, "Can Be Occluded", IsOccludee, SetOccludee, bool, true, AM_DEFAULT);
  63. ATTRIBUTE(CustomGeometry, VAR_BOOL, "Cast Shadows", castShadows_, false, AM_DEFAULT);
  64. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_FLOAT, "Draw Distance", GetDrawDistance, SetDrawDistance, float, 0.0f, AM_DEFAULT);
  65. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_FLOAT, "Shadow Distance", GetShadowDistance, SetShadowDistance, float, 0.0f, AM_DEFAULT);
  66. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_FLOAT, "LOD Bias", GetLodBias, SetLodBias, float, 1.0f, AM_DEFAULT);
  67. COPY_BASE_ATTRIBUTES(CustomGeometry, Drawable);
  68. }
  69. void CustomGeometry::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  70. {
  71. RayQueryLevel level = query.level_;
  72. switch (level)
  73. {
  74. case RAY_AABB_NOSUBOBJECTS:
  75. case RAY_AABB:
  76. Drawable::ProcessRayQuery(query, results);
  77. break;
  78. case RAY_OBB:
  79. case RAY_TRIANGLE:
  80. Matrix3x4 inverse(node_->GetWorldTransform().Inverse());
  81. Ray localRay = query.ray_.Transformed(inverse);
  82. float distance = localRay.HitDistance(boundingBox_);
  83. if (distance < query.maxDistance_)
  84. {
  85. if (level == RAY_TRIANGLE)
  86. {
  87. for (unsigned i = 0; i < batches_.Size(); ++i)
  88. {
  89. Geometry* geometry = batches_[i].geometry_;
  90. if (geometry)
  91. {
  92. distance = geometry->GetHitDistance(localRay);
  93. if (distance < query.maxDistance_)
  94. {
  95. RayQueryResult result;
  96. result.drawable_ = this;
  97. result.node_ = node_;
  98. result.distance_ = distance;
  99. result.subObject_ = M_MAX_UNSIGNED;
  100. results.Push(result);
  101. break;
  102. }
  103. }
  104. }
  105. }
  106. else
  107. {
  108. RayQueryResult result;
  109. result.drawable_ = this;
  110. result.node_ = node_;
  111. result.distance_ = distance;
  112. result.subObject_ = M_MAX_UNSIGNED;
  113. results.Push(result);
  114. }
  115. }
  116. break;
  117. }
  118. }
  119. Geometry* CustomGeometry::GetLodGeometry(unsigned batchIndex, unsigned level)
  120. {
  121. return batchIndex < geometries_.Size() ? geometries_[batchIndex] : (Geometry*)0;
  122. }
  123. unsigned CustomGeometry::GetNumOccluderTriangles()
  124. {
  125. unsigned triangles = 0;
  126. for (unsigned i = 0; i < batches_.Size(); ++i)
  127. {
  128. Geometry* geometry = GetLodGeometry(i, 0);
  129. if (!geometry)
  130. continue;
  131. // Check that the material is suitable for occlusion (default material always is)
  132. Material* mat = batches_[i].material_;
  133. if (mat && !mat->GetOcclusion())
  134. continue;
  135. triangles += geometry->GetVertexCount() / 3;
  136. }
  137. return triangles;
  138. }
  139. bool CustomGeometry::DrawOcclusion(OcclusionBuffer* buffer)
  140. {
  141. bool success = true;
  142. for (unsigned i = 0; i < batches_.Size(); ++i)
  143. {
  144. Geometry* geometry = GetLodGeometry(i, 0);
  145. if (!geometry)
  146. continue;
  147. // Check that the material is suitable for occlusion (default material always is) and set culling mode
  148. Material* material = batches_[i].material_;
  149. if (material)
  150. {
  151. if (!material->GetOcclusion())
  152. continue;
  153. buffer->SetCullMode(material->GetCullMode());
  154. }
  155. else
  156. buffer->SetCullMode(CULL_CCW);
  157. const unsigned char* vertexData;
  158. unsigned vertexSize;
  159. const unsigned char* indexData;
  160. unsigned indexSize;
  161. unsigned elementMask;
  162. geometry->GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  163. // Check for valid geometry data
  164. if (!vertexData)
  165. continue;
  166. // Draw and check for running out of triangles
  167. success = buffer->Draw(node_->GetWorldTransform(), vertexData, vertexSize, geometry->GetVertexStart(), geometry->GetVertexCount());
  168. if (!success)
  169. break;
  170. }
  171. return success;
  172. }
  173. void CustomGeometry::Clear()
  174. {
  175. elementMask_ = MASK_POSITION;
  176. batches_.Clear();
  177. geometries_.Clear();
  178. primitiveTypes_.Clear();
  179. vertices_.Clear();
  180. }
  181. void CustomGeometry::SetNumGeometries(unsigned num)
  182. {
  183. batches_.Resize(num);
  184. geometries_.Resize(num);
  185. primitiveTypes_.Resize(num);
  186. vertices_.Resize(num);
  187. for (unsigned i = 0; i < geometries_.Size(); ++i)
  188. {
  189. if (!geometries_[i])
  190. geometries_[i] = new Geometry(context_);
  191. batches_[i].geometry_ = geometries_[i];
  192. }
  193. }
  194. void CustomGeometry::BeginGeometry(unsigned index, PrimitiveType type)
  195. {
  196. if (index > geometries_.Size())
  197. {
  198. LOGERROR("Geometry index out of bounds");
  199. return;
  200. }
  201. geometryIndex_ = index;
  202. primitiveTypes_[index] = type;
  203. vertices_[index].Clear();
  204. }
  205. void CustomGeometry::DefineVertex(const Vector3& position)
  206. {
  207. if (vertices_.Size() < geometryIndex_)
  208. return;
  209. vertices_[geometryIndex_].Resize(vertices_[geometryIndex_].Size() + 1);
  210. vertices_[geometryIndex_].Back().position_ = position;
  211. }
  212. void CustomGeometry::DefineNormal(const Vector3& normal)
  213. {
  214. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  215. return;
  216. vertices_[geometryIndex_].Back().normal_ = normal;
  217. elementMask_ |= MASK_NORMAL;
  218. }
  219. void CustomGeometry::DefineColor(const Color& color)
  220. {
  221. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  222. return;
  223. vertices_[geometryIndex_].Back().color_ = color.ToUInt();
  224. elementMask_ |= MASK_COLOR;
  225. }
  226. void CustomGeometry::DefineTexCoord(const Vector2& texCoord)
  227. {
  228. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  229. return;
  230. vertices_[geometryIndex_].Back().texCoord_ = texCoord;
  231. elementMask_ |= MASK_TEXCOORD1;
  232. }
  233. void CustomGeometry::DefineTangent(const Vector4& tangent)
  234. {
  235. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  236. return;
  237. vertices_[geometryIndex_].Back().tangent_ = tangent;
  238. elementMask_ |= MASK_TANGENT;
  239. }
  240. void CustomGeometry::Commit()
  241. {
  242. PROFILE(CommitCustomGeometry);
  243. unsigned totalVertices = 0;
  244. boundingBox_.Clear();
  245. for (unsigned i = 0; i < vertices_.Size(); ++i)
  246. {
  247. totalVertices += vertices_[i].Size();
  248. for (unsigned j = 0; j < vertices_[i].Size(); ++j)
  249. boundingBox_.Merge(vertices_[i][j].position_);
  250. }
  251. vertexBuffer_->SetSize(totalVertices, elementMask_);
  252. if (totalVertices)
  253. {
  254. unsigned char* dest = (unsigned char*)vertexBuffer_->Lock(0, totalVertices, true);
  255. if (dest)
  256. {
  257. unsigned vertexStart = 0;
  258. for (unsigned i = 0; i < vertices_.Size(); ++i)
  259. {
  260. unsigned vertexCount = 0;
  261. for (unsigned j = 0; j < vertices_[i].Size(); ++j)
  262. {
  263. *((Vector3*)dest) = vertices_[i][j].position_;
  264. dest += sizeof(Vector3);
  265. if (elementMask_ & MASK_NORMAL)
  266. {
  267. *((Vector3*)dest) = vertices_[i][j].normal_;
  268. dest += sizeof(Vector3);
  269. }
  270. if (elementMask_ & MASK_COLOR)
  271. {
  272. *((unsigned*)dest) = vertices_[i][j].color_;
  273. dest += sizeof(unsigned);
  274. }
  275. if (elementMask_ & MASK_TEXCOORD1)
  276. {
  277. *((Vector2*)dest) = vertices_[i][j].texCoord_;
  278. dest += sizeof(Vector2);
  279. }
  280. if (elementMask_ & MASK_TANGENT)
  281. {
  282. *((Vector4*)dest) = vertices_[i][j].tangent_;
  283. dest += sizeof(Vector4);
  284. }
  285. ++vertexCount;
  286. }
  287. geometries_[i]->SetVertexBuffer(0, vertexBuffer_, elementMask_);
  288. geometries_[i]->SetDrawRange(primitiveTypes_[i], 0, 0, vertexStart, vertexCount);
  289. vertexStart += vertexCount;
  290. }
  291. vertexBuffer_->Unlock();
  292. }
  293. else
  294. LOGERROR("Failed to lock custom geometry vertex buffer");
  295. }
  296. else
  297. {
  298. for (unsigned i = 0; i < geometries_.Size(); ++i)
  299. {
  300. geometries_[i]->SetVertexBuffer(0, vertexBuffer_, elementMask_);
  301. geometries_[i]->SetDrawRange(primitiveTypes_[i], 0, 0, 0, 0);
  302. }
  303. }
  304. vertexBuffer_->ClearDataLost();
  305. }
  306. void CustomGeometry::SetMaterial(Material* material)
  307. {
  308. for (unsigned i = 0; i < batches_.Size(); ++i)
  309. batches_[i].material_ = material;
  310. MarkNetworkUpdate();
  311. }
  312. bool CustomGeometry::SetMaterial(unsigned index, Material* material)
  313. {
  314. if (index >= batches_.Size())
  315. {
  316. LOGERROR("Material index out of bounds");
  317. return false;
  318. }
  319. batches_[index].material_ = material;
  320. MarkNetworkUpdate();
  321. return true;
  322. }
  323. Material* CustomGeometry::GetMaterial(unsigned index) const
  324. {
  325. return index < batches_.Size() ? batches_[index].material_ : (Material*)0;
  326. }
  327. void CustomGeometry::SetGeometryDataAttr(PODVector<unsigned char> value)
  328. {
  329. if (value.Empty())
  330. return;
  331. MemoryBuffer buffer(value);
  332. SetNumGeometries(buffer.ReadVLE());
  333. elementMask_ = buffer.ReadUInt();
  334. for (unsigned i = 0; i < geometries_.Size(); ++i)
  335. {
  336. unsigned numVertices = buffer.ReadVLE();
  337. vertices_[i].Resize(numVertices);
  338. primitiveTypes_[i] = (PrimitiveType)buffer.ReadUByte();
  339. for (unsigned j = 0; j < numVertices; ++j)
  340. {
  341. if (elementMask_ & MASK_POSITION)
  342. vertices_[i][j].position_ = buffer.ReadVector3();
  343. if (elementMask_ & MASK_NORMAL)
  344. vertices_[i][j].normal_ = buffer.ReadVector3();
  345. if (elementMask_ & MASK_COLOR)
  346. vertices_[i][j].color_ = buffer.ReadUInt();
  347. if (elementMask_ & MASK_TEXCOORD1)
  348. vertices_[i][j].texCoord_ = buffer.ReadVector2();
  349. if (elementMask_ & MASK_TANGENT)
  350. vertices_[i][j].tangent_ = buffer.ReadVector4();
  351. }
  352. }
  353. Commit();
  354. }
  355. void CustomGeometry::SetMaterialsAttr(const ResourceRefList& value)
  356. {
  357. ResourceCache* cache = GetSubsystem<ResourceCache>();
  358. for (unsigned i = 0; i < value.ids_.Size(); ++i)
  359. SetMaterial(i, cache->GetResource<Material>(value.ids_[i]));
  360. }
  361. PODVector<unsigned char> CustomGeometry::GetGeometryDataAttr() const
  362. {
  363. VectorBuffer ret;
  364. ret.WriteVLE(geometries_.Size());
  365. ret.WriteUInt(elementMask_);
  366. for (unsigned i = 0; i < geometries_.Size(); ++i)
  367. {
  368. unsigned numVertices = vertices_[i].Size();
  369. ret.WriteVLE(numVertices);
  370. ret.WriteUByte(primitiveTypes_[i]);
  371. for (unsigned j = 0; j < numVertices; ++j)
  372. {
  373. if (elementMask_ & MASK_POSITION)
  374. ret.WriteVector3(vertices_[i][j].position_);
  375. if (elementMask_ & MASK_NORMAL)
  376. ret.WriteVector3(vertices_[i][j].normal_);
  377. if (elementMask_ & MASK_COLOR)
  378. ret.WriteUInt(vertices_[i][j].color_);
  379. if (elementMask_ & MASK_TEXCOORD1)
  380. ret.WriteVector2(vertices_[i][j].texCoord_);
  381. if (elementMask_ & MASK_TANGENT)
  382. ret.WriteVector4(vertices_[i][j].tangent_);
  383. }
  384. }
  385. return ret.GetBuffer();
  386. }
  387. const ResourceRefList& CustomGeometry::GetMaterialsAttr() const
  388. {
  389. materialsAttr_.ids_.Resize(batches_.Size());
  390. for (unsigned i = 0; i < batches_.Size(); ++i)
  391. materialsAttr_.ids_[i] = batches_[i].material_ ? batches_[i].material_->GetNameHash() : StringHash();
  392. return materialsAttr_;
  393. }
  394. void CustomGeometry::OnWorldBoundingBoxUpdate()
  395. {
  396. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  397. }
  398. }