Geometry.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. //
  2. // Copyright (c) 2008-2015 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. positionBufferIndex_(M_MAX_UNSIGNED),
  40. rawVertexSize_(0),
  41. rawElementMask_(0),
  42. rawIndexSize_(0),
  43. lodDistance_(0.0f)
  44. {
  45. SetNumVertexBuffers(1);
  46. }
  47. Geometry::~Geometry()
  48. {
  49. }
  50. bool Geometry::SetNumVertexBuffers(unsigned num)
  51. {
  52. if (num >= MAX_VERTEX_STREAMS)
  53. {
  54. URHO3D_LOGERROR("Too many vertex streams");
  55. return false;
  56. }
  57. unsigned oldSize = vertexBuffers_.Size();
  58. vertexBuffers_.Resize(num);
  59. elementMasks_.Resize(num);
  60. for (unsigned i = oldSize; i < num; ++i)
  61. elementMasks_[i] = MASK_NONE;
  62. GetPositionBufferIndex();
  63. return true;
  64. }
  65. bool Geometry::SetVertexBuffer(unsigned index, VertexBuffer* buffer, unsigned elementMask)
  66. {
  67. if (index >= vertexBuffers_.Size())
  68. {
  69. URHO3D_LOGERROR("Stream index out of bounds");
  70. return false;
  71. }
  72. vertexBuffers_[index] = buffer;
  73. if (buffer)
  74. {
  75. if (elementMask == MASK_DEFAULT)
  76. elementMasks_[index] = buffer->GetElementMask();
  77. else
  78. elementMasks_[index] = elementMask;
  79. }
  80. GetPositionBufferIndex();
  81. return true;
  82. }
  83. void Geometry::SetIndexBuffer(IndexBuffer* buffer)
  84. {
  85. indexBuffer_ = buffer;
  86. }
  87. bool Geometry::SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, bool getUsedVertexRange)
  88. {
  89. if (!indexBuffer_ && !rawIndexData_)
  90. {
  91. URHO3D_LOGERROR("Null index buffer and no raw index data, can not define indexed draw range");
  92. return false;
  93. }
  94. if (indexBuffer_ && indexStart + indexCount > indexBuffer_->GetIndexCount())
  95. {
  96. URHO3D_LOGERROR("Illegal draw range " + String(indexStart) + " to " + String(indexStart + indexCount - 1) + ", index buffer has " +
  97. String(indexBuffer_->GetIndexCount()) + " indices");
  98. return false;
  99. }
  100. primitiveType_ = type;
  101. indexStart_ = indexStart;
  102. indexCount_ = indexCount;
  103. // Get min.vertex index and num of vertices from index buffer. If it fails, use full range as fallback
  104. if (indexCount)
  105. {
  106. vertexStart_ = 0;
  107. vertexCount_ = vertexBuffers_[0] ? vertexBuffers_[0]->GetVertexCount() : 0;
  108. if (getUsedVertexRange && indexBuffer_)
  109. indexBuffer_->GetUsedVertexRange(indexStart_, indexCount_, vertexStart_, vertexCount_);
  110. }
  111. else
  112. {
  113. vertexStart_ = 0;
  114. vertexCount_ = 0;
  115. }
  116. return true;
  117. }
  118. bool Geometry::SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount,
  119. bool checkIllegal)
  120. {
  121. if (indexBuffer_)
  122. {
  123. // We can allow setting an illegal draw range now if the caller guarantees to resize / fill the buffer later
  124. if (checkIllegal && indexStart + indexCount > indexBuffer_->GetIndexCount())
  125. {
  126. URHO3D_LOGERROR("Illegal draw range " + String(indexStart) + " to " + String(indexStart + indexCount - 1) +
  127. ", index buffer has " + String(indexBuffer_->GetIndexCount()) + " indices");
  128. return false;
  129. }
  130. }
  131. else if (!rawIndexData_)
  132. {
  133. indexStart = 0;
  134. indexCount = 0;
  135. }
  136. primitiveType_ = type;
  137. indexStart_ = indexStart;
  138. indexCount_ = indexCount;
  139. vertexStart_ = minVertex;
  140. vertexCount_ = vertexCount;
  141. return true;
  142. }
  143. void Geometry::SetLodDistance(float distance)
  144. {
  145. if (distance < 0.0f)
  146. distance = 0.0f;
  147. lodDistance_ = distance;
  148. }
  149. void Geometry::SetRawVertexData(SharedArrayPtr<unsigned char> data, unsigned vertexSize, unsigned elementMask)
  150. {
  151. rawVertexData_ = data;
  152. rawVertexSize_ = vertexSize;
  153. rawElementMask_ = elementMask;
  154. }
  155. void Geometry::SetRawIndexData(SharedArrayPtr<unsigned char> data, unsigned indexSize)
  156. {
  157. rawIndexData_ = data;
  158. rawIndexSize_ = indexSize;
  159. }
  160. void Geometry::Draw(Graphics* graphics)
  161. {
  162. if (indexBuffer_ && indexCount_ > 0)
  163. {
  164. graphics->SetIndexBuffer(indexBuffer_);
  165. graphics->SetVertexBuffers(vertexBuffers_, elementMasks_);
  166. graphics->Draw(primitiveType_, indexStart_, indexCount_, vertexStart_, vertexCount_);
  167. }
  168. else if (vertexCount_ > 0)
  169. {
  170. graphics->SetVertexBuffers(vertexBuffers_, elementMasks_);
  171. graphics->Draw(primitiveType_, vertexStart_, vertexCount_);
  172. }
  173. }
  174. VertexBuffer* Geometry::GetVertexBuffer(unsigned index) const
  175. {
  176. return index < vertexBuffers_.Size() ? vertexBuffers_[index] : (VertexBuffer*)0;
  177. }
  178. unsigned Geometry::GetVertexElementMask(unsigned index) const
  179. {
  180. return index < elementMasks_.Size() ? elementMasks_[index] : 0;
  181. }
  182. unsigned short Geometry::GetBufferHash() const
  183. {
  184. unsigned short hash = 0;
  185. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  186. {
  187. VertexBuffer* vBuf = vertexBuffers_[i];
  188. hash += *((unsigned short*)&vBuf);
  189. }
  190. IndexBuffer* iBuf = indexBuffer_;
  191. hash += *((unsigned short*)&iBuf);
  192. return hash;
  193. }
  194. void Geometry::GetRawData(const unsigned char*& vertexData, unsigned& vertexSize, const unsigned char*& indexData,
  195. unsigned& indexSize, unsigned& elementMask) const
  196. {
  197. if (rawVertexData_)
  198. {
  199. vertexData = rawVertexData_;
  200. vertexSize = rawVertexSize_;
  201. elementMask = rawElementMask_;
  202. }
  203. else
  204. {
  205. if (positionBufferIndex_ < vertexBuffers_.Size() && vertexBuffers_[positionBufferIndex_])
  206. {
  207. vertexData = vertexBuffers_[positionBufferIndex_]->GetShadowData();
  208. if (vertexData)
  209. {
  210. vertexSize = vertexBuffers_[positionBufferIndex_]->GetVertexSize();
  211. elementMask = vertexBuffers_[positionBufferIndex_]->GetElementMask();
  212. }
  213. else
  214. {
  215. vertexSize = 0;
  216. elementMask = 0;
  217. }
  218. }
  219. else
  220. {
  221. vertexData = 0;
  222. vertexSize = 0;
  223. elementMask = 0;
  224. }
  225. }
  226. if (rawIndexData_)
  227. {
  228. indexData = rawIndexData_;
  229. indexSize = rawIndexSize_;
  230. }
  231. else
  232. {
  233. if (indexBuffer_)
  234. {
  235. indexData = indexBuffer_->GetShadowData();
  236. if (indexData)
  237. indexSize = indexBuffer_->GetIndexSize();
  238. else
  239. indexSize = 0;
  240. }
  241. else
  242. {
  243. indexData = 0;
  244. indexSize = 0;
  245. }
  246. }
  247. }
  248. void Geometry::GetRawDataShared(SharedArrayPtr<unsigned char>& vertexData, unsigned& vertexSize,
  249. SharedArrayPtr<unsigned char>& indexData, unsigned& indexSize, unsigned& elementMask) const
  250. {
  251. if (rawVertexData_)
  252. {
  253. vertexData = rawVertexData_;
  254. vertexSize = rawVertexSize_;
  255. elementMask = rawElementMask_;
  256. }
  257. else
  258. {
  259. if (positionBufferIndex_ < vertexBuffers_.Size() && vertexBuffers_[positionBufferIndex_])
  260. {
  261. vertexData = vertexBuffers_[positionBufferIndex_]->GetShadowDataShared();
  262. if (vertexData)
  263. {
  264. vertexSize = vertexBuffers_[positionBufferIndex_]->GetVertexSize();
  265. elementMask = vertexBuffers_[positionBufferIndex_]->GetElementMask();
  266. }
  267. else
  268. {
  269. vertexSize = 0;
  270. elementMask = 0;
  271. }
  272. }
  273. else
  274. {
  275. vertexData = 0;
  276. vertexSize = 0;
  277. elementMask = 0;
  278. }
  279. }
  280. if (rawIndexData_)
  281. {
  282. indexData = rawIndexData_;
  283. indexSize = rawIndexSize_;
  284. }
  285. else
  286. {
  287. if (indexBuffer_)
  288. {
  289. indexData = indexBuffer_->GetShadowDataShared();
  290. if (indexData)
  291. indexSize = indexBuffer_->GetIndexSize();
  292. else
  293. indexSize = 0;
  294. }
  295. else
  296. {
  297. indexData = 0;
  298. indexSize = 0;
  299. }
  300. }
  301. }
  302. float Geometry::GetHitDistance(const Ray& ray, Vector3* outNormal, Vector2* outUV) const
  303. {
  304. const unsigned char* vertexData;
  305. const unsigned char* indexData;
  306. unsigned vertexSize;
  307. unsigned indexSize;
  308. unsigned elementMask;
  309. unsigned uvOffset = 0;
  310. GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  311. if (vertexData)
  312. {
  313. if (outUV)
  314. {
  315. if ((elementMask & MASK_TEXCOORD1) == 0)
  316. {
  317. // requested UV output, but no texture data in vertex buffer
  318. URHO3D_LOGWARNING("Illegal GetHitDistance call: UV return requested on vertex buffer without UV coords");
  319. *outUV = Vector2::ZERO;
  320. outUV = 0;
  321. }
  322. else
  323. uvOffset = VertexBuffer::GetElementOffset(elementMask, ELEMENT_TEXCOORD1);
  324. }
  325. return indexData ? ray.HitDistance(vertexData, vertexSize, indexData, indexSize, indexStart_, indexCount_, outNormal, outUV,
  326. uvOffset) :
  327. ray.HitDistance(vertexData, vertexSize, vertexStart_, vertexCount_, outNormal, outUV, uvOffset);
  328. }
  329. return M_INFINITY;
  330. }
  331. bool Geometry::IsInside(const Ray& ray) const
  332. {
  333. const unsigned char* vertexData;
  334. const unsigned char* indexData;
  335. unsigned vertexSize;
  336. unsigned indexSize;
  337. unsigned elementMask;
  338. GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  339. return vertexData ? (indexData ? ray.InsideGeometry(vertexData, vertexSize, indexData, indexSize, indexStart_, indexCount_) :
  340. ray.InsideGeometry(vertexData, vertexSize, vertexStart_, vertexCount_)) : false;
  341. }
  342. void Geometry::GetPositionBufferIndex()
  343. {
  344. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  345. {
  346. if (vertexBuffers_[i] && vertexBuffers_[i]->GetElementMask() & MASK_POSITION)
  347. {
  348. positionBufferIndex_ = i;
  349. return;
  350. }
  351. }
  352. // No vertex buffer with positions
  353. positionBufferIndex_ = M_MAX_UNSIGNED;
  354. }
  355. }