Geometry.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 = vertexBuffer_.Size();
  56. vertexBuffer_.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 >= vertexBuffer_.Size())
  66. {
  67. LOGERROR("Stream index out of bounds");
  68. return false;
  69. }
  70. vertexBuffer_[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_ = vertexBuffer_[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(vertexBuffer_, elementMasks_);
  136. graphics->Draw(primitiveType_, indexStart_, indexCount_, vertexStart_, vertexCount_);
  137. }
  138. VertexBuffer* Geometry::GetVertexBuffer(unsigned index) const
  139. {
  140. return index < vertexBuffer_.Size() ? vertexBuffer_[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 < vertexBuffer_.Size(); ++i)
  150. {
  151. VertexBuffer* vBuf = vertexBuffer_[i];
  152. hash += *((unsigned short*)&vBuf);
  153. }
  154. IndexBuffer* iBuf = indexBuffer_;
  155. hash += *((unsigned short*)&iBuf);
  156. return hash;
  157. }
  158. float Geometry::GetDistance(const Ray& ray)
  159. {
  160. const unsigned char* vertexData;
  161. const unsigned char* indexData;
  162. unsigned vertexSize;
  163. unsigned indexSize;
  164. LockRawData(vertexData, vertexSize, indexData, indexSize);
  165. if ((!indexData) || (!vertexData))
  166. return M_INFINITY;
  167. float nearest = ray.GetDistance(vertexData, vertexSize, indexData, indexSize, indexStart_, indexCount_);
  168. UnlockRawData();
  169. return nearest;
  170. }
  171. void Geometry::LockRawData(const unsigned char*& vertexData, unsigned& vertexSize, const unsigned char*& indexData, unsigned& indexSize)
  172. {
  173. if ((indexBuffer_) && (positionBufferIndex_ < vertexBuffer_.Size()))
  174. {
  175. VertexBuffer* positionBuffer = vertexBuffer_[positionBufferIndex_];
  176. vertexData = reinterpret_cast<const unsigned char*>(positionBuffer->Lock(0, positionBuffer->GetVertexCount(), LOCK_READONLY));
  177. vertexSize = positionBuffer->GetVertexSize();
  178. indexData = reinterpret_cast<const unsigned char*>(indexBuffer_->Lock(0, indexBuffer_->GetIndexCount(), LOCK_READONLY));
  179. indexSize = indexBuffer_->GetIndexSize();
  180. if ((vertexData) && (indexData))
  181. return;
  182. // One of the locks failed; unlock the other
  183. if (vertexData)
  184. positionBuffer->Unlock();
  185. if (indexData)
  186. indexBuffer_->Unlock();
  187. }
  188. // Lock failed, or no buffers: return null pointers and sizes
  189. vertexData = 0;
  190. vertexSize = 0;
  191. indexData = 0;
  192. indexSize = 0;
  193. }
  194. void Geometry::UnlockRawData()
  195. {
  196. if ((indexBuffer_) && (positionBufferIndex_ < vertexBuffer_.Size()))
  197. {
  198. vertexBuffer_[positionBufferIndex_]->Unlock();
  199. indexBuffer_->Unlock();
  200. }
  201. }
  202. void Geometry::GetPositionBufferIndex()
  203. {
  204. for (unsigned i = 0; i < vertexBuffer_.Size(); ++i)
  205. {
  206. if ((vertexBuffer_[i]) && (vertexBuffer_[i]->GetElementMask() & MASK_POSITION))
  207. {
  208. positionBufferIndex_ = i;
  209. return;
  210. }
  211. }
  212. // No vertex buffer with positions
  213. positionBufferIndex_ = M_MAX_UNSIGNED;
  214. }