Geometry.cpp 12 KB

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