CustomGeometry.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 "Node.h"
  31. #include "OcclusionBuffer.h"
  32. #include "OctreeQuery.h"
  33. #include "Profiler.h"
  34. #include "VertexBuffer.h"
  35. #include "DebugNew.h"
  36. namespace Urho3D
  37. {
  38. extern const char* STATIC_CATEGORY;
  39. OBJECTTYPESTATIC(CustomGeometry);
  40. CustomGeometry::CustomGeometry(Context* context) :
  41. Drawable(context, DRAWABLE_GEOMETRY),
  42. vertexBuffer_(new VertexBuffer(context)),
  43. elementMask_(MASK_POSITION),
  44. geometryIndex_(0)
  45. {
  46. vertexBuffer_->SetShadowed(true);
  47. SetNumGeometries(1);
  48. }
  49. CustomGeometry::~CustomGeometry()
  50. {
  51. }
  52. void CustomGeometry::RegisterObject(Context* context)
  53. {
  54. context->RegisterComponentFactory<CustomGeometry>(STATIC_CATEGORY);
  55. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  56. ATTRIBUTE(CustomGeometry, VAR_BOOL, "Is Occluder", occluder_, false, AM_DEFAULT);
  57. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_BOOL, "Can Be Occluded", IsOccludee, SetOccludee, bool, true, AM_DEFAULT);
  58. ATTRIBUTE(CustomGeometry, VAR_BOOL, "Cast Shadows", castShadows_, false, AM_DEFAULT);
  59. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_FLOAT, "Draw Distance", GetDrawDistance, SetDrawDistance, float, 0.0f, AM_DEFAULT);
  60. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_FLOAT, "Shadow Distance", GetShadowDistance, SetShadowDistance, float, 0.0f, AM_DEFAULT);
  61. ACCESSOR_ATTRIBUTE(CustomGeometry, VAR_FLOAT, "LOD Bias", GetLodBias, SetLodBias, float, 1.0f, AM_DEFAULT);
  62. COPY_BASE_ATTRIBUTES(CustomGeometry, Drawable);
  63. }
  64. void CustomGeometry::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  65. {
  66. RayQueryLevel level = query.level_;
  67. switch (level)
  68. {
  69. case RAY_AABB_NOSUBOBJECTS:
  70. case RAY_AABB:
  71. Drawable::ProcessRayQuery(query, results);
  72. break;
  73. case RAY_OBB:
  74. case RAY_TRIANGLE:
  75. Matrix3x4 inverse(node_->GetWorldTransform().Inverse());
  76. Ray localRay(inverse * query.ray_.origin_, inverse * Vector4(query.ray_.direction_, 0.0f));
  77. float distance = localRay.HitDistance(boundingBox_);
  78. if (distance < query.maxDistance_)
  79. {
  80. if (level == RAY_TRIANGLE)
  81. {
  82. for (unsigned i = 0; i < batches_.Size(); ++i)
  83. {
  84. Geometry* geometry = batches_[i].geometry_;
  85. if (geometry)
  86. {
  87. distance = geometry->GetHitDistance(localRay);
  88. if (distance < query.maxDistance_)
  89. {
  90. RayQueryResult result;
  91. result.drawable_ = this;
  92. result.node_ = node_;
  93. result.distance_ = distance;
  94. result.subObject_ = M_MAX_UNSIGNED;
  95. results.Push(result);
  96. break;
  97. }
  98. }
  99. }
  100. }
  101. else
  102. {
  103. RayQueryResult result;
  104. result.drawable_ = this;
  105. result.node_ = node_;
  106. result.distance_ = distance;
  107. result.subObject_ = M_MAX_UNSIGNED;
  108. results.Push(result);
  109. }
  110. }
  111. break;
  112. }
  113. }
  114. Geometry* CustomGeometry::GetLodGeometry(unsigned batchIndex, unsigned level)
  115. {
  116. return batchIndex < geometries_.Size() ? geometries_[batchIndex] : (Geometry*)0;
  117. }
  118. unsigned CustomGeometry::GetNumOccluderTriangles()
  119. {
  120. unsigned triangles = 0;
  121. for (unsigned i = 0; i < batches_.Size(); ++i)
  122. {
  123. Geometry* geometry = GetLodGeometry(i, 0);
  124. if (!geometry)
  125. continue;
  126. // Check that the material is suitable for occlusion (default material always is)
  127. Material* mat = batches_[i].material_;
  128. if (mat && !mat->GetOcclusion())
  129. continue;
  130. triangles += geometry->GetVertexCount() / 3;
  131. }
  132. return triangles;
  133. }
  134. bool CustomGeometry::DrawOcclusion(OcclusionBuffer* buffer)
  135. {
  136. bool success = true;
  137. for (unsigned i = 0; i < batches_.Size(); ++i)
  138. {
  139. Geometry* geometry = GetLodGeometry(i, 0);
  140. if (!geometry)
  141. continue;
  142. // Check that the material is suitable for occlusion (default material always is) and set culling mode
  143. Material* material = batches_[i].material_;
  144. if (material)
  145. {
  146. if (!material->GetOcclusion())
  147. continue;
  148. buffer->SetCullMode(material->GetCullMode());
  149. }
  150. else
  151. buffer->SetCullMode(CULL_CCW);
  152. const unsigned char* vertexData;
  153. unsigned vertexSize;
  154. const unsigned char* indexData;
  155. unsigned indexSize;
  156. unsigned elementMask;
  157. geometry->GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  158. // Check for valid geometry data
  159. if (!vertexData)
  160. continue;
  161. // Draw and check for running out of triangles
  162. success = buffer->Draw(node_->GetWorldTransform(), vertexData, vertexSize, geometry->GetVertexStart(), geometry->GetVertexCount());
  163. if (!success)
  164. break;
  165. }
  166. return success;
  167. }
  168. void CustomGeometry::Clear()
  169. {
  170. elementMask_ = MASK_POSITION;
  171. batches_.Clear();
  172. geometries_.Clear();
  173. primitiveTypes_.Clear();
  174. vertices_.Clear();
  175. }
  176. void CustomGeometry::SetNumGeometries(unsigned num)
  177. {
  178. batches_.Resize(num);
  179. geometries_.Resize(num);
  180. primitiveTypes_.Resize(num);
  181. vertices_.Resize(num);
  182. for (unsigned i = 0; i < geometries_.Size(); ++i)
  183. {
  184. if (!geometries_[i])
  185. geometries_[i] = new Geometry(context_);
  186. batches_[i].geometry_ = geometries_[i];
  187. }
  188. }
  189. void CustomGeometry::BeginGeometry(unsigned index, PrimitiveType type)
  190. {
  191. if (index > geometries_.Size())
  192. {
  193. LOGERROR("Geometry index out of bounds");
  194. return;
  195. }
  196. geometryIndex_ = index;
  197. primitiveTypes_[index] = type;
  198. vertices_[index].Clear();
  199. }
  200. void CustomGeometry::DefineVertex(const Vector3& position)
  201. {
  202. if (vertices_.Size() < geometryIndex_)
  203. return;
  204. vertices_[geometryIndex_].Resize(vertices_[geometryIndex_].Size() + 1);
  205. vertices_[geometryIndex_].Back().position_ = position;
  206. }
  207. void CustomGeometry::DefineNormal(const Vector3& normal)
  208. {
  209. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  210. return;
  211. vertices_[geometryIndex_].Back().normal_ = normal;
  212. elementMask_ |= MASK_NORMAL;
  213. }
  214. void CustomGeometry::DefineColor(const Color& color)
  215. {
  216. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  217. return;
  218. vertices_[geometryIndex_].Back().color_ = color.ToUInt();
  219. elementMask_ |= MASK_COLOR;
  220. }
  221. void CustomGeometry::DefineTexCoord(const Vector2& texCoord)
  222. {
  223. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  224. return;
  225. vertices_[geometryIndex_].Back().texCoord_ = texCoord;
  226. elementMask_ |= MASK_TEXCOORD1;
  227. }
  228. void CustomGeometry::DefineTangent(const Vector4& tangent)
  229. {
  230. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  231. return;
  232. vertices_[geometryIndex_].Back().tangent_ = tangent;
  233. elementMask_ |= MASK_TANGENT;
  234. }
  235. void CustomGeometry::Commit()
  236. {
  237. PROFILE(CommitCustomGeometry);
  238. unsigned totalVertices = 0;
  239. boundingBox_.Clear();
  240. for (unsigned i = 0; i < vertices_.Size(); ++i)
  241. {
  242. totalVertices += vertices_[i].Size();
  243. for (unsigned j = 0; j < vertices_[i].Size(); ++j)
  244. boundingBox_.Merge(vertices_[i][j].position_);
  245. }
  246. vertexBuffer_->SetSize(totalVertices, elementMask_);
  247. if (totalVertices)
  248. {
  249. unsigned char* dest = (unsigned char*)vertexBuffer_->Lock(0, totalVertices, true);
  250. if (dest)
  251. {
  252. unsigned vertexStart = 0;
  253. for (unsigned i = 0; i < vertices_.Size(); ++i)
  254. {
  255. unsigned vertexCount = 0;
  256. for (unsigned j = 0; j < vertices_[i].Size(); ++j)
  257. {
  258. *((Vector3*)dest) = vertices_[i][j].position_;
  259. dest += sizeof(Vector3);
  260. if (elementMask_ & MASK_NORMAL)
  261. {
  262. *((Vector3*)dest) = vertices_[i][j].normal_;
  263. dest += sizeof(Vector3);
  264. }
  265. if (elementMask_ & MASK_COLOR)
  266. {
  267. *((unsigned*)dest) = vertices_[i][j].color_;
  268. dest += sizeof(unsigned);
  269. }
  270. if (elementMask_ & MASK_TEXCOORD1)
  271. {
  272. *((Vector2*)dest) = vertices_[i][j].texCoord_;
  273. dest += sizeof(Vector2);
  274. }
  275. if (elementMask_ & MASK_TANGENT)
  276. {
  277. *((Vector4*)dest) = vertices_[i][j].tangent_;
  278. dest += sizeof(Vector4);
  279. }
  280. ++vertexCount;
  281. }
  282. geometries_[i]->SetVertexBuffer(0, vertexBuffer_, elementMask_);
  283. geometries_[i]->SetDrawRange(primitiveTypes_[i], 0, 0, vertexStart, vertexCount);
  284. vertexStart += vertexCount;
  285. }
  286. vertexBuffer_->Unlock();
  287. }
  288. else
  289. LOGERROR("Failed to lock custom geometry vertex buffer");
  290. }
  291. else
  292. {
  293. for (unsigned i = 0; i < geometries_.Size(); ++i)
  294. {
  295. geometries_[i]->SetVertexBuffer(0, vertexBuffer_, elementMask_);
  296. geometries_[i]->SetDrawRange(primitiveTypes_[i], 0, 0, 0, 0);
  297. }
  298. }
  299. vertexBuffer_->ClearDataLost();
  300. }
  301. void CustomGeometry::SetMaterial(Material* material)
  302. {
  303. for (unsigned i = 0; i < batches_.Size(); ++i)
  304. batches_[i].material_ = material;
  305. MarkNetworkUpdate();
  306. }
  307. bool CustomGeometry::SetMaterial(unsigned index, Material* material)
  308. {
  309. if (index >= batches_.Size())
  310. {
  311. LOGERROR("Material index out of bounds");
  312. return false;
  313. }
  314. batches_[index].material_ = material;
  315. MarkNetworkUpdate();
  316. return true;
  317. }
  318. Material* CustomGeometry::GetMaterial(unsigned index) const
  319. {
  320. return index < batches_.Size() ? batches_[index].material_ : (Material*)0;
  321. }
  322. void CustomGeometry::OnWorldBoundingBoxUpdate()
  323. {
  324. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  325. }
  326. }