CustomGeometry.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Batch.h"
  25. #include "Camera.h"
  26. #include "Context.h"
  27. #include "CustomGeometry.h"
  28. #include "Geometry.h"
  29. #include "Log.h"
  30. #include "Material.h"
  31. #include "Node.h"
  32. #include "OcclusionBuffer.h"
  33. #include "OctreeQuery.h"
  34. #include "Profiler.h"
  35. #include "VertexBuffer.h"
  36. #include "DebugNew.h"
  37. namespace Urho3D
  38. {
  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->RegisterFactory<CustomGeometry>();
  55. ATTRIBUTE(CustomGeometry, VAR_BOOL, "Is Visible", visible_, 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. // After a pretest using the OBB, do the actual test using triangle geometry
  83. unsigned i = 0;
  84. while (i < batches_.Size())
  85. {
  86. Geometry* geometry = batches_[i++].geometry_;
  87. if (geometry)
  88. {
  89. distance = geometry->GetHitDistance(localRay);
  90. if (distance <= query.maxDistance_)
  91. break;
  92. }
  93. }
  94. if (i == batches_.Size())
  95. break;
  96. }
  97. // If the code reaches here then we have a hit
  98. RayQueryResult result;
  99. result.drawable_ = this;
  100. result.node_ = node_;
  101. result.distance_ = distance;
  102. result.subObject_ = M_MAX_UNSIGNED;
  103. results.Push(result);
  104. }
  105. break;
  106. }
  107. }
  108. Geometry* CustomGeometry::GetLodGeometry(unsigned batchIndex, unsigned level)
  109. {
  110. return batchIndex < geometries_.Size() ? geometries_[batchIndex] : (Geometry*)0;
  111. }
  112. unsigned CustomGeometry::GetNumOccluderTriangles()
  113. {
  114. unsigned triangles = 0;
  115. for (unsigned i = 0; i < batches_.Size(); ++i)
  116. {
  117. Geometry* geometry = GetLodGeometry(i, 0);
  118. if (!geometry)
  119. continue;
  120. // Check that the material is suitable for occlusion (default material always is)
  121. Material* mat = batches_[i].material_;
  122. if (mat && !mat->GetOcclusion())
  123. continue;
  124. triangles += geometry->GetVertexCount() / 3;
  125. }
  126. return triangles;
  127. }
  128. bool CustomGeometry::DrawOcclusion(OcclusionBuffer* buffer)
  129. {
  130. bool success = true;
  131. for (unsigned i = 0; i < batches_.Size(); ++i)
  132. {
  133. Geometry* geometry = GetLodGeometry(i, 0);
  134. if (!geometry)
  135. continue;
  136. // Check that the material is suitable for occlusion (default material always is) and set culling mode
  137. Material* material = batches_[i].material_;
  138. if (material)
  139. {
  140. if (!material->GetOcclusion())
  141. continue;
  142. buffer->SetCullMode(material->GetCullMode());
  143. }
  144. else
  145. buffer->SetCullMode(CULL_CCW);
  146. const unsigned char* vertexData;
  147. unsigned vertexSize;
  148. const unsigned char* indexData;
  149. unsigned indexSize;
  150. unsigned elementMask;
  151. geometry->GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  152. // Check for valid geometry data
  153. if (!vertexData)
  154. continue;
  155. // Draw and check for running out of triangles
  156. success = buffer->Draw(node_->GetWorldTransform(), vertexData, vertexSize, geometry->GetVertexStart(), geometry->GetVertexCount());
  157. if (!success)
  158. break;
  159. }
  160. return success;
  161. }
  162. void CustomGeometry::Clear()
  163. {
  164. elementMask_ = MASK_POSITION;
  165. batches_.Clear();
  166. geometries_.Clear();
  167. primitiveTypes_.Clear();
  168. vertices_.Clear();
  169. }
  170. void CustomGeometry::SetNumGeometries(unsigned num)
  171. {
  172. batches_.Resize(num);
  173. geometries_.Resize(num);
  174. primitiveTypes_.Resize(num);
  175. vertices_.Resize(num);
  176. for (unsigned i = 0; i < geometries_.Size(); ++i)
  177. {
  178. if (!geometries_[i])
  179. geometries_[i] = new Geometry(context_);
  180. batches_[i].geometry_ = geometries_[i];
  181. }
  182. }
  183. void CustomGeometry::BeginGeometry(unsigned index, PrimitiveType type)
  184. {
  185. if (index > geometries_.Size())
  186. {
  187. LOGERROR("Geometry index out of bounds");
  188. return;
  189. }
  190. geometryIndex_ = index;
  191. primitiveTypes_[index] = type;
  192. vertices_[index].Clear();
  193. }
  194. void CustomGeometry::DefineVertex(const Vector3& position)
  195. {
  196. if (vertices_.Size() < geometryIndex_)
  197. return;
  198. vertices_[geometryIndex_].Resize(vertices_[geometryIndex_].Size() + 1);
  199. vertices_[geometryIndex_].Back().position_ = position;
  200. }
  201. void CustomGeometry::DefineNormal(const Vector3& normal)
  202. {
  203. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  204. return;
  205. vertices_[geometryIndex_].Back().normal_ = normal;
  206. elementMask_ |= MASK_NORMAL;
  207. }
  208. void CustomGeometry::DefineColor(const Color& color)
  209. {
  210. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  211. return;
  212. vertices_[geometryIndex_].Back().color_ = color.ToUInt();
  213. elementMask_ |= MASK_COLOR;
  214. }
  215. void CustomGeometry::DefineTexCoord(const Vector2& texCoord)
  216. {
  217. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  218. return;
  219. vertices_[geometryIndex_].Back().texCoord_ = texCoord;
  220. elementMask_ |= MASK_TEXCOORD1;
  221. }
  222. void CustomGeometry::DefineTangent(const Vector4& tangent)
  223. {
  224. if (vertices_.Size() < geometryIndex_ || vertices_[geometryIndex_].Empty())
  225. return;
  226. vertices_[geometryIndex_].Back().tangent_ = tangent;
  227. elementMask_ |= MASK_TANGENT;
  228. }
  229. void CustomGeometry::Commit()
  230. {
  231. PROFILE(CommitCustomGeometry);
  232. unsigned totalVertices = 0;
  233. boundingBox_.Clear();
  234. for (unsigned i = 0; i < vertices_.Size(); ++i)
  235. {
  236. totalVertices += vertices_[i].Size();
  237. for (unsigned j = 0; j < vertices_[i].Size(); ++j)
  238. boundingBox_.Merge(vertices_[i][j].position_);
  239. }
  240. vertexBuffer_->SetSize(totalVertices, elementMask_);
  241. if (totalVertices)
  242. {
  243. unsigned char* dest = (unsigned char*)vertexBuffer_->Lock(0, totalVertices, true);
  244. if (dest)
  245. {
  246. unsigned vertexStart = 0;
  247. for (unsigned i = 0; i < vertices_.Size(); ++i)
  248. {
  249. unsigned vertexCount = 0;
  250. for (unsigned j = 0; j < vertices_[i].Size(); ++j)
  251. {
  252. *((Vector3*)dest) = vertices_[i][j].position_;
  253. dest += sizeof(Vector3);
  254. if (elementMask_ & MASK_NORMAL)
  255. {
  256. *((Vector3*)dest) = vertices_[i][j].normal_;
  257. dest += sizeof(Vector3);
  258. }
  259. if (elementMask_ & MASK_COLOR)
  260. {
  261. *((unsigned*)dest) = vertices_[i][j].color_;
  262. dest += sizeof(unsigned);
  263. }
  264. if (elementMask_ & MASK_TEXCOORD1)
  265. {
  266. *((Vector2*)dest) = vertices_[i][j].texCoord_;
  267. dest += sizeof(Vector2);
  268. }
  269. if (elementMask_ & MASK_TANGENT)
  270. {
  271. *((Vector4*)dest) = vertices_[i][j].tangent_;
  272. dest += sizeof(Vector4);
  273. }
  274. ++vertexCount;
  275. }
  276. geometries_[i]->SetVertexBuffer(0, vertexBuffer_, elementMask_);
  277. geometries_[i]->SetDrawRange(primitiveTypes_[i], 0, 0, vertexStart, vertexCount);
  278. vertexStart += vertexCount;
  279. }
  280. vertexBuffer_->Unlock();
  281. }
  282. else
  283. LOGERROR("Failed to lock custom geometry vertex buffer");
  284. }
  285. else
  286. {
  287. for (unsigned i = 0; i < geometries_.Size(); ++i)
  288. {
  289. geometries_[i]->SetVertexBuffer(0, vertexBuffer_, elementMask_);
  290. geometries_[i]->SetDrawRange(primitiveTypes_[i], 0, 0, 0, 0);
  291. }
  292. }
  293. vertexBuffer_->ClearDataLost();
  294. }
  295. void CustomGeometry::SetMaterial(Material* material)
  296. {
  297. for (unsigned i = 0; i < batches_.Size(); ++i)
  298. batches_[i].material_ = material;
  299. MarkNetworkUpdate();
  300. }
  301. bool CustomGeometry::SetMaterial(unsigned index, Material* material)
  302. {
  303. if (index >= batches_.Size())
  304. {
  305. LOGERROR("Material index out of bounds");
  306. return false;
  307. }
  308. batches_[index].material_ = material;
  309. MarkNetworkUpdate();
  310. return true;
  311. }
  312. Material* CustomGeometry::GetMaterial(unsigned index) const
  313. {
  314. return index < batches_.Size() ? batches_[index].material_ : (Material*)0;
  315. }
  316. void CustomGeometry::OnWorldBoundingBoxUpdate()
  317. {
  318. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  319. }
  320. }