Improved OpenGL Shader Language support for game engine. The python interface is...
[blender.git] / source / gameengine / Rasterizer / RAS_MeshObject.h
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32 #ifndef __RAS_MESHOBJECT
33 #define __RAS_MESHOBJECT
34
35 #ifdef WIN32
36 // disable the STL warnings ("debug information length > 255")
37 #pragma warning (disable:4786)
38 #endif
39
40 #include <vector>
41 #include <set>
42
43 #include "RAS_Polygon.h"
44 #include "RAS_MaterialBucket.h"
45 #include "MT_Transform.h"
46
47 #include "GEN_HashedPtr.h"
48
49 /**
50  * This class holds an array of vertices and indicies.
51  */
52 class KX_ArrayOptimizer
53 {
54 public:
55         KX_ArrayOptimizer(int index) 
56                 : m_index1(index)
57         {};
58         virtual ~KX_ArrayOptimizer();
59         
60         vector<KX_VertexArray*>         m_VertexArrayCache1;
61         vector<int>                                     m_TriangleArrayCount;
62         vector<KX_IndexArray*>          m_IndexArrayCache1;
63
64         /**
65                 order in which they are stored into the mesh
66         */
67         int                                                     m_index1;
68 };
69
70 /**
71  * This struct holds a triangle.
72  */
73 struct  RAS_TriangleIndex
74 {
75 public:
76         int     m_index[3];
77         int     m_array;
78         RAS_IPolyMaterial*      m_matid;
79         bool    m_collider;
80 };
81
82 /**
83  * This class looks horribly broken.  Only m_matid is used, and
84  * m_matid is a (int) RAS_IPolyMaterial*.  
85  * --> m_matid == lhs.m_matid should be *m_matid == *lhs.m_matid
86  */
87 class   RAS_MatArrayIndex
88 {
89 public:
90
91         int     m_arrayindex1;
92         RAS_IPolyMaterial*      m_matid;
93         int     m_array;
94         int     m_index;
95
96 /*
97         inline bool     Less(const RAS_MatArrayIndex& lhs) const {
98                 bool result = 
99                         (       (m_matid < lhs.m_matid)         ||
100                                 ((m_matid == lhs.m_matid)&&(m_array < lhs.m_array)) ||
101                                 ((m_matid == lhs.m_matid) && (m_array == lhs.m_array) &&
102                                 (m_index < lhs.m_index))
103                                         
104                         );
105                 return result;
106                         
107         }
108 */
109         
110 };
111 /*
112 inline  bool operator <( const RAS_MatArrayIndex& rhs,const RAS_MatArrayIndex& lhs)
113 {
114         return ( rhs.Less(lhs));
115 }*/
116
117 /**
118  * RAS_MeshObject stores mesh data for the renderer.
119  */
120 class RAS_MeshObject
121 {
122         
123         //      GEN_Map<class RAS_IPolyMaterial,KX_ArrayOptimizer*> m_matVertexArrayS;
124         //vector<class RAS_IPolyMaterial*,KX_ArrayOptimizer> m_vertexArrays;
125         virtual KX_ArrayOptimizer* GetArrayOptimizer(RAS_IPolyMaterial* polymat);
126         //vector<RAS_Polygon*>          m_polygons;
127         
128         unsigned int                            m_debugcolor;
129         bool                                            m_bModified;
130         int                                                     m_lightlayer;
131         
132         vector<class RAS_Polygon*>      m_Polygons;
133         STR_String                                      m_name;
134         static STR_String                       s_emptyname;
135         bool                                    m_zsort;
136         
137         struct polygonSlot;
138         struct backtofront;
139         struct fronttoback;
140
141         
142 protected:
143         enum { BUCKET_MAX_INDICES = 65535 };//2048};//8192};
144         enum { BUCKET_MAX_TRIANGLES = 65535 };
145         
146         GEN_Map<class RAS_IPolyMaterial,KX_ArrayOptimizer*> m_matVertexArrayS;
147         
148         RAS_MaterialBucket::Set                 m_materials;
149 public:
150         // for now, meshes need to be in a certain layer (to avoid sorting on lights in realtime)
151         RAS_MeshObject(int lightlayer);
152         virtual ~RAS_MeshObject();
153
154         vector<RAS_IPolyMaterial*>                              m_sortedMaterials;
155         vector<vector<RAS_MatArrayIndex> >              m_xyz_index_to_vertex_index_mapping;
156         vector<RAS_TriangleIndex >                              m_triangle_indices;
157         
158         int                                                     m_class;
159
160         unsigned int                            GetLightLayer();
161         int                                     NumMaterials();
162         const STR_String&       GetMaterialName(unsigned int matid);
163         RAS_MaterialBucket*     GetMaterialBucket(unsigned int matid);
164         const STR_String&       GetTextureName(unsigned int matid);
165         virtual void            AddPolygon(RAS_Polygon* poly);
166         void                            UpdateMaterialList();
167         
168         int                                     NumPolygons();
169         RAS_Polygon*            GetPolygon(int num);
170         
171         virtual void            Bucketize(
172                                                         double* oglmatrix,
173                                                         void* clientobj,
174                                                         bool useObjectColor,
175                                                         const MT_Vector4& rgbavec
176                                                 );
177
178         void                            RemoveFromBuckets(
179                                                         double* oglmatrix,
180                                                         void* clientobj
181                                                 );
182
183         void                            MarkVisible(
184                                                         double* oglmatrix,
185                                                         void* clientobj,
186                                                         bool visible,
187                                                         bool useObjectColor,
188                                                         const MT_Vector4& rgbavec
189                                                 );
190
191         void                            DebugColor(unsigned int abgr);
192         
193         /**
194          *  Sorts the polygons by their transformed z values.
195          */
196         void                            SortPolygons(const MT_Transform &transform);
197
198         void                            SchedulePolygons(
199                                                         const MT_Transform &transform,
200                                                         int drawingmode
201                                                 );
202
203         void                            ClearArrayData();
204         
205         RAS_MaterialBucket::Set::iterator GetFirstMaterial();
206         RAS_MaterialBucket::Set::iterator GetLastMaterial();
207         
208         virtual RAS_TexVert*    GetVertex(
209                                                                 short array,
210                                                                 unsigned int index,
211                                                                 RAS_IPolyMaterial* polymat
212                                                         );
213         
214         virtual int                     FindVertexArray(
215                                                         int numverts,
216                                                         RAS_IPolyMaterial* polymat
217                                                 );
218         
219         void                            SchedulePoly(
220                                                         const KX_VertexIndex& idx,
221                                                         int numverts,
222                                                         RAS_IPolyMaterial* mat
223                                                 );
224
225         void                            ScheduleWireframePoly(
226                                                         const KX_VertexIndex& idx,
227                                                         int numverts,
228                                                         int edgecode,
229                                                         RAS_IPolyMaterial* mat
230                                                 );
231         
232         // find (and share) or add vertices
233         // for some speedup, only the last 20 added vertices are searched for equality
234         
235         virtual int                     FindOrAddVertex(
236                                                         int vtxarray,
237                                                         const MT_Point3& xyz,
238                                                         const MT_Point2& uv,
239                                                         const MT_Vector4& tangent,
240                                                         const unsigned int rgbacolor,
241                                                         const MT_Vector3& normal,
242                                                         RAS_IPolyMaterial* mat,
243                                                         int orgindex
244                                                 );
245         
246         const vecVertexArray&   GetVertexCache (RAS_IPolyMaterial* mat);
247         
248         int                                     GetVertexArrayLength(RAS_IPolyMaterial* mat);
249
250         RAS_TexVert*            GetVertex(
251                                                         unsigned int matid,
252                                                         unsigned int index
253                                                 );
254
255         const vecIndexArrays& GetIndexCache (RAS_IPolyMaterial* mat);
256         void                            SetName(STR_String name);
257         const STR_String&       GetName();
258 };
259
260 #endif //__RAS_MESHOBJECT
261