Geometry.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. lodDistance_(0.0f)
  41. {
  42. SetNumVertexBuffers(1);
  43. }
  44. Geometry::~Geometry()
  45. {
  46. }
  47. bool Geometry::SetNumVertexBuffers(unsigned num)
  48. {
  49. if (num >= MAX_VERTEX_STREAMS)
  50. {
  51. LOGERROR("Too many vertex streams");
  52. return false;
  53. }
  54. unsigned oldSize = vertexBuffers_.Size();
  55. vertexBuffers_.Resize(num);
  56. elementMasks_.Resize(num);
  57. for (unsigned i = oldSize; i < num; ++i)
  58. elementMasks_[i] = MASK_NONE;
  59. GetPositionBufferIndex();
  60. return true;
  61. }
  62. bool Geometry::SetVertexBuffer(unsigned index, VertexBuffer* buffer, unsigned elementMask)
  63. {
  64. if (index >= vertexBuffers_.Size())
  65. {
  66. LOGERROR("Stream index out of bounds");
  67. return false;
  68. }
  69. vertexBuffers_[index] = buffer;
  70. if (buffer)
  71. {
  72. if (elementMask == MASK_DEFAULT)
  73. elementMasks_[index] = buffer->GetElementMask();
  74. else
  75. elementMasks_[index] = elementMask;
  76. }
  77. GetPositionBufferIndex();
  78. return true;
  79. }
  80. void Geometry::SetIndexBuffer(IndexBuffer* buffer)
  81. {
  82. indexBuffer_ = buffer;
  83. }
  84. bool Geometry::SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, bool getUsedVertexRange)
  85. {
  86. if (!indexBuffer_)
  87. {
  88. LOGERROR("Null index buffer, can not define indexed draw range");
  89. return false;
  90. }
  91. if (indexStart + indexCount > indexBuffer_->GetIndexCount())
  92. {
  93. LOGERROR("Illegal draw range");
  94. return false;
  95. }
  96. primitiveType_ = type;
  97. indexStart_ = indexStart;
  98. indexCount_ = indexCount;
  99. // Get min.vertex index and num of vertices from index buffer. If it fails, use full range as fallback
  100. vertexStart_ = 0;
  101. vertexCount_ = vertexBuffers_[0]->GetVertexCount();
  102. if (getUsedVertexRange)
  103. indexBuffer_->GetUsedVertexRange(indexStart_, indexCount_, vertexStart_, vertexCount_);
  104. return true;
  105. }
  106. bool Geometry::SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount)
  107. {
  108. if (indexBuffer_)
  109. {
  110. if (indexStart + indexCount > indexBuffer_->GetIndexCount())
  111. {
  112. LOGERROR("Illegal draw range");
  113. return false;
  114. }
  115. }
  116. else
  117. {
  118. indexStart = 0;
  119. indexCount = 0;
  120. }
  121. primitiveType_ = type;
  122. indexStart_ = indexStart;
  123. indexCount_ = indexCount;
  124. vertexStart_ = minVertex;
  125. vertexCount_ = vertexCount;
  126. return true;
  127. }
  128. void Geometry::SetLodDistance(float distance)
  129. {
  130. if (distance < 0.0f)
  131. distance = 0.0f;
  132. lodDistance_ = distance;
  133. }
  134. void Geometry::Draw(Graphics* graphics)
  135. {
  136. if (indexBuffer_)
  137. {
  138. graphics->SetIndexBuffer(indexBuffer_);
  139. graphics->SetVertexBuffers(vertexBuffers_, elementMasks_);
  140. graphics->Draw(primitiveType_, indexStart_, indexCount_, vertexStart_, vertexCount_);
  141. }
  142. else
  143. {
  144. graphics->SetVertexBuffers(vertexBuffers_, elementMasks_);
  145. graphics->Draw(primitiveType_, vertexStart_, vertexCount_);
  146. }
  147. }
  148. VertexBuffer* Geometry::GetVertexBuffer(unsigned index) const
  149. {
  150. return index < vertexBuffers_.Size() ? vertexBuffers_[index] : (VertexBuffer*)0;
  151. }
  152. unsigned Geometry::GetVertexElementMask(unsigned index) const
  153. {
  154. return index < elementMasks_.Size() ? elementMasks_[index] : 0;
  155. }
  156. bool Geometry::IsEmpty() const
  157. {
  158. if (indexBuffer_)
  159. return indexCount_ == 0;
  160. else
  161. return vertexCount_ == 0;
  162. }
  163. unsigned short Geometry::GetBufferHash() const
  164. {
  165. unsigned short hash = 0;
  166. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  167. {
  168. VertexBuffer* vBuf = vertexBuffers_[i];
  169. hash += *((unsigned short*)&vBuf);
  170. }
  171. IndexBuffer* iBuf = indexBuffer_;
  172. hash += *((unsigned short*)&iBuf);
  173. return hash;
  174. }
  175. void Geometry::GetRawData(const unsigned char*& vertexData, unsigned& vertexSize, const unsigned char*& indexData, unsigned& indexSize, unsigned& elementMask)
  176. {
  177. if (positionBufferIndex_ < vertexBuffers_.Size() && vertexBuffers_[positionBufferIndex_])
  178. {
  179. vertexData = vertexBuffers_[positionBufferIndex_]->GetShadowData();
  180. if (vertexData)
  181. {
  182. vertexSize = vertexBuffers_[positionBufferIndex_]->GetVertexSize();
  183. elementMask = vertexBuffers_[positionBufferIndex_]->GetElementMask();
  184. }
  185. else
  186. {
  187. vertexSize = 0;
  188. elementMask = 0;
  189. }
  190. }
  191. else
  192. {
  193. vertexData = 0;
  194. vertexSize = 0;
  195. elementMask = 0;
  196. }
  197. if (indexBuffer_)
  198. {
  199. indexData = indexBuffer_->GetShadowData();
  200. if (indexData)
  201. indexSize = indexBuffer_->GetIndexSize();
  202. else
  203. indexSize = 0;
  204. }
  205. else
  206. {
  207. indexData = 0;
  208. indexSize = 0;
  209. }
  210. }
  211. float Geometry::GetDistance(const Ray& ray)
  212. {
  213. const unsigned char* rawVertexData;
  214. const unsigned char* rawIndexData;
  215. unsigned vertexSize;
  216. unsigned indexSize;
  217. unsigned elementMask;
  218. GetRawData(rawVertexData, vertexSize, rawIndexData, indexSize, elementMask);
  219. if (!rawVertexData || !rawIndexData)
  220. return M_INFINITY;
  221. return ray.HitDistance(rawVertexData, vertexSize, rawIndexData, indexSize, indexStart_, indexCount_);
  222. }
  223. void Geometry::GetPositionBufferIndex()
  224. {
  225. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  226. {
  227. if (vertexBuffers_[i] && vertexBuffers_[i]->GetElementMask() & MASK_POSITION)
  228. {
  229. positionBufferIndex_ = i;
  230. return;
  231. }
  232. }
  233. // No vertex buffer with positions
  234. positionBufferIndex_ = M_MAX_UNSIGNED;
  235. }