Geometry.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // Copyright (c) 2008-2014 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 "../IO/Log.h"
  27. #include "../Math/Ray.h"
  28. #include "../Graphics/VertexBuffer.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. 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. 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. 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. 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, bool checkIllegal)
  119. {
  120. if (indexBuffer_)
  121. {
  122. // We can allow setting an illegal draw range now if the caller guarantees to resize / fill the buffer later
  123. if (checkIllegal && indexStart + indexCount > indexBuffer_->GetIndexCount())
  124. {
  125. LOGERROR("Illegal draw range " + String(indexStart) + " to " + String(indexStart + indexCount - 1) +
  126. ", index buffer has " + String(indexBuffer_->GetIndexCount()) + " indices");
  127. return false;
  128. }
  129. }
  130. else if (!rawIndexData_)
  131. {
  132. indexStart = 0;
  133. indexCount = 0;
  134. }
  135. primitiveType_ = type;
  136. indexStart_ = indexStart;
  137. indexCount_ = indexCount;
  138. vertexStart_ = minVertex;
  139. vertexCount_ = vertexCount;
  140. return true;
  141. }
  142. void Geometry::SetLodDistance(float distance)
  143. {
  144. if (distance < 0.0f)
  145. distance = 0.0f;
  146. lodDistance_ = distance;
  147. }
  148. void Geometry::SetRawVertexData(SharedArrayPtr<unsigned char> data, unsigned vertexSize, unsigned elementMask)
  149. {
  150. rawVertexData_ = data;
  151. rawVertexSize_ = vertexSize;
  152. rawElementMask_ = elementMask;
  153. }
  154. void Geometry::SetRawIndexData(SharedArrayPtr<unsigned char> data, unsigned indexSize)
  155. {
  156. rawIndexData_ = data;
  157. rawIndexSize_ = indexSize;
  158. }
  159. void Geometry::Draw(Graphics* graphics)
  160. {
  161. if (indexBuffer_ && indexCount_ > 0)
  162. {
  163. graphics->SetIndexBuffer(indexBuffer_);
  164. graphics->SetVertexBuffers(vertexBuffers_, elementMasks_);
  165. graphics->Draw(primitiveType_, indexStart_, indexCount_, vertexStart_, vertexCount_);
  166. }
  167. else if (vertexCount_ > 0)
  168. {
  169. graphics->SetVertexBuffers(vertexBuffers_, elementMasks_);
  170. graphics->Draw(primitiveType_, vertexStart_, vertexCount_);
  171. }
  172. }
  173. VertexBuffer* Geometry::GetVertexBuffer(unsigned index) const
  174. {
  175. return index < vertexBuffers_.Size() ? vertexBuffers_[index] : (VertexBuffer*)0;
  176. }
  177. unsigned Geometry::GetVertexElementMask(unsigned index) const
  178. {
  179. return index < elementMasks_.Size() ? elementMasks_[index] : 0;
  180. }
  181. unsigned short Geometry::GetBufferHash() const
  182. {
  183. unsigned short hash = 0;
  184. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  185. {
  186. VertexBuffer* vBuf = vertexBuffers_[i];
  187. hash += *((unsigned short*)&vBuf);
  188. }
  189. IndexBuffer* iBuf = indexBuffer_;
  190. hash += *((unsigned short*)&iBuf);
  191. return hash;
  192. }
  193. void Geometry::GetRawData(const unsigned char*& vertexData, unsigned& vertexSize, const unsigned char*& indexData,
  194. unsigned& indexSize, unsigned& elementMask) const
  195. {
  196. if (rawVertexData_)
  197. {
  198. vertexData = rawVertexData_;
  199. vertexSize = rawVertexSize_;
  200. elementMask = rawElementMask_;
  201. }
  202. else
  203. {
  204. if (positionBufferIndex_ < vertexBuffers_.Size() && vertexBuffers_[positionBufferIndex_])
  205. {
  206. vertexData = vertexBuffers_[positionBufferIndex_]->GetShadowData();
  207. if (vertexData)
  208. {
  209. vertexSize = vertexBuffers_[positionBufferIndex_]->GetVertexSize();
  210. elementMask = vertexBuffers_[positionBufferIndex_]->GetElementMask();
  211. }
  212. else
  213. {
  214. vertexSize = 0;
  215. elementMask = 0;
  216. }
  217. }
  218. else
  219. {
  220. vertexData = 0;
  221. vertexSize = 0;
  222. elementMask = 0;
  223. }
  224. }
  225. if (rawIndexData_)
  226. {
  227. indexData = rawIndexData_;
  228. indexSize = rawIndexSize_;
  229. }
  230. else
  231. {
  232. if (indexBuffer_)
  233. {
  234. indexData = indexBuffer_->GetShadowData();
  235. if (indexData)
  236. indexSize = indexBuffer_->GetIndexSize();
  237. else
  238. indexSize = 0;
  239. }
  240. else
  241. {
  242. indexData = 0;
  243. indexSize = 0;
  244. }
  245. }
  246. }
  247. void Geometry::GetRawDataShared(SharedArrayPtr<unsigned char>& vertexData, unsigned& vertexSize,
  248. SharedArrayPtr<unsigned char>& indexData, unsigned& indexSize, unsigned& elementMask) const
  249. {
  250. if (rawVertexData_)
  251. {
  252. vertexData = rawVertexData_;
  253. vertexSize = rawVertexSize_;
  254. elementMask = rawElementMask_;
  255. }
  256. else
  257. {
  258. if (positionBufferIndex_ < vertexBuffers_.Size() && vertexBuffers_[positionBufferIndex_])
  259. {
  260. vertexData = vertexBuffers_[positionBufferIndex_]->GetShadowDataShared();
  261. if (vertexData)
  262. {
  263. vertexSize = vertexBuffers_[positionBufferIndex_]->GetVertexSize();
  264. elementMask = vertexBuffers_[positionBufferIndex_]->GetElementMask();
  265. }
  266. else
  267. {
  268. vertexSize = 0;
  269. elementMask = 0;
  270. }
  271. }
  272. else
  273. {
  274. vertexData = 0;
  275. vertexSize = 0;
  276. elementMask = 0;
  277. }
  278. }
  279. if (rawIndexData_)
  280. {
  281. indexData = rawIndexData_;
  282. indexSize = rawIndexSize_;
  283. }
  284. else
  285. {
  286. if (indexBuffer_)
  287. {
  288. indexData = indexBuffer_->GetShadowDataShared();
  289. if (indexData)
  290. indexSize = indexBuffer_->GetIndexSize();
  291. else
  292. indexSize = 0;
  293. }
  294. else
  295. {
  296. indexData = 0;
  297. indexSize = 0;
  298. }
  299. }
  300. }
  301. float Geometry::GetHitDistance(const Ray& ray, Vector3* outNormal) const
  302. {
  303. const unsigned char* vertexData;
  304. const unsigned char* indexData;
  305. unsigned vertexSize;
  306. unsigned indexSize;
  307. unsigned elementMask;
  308. GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  309. if (vertexData && indexData)
  310. return ray.HitDistance(vertexData, vertexSize, indexData, indexSize, indexStart_, indexCount_, outNormal);
  311. else if (vertexData)
  312. return ray.HitDistance(vertexData, vertexSize, vertexStart_, vertexCount_, outNormal);
  313. else
  314. return M_INFINITY;
  315. }
  316. bool Geometry::IsInside(const Ray& ray) const
  317. {
  318. const unsigned char* vertexData;
  319. const unsigned char* indexData;
  320. unsigned vertexSize;
  321. unsigned indexSize;
  322. unsigned elementMask;
  323. GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  324. if (vertexData && indexData)
  325. return ray.InsideGeometry(vertexData, vertexSize, indexData, indexSize, indexStart_, indexCount_);
  326. else if (vertexData)
  327. return ray.InsideGeometry(vertexData, vertexSize, vertexStart_, vertexCount_);
  328. else
  329. return false;
  330. }
  331. void Geometry::GetPositionBufferIndex()
  332. {
  333. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  334. {
  335. if (vertexBuffers_[i] && vertexBuffers_[i]->GetElementMask() & MASK_POSITION)
  336. {
  337. positionBufferIndex_ = i;
  338. return;
  339. }
  340. }
  341. // No vertex buffer with positions
  342. positionBufferIndex_ = M_MAX_UNSIGNED;
  343. }
  344. }