Geometry.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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_ && indexCount_ > 0)
  137. {
  138. graphics->SetIndexBuffer(indexBuffer_);
  139. graphics->SetVertexBuffers(vertexBuffers_, elementMasks_);
  140. graphics->Draw(primitiveType_, indexStart_, indexCount_, vertexStart_, vertexCount_);
  141. }
  142. else if (vertexCount_ > 0)
  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. unsigned short Geometry::GetBufferHash() const
  157. {
  158. unsigned short hash = 0;
  159. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  160. {
  161. VertexBuffer* vBuf = vertexBuffers_[i];
  162. hash += *((unsigned short*)&vBuf);
  163. }
  164. IndexBuffer* iBuf = indexBuffer_;
  165. hash += *((unsigned short*)&iBuf);
  166. return hash;
  167. }
  168. void Geometry::GetRawData(const unsigned char*& vertexData, unsigned& vertexSize, const unsigned char*& indexData, unsigned& indexSize, unsigned& elementMask)
  169. {
  170. if (positionBufferIndex_ < vertexBuffers_.Size() && vertexBuffers_[positionBufferIndex_])
  171. {
  172. vertexData = vertexBuffers_[positionBufferIndex_]->GetShadowData();
  173. if (vertexData)
  174. {
  175. vertexSize = vertexBuffers_[positionBufferIndex_]->GetVertexSize();
  176. elementMask = vertexBuffers_[positionBufferIndex_]->GetElementMask();
  177. }
  178. else
  179. {
  180. vertexSize = 0;
  181. elementMask = 0;
  182. }
  183. }
  184. else
  185. {
  186. vertexData = 0;
  187. vertexSize = 0;
  188. elementMask = 0;
  189. }
  190. if (indexBuffer_)
  191. {
  192. indexData = indexBuffer_->GetShadowData();
  193. if (indexData)
  194. indexSize = indexBuffer_->GetIndexSize();
  195. else
  196. indexSize = 0;
  197. }
  198. else
  199. {
  200. indexData = 0;
  201. indexSize = 0;
  202. }
  203. }
  204. float Geometry::GetDistance(const Ray& ray)
  205. {
  206. const unsigned char* rawVertexData;
  207. const unsigned char* rawIndexData;
  208. unsigned vertexSize;
  209. unsigned indexSize;
  210. unsigned elementMask;
  211. GetRawData(rawVertexData, vertexSize, rawIndexData, indexSize, elementMask);
  212. if (!rawVertexData || !rawIndexData)
  213. return M_INFINITY;
  214. return ray.HitDistance(rawVertexData, vertexSize, rawIndexData, indexSize, indexStart_, indexCount_);
  215. }
  216. void Geometry::GetPositionBufferIndex()
  217. {
  218. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  219. {
  220. if (vertexBuffers_[i] && vertexBuffers_[i]->GetElementMask() & MASK_POSITION)
  221. {
  222. positionBufferIndex_ = i;
  223. return;
  224. }
  225. }
  226. // No vertex buffer with positions
  227. positionBufferIndex_ = M_MAX_UNSIGNED;
  228. }