Geometry.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. Geometry::Geometry(Context* context) :
  33. Object(context),
  34. primitiveType_(TRIANGLE_LIST),
  35. indexStart_(0),
  36. indexCount_(0),
  37. vertexStart_(0),
  38. vertexCount_(0),
  39. positionBufferIndex_(M_MAX_UNSIGNED),
  40. rawVertexSize_(0),
  41. rawElementMask_(0),
  42. rawIndexSize_(0),
  43. lodDistance_(0.0f)
  44. {
  45. SetNumVertexBuffers(1);
  46. }
  47. Geometry::~Geometry()
  48. {
  49. }
  50. bool Geometry::SetNumVertexBuffers(unsigned num)
  51. {
  52. if (num >= MAX_VERTEX_STREAMS)
  53. {
  54. LOGERROR("Too many vertex streams");
  55. return false;
  56. }
  57. unsigned oldSize = vertexBuffers_.Size();
  58. vertexBuffers_.Resize(num);
  59. elementMasks_.Resize(num);
  60. for (unsigned i = oldSize; i < num; ++i)
  61. elementMasks_[i] = MASK_NONE;
  62. GetPositionBufferIndex();
  63. return true;
  64. }
  65. bool Geometry::SetVertexBuffer(unsigned index, VertexBuffer* buffer, unsigned elementMask)
  66. {
  67. if (index >= vertexBuffers_.Size())
  68. {
  69. LOGERROR("Stream index out of bounds");
  70. return false;
  71. }
  72. vertexBuffers_[index] = buffer;
  73. if (buffer)
  74. {
  75. if (elementMask == MASK_DEFAULT)
  76. elementMasks_[index] = buffer->GetElementMask();
  77. else
  78. elementMasks_[index] = elementMask;
  79. }
  80. GetPositionBufferIndex();
  81. return true;
  82. }
  83. void Geometry::SetIndexBuffer(IndexBuffer* buffer)
  84. {
  85. indexBuffer_ = buffer;
  86. }
  87. bool Geometry::SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, bool getUsedVertexRange)
  88. {
  89. if (!indexBuffer_ && !rawIndexData_)
  90. {
  91. LOGERROR("Null index buffer and no raw index data, can not define indexed draw range");
  92. return false;
  93. }
  94. if (indexBuffer_ && indexStart + indexCount > indexBuffer_->GetIndexCount())
  95. {
  96. LOGERROR("Illegal draw range " + String(indexStart) + " to " + String(indexStart + indexCount - 1) + ", index buffer has " +
  97. String(indexBuffer_->GetIndexCount()) + " indices");
  98. return false;
  99. }
  100. primitiveType_ = type;
  101. indexStart_ = indexStart;
  102. indexCount_ = indexCount;
  103. // Get min.vertex index and num of vertices from index buffer. If it fails, use full range as fallback
  104. if (indexCount)
  105. {
  106. vertexStart_ = 0;
  107. vertexCount_ = vertexBuffers_[0] ? vertexBuffers_[0]->GetVertexCount() : 0;
  108. if (getUsedVertexRange && indexBuffer_)
  109. indexBuffer_->GetUsedVertexRange(indexStart_, indexCount_, vertexStart_, vertexCount_);
  110. }
  111. else
  112. {
  113. vertexStart_ = 0;
  114. vertexCount_ = 0;
  115. }
  116. return true;
  117. }
  118. bool Geometry::SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount)
  119. {
  120. if (indexBuffer_)
  121. {
  122. if (indexStart + indexCount > indexBuffer_->GetIndexCount())
  123. {
  124. LOGERROR("Illegal draw range " + String(indexStart) + " to " + String(indexStart + indexCount - 1) +
  125. ", index buffer has " + String(indexBuffer_->GetIndexCount()) + " indices");
  126. return false;
  127. }
  128. }
  129. else if (!rawIndexData_)
  130. {
  131. indexStart = 0;
  132. indexCount = 0;
  133. }
  134. primitiveType_ = type;
  135. indexStart_ = indexStart;
  136. indexCount_ = indexCount;
  137. vertexStart_ = minVertex;
  138. vertexCount_ = vertexCount;
  139. return true;
  140. }
  141. void Geometry::SetLodDistance(float distance)
  142. {
  143. if (distance < 0.0f)
  144. distance = 0.0f;
  145. lodDistance_ = distance;
  146. }
  147. void Geometry::SetRawVertexData(SharedArrayPtr<unsigned char> data, unsigned vertexSize, unsigned elementMask)
  148. {
  149. rawVertexData_ = data;
  150. rawVertexSize_ = vertexSize;
  151. rawElementMask_ = elementMask;
  152. }
  153. void Geometry::SetRawIndexData(SharedArrayPtr<unsigned char> data, unsigned indexSize)
  154. {
  155. rawIndexData_ = data;
  156. rawIndexSize_ = indexSize;
  157. }
  158. void Geometry::Draw(Graphics* graphics)
  159. {
  160. if (indexBuffer_ && indexCount_ > 0)
  161. {
  162. graphics->SetIndexBuffer(indexBuffer_);
  163. graphics->SetVertexBuffers(vertexBuffers_, elementMasks_);
  164. graphics->Draw(primitiveType_, indexStart_, indexCount_, vertexStart_, vertexCount_);
  165. }
  166. else if (vertexCount_ > 0)
  167. {
  168. graphics->SetVertexBuffers(vertexBuffers_, elementMasks_);
  169. graphics->Draw(primitiveType_, vertexStart_, vertexCount_);
  170. }
  171. }
  172. VertexBuffer* Geometry::GetVertexBuffer(unsigned index) const
  173. {
  174. return index < vertexBuffers_.Size() ? vertexBuffers_[index] : (VertexBuffer*)0;
  175. }
  176. unsigned Geometry::GetVertexElementMask(unsigned index) const
  177. {
  178. return index < elementMasks_.Size() ? elementMasks_[index] : 0;
  179. }
  180. unsigned short Geometry::GetBufferHash() const
  181. {
  182. unsigned short hash = 0;
  183. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  184. {
  185. VertexBuffer* vBuf = vertexBuffers_[i];
  186. hash += *((unsigned short*)&vBuf);
  187. }
  188. IndexBuffer* iBuf = indexBuffer_;
  189. hash += *((unsigned short*)&iBuf);
  190. return hash;
  191. }
  192. void Geometry::GetRawData(const unsigned char*& vertexData, unsigned& vertexSize, const unsigned char*& indexData,
  193. unsigned& indexSize, unsigned& elementMask) const
  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) const
  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. bool Geometry::IsInside(const Ray& ray) const
  262. {
  263. const unsigned char* vertexData;
  264. const unsigned char* indexData;
  265. unsigned vertexSize;
  266. unsigned indexSize;
  267. unsigned elementMask;
  268. GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  269. if (vertexData && indexData)
  270. return ray.InsideGeometry(vertexData, vertexSize, indexData, indexSize, indexStart_, indexCount_);
  271. else if (vertexData)
  272. return ray.InsideGeometry(vertexData, vertexSize, vertexStart_, vertexCount_);
  273. else
  274. return false;
  275. }
  276. void Geometry::GetPositionBufferIndex()
  277. {
  278. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  279. {
  280. if (vertexBuffers_[i] && vertexBuffers_[i]->GetElementMask() & MASK_POSITION)
  281. {
  282. positionBufferIndex_ = i;
  283. return;
  284. }
  285. }
  286. // No vertex buffer with positions
  287. positionBufferIndex_ = M_MAX_UNSIGNED;
  288. }
  289. }