CustomGeometry.cpp 15 KB

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