CustomGeometry.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 (level == RAY_TRIANGLE && distance < query.maxDistance_)
  84. {
  85. distance = M_INFINITY;
  86. for (unsigned i = 0; i < batches_.Size(); ++i)
  87. {
  88. Geometry* geometry = batches_[i].geometry_;
  89. if (geometry)
  90. {
  91. float geometryDistance = geometry->GetHitDistance(localRay);
  92. if (geometryDistance < query.maxDistance_ && geometryDistance < distance)
  93. distance = geometryDistance;
  94. }
  95. }
  96. }
  97. if (distance < query.maxDistance_)
  98. {
  99. RayQueryResult result;
  100. result.drawable_ = this;
  101. result.node_ = node_;
  102. result.distance_ = distance;
  103. result.subObject_ = M_MAX_UNSIGNED;
  104. results.Push(result);
  105. }
  106. break;
  107. }
  108. }
  109. Geometry* CustomGeometry::GetLodGeometry(unsigned batchIndex, unsigned level)
  110. {
  111. return batchIndex < geometries_.Size() ? geometries_[batchIndex] : (Geometry*)0;
  112. }
  113. unsigned CustomGeometry::GetNumOccluderTriangles()
  114. {
  115. unsigned triangles = 0;
  116. for (unsigned i = 0; i < batches_.Size(); ++i)
  117. {
  118. Geometry* geometry = GetLodGeometry(i, 0);
  119. if (!geometry)
  120. continue;
  121. // Check that the material is suitable for occlusion (default material always is)
  122. Material* mat = batches_[i].material_;
  123. if (mat && !mat->GetOcclusion())
  124. continue;
  125. triangles += geometry->GetVertexCount() / 3;
  126. }
  127. return triangles;
  128. }
  129. bool CustomGeometry::DrawOcclusion(OcclusionBuffer* buffer)
  130. {
  131. bool success = true;
  132. for (unsigned i = 0; i < batches_.Size(); ++i)
  133. {
  134. Geometry* geometry = GetLodGeometry(i, 0);
  135. if (!geometry)
  136. continue;
  137. // Check that the material is suitable for occlusion (default material always is) and set culling mode
  138. Material* material = batches_[i].material_;
  139. if (material)
  140. {
  141. if (!material->GetOcclusion())
  142. continue;
  143. buffer->SetCullMode(material->GetCullMode());
  144. }
  145. else
  146. buffer->SetCullMode(CULL_CCW);
  147. const unsigned char* vertexData;
  148. unsigned vertexSize;
  149. const unsigned char* indexData;
  150. unsigned indexSize;
  151. unsigned elementMask;
  152. geometry->GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  153. // Check for valid geometry data
  154. if (!vertexData)
  155. continue;
  156. // Draw and check for running out of triangles
  157. success = buffer->Draw(node_->GetWorldTransform(), vertexData, vertexSize, geometry->GetVertexStart(), geometry->GetVertexCount());
  158. if (!success)
  159. break;
  160. }
  161. return success;
  162. }
  163. void CustomGeometry::Clear()
  164. {
  165. elementMask_ = MASK_POSITION;
  166. batches_.Clear();
  167. geometries_.Clear();
  168. primitiveTypes_.Clear();
  169. vertices_.Clear();
  170. }
  171. void CustomGeometry::SetNumGeometries(unsigned num)
  172. {
  173. batches_.Resize(num);
  174. geometries_.Resize(num);
  175. primitiveTypes_.Resize(num);
  176. vertices_.Resize(num);
  177. for (unsigned i = 0; i < geometries_.Size(); ++i)
  178. {
  179. if (!geometries_[i])
  180. geometries_[i] = new Geometry(context_);
  181. batches_[i].geometry_ = geometries_[i];
  182. }
  183. }
  184. void CustomGeometry::BeginGeometry(unsigned index, PrimitiveType type)
  185. {
  186. if (index > geometries_.Size())
  187. {
  188. LOGERROR("Geometry index out of bounds");
  189. return;
  190. }
  191. geometryIndex_ = index;
  192. primitiveTypes_[index] = type;
  193. vertices_[index].Clear();
  194. }
  195. void CustomGeometry::DefineVertex(const Vector3& position)
  196. {
  197. if (vertices_.Size() < geometryIndex_)
  198. return;
  199. vertices_[geometryIndex_].Resize(vertices_[geometryIndex_].Size() + 1);
  200. vertices_[geometryIndex_].Back().position_ = position;
  201. }
  202. void CustomGeometry::DefineNormal(const Vector3& normal)
  203. {
  204. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  205. return;
  206. vertices_[geometryIndex_].Back().normal_ = normal;
  207. elementMask_ |= MASK_NORMAL;
  208. }
  209. void CustomGeometry::DefineColor(const Color& color)
  210. {
  211. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  212. return;
  213. vertices_[geometryIndex_].Back().color_ = color.ToUInt();
  214. elementMask_ |= MASK_COLOR;
  215. }
  216. void CustomGeometry::DefineTexCoord(const Vector2& texCoord)
  217. {
  218. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  219. return;
  220. vertices_[geometryIndex_].Back().texCoord_ = texCoord;
  221. elementMask_ |= MASK_TEXCOORD1;
  222. }
  223. void CustomGeometry::DefineTangent(const Vector4& tangent)
  224. {
  225. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  226. return;
  227. vertices_[geometryIndex_].Back().tangent_ = tangent;
  228. elementMask_ |= MASK_TANGENT;
  229. }
  230. void CustomGeometry::Commit()
  231. {
  232. PROFILE(CommitCustomGeometry);
  233. unsigned totalVertices = 0;
  234. boundingBox_.Clear();
  235. for (unsigned i = 0; i < vertices_.Size(); ++i)
  236. {
  237. totalVertices += vertices_[i].Size();
  238. for (unsigned j = 0; j < vertices_[i].Size(); ++j)
  239. boundingBox_.Merge(vertices_[i][j].position_);
  240. }
  241. vertexBuffer_->SetSize(totalVertices, elementMask_);
  242. if (totalVertices)
  243. {
  244. unsigned char* dest = (unsigned char*)vertexBuffer_->Lock(0, totalVertices, true);
  245. if (dest)
  246. {
  247. unsigned vertexStart = 0;
  248. for (unsigned i = 0; i < vertices_.Size(); ++i)
  249. {
  250. unsigned vertexCount = 0;
  251. for (unsigned j = 0; j < vertices_[i].Size(); ++j)
  252. {
  253. *((Vector3*)dest) = vertices_[i][j].position_;
  254. dest += sizeof(Vector3);
  255. if (elementMask_ & MASK_NORMAL)
  256. {
  257. *((Vector3*)dest) = vertices_[i][j].normal_;
  258. dest += sizeof(Vector3);
  259. }
  260. if (elementMask_ & MASK_COLOR)
  261. {
  262. *((unsigned*)dest) = vertices_[i][j].color_;
  263. dest += sizeof(unsigned);
  264. }
  265. if (elementMask_ & MASK_TEXCOORD1)
  266. {
  267. *((Vector2*)dest) = vertices_[i][j].texCoord_;
  268. dest += sizeof(Vector2);
  269. }
  270. if (elementMask_ & MASK_TANGENT)
  271. {
  272. *((Vector4*)dest) = vertices_[i][j].tangent_;
  273. dest += sizeof(Vector4);
  274. }
  275. ++vertexCount;
  276. }
  277. geometries_[i]->SetVertexBuffer(0, vertexBuffer_, elementMask_);
  278. geometries_[i]->SetDrawRange(primitiveTypes_[i], 0, 0, vertexStart, vertexCount);
  279. vertexStart += vertexCount;
  280. }
  281. vertexBuffer_->Unlock();
  282. }
  283. else
  284. LOGERROR("Failed to lock custom geometry vertex buffer");
  285. }
  286. else
  287. {
  288. for (unsigned i = 0; i < geometries_.Size(); ++i)
  289. {
  290. geometries_[i]->SetVertexBuffer(0, vertexBuffer_, elementMask_);
  291. geometries_[i]->SetDrawRange(primitiveTypes_[i], 0, 0, 0, 0);
  292. }
  293. }
  294. vertexBuffer_->ClearDataLost();
  295. }
  296. void CustomGeometry::SetMaterial(Material* material)
  297. {
  298. for (unsigned i = 0; i < batches_.Size(); ++i)
  299. batches_[i].material_ = material;
  300. MarkNetworkUpdate();
  301. }
  302. bool CustomGeometry::SetMaterial(unsigned index, Material* material)
  303. {
  304. if (index >= batches_.Size())
  305. {
  306. LOGERROR("Material index out of bounds");
  307. return false;
  308. }
  309. batches_[index].material_ = material;
  310. MarkNetworkUpdate();
  311. return true;
  312. }
  313. Material* CustomGeometry::GetMaterial(unsigned index) const
  314. {
  315. return index < batches_.Size() ? batches_[index].material_ : (Material*)0;
  316. }
  317. void CustomGeometry::SetGeometryDataAttr(PODVector<unsigned char> value)
  318. {
  319. if (value.Empty())
  320. return;
  321. MemoryBuffer buffer(value);
  322. SetNumGeometries(buffer.ReadVLE());
  323. elementMask_ = buffer.ReadUInt();
  324. for (unsigned i = 0; i < geometries_.Size(); ++i)
  325. {
  326. unsigned numVertices = buffer.ReadVLE();
  327. vertices_[i].Resize(numVertices);
  328. primitiveTypes_[i] = (PrimitiveType)buffer.ReadUByte();
  329. for (unsigned j = 0; j < numVertices; ++j)
  330. {
  331. if (elementMask_ & MASK_POSITION)
  332. vertices_[i][j].position_ = buffer.ReadVector3();
  333. if (elementMask_ & MASK_NORMAL)
  334. vertices_[i][j].normal_ = buffer.ReadVector3();
  335. if (elementMask_ & MASK_COLOR)
  336. vertices_[i][j].color_ = buffer.ReadUInt();
  337. if (elementMask_ & MASK_TEXCOORD1)
  338. vertices_[i][j].texCoord_ = buffer.ReadVector2();
  339. if (elementMask_ & MASK_TANGENT)
  340. vertices_[i][j].tangent_ = buffer.ReadVector4();
  341. }
  342. }
  343. Commit();
  344. }
  345. void CustomGeometry::SetMaterialsAttr(const ResourceRefList& value)
  346. {
  347. ResourceCache* cache = GetSubsystem<ResourceCache>();
  348. for (unsigned i = 0; i < value.names_.Size(); ++i)
  349. SetMaterial(i, cache->GetResource<Material>(value.names_[i]));
  350. }
  351. PODVector<unsigned char> CustomGeometry::GetGeometryDataAttr() const
  352. {
  353. VectorBuffer ret;
  354. ret.WriteVLE(geometries_.Size());
  355. ret.WriteUInt(elementMask_);
  356. for (unsigned i = 0; i < geometries_.Size(); ++i)
  357. {
  358. unsigned numVertices = vertices_[i].Size();
  359. ret.WriteVLE(numVertices);
  360. ret.WriteUByte(primitiveTypes_[i]);
  361. for (unsigned j = 0; j < numVertices; ++j)
  362. {
  363. if (elementMask_ & MASK_POSITION)
  364. ret.WriteVector3(vertices_[i][j].position_);
  365. if (elementMask_ & MASK_NORMAL)
  366. ret.WriteVector3(vertices_[i][j].normal_);
  367. if (elementMask_ & MASK_COLOR)
  368. ret.WriteUInt(vertices_[i][j].color_);
  369. if (elementMask_ & MASK_TEXCOORD1)
  370. ret.WriteVector2(vertices_[i][j].texCoord_);
  371. if (elementMask_ & MASK_TANGENT)
  372. ret.WriteVector4(vertices_[i][j].tangent_);
  373. }
  374. }
  375. return ret.GetBuffer();
  376. }
  377. const ResourceRefList& CustomGeometry::GetMaterialsAttr() const
  378. {
  379. materialsAttr_.names_.Resize(batches_.Size());
  380. for (unsigned i = 0; i < batches_.Size(); ++i)
  381. materialsAttr_.names_[i] = GetResourceName(batches_[i].material_);
  382. return materialsAttr_;
  383. }
  384. void CustomGeometry::OnWorldBoundingBoxUpdate()
  385. {
  386. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  387. }
  388. }