Geometry.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. OBJECTTYPESTATIC(Geometry);
  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_)
  90. {
  91. LOGERROR("Null index buffer, can not define indexed draw range");
  92. return false;
  93. }
  94. if (indexStart + indexCount > indexBuffer_->GetIndexCount())
  95. {
  96. LOGERROR("Illegal draw range");
  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]->GetVertexCount();
  107. if (getUsedVertexRange)
  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)
  118. {
  119. if (indexBuffer_)
  120. {
  121. if (indexStart + indexCount > indexBuffer_->GetIndexCount())
  122. {
  123. LOGERROR("Illegal draw range");
  124. return false;
  125. }
  126. }
  127. else
  128. {
  129. indexStart = 0;
  130. indexCount = 0;
  131. }
  132. primitiveType_ = type;
  133. indexStart_ = indexStart;
  134. indexCount_ = indexCount;
  135. vertexStart_ = minVertex;
  136. vertexCount_ = vertexCount;
  137. return true;
  138. }
  139. void Geometry::SetLodDistance(float distance)
  140. {
  141. if (distance < 0.0f)
  142. distance = 0.0f;
  143. lodDistance_ = distance;
  144. }
  145. void Geometry::SetRawVertexData(SharedArrayPtr<unsigned char> data, unsigned vertexSize, unsigned elementMask)
  146. {
  147. rawVertexData_ = data;
  148. rawVertexSize_ = vertexSize;
  149. rawElementMask_ = elementMask;
  150. }
  151. void Geometry::SetRawIndexData(SharedArrayPtr<unsigned char> data, unsigned indexSize)
  152. {
  153. rawIndexData_ = data;
  154. rawIndexSize_ = indexSize;
  155. }
  156. void Geometry::Draw(Graphics* graphics)
  157. {
  158. if (indexBuffer_ && indexCount_ > 0)
  159. {
  160. graphics->SetIndexBuffer(indexBuffer_);
  161. graphics->SetVertexBuffers(vertexBuffers_, elementMasks_);
  162. graphics->Draw(primitiveType_, indexStart_, indexCount_, vertexStart_, vertexCount_);
  163. }
  164. else if (vertexCount_ > 0)
  165. {
  166. graphics->SetVertexBuffers(vertexBuffers_, elementMasks_);
  167. graphics->Draw(primitiveType_, vertexStart_, vertexCount_);
  168. }
  169. }
  170. VertexBuffer* Geometry::GetVertexBuffer(unsigned index) const
  171. {
  172. return index < vertexBuffers_.Size() ? vertexBuffers_[index] : (VertexBuffer*)0;
  173. }
  174. unsigned Geometry::GetVertexElementMask(unsigned index) const
  175. {
  176. return index < elementMasks_.Size() ? elementMasks_[index] : 0;
  177. }
  178. unsigned short Geometry::GetBufferHash() const
  179. {
  180. unsigned short hash = 0;
  181. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  182. {
  183. VertexBuffer* vBuf = vertexBuffers_[i];
  184. hash += *((unsigned short*)&vBuf);
  185. }
  186. IndexBuffer* iBuf = indexBuffer_;
  187. hash += *((unsigned short*)&iBuf);
  188. return hash;
  189. }
  190. void Geometry::GetRawData(const unsigned char*& vertexData, unsigned& vertexSize, const unsigned char*& indexData, unsigned& indexSize, unsigned& elementMask)
  191. {
  192. if (rawVertexData_)
  193. {
  194. vertexData = rawVertexData_.Get();
  195. vertexSize = rawVertexSize_;
  196. elementMask = rawElementMask_;
  197. }
  198. else
  199. {
  200. if (positionBufferIndex_ < vertexBuffers_.Size() && vertexBuffers_[positionBufferIndex_])
  201. {
  202. vertexData = vertexBuffers_[positionBufferIndex_]->GetShadowData();
  203. if (vertexData)
  204. {
  205. vertexSize = vertexBuffers_[positionBufferIndex_]->GetVertexSize();
  206. elementMask = vertexBuffers_[positionBufferIndex_]->GetElementMask();
  207. }
  208. else
  209. {
  210. vertexSize = 0;
  211. elementMask = 0;
  212. }
  213. }
  214. else
  215. {
  216. vertexData = 0;
  217. vertexSize = 0;
  218. elementMask = 0;
  219. }
  220. }
  221. if (rawIndexData_)
  222. {
  223. indexData = rawIndexData_.Get();
  224. indexSize = rawIndexSize_;
  225. }
  226. else
  227. {
  228. if (indexBuffer_)
  229. {
  230. indexData = indexBuffer_->GetShadowData();
  231. if (indexData)
  232. indexSize = indexBuffer_->GetIndexSize();
  233. else
  234. indexSize = 0;
  235. }
  236. else
  237. {
  238. indexData = 0;
  239. indexSize = 0;
  240. }
  241. }
  242. }
  243. float Geometry::GetHitDistance(const Ray& ray)
  244. {
  245. const unsigned char* vertexData;
  246. const unsigned char* indexData;
  247. unsigned vertexSize;
  248. unsigned indexSize;
  249. unsigned elementMask;
  250. GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  251. if (!vertexData || !indexData)
  252. return M_INFINITY;
  253. return ray.HitDistance(vertexData, vertexSize, indexData, indexSize, indexStart_, indexCount_);
  254. }
  255. void Geometry::GetPositionBufferIndex()
  256. {
  257. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  258. {
  259. if (vertexBuffers_[i] && vertexBuffers_[i]->GetElementMask() & MASK_POSITION)
  260. {
  261. positionBufferIndex_ = i;
  262. return;
  263. }
  264. }
  265. // No vertex buffer with positions
  266. positionBufferIndex_ = M_MAX_UNSIGNED;
  267. }