Geometry.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. lodDistance_(0.0f),
  41. positionBufferIndex_(M_MAX_UNSIGNED)
  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::SetRawData(const SharedArrayPtr<unsigned char>& vertexData, const SharedArrayPtr<unsigned char>& indexData)
  133. {
  134. rawVertexData_ = vertexData;
  135. rawIndexData_ = indexData;
  136. }
  137. void Geometry::Draw(Graphics* graphics)
  138. {
  139. graphics->SetIndexBuffer(indexBuffer_);
  140. graphics->SetVertexBuffers(vertexBuffers_, elementMasks_);
  141. graphics->Draw(primitiveType_, indexStart_, indexCount_, vertexStart_, vertexCount_);
  142. }
  143. VertexBuffer* Geometry::GetVertexBuffer(unsigned index) const
  144. {
  145. return index < vertexBuffers_.Size() ? vertexBuffers_[index] : (VertexBuffer*)0;
  146. }
  147. unsigned Geometry::GetVertexElementMask(unsigned index) const
  148. {
  149. return index < elementMasks_.Size() ? elementMasks_[index] : 0;
  150. }
  151. unsigned short Geometry::GetBufferHash() const
  152. {
  153. unsigned short hash = 0;
  154. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  155. {
  156. VertexBuffer* vBuf = vertexBuffers_[i];
  157. hash += *((unsigned short*)&vBuf);
  158. }
  159. IndexBuffer* iBuf = indexBuffer_;
  160. hash += *((unsigned short*)&iBuf);
  161. return hash;
  162. }
  163. void Geometry::GetRawData(const unsigned char*& vertexData, unsigned& vertexSize, const unsigned char*& indexData, unsigned& indexSize)
  164. {
  165. if (rawVertexData_)
  166. {
  167. vertexData = rawVertexData_.Get();
  168. vertexSize = 3 * sizeof(float);
  169. }
  170. else
  171. {
  172. vertexData = 0;
  173. vertexSize = 0;
  174. }
  175. if (rawIndexData_ && indexBuffer_)
  176. {
  177. indexData = rawIndexData_.Get();
  178. indexSize = indexBuffer_->GetIndexSize();
  179. }
  180. else
  181. {
  182. indexData = 0;
  183. indexSize = 0;
  184. }
  185. }
  186. float Geometry::GetDistance(const Ray& ray)
  187. {
  188. if (!rawIndexData_ || !rawVertexData_ || !indexBuffer_)
  189. return M_INFINITY;
  190. return ray.HitDistance(rawVertexData_.Get(), 3 * sizeof(float), rawIndexData_.Get(), indexBuffer_->GetIndexSize(), indexStart_, indexCount_);
  191. }
  192. void Geometry::GetPositionBufferIndex()
  193. {
  194. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  195. {
  196. if (vertexBuffers_[i] && vertexBuffers_[i]->GetElementMask() & MASK_POSITION)
  197. {
  198. positionBufferIndex_ = i;
  199. return;
  200. }
  201. }
  202. // No vertex buffer with positions
  203. positionBufferIndex_ = M_MAX_UNSIGNED;
  204. }