Geometry.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Geometry.h"
  24. #include "Graphics.h"
  25. #include "IndexBuffer.h"
  26. #include "Log.h"
  27. #include "Ray.h"
  28. #include "VertexBuffer.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  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. rawVertexSize_(0),
  42. rawElementMask_(0),
  43. rawIndexSize_(0),
  44. lodDistance_(0.0f)
  45. {
  46. SetNumVertexBuffers(1);
  47. }
  48. Geometry::~Geometry()
  49. {
  50. }
  51. bool Geometry::SetNumVertexBuffers(unsigned num)
  52. {
  53. if (num >= MAX_VERTEX_STREAMS)
  54. {
  55. LOGERROR("Too many vertex streams");
  56. return false;
  57. }
  58. unsigned oldSize = vertexBuffers_.Size();
  59. vertexBuffers_.Resize(num);
  60. elementMasks_.Resize(num);
  61. for (unsigned i = oldSize; i < num; ++i)
  62. elementMasks_[i] = MASK_NONE;
  63. GetPositionBufferIndex();
  64. return true;
  65. }
  66. bool Geometry::SetVertexBuffer(unsigned index, VertexBuffer* buffer, unsigned elementMask)
  67. {
  68. if (index >= vertexBuffers_.Size())
  69. {
  70. LOGERROR("Stream index out of bounds");
  71. return false;
  72. }
  73. vertexBuffers_[index] = buffer;
  74. if (buffer)
  75. {
  76. if (elementMask == MASK_DEFAULT)
  77. elementMasks_[index] = buffer->GetElementMask();
  78. else
  79. elementMasks_[index] = elementMask;
  80. }
  81. GetPositionBufferIndex();
  82. return true;
  83. }
  84. void Geometry::SetIndexBuffer(IndexBuffer* buffer)
  85. {
  86. indexBuffer_ = buffer;
  87. }
  88. bool Geometry::SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, bool getUsedVertexRange)
  89. {
  90. if (!indexBuffer_)
  91. {
  92. LOGERROR("Null index buffer, can not define indexed draw range");
  93. return false;
  94. }
  95. if (indexStart + indexCount > indexBuffer_->GetIndexCount())
  96. {
  97. LOGERROR("Illegal draw range " + String(indexStart) + " to " + String(indexStart + indexCount - 1) + ", index buffer has " +
  98. String(indexBuffer_->GetIndexCount()) + " indices");
  99. return false;
  100. }
  101. primitiveType_ = type;
  102. indexStart_ = indexStart;
  103. indexCount_ = indexCount;
  104. // Get min.vertex index and num of vertices from index buffer. If it fails, use full range as fallback
  105. if (indexCount)
  106. {
  107. vertexStart_ = 0;
  108. vertexCount_ = vertexBuffers_[0]->GetVertexCount();
  109. if (getUsedVertexRange)
  110. indexBuffer_->GetUsedVertexRange(indexStart_, indexCount_, vertexStart_, vertexCount_);
  111. }
  112. else
  113. {
  114. vertexStart_ = 0;
  115. vertexCount_ = 0;
  116. }
  117. return true;
  118. }
  119. bool Geometry::SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount)
  120. {
  121. if (indexBuffer_)
  122. {
  123. if (indexStart + indexCount > indexBuffer_->GetIndexCount())
  124. {
  125. LOGERROR("Illegal draw range " + String(indexStart) + " to " + String(indexStart + indexCount - 1) +
  126. ", index buffer has " + String(indexBuffer_->GetIndexCount()) + " indices");
  127. return false;
  128. }
  129. }
  130. else
  131. {
  132. indexStart = 0;
  133. indexCount = 0;
  134. }
  135. primitiveType_ = type;
  136. indexStart_ = indexStart;
  137. indexCount_ = indexCount;
  138. vertexStart_ = minVertex;
  139. vertexCount_ = vertexCount;
  140. return true;
  141. }
  142. void Geometry::SetLodDistance(float distance)
  143. {
  144. if (distance < 0.0f)
  145. distance = 0.0f;
  146. lodDistance_ = distance;
  147. }
  148. void Geometry::SetRawVertexData(SharedArrayPtr<unsigned char> data, unsigned vertexSize, unsigned elementMask)
  149. {
  150. rawVertexData_ = data;
  151. rawVertexSize_ = vertexSize;
  152. rawElementMask_ = elementMask;
  153. }
  154. void Geometry::SetRawIndexData(SharedArrayPtr<unsigned char> data, unsigned indexSize)
  155. {
  156. rawIndexData_ = data;
  157. rawIndexSize_ = indexSize;
  158. }
  159. void Geometry::Draw(Graphics* graphics)
  160. {
  161. if (indexBuffer_ && indexCount_ > 0)
  162. {
  163. graphics->SetIndexBuffer(indexBuffer_);
  164. graphics->SetVertexBuffers(vertexBuffers_, elementMasks_);
  165. graphics->Draw(primitiveType_, indexStart_, indexCount_, vertexStart_, vertexCount_);
  166. }
  167. else if (vertexCount_ > 0)
  168. {
  169. graphics->SetVertexBuffers(vertexBuffers_, elementMasks_);
  170. graphics->Draw(primitiveType_, vertexStart_, vertexCount_);
  171. }
  172. }
  173. VertexBuffer* Geometry::GetVertexBuffer(unsigned index) const
  174. {
  175. return index < vertexBuffers_.Size() ? vertexBuffers_[index] : (VertexBuffer*)0;
  176. }
  177. unsigned Geometry::GetVertexElementMask(unsigned index) const
  178. {
  179. return index < elementMasks_.Size() ? elementMasks_[index] : 0;
  180. }
  181. unsigned short Geometry::GetBufferHash() const
  182. {
  183. unsigned short hash = 0;
  184. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  185. {
  186. VertexBuffer* vBuf = vertexBuffers_[i];
  187. hash += *((unsigned short*)&vBuf);
  188. }
  189. IndexBuffer* iBuf = indexBuffer_;
  190. hash += *((unsigned short*)&iBuf);
  191. return hash;
  192. }
  193. void Geometry::GetRawData(const unsigned char*& vertexData, unsigned& vertexSize, const unsigned char*& indexData, unsigned& indexSize, unsigned& elementMask)
  194. {
  195. if (rawVertexData_)
  196. {
  197. vertexData = rawVertexData_.Get();
  198. vertexSize = rawVertexSize_;
  199. elementMask = rawElementMask_;
  200. }
  201. else
  202. {
  203. if (positionBufferIndex_ < vertexBuffers_.Size() && vertexBuffers_[positionBufferIndex_])
  204. {
  205. vertexData = vertexBuffers_[positionBufferIndex_]->GetShadowData();
  206. if (vertexData)
  207. {
  208. vertexSize = vertexBuffers_[positionBufferIndex_]->GetVertexSize();
  209. elementMask = vertexBuffers_[positionBufferIndex_]->GetElementMask();
  210. }
  211. else
  212. {
  213. vertexSize = 0;
  214. elementMask = 0;
  215. }
  216. }
  217. else
  218. {
  219. vertexData = 0;
  220. vertexSize = 0;
  221. elementMask = 0;
  222. }
  223. }
  224. if (rawIndexData_)
  225. {
  226. indexData = rawIndexData_.Get();
  227. indexSize = rawIndexSize_;
  228. }
  229. else
  230. {
  231. if (indexBuffer_)
  232. {
  233. indexData = indexBuffer_->GetShadowData();
  234. if (indexData)
  235. indexSize = indexBuffer_->GetIndexSize();
  236. else
  237. indexSize = 0;
  238. }
  239. else
  240. {
  241. indexData = 0;
  242. indexSize = 0;
  243. }
  244. }
  245. }
  246. float Geometry::GetHitDistance(const Ray& ray)
  247. {
  248. const unsigned char* vertexData;
  249. const unsigned char* indexData;
  250. unsigned vertexSize;
  251. unsigned indexSize;
  252. unsigned elementMask;
  253. GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  254. if (vertexData && indexData)
  255. return ray.HitDistance(vertexData, vertexSize, indexData, indexSize, indexStart_, indexCount_);
  256. else if (vertexData)
  257. return ray.HitDistance(vertexData, vertexSize, vertexStart_, vertexCount_);
  258. else
  259. return M_INFINITY;
  260. }
  261. void Geometry::GetPositionBufferIndex()
  262. {
  263. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  264. {
  265. if (vertexBuffers_[i] && vertexBuffers_[i]->GetElementMask() & MASK_POSITION)
  266. {
  267. positionBufferIndex_ = i;
  268. return;
  269. }
  270. }
  271. // No vertex buffer with positions
  272. positionBufferIndex_ = M_MAX_UNSIGNED;
  273. }
  274. }