Geometry.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //
  2. // Copyright (c) 2008-2020 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 "../Graphics/Geometry.h"
  24. #include "../Graphics/Graphics.h"
  25. #include "../Graphics/IndexBuffer.h"
  26. #include "../Graphics/VertexBuffer.h"
  27. #include "../IO/Log.h"
  28. #include "../Math/Ray.h"
  29. #include "../DebugNew.h"
  30. namespace Urho3D
  31. {
  32. Geometry::Geometry(Context* context) :
  33. Object(context),
  34. primitiveType_(TRIANGLE_LIST),
  35. indexStart_(0),
  36. indexCount_(0),
  37. vertexStart_(0),
  38. vertexCount_(0),
  39. rawVertexSize_(0),
  40. rawIndexSize_(0),
  41. lodDistance_(0.0f)
  42. {
  43. SetNumVertexBuffers(1);
  44. }
  45. Geometry::~Geometry() = default;
  46. bool Geometry::SetNumVertexBuffers(unsigned num)
  47. {
  48. if (num >= MAX_VERTEX_STREAMS)
  49. {
  50. URHO3D_LOGERROR("Too many vertex streams");
  51. return false;
  52. }
  53. unsigned oldSize = vertexBuffers_.Size();
  54. vertexBuffers_.Resize(num);
  55. return true;
  56. }
  57. bool Geometry::SetVertexBuffer(unsigned index, VertexBuffer* buffer)
  58. {
  59. if (index >= vertexBuffers_.Size())
  60. {
  61. URHO3D_LOGERROR("Stream index out of bounds");
  62. return false;
  63. }
  64. vertexBuffers_[index] = buffer;
  65. return true;
  66. }
  67. void Geometry::SetIndexBuffer(IndexBuffer* buffer)
  68. {
  69. indexBuffer_ = buffer;
  70. }
  71. bool Geometry::SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, bool getUsedVertexRange)
  72. {
  73. if (!indexBuffer_ && !rawIndexData_)
  74. {
  75. URHO3D_LOGERROR("Null index buffer and no raw index data, can not define indexed draw range");
  76. return false;
  77. }
  78. if (indexBuffer_ && indexStart + indexCount > indexBuffer_->GetIndexCount())
  79. {
  80. URHO3D_LOGERROR("Illegal draw range " + String(indexStart) + " to " + String(indexStart + indexCount - 1) + ", index buffer has " +
  81. String(indexBuffer_->GetIndexCount()) + " indices");
  82. return false;
  83. }
  84. primitiveType_ = type;
  85. indexStart_ = indexStart;
  86. indexCount_ = indexCount;
  87. // Get min.vertex index and num of vertices from index buffer. If it fails, use full range as fallback
  88. if (indexCount)
  89. {
  90. vertexStart_ = 0;
  91. vertexCount_ = vertexBuffers_[0] ? vertexBuffers_[0]->GetVertexCount() : 0;
  92. if (getUsedVertexRange && indexBuffer_)
  93. indexBuffer_->GetUsedVertexRange(indexStart_, indexCount_, vertexStart_, vertexCount_);
  94. }
  95. else
  96. {
  97. vertexStart_ = 0;
  98. vertexCount_ = 0;
  99. }
  100. return true;
  101. }
  102. bool Geometry::SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned vertexStart, unsigned vertexCount,
  103. bool checkIllegal)
  104. {
  105. if (indexBuffer_)
  106. {
  107. // We can allow setting an illegal draw range now if the caller guarantees to resize / fill the buffer later
  108. if (checkIllegal && indexStart + indexCount > indexBuffer_->GetIndexCount())
  109. {
  110. URHO3D_LOGERROR("Illegal draw range " + String(indexStart) + " to " + String(indexStart + indexCount - 1) +
  111. ", index buffer has " + String(indexBuffer_->GetIndexCount()) + " indices");
  112. return false;
  113. }
  114. }
  115. else if (!rawIndexData_)
  116. {
  117. indexStart = 0;
  118. indexCount = 0;
  119. }
  120. primitiveType_ = type;
  121. indexStart_ = indexStart;
  122. indexCount_ = indexCount;
  123. vertexStart_ = vertexStart;
  124. vertexCount_ = vertexCount;
  125. return true;
  126. }
  127. void Geometry::SetLodDistance(float distance)
  128. {
  129. if (distance < 0.0f)
  130. distance = 0.0f;
  131. lodDistance_ = distance;
  132. }
  133. void Geometry::SetRawVertexData(const SharedArrayPtr<unsigned char>& data, const PODVector<VertexElement>& elements)
  134. {
  135. rawVertexData_ = data;
  136. rawVertexSize_ = VertexBuffer::GetVertexSize(elements);
  137. rawElements_ = elements;
  138. }
  139. void Geometry::SetRawVertexData(const SharedArrayPtr<unsigned char>& data, unsigned elementMask)
  140. {
  141. rawVertexData_ = data;
  142. rawVertexSize_ = VertexBuffer::GetVertexSize(elementMask);
  143. rawElements_ = VertexBuffer::GetElements(elementMask);
  144. }
  145. void Geometry::SetRawIndexData(const SharedArrayPtr<unsigned char>& data, unsigned indexSize)
  146. {
  147. rawIndexData_ = data;
  148. rawIndexSize_ = indexSize;
  149. }
  150. void Geometry::Draw(Graphics* graphics)
  151. {
  152. if (indexBuffer_ && indexCount_ > 0)
  153. {
  154. graphics->SetIndexBuffer(indexBuffer_);
  155. graphics->SetVertexBuffers(vertexBuffers_);
  156. graphics->Draw(primitiveType_, indexStart_, indexCount_, vertexStart_, vertexCount_);
  157. }
  158. else if (vertexCount_ > 0)
  159. {
  160. graphics->SetVertexBuffers(vertexBuffers_);
  161. graphics->Draw(primitiveType_, vertexStart_, vertexCount_);
  162. }
  163. }
  164. VertexBuffer* Geometry::GetVertexBuffer(unsigned index) const
  165. {
  166. return index < vertexBuffers_.Size() ? vertexBuffers_[index] : nullptr;
  167. }
  168. unsigned short Geometry::GetBufferHash() const
  169. {
  170. unsigned short hash = 0;
  171. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  172. {
  173. VertexBuffer* vBuf = vertexBuffers_[i];
  174. hash += *((unsigned short*)&vBuf);
  175. }
  176. IndexBuffer* iBuf = indexBuffer_;
  177. hash += *((unsigned short*)&iBuf);
  178. return hash;
  179. }
  180. void Geometry::GetRawData(const unsigned char*& vertexData, unsigned& vertexSize, const unsigned char*& indexData,
  181. unsigned& indexSize, const PODVector<VertexElement>*& elements) const
  182. {
  183. if (rawVertexData_)
  184. {
  185. vertexData = rawVertexData_;
  186. vertexSize = rawVertexSize_;
  187. elements = &rawElements_;
  188. }
  189. else if (vertexBuffers_.Size() && vertexBuffers_[0])
  190. {
  191. vertexData = vertexBuffers_[0]->GetShadowData();
  192. vertexSize = vertexBuffers_[0]->GetVertexSize();
  193. elements = &vertexBuffers_[0]->GetElements();
  194. }
  195. else
  196. {
  197. vertexData = nullptr;
  198. vertexSize = 0;
  199. elements = nullptr;
  200. }
  201. if (rawIndexData_)
  202. {
  203. indexData = rawIndexData_;
  204. indexSize = rawIndexSize_;
  205. }
  206. else
  207. {
  208. if (indexBuffer_)
  209. {
  210. indexData = indexBuffer_->GetShadowData();
  211. if (indexData)
  212. indexSize = indexBuffer_->GetIndexSize();
  213. else
  214. indexSize = 0;
  215. }
  216. else
  217. {
  218. indexData = nullptr;
  219. indexSize = 0;
  220. }
  221. }
  222. }
  223. void Geometry::GetRawDataShared(SharedArrayPtr<unsigned char>& vertexData, unsigned& vertexSize,
  224. SharedArrayPtr<unsigned char>& indexData, unsigned& indexSize, const PODVector<VertexElement>*& elements) const
  225. {
  226. if (rawVertexData_)
  227. {
  228. vertexData = rawVertexData_;
  229. vertexSize = rawVertexSize_;
  230. elements = &rawElements_;
  231. }
  232. else if (vertexBuffers_.Size() && vertexBuffers_[0])
  233. {
  234. vertexData = vertexBuffers_[0]->GetShadowDataShared();
  235. vertexSize = vertexBuffers_[0]->GetVertexSize();
  236. elements = &vertexBuffers_[0]->GetElements();
  237. }
  238. else
  239. {
  240. vertexData = nullptr;
  241. vertexSize = 0;
  242. elements = nullptr;
  243. }
  244. if (rawIndexData_)
  245. {
  246. indexData = rawIndexData_;
  247. indexSize = rawIndexSize_;
  248. }
  249. else
  250. {
  251. if (indexBuffer_)
  252. {
  253. indexData = indexBuffer_->GetShadowDataShared();
  254. if (indexData)
  255. indexSize = indexBuffer_->GetIndexSize();
  256. else
  257. indexSize = 0;
  258. }
  259. else
  260. {
  261. indexData = nullptr;
  262. indexSize = 0;
  263. }
  264. }
  265. }
  266. float Geometry::GetHitDistance(const Ray& ray, Vector3* outNormal, Vector2* outUV) const
  267. {
  268. const unsigned char* vertexData;
  269. const unsigned char* indexData;
  270. unsigned vertexSize;
  271. unsigned indexSize;
  272. const PODVector<VertexElement>* elements;
  273. GetRawData(vertexData, vertexSize, indexData, indexSize, elements);
  274. if (!vertexData || !elements || VertexBuffer::GetElementOffset(*elements, TYPE_VECTOR3, SEM_POSITION) != 0)
  275. return M_INFINITY;
  276. unsigned uvOffset = VertexBuffer::GetElementOffset(*elements, TYPE_VECTOR2, SEM_TEXCOORD);
  277. if (outUV && uvOffset == M_MAX_UNSIGNED)
  278. {
  279. // requested UV output, but no texture data in vertex buffer
  280. URHO3D_LOGWARNING("Illegal GetHitDistance call: UV return requested on vertex buffer without UV coords");
  281. *outUV = Vector2::ZERO;
  282. outUV = nullptr;
  283. }
  284. return indexData ? ray.HitDistance(vertexData, vertexSize, indexData, indexSize, indexStart_, indexCount_, outNormal, outUV,
  285. uvOffset) : ray.HitDistance(vertexData, vertexSize, vertexStart_, vertexCount_, outNormal, outUV, uvOffset);
  286. }
  287. bool Geometry::IsInside(const Ray& ray) const
  288. {
  289. const unsigned char* vertexData;
  290. const unsigned char* indexData;
  291. unsigned vertexSize;
  292. unsigned indexSize;
  293. const PODVector<VertexElement>* elements;
  294. GetRawData(vertexData, vertexSize, indexData, indexSize, elements);
  295. return vertexData ? (indexData ? ray.InsideGeometry(vertexData, vertexSize, indexData, indexSize, indexStart_, indexCount_) :
  296. ray.InsideGeometry(vertexData, vertexSize, vertexStart_, vertexCount_)) : false;
  297. }
  298. }