Geometry.cpp 6.8 KB

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