Geometry.cpp 8.6 KB

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