RAS_MaterialBucket.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  19. * All rights reserved.
  20. *
  21. * The Original Code is: all of this file.
  22. *
  23. * Contributor(s): none yet.
  24. *
  25. * ***** END GPL LICENSE BLOCK *****
  26. */
  27. /** \file RAS_MaterialBucket.h
  28. * \ingroup bgerast
  29. */
  30. #ifndef __RAS_MATERIALBUCKET_H__
  31. #define __RAS_MATERIALBUCKET_H__
  32. #include "RAS_TexVert.h"
  33. #include "CTR_Map.h"
  34. #include "SG_QList.h"
  35. #include "MT_Transform.h"
  36. #include "MT_Matrix4x4.h"
  37. #include <vector>
  38. #include <set>
  39. #include <list>
  40. class RAS_MaterialBucket;
  41. struct DerivedMesh;
  42. class CTR_HashedPtr;
  43. class RAS_Deformer;
  44. class RAS_IPolyMaterial;
  45. class RAS_IRasterizer;
  46. class RAS_MeshObject;
  47. using namespace std;
  48. /* Display List Slot */
  49. class KX_ListSlot
  50. {
  51. protected:
  52. int m_refcount;
  53. public:
  54. KX_ListSlot() { m_refcount = 1; }
  55. virtual ~KX_ListSlot() {}
  56. virtual int Release() {
  57. if (--m_refcount > 0)
  58. return m_refcount;
  59. delete this;
  60. return 0;
  61. }
  62. virtual KX_ListSlot* AddRef() {
  63. m_refcount++;
  64. return this;
  65. }
  66. virtual void SetModified(bool mod)=0;
  67. };
  68. /* An array with data used for OpenGL drawing */
  69. class RAS_DisplayArray
  70. {
  71. public:
  72. /** The offset relation to the previous RAS_DisplayArray.
  73. * For the user vertex are one big list but in C++ source
  74. * it's two different lists if we use quads and triangles.
  75. * So to fix that we add an offset.
  76. * This value is set in UpdateDisplayArraysOffset().
  77. */
  78. unsigned int m_offset;
  79. vector<RAS_TexVert> m_vertex;
  80. vector<unsigned short> m_index;
  81. /* LINE currently isn't used */
  82. enum { LINE = 2, TRIANGLE = 3, QUAD = 4 } m_type;
  83. //RAS_MeshSlot *m_origSlot;
  84. /* Number of RAS_MeshSlot using this array */
  85. int m_users;
  86. enum { BUCKET_MAX_INDEX = 65535 };
  87. enum { BUCKET_MAX_VERTEX = 65535 };
  88. };
  89. /* Entry of a RAS_MeshObject into RAS_MaterialBucket */
  90. typedef std::vector<RAS_DisplayArray*> RAS_DisplayArrayList;
  91. // The QList is used to link the mesh slots to the object
  92. // The DList is used to link the visible mesh slots to the material bucket
  93. class RAS_MeshSlot : public SG_QList
  94. {
  95. friend class RAS_ListRasterizer;
  96. private:
  97. // indices into display arrays
  98. int m_startarray;
  99. int m_endarray;
  100. int m_startindex;
  101. int m_endindex;
  102. int m_startvertex;
  103. int m_endvertex;
  104. RAS_DisplayArrayList m_displayArrays;
  105. // for construction only
  106. RAS_DisplayArray* m_currentArray;
  107. public:
  108. // for rendering
  109. RAS_MaterialBucket* m_bucket;
  110. RAS_MeshObject* m_mesh;
  111. void* m_clientObj;
  112. RAS_Deformer* m_pDeformer;
  113. DerivedMesh* m_pDerivedMesh;
  114. float* m_OpenGLMatrix;
  115. // visibility
  116. bool m_bVisible;
  117. bool m_bCulled;
  118. // object color
  119. bool m_bObjectColor;
  120. MT_Vector4 m_RGBAcolor;
  121. // display lists
  122. KX_ListSlot* m_DisplayList;
  123. bool m_bDisplayList;
  124. // joined mesh slots
  125. RAS_MeshSlot* m_joinSlot;
  126. MT_Matrix4x4 m_joinInvTransform;
  127. list<RAS_MeshSlot*> m_joinedSlots;
  128. RAS_MeshSlot();
  129. RAS_MeshSlot(const RAS_MeshSlot& slot);
  130. virtual ~RAS_MeshSlot();
  131. void init(RAS_MaterialBucket *bucket, int numverts);
  132. struct iterator {
  133. RAS_DisplayArray *array;
  134. RAS_TexVert *vertex;
  135. unsigned short *index;
  136. size_t startvertex;
  137. size_t endvertex;
  138. size_t totindex;
  139. size_t arraynum;
  140. };
  141. void begin(iterator& it);
  142. void next(iterator& it);
  143. bool end(iterator& it);
  144. /* used during construction */
  145. void SetDisplayArray(int numverts);
  146. RAS_DisplayArray *CurrentDisplayArray();
  147. void SetDeformer(RAS_Deformer* deformer);
  148. void AddPolygon(int numverts);
  149. int AddVertex(const RAS_TexVert& tv);
  150. void AddPolygonVertex(int offset);
  151. /// Update offset of each display array
  152. void UpdateDisplayArraysOffset();
  153. /* optimization */
  154. bool Split(bool force=false);
  155. bool Join(RAS_MeshSlot *target, MT_Scalar distance);
  156. bool Equals(RAS_MeshSlot *target);
  157. #ifdef USE_SPLIT
  158. bool IsCulled();
  159. #else
  160. bool IsCulled() { return m_bCulled; }
  161. #endif
  162. void SetCulled(bool culled) { m_bCulled = culled; }
  163. #ifdef WITH_CXX_GUARDEDALLOC
  164. MEM_CXX_CLASS_ALLOC_FUNCS("GE:RAS_MeshSlot")
  165. #endif
  166. };
  167. /* Used by RAS_MeshObject, to point to it's slots in a bucket */
  168. class RAS_MeshMaterial
  169. {
  170. public:
  171. RAS_MeshSlot *m_baseslot;
  172. class RAS_MaterialBucket *m_bucket;
  173. /* the KX_GameObject is used as a key here */
  174. CTR_Map<CTR_HashedPtr,RAS_MeshSlot*> m_slots;
  175. #ifdef WITH_CXX_GUARDEDALLOC
  176. MEM_CXX_CLASS_ALLOC_FUNCS("GE:RAS_MeshMaterial")
  177. #endif
  178. };
  179. /* Contains a list of display arrays with the same material,
  180. * and a mesh slot for each mesh that uses display arrays in
  181. * this bucket */
  182. class RAS_MaterialBucket
  183. {
  184. public:
  185. RAS_MaterialBucket(RAS_IPolyMaterial* mat);
  186. virtual ~RAS_MaterialBucket();
  187. /* Bucket Sorting */
  188. struct less;
  189. typedef set<RAS_MaterialBucket*, less> Set;
  190. /* Material Properties */
  191. RAS_IPolyMaterial* GetPolyMaterial() const;
  192. bool IsAlpha() const;
  193. bool IsZSort() const;
  194. /* Rendering */
  195. bool ActivateMaterial(const MT_Transform& cameratrans, RAS_IRasterizer* rasty);
  196. void RenderMeshSlot(const MT_Transform& cameratrans, RAS_IRasterizer* rasty, RAS_MeshSlot &ms);
  197. /* Mesh Slot Access */
  198. list<RAS_MeshSlot>::iterator msBegin();
  199. list<RAS_MeshSlot>::iterator msEnd();
  200. class RAS_MeshSlot* AddMesh(int numverts);
  201. class RAS_MeshSlot* CopyMesh(class RAS_MeshSlot *ms);
  202. void RemoveMesh(class RAS_MeshSlot* ms);
  203. void Optimize(MT_Scalar distance);
  204. void ActivateMesh(RAS_MeshSlot* slot)
  205. {
  206. m_activeMeshSlotsHead.AddBack(slot);
  207. }
  208. SG_DList& GetActiveMeshSlots()
  209. {
  210. return m_activeMeshSlotsHead;
  211. }
  212. RAS_MeshSlot* GetNextActiveMeshSlot()
  213. {
  214. return (RAS_MeshSlot*)m_activeMeshSlotsHead.Remove();
  215. }
  216. private:
  217. list<RAS_MeshSlot> m_meshSlots; // all the mesh slots
  218. RAS_IPolyMaterial* m_material;
  219. SG_DList m_activeMeshSlotsHead; // only those which must be rendered
  220. #ifdef WITH_CXX_GUARDEDALLOC
  221. MEM_CXX_CLASS_ALLOC_FUNCS("GE:RAS_MaterialBucket")
  222. #endif
  223. };
  224. #endif /* __RAS_MATERIAL_BUCKET */