CustomGeometry.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. OBJECTTYPESTATIC(CustomGeometry);
  43. CustomGeometry::CustomGeometry(Context* context) :
  44. Drawable(context, DRAWABLE_GEOMETRY),
  45. vertexBuffer_(new VertexBuffer(context)),
  46. elementMask_(MASK_POSITION),
  47. geometryIndex_(0),
  48. materialsAttr_(Material::GetTypeStatic())
  49. {
  50. vertexBuffer_->SetShadowed(true);
  51. SetNumGeometries(1);
  52. }
  53. CustomGeometry::~CustomGeometry()
  54. {
  55. }
  56. void CustomGeometry::RegisterObject(Context* context)
  57. {
  58. context->RegisterFactory<CustomGeometry>(GEOMETRY_CATEGORY);
  59. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  60. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_BUFFER, "Geometry Data", GetGeometryDataAttr, SetGeometryDataAttr, PODVector<unsigned char>, Variant::emptyBuffer, AM_FILE|AM_NOEDIT);
  61. REF_ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_RESOURCEREFLIST, "Materials", GetMaterialsAttr, SetMaterialsAttr, ResourceRefList, ResourceRefList(Material::GetTypeStatic()), AM_DEFAULT);
  62. ATTRIBUTE(CustomGeometry, VAR_BOOL, "Is Occluder", occluder_, false, AM_DEFAULT);
  63. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_BOOL, "Can Be Occluded", IsOccludee, SetOccludee, bool, true, AM_DEFAULT);
  64. ATTRIBUTE(CustomGeometry, VAR_BOOL, "Cast Shadows", castShadows_, false, AM_DEFAULT);
  65. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_FLOAT, "Draw Distance", GetDrawDistance, SetDrawDistance, float, 0.0f, AM_DEFAULT);
  66. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_FLOAT, "Shadow Distance", GetShadowDistance, SetShadowDistance, float, 0.0f, AM_DEFAULT);
  67. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_FLOAT, "LOD Bias", GetLodBias, SetLodBias, float, 1.0f, AM_DEFAULT);
  68. COPY_BASE_ATTRIBUTES(CustomGeometry, Drawable);
  69. }
  70. void CustomGeometry::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  71. {
  72. RayQueryLevel level = query.level_;
  73. switch (level)
  74. {
  75. case RAY_AABB_NOSUBOBJECTS:
  76. case RAY_AABB:
  77. Drawable::ProcessRayQuery(query, results);
  78. break;
  79. case RAY_OBB:
  80. case RAY_TRIANGLE:
  81. Matrix3x4 inverse(node_->GetWorldTransform().Inverse());
  82. Ray localRay(inverse * query.ray_.origin_, inverse * Vector4(query.ray_.direction_, 0.0f));
  83. float distance = localRay.HitDistance(boundingBox_);
  84. if (distance < query.maxDistance_)
  85. {
  86. if (level == RAY_TRIANGLE)
  87. {
  88. for (unsigned i = 0; i < batches_.Size(); ++i)
  89. {
  90. Geometry* geometry = batches_[i].geometry_;
  91. if (geometry)
  92. {
  93. distance = geometry->GetHitDistance(localRay);
  94. if (distance < query.maxDistance_)
  95. {
  96. RayQueryResult result;
  97. result.drawable_ = this;
  98. result.node_ = node_;
  99. result.distance_ = distance;
  100. result.subObject_ = M_MAX_UNSIGNED;
  101. results.Push(result);
  102. break;
  103. }
  104. }
  105. }
  106. }
  107. else
  108. {
  109. RayQueryResult result;
  110. result.drawable_ = this;
  111. result.node_ = node_;
  112. result.distance_ = distance;
  113. result.subObject_ = M_MAX_UNSIGNED;
  114. results.Push(result);
  115. }
  116. }
  117. break;
  118. }
  119. }
  120. Geometry* CustomGeometry::GetLodGeometry(unsigned batchIndex, unsigned level)
  121. {
  122. return batchIndex < geometries_.Size() ? geometries_[batchIndex] : (Geometry*)0;
  123. }
  124. unsigned CustomGeometry::GetNumOccluderTriangles()
  125. {
  126. unsigned triangles = 0;
  127. for (unsigned i = 0; i < batches_.Size(); ++i)
  128. {
  129. Geometry* geometry = GetLodGeometry(i, 0);
  130. if (!geometry)
  131. continue;
  132. // Check that the material is suitable for occlusion (default material always is)
  133. Material* mat = batches_[i].material_;
  134. if (mat && !mat->GetOcclusion())
  135. continue;
  136. triangles += geometry->GetVertexCount() / 3;
  137. }
  138. return triangles;
  139. }
  140. bool CustomGeometry::DrawOcclusion(OcclusionBuffer* buffer)
  141. {
  142. bool success = true;
  143. for (unsigned i = 0; i < batches_.Size(); ++i)
  144. {
  145. Geometry* geometry = GetLodGeometry(i, 0);
  146. if (!geometry)
  147. continue;
  148. // Check that the material is suitable for occlusion (default material always is) and set culling mode
  149. Material* material = batches_[i].material_;
  150. if (material)
  151. {
  152. if (!material->GetOcclusion())
  153. continue;
  154. buffer->SetCullMode(material->GetCullMode());
  155. }
  156. else
  157. buffer->SetCullMode(CULL_CCW);
  158. const unsigned char* vertexData;
  159. unsigned vertexSize;
  160. const unsigned char* indexData;
  161. unsigned indexSize;
  162. unsigned elementMask;
  163. geometry->GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  164. // Check for valid geometry data
  165. if (!vertexData)
  166. continue;
  167. // Draw and check for running out of triangles
  168. success = buffer->Draw(node_->GetWorldTransform(), vertexData, vertexSize, geometry->GetVertexStart(), geometry->GetVertexCount());
  169. if (!success)
  170. break;
  171. }
  172. return success;
  173. }
  174. void CustomGeometry::Clear()
  175. {
  176. elementMask_ = MASK_POSITION;
  177. batches_.Clear();
  178. geometries_.Clear();
  179. primitiveTypes_.Clear();
  180. vertices_.Clear();
  181. }
  182. void CustomGeometry::SetNumGeometries(unsigned num)
  183. {
  184. batches_.Resize(num);
  185. geometries_.Resize(num);
  186. primitiveTypes_.Resize(num);
  187. vertices_.Resize(num);
  188. for (unsigned i = 0; i < geometries_.Size(); ++i)
  189. {
  190. if (!geometries_[i])
  191. geometries_[i] = new Geometry(context_);
  192. batches_[i].geometry_ = geometries_[i];
  193. }
  194. }
  195. void CustomGeometry::BeginGeometry(unsigned index, PrimitiveType type)
  196. {
  197. if (index > geometries_.Size())
  198. {
  199. LOGERROR("Geometry index out of bounds");
  200. return;
  201. }
  202. geometryIndex_ = index;
  203. primitiveTypes_[index] = type;
  204. vertices_[index].Clear();
  205. }
  206. void CustomGeometry::DefineVertex(const Vector3& position)
  207. {
  208. if (vertices_.Size() < geometryIndex_)
  209. return;
  210. vertices_[geometryIndex_].Resize(vertices_[geometryIndex_].Size() + 1);
  211. vertices_[geometryIndex_].Back().position_ = position;
  212. }
  213. void CustomGeometry::DefineNormal(const Vector3& normal)
  214. {
  215. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  216. return;
  217. vertices_[geometryIndex_].Back().normal_ = normal;
  218. elementMask_ |= MASK_NORMAL;
  219. }
  220. void CustomGeometry::DefineColor(const Color& color)
  221. {
  222. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  223. return;
  224. vertices_[geometryIndex_].Back().color_ = color.ToUInt();
  225. elementMask_ |= MASK_COLOR;
  226. }
  227. void CustomGeometry::DefineTexCoord(const Vector2& texCoord)
  228. {
  229. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  230. return;
  231. vertices_[geometryIndex_].Back().texCoord_ = texCoord;
  232. elementMask_ |= MASK_TEXCOORD1;
  233. }
  234. void CustomGeometry::DefineTangent(const Vector4& tangent)
  235. {
  236. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  237. return;
  238. vertices_[geometryIndex_].Back().tangent_ = tangent;
  239. elementMask_ |= MASK_TANGENT;
  240. }
  241. void CustomGeometry::Commit()
  242. {
  243. PROFILE(CommitCustomGeometry);
  244. unsigned totalVertices = 0;
  245. boundingBox_.Clear();
  246. for (unsigned i = 0; i < vertices_.Size(); ++i)
  247. {
  248. totalVertices += vertices_[i].Size();
  249. for (unsigned j = 0; j < vertices_[i].Size(); ++j)
  250. boundingBox_.Merge(vertices_[i][j].position_);
  251. }
  252. vertexBuffer_->SetSize(totalVertices, elementMask_);
  253. if (totalVertices)
  254. {
  255. unsigned char* dest = (unsigned char*)vertexBuffer_->Lock(0, totalVertices, true);
  256. if (dest)
  257. {
  258. unsigned vertexStart = 0;
  259. for (unsigned i = 0; i < vertices_.Size(); ++i)
  260. {
  261. unsigned vertexCount = 0;
  262. for (unsigned j = 0; j < vertices_[i].Size(); ++j)
  263. {
  264. *((Vector3*)dest) = vertices_[i][j].position_;
  265. dest += sizeof(Vector3);
  266. if (elementMask_ & MASK_NORMAL)
  267. {
  268. *((Vector3*)dest) = vertices_[i][j].normal_;
  269. dest += sizeof(Vector3);
  270. }
  271. if (elementMask_ & MASK_COLOR)
  272. {
  273. *((unsigned*)dest) = vertices_[i][j].color_;
  274. dest += sizeof(unsigned);
  275. }
  276. if (elementMask_ & MASK_TEXCOORD1)
  277. {
  278. *((Vector2*)dest) = vertices_[i][j].texCoord_;
  279. dest += sizeof(Vector2);
  280. }
  281. if (elementMask_ & MASK_TANGENT)
  282. {
  283. *((Vector4*)dest) = vertices_[i][j].tangent_;
  284. dest += sizeof(Vector4);
  285. }
  286. ++vertexCount;
  287. }
  288. geometries_[i]->SetVertexBuffer(0, vertexBuffer_, elementMask_);
  289. geometries_[i]->SetDrawRange(primitiveTypes_[i], 0, 0, vertexStart, vertexCount);
  290. vertexStart += vertexCount;
  291. }
  292. vertexBuffer_->Unlock();
  293. }
  294. else
  295. LOGERROR("Failed to lock custom geometry vertex buffer");
  296. }
  297. else
  298. {
  299. for (unsigned i = 0; i < geometries_.Size(); ++i)
  300. {
  301. geometries_[i]->SetVertexBuffer(0, vertexBuffer_, elementMask_);
  302. geometries_[i]->SetDrawRange(primitiveTypes_[i], 0, 0, 0, 0);
  303. }
  304. }
  305. vertexBuffer_->ClearDataLost();
  306. }
  307. void CustomGeometry::SetMaterial(Material* material)
  308. {
  309. for (unsigned i = 0; i < batches_.Size(); ++i)
  310. batches_[i].material_ = material;
  311. MarkNetworkUpdate();
  312. }
  313. bool CustomGeometry::SetMaterial(unsigned index, Material* material)
  314. {
  315. if (index >= batches_.Size())
  316. {
  317. LOGERROR("Material index out of bounds");
  318. return false;
  319. }
  320. batches_[index].material_ = material;
  321. MarkNetworkUpdate();
  322. return true;
  323. }
  324. Material* CustomGeometry::GetMaterial(unsigned index) const
  325. {
  326. return index < batches_.Size() ? batches_[index].material_ : (Material*)0;
  327. }
  328. void CustomGeometry::SetGeometryDataAttr(PODVector<unsigned char> value)
  329. {
  330. if (value.Empty())
  331. return;
  332. MemoryBuffer buffer(value);
  333. SetNumGeometries(buffer.ReadVLE());
  334. elementMask_ = buffer.ReadUInt();
  335. for (unsigned i = 0; i < geometries_.Size(); ++i)
  336. {
  337. unsigned numVertices = buffer.ReadVLE();
  338. vertices_[i].Resize(numVertices);
  339. primitiveTypes_[i] = (PrimitiveType)buffer.ReadUByte();
  340. for (unsigned j = 0; j < numVertices; ++j)
  341. {
  342. if (elementMask_ & MASK_POSITION)
  343. vertices_[i][j].position_ = buffer.ReadVector3();
  344. if (elementMask_ & MASK_NORMAL)
  345. vertices_[i][j].normal_ = buffer.ReadVector3();
  346. if (elementMask_ & MASK_COLOR)
  347. vertices_[i][j].color_ = buffer.ReadUInt();
  348. if (elementMask_ & MASK_TEXCOORD1)
  349. vertices_[i][j].texCoord_ = buffer.ReadVector2();
  350. if (elementMask_ & MASK_TANGENT)
  351. vertices_[i][j].tangent_ = buffer.ReadVector4();
  352. }
  353. }
  354. Commit();
  355. }
  356. void CustomGeometry::SetMaterialsAttr(const ResourceRefList& value)
  357. {
  358. ResourceCache* cache = GetSubsystem<ResourceCache>();
  359. for (unsigned i = 0; i < value.ids_.Size(); ++i)
  360. SetMaterial(i, cache->GetResource<Material>(value.ids_[i]));
  361. }
  362. PODVector<unsigned char> CustomGeometry::GetGeometryDataAttr() const
  363. {
  364. VectorBuffer ret;
  365. ret.WriteVLE(geometries_.Size());
  366. ret.WriteUInt(elementMask_);
  367. for (unsigned i = 0; i < geometries_.Size(); ++i)
  368. {
  369. unsigned numVertices = vertices_[i].Size();
  370. ret.WriteVLE(numVertices);
  371. ret.WriteUByte(primitiveTypes_[i]);
  372. for (unsigned j = 0; j < numVertices; ++j)
  373. {
  374. if (elementMask_ & MASK_POSITION)
  375. ret.WriteVector3(vertices_[i][j].position_);
  376. if (elementMask_ & MASK_NORMAL)
  377. ret.WriteVector3(vertices_[i][j].normal_);
  378. if (elementMask_ & MASK_COLOR)
  379. ret.WriteUInt(vertices_[i][j].color_);
  380. if (elementMask_ & MASK_TEXCOORD1)
  381. ret.WriteVector2(vertices_[i][j].texCoord_);
  382. if (elementMask_ & MASK_TANGENT)
  383. ret.WriteVector4(vertices_[i][j].tangent_);
  384. }
  385. }
  386. return ret.GetBuffer();
  387. }
  388. const ResourceRefList& CustomGeometry::GetMaterialsAttr() const
  389. {
  390. materialsAttr_.ids_.Resize(batches_.Size());
  391. for (unsigned i = 0; i < batches_.Size(); ++i)
  392. materialsAttr_.ids_[i] = batches_[i].material_ ? batches_[i].material_->GetNameHash() : StringHash();
  393. return materialsAttr_;
  394. }
  395. void CustomGeometry::OnWorldBoundingBoxUpdate()
  396. {
  397. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  398. }
  399. }