2
0

Geometry.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. //
  2. // Copyright (c) 2008-2014 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_;
  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_;
  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. void Geometry::GetRawDataShared(SharedArrayPtr<unsigned char>& vertexData, unsigned& vertexSize,
  247. SharedArrayPtr<unsigned char>& indexData, unsigned& indexSize, unsigned& elementMask) const
  248. {
  249. if (rawVertexData_)
  250. {
  251. vertexData = rawVertexData_;
  252. vertexSize = rawVertexSize_;
  253. elementMask = rawElementMask_;
  254. }
  255. else
  256. {
  257. if (positionBufferIndex_ < vertexBuffers_.Size() && vertexBuffers_[positionBufferIndex_])
  258. {
  259. vertexData = vertexBuffers_[positionBufferIndex_]->GetShadowDataShared();
  260. if (vertexData)
  261. {
  262. vertexSize = vertexBuffers_[positionBufferIndex_]->GetVertexSize();
  263. elementMask = vertexBuffers_[positionBufferIndex_]->GetElementMask();
  264. }
  265. else
  266. {
  267. vertexSize = 0;
  268. elementMask = 0;
  269. }
  270. }
  271. else
  272. {
  273. vertexData = 0;
  274. vertexSize = 0;
  275. elementMask = 0;
  276. }
  277. }
  278. if (rawIndexData_)
  279. {
  280. indexData = rawIndexData_;
  281. indexSize = rawIndexSize_;
  282. }
  283. else
  284. {
  285. if (indexBuffer_)
  286. {
  287. indexData = indexBuffer_->GetShadowDataShared();
  288. if (indexData)
  289. indexSize = indexBuffer_->GetIndexSize();
  290. else
  291. indexSize = 0;
  292. }
  293. else
  294. {
  295. indexData = 0;
  296. indexSize = 0;
  297. }
  298. }
  299. }
  300. float Geometry::GetHitDistance(const Ray& ray, Vector3* outNormal) const
  301. {
  302. const unsigned char* vertexData;
  303. const unsigned char* indexData;
  304. unsigned vertexSize;
  305. unsigned indexSize;
  306. unsigned elementMask;
  307. GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  308. if (vertexData && indexData)
  309. return ray.HitDistance(vertexData, vertexSize, indexData, indexSize, indexStart_, indexCount_, outNormal);
  310. else if (vertexData)
  311. return ray.HitDistance(vertexData, vertexSize, vertexStart_, vertexCount_, outNormal);
  312. else
  313. return M_INFINITY;
  314. }
  315. bool Geometry::IsInside(const Ray& ray) const
  316. {
  317. const unsigned char* vertexData;
  318. const unsigned char* indexData;
  319. unsigned vertexSize;
  320. unsigned indexSize;
  321. unsigned elementMask;
  322. GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  323. if (vertexData && indexData)
  324. return ray.InsideGeometry(vertexData, vertexSize, indexData, indexSize, indexStart_, indexCount_);
  325. else if (vertexData)
  326. return ray.InsideGeometry(vertexData, vertexSize, vertexStart_, vertexCount_);
  327. else
  328. return false;
  329. }
  330. void Geometry::GetPositionBufferIndex()
  331. {
  332. for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
  333. {
  334. if (vertexBuffers_[i] && vertexBuffers_[i]->GetElementMask() & MASK_POSITION)
  335. {
  336. positionBufferIndex_ = i;
  337. return;
  338. }
  339. }
  340. // No vertex buffer with positions
  341. positionBufferIndex_ = M_MAX_UNSIGNED;
  342. }
  343. }