Geometry.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  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 "Geometry.h"
  25. #include "Graphics.h"
  26. #include "IndexBuffer.h"
  27. #include "Log.h"
  28. #include "Ray.h"
  29. #include "VertexBuffer.h"
  30. #include "DebugNew.h"
  31. namespace Urho3D
  32. {
  33. OBJECTTYPESTATIC(Geometry);
  34. Geometry::Geometry(Context* context) :
  35. Object(context),
  36. primitiveType_(TRIANGLE_LIST),
  37. indexStart_(0),
  38. indexCount_(0),
  39. vertexStart_(0),
  40. vertexCount_(0),
  41. positionBufferIndex_(M_MAX_UNSIGNED),
  42. rawVertexSize_(0),
  43. rawElementMask_(0),
  44. rawIndexSize_(0),
  45. lodDistance_(0.0f)
  46. {
  47. SetNumVertexBuffers(1);
  48. }
  49. Geometry::~Geometry()
  50. {
  51. }
  52. bool Geometry::SetNumVertexBuffers(unsigned num)
  53. {
  54. if (num >= MAX_VERTEX_STREAMS)
  55. {
  56. LOGERROR("Too many vertex streams");
  57. return false;
  58. }
  59. unsigned oldSize = vertexBuffers_.Size();
  60. vertexBuffers_.Resize(num);
  61. elementMasks_.Resize(num);
  62. for (unsigned i = oldSize; i < num; ++i)
  63. elementMasks_[i] = MASK_NONE;
  64. GetPositionBufferIndex();
  65. return true;
  66. }
  67. bool Geometry::SetVertexBuffer(unsigned index, VertexBuffer* buffer, unsigned elementMask)
  68. {
  69. if (index >= vertexBuffers_.Size())
  70. {
  71. LOGERROR("Stream index out of bounds");
  72. return false;
  73. }
  74. vertexBuffers_[index] = buffer;
  75. if (buffer)
  76. {
  77. if (elementMask == MASK_DEFAULT)
  78. elementMasks_[index] = buffer->GetElementMask();
  79. else
  80. elementMasks_[index] = elementMask;
  81. }
  82. GetPositionBufferIndex();
  83. return true;
  84. }
  85. void Geometry::SetIndexBuffer(IndexBuffer* buffer)
  86. {
  87. indexBuffer_ = buffer;
  88. }
  89. bool Geometry::SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, bool getUsedVertexRange)
  90. {
  91. if (!indexBuffer_)
  92. {
  93. LOGERROR("Null index buffer, can not define indexed draw range");
  94. return false;
  95. }
  96. if (indexStart + indexCount > indexBuffer_->GetIndexCount())
  97. {
  98. LOGERROR("Illegal draw range");
  99. return false;
  100. }
  101. primitiveType_ = type;
  102. indexStart_ = indexStart;
  103. indexCount_ = indexCount;
  104. // Get min.vertex index and num of vertices from index buffer. If it fails, use full range as fallback
  105. if (indexCount)
  106. {
  107. vertexStart_ = 0;
  108. vertexCount_ = vertexBuffers_[0]->GetVertexCount();
  109. if (getUsedVertexRange)
  110. indexBuffer_->GetUsedVertexRange(indexStart_, indexCount_, vertexStart_, vertexCount_);
  111. }
  112. else
  113. {
  114. vertexStart_ = 0;
  115. vertexCount_ = 0;
  116. }
  117. return true;
  118. }
  119. bool Geometry::SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount)
  120. {
  121. if (indexBuffer_)
  122. {
  123. if (indexStart + indexCount > indexBuffer_->GetIndexCount())
  124. {
  125. LOGERROR("Illegal draw range");
  126. return false;
  127. }
  128. }
  129. else
  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, unsigned& indexSize, unsigned& elementMask)
  193. {
  194. if (rawVertexData_)
  195. {
  196. vertexData = rawVertexData_.Get();
  197. vertexSize = rawVertexSize_;
  198. elementMask = rawElementMask_;
  199. }
  200. else
  201. {
  202. if (positionBufferIndex_ < vertexBuffers_.Size() && vertexBuffers_[positionBufferIndex_])
  203. {
  204. vertexData = vertexBuffers_[positionBufferIndex_]->GetShadowData();
  205. if (vertexData)
  206. {
  207. vertexSize = vertexBuffers_[positionBufferIndex_]->GetVertexSize();
  208. elementMask = vertexBuffers_[positionBufferIndex_]->GetElementMask();
  209. }
  210. else
  211. {
  212. vertexSize = 0;
  213. elementMask = 0;
  214. }
  215. }
  216. else
  217. {
  218. vertexData = 0;
  219. vertexSize = 0;
  220. elementMask = 0;
  221. }
  222. }
  223. if (rawIndexData_)
  224. {
  225. indexData = rawIndexData_.Get();
  226. indexSize = rawIndexSize_;
  227. }
  228. else
  229. {
  230. if (indexBuffer_)
  231. {
  232. indexData = indexBuffer_->GetShadowData();
  233. if (indexData)
  234. indexSize = indexBuffer_->GetIndexSize();
  235. else
  236. indexSize = 0;
  237. }
  238. else
  239. {
  240. indexData = 0;
  241. indexSize = 0;
  242. }
  243. }
  244. }
  245. float Geometry::GetHitDistance(const Ray& ray)
  246. {
  247. const unsigned char* vertexData;
  248. const unsigned char* indexData;
  249. unsigned vertexSize;
  250. unsigned indexSize;
  251. unsigned elementMask;
  252. GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  253. if (!vertexData || !indexData)
  254. return M_INFINITY;
  255. return ray.HitDistance(vertexData, vertexSize, indexData, indexSize, indexStart_, indexCount_);
  256. }
  257. void Geometry::GetPositionBufferIndex()
  258. {
  259. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  260. {
  261. if (vertexBuffers_[i] && vertexBuffers_[i]->GetElementMask() & MASK_POSITION)
  262. {
  263. positionBufferIndex_ = i;
  264. return;
  265. }
  266. }
  267. // No vertex buffer with positions
  268. positionBufferIndex_ = M_MAX_UNSIGNED;
  269. }
  270. }