svn merge ^/trunk/blender -r43124:43160
[blender.git] / source / gameengine / Rasterizer / RAS_MeshObject.h
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
28 /** \file RAS_MeshObject.h
29  *  \ingroup bgerast
30  */
31
32 #ifndef __RAS_MESHOBJECT
33 #define __RAS_MESHOBJECT
34
35 #if defined(WIN32) && !defined(FREE_WINDOWS)
36 // disable the STL warnings ("debug information length > 255")
37 #pragma warning (disable:4786)
38 #endif
39
40 #include <vector>
41 #include <set>
42 #include <list>
43
44 #include "RAS_Polygon.h"
45 #include "RAS_MaterialBucket.h"
46 #include "MT_Transform.h"
47
48 #include "CTR_HashedPtr.h"
49
50 struct Mesh;
51 class RAS_Deformer;
52
53 /* RAS_MeshObject is a mesh used for rendering. It stores polygons,
54  * but the actual vertices and index arrays are stored in material
55  * buckets, referenced by the list of RAS_MeshMaterials. */
56
57 class RAS_MeshObject
58 {
59 private:
60         unsigned int                            m_debugcolor;
61
62         bool                                            m_bModified;
63         bool                                            m_bMeshModified;
64
65         STR_String                                      m_name;
66         static STR_String                       s_emptyname;
67
68         vector<class RAS_Polygon*>      m_Polygons;
69
70         /* polygon sorting */
71         struct polygonSlot;
72         struct backtofront;
73         struct fronttoback;
74
75 protected:
76         vector<int>                                             m_cacheWeightIndex;
77         list<RAS_MeshMaterial>                  m_materials;
78         Mesh*                                                   m_mesh;
79
80 public:
81         // for now, meshes need to be in a certain layer (to avoid sorting on lights in realtime)
82         RAS_MeshObject(Mesh* mesh);
83         virtual ~RAS_MeshObject();
84
85
86         // for shape keys, 
87         void CheckWeightCache(struct Object* obj);
88         
89         /* materials */
90         int                                     NumMaterials();
91         const STR_String&       GetMaterialName(unsigned int matid);
92         const STR_String&       GetTextureName(unsigned int matid);
93
94         RAS_MeshMaterial*       GetMeshMaterial(unsigned int matid);
95         RAS_MeshMaterial*       GetMeshMaterial(RAS_IPolyMaterial *mat);
96         int                                     GetMaterialId(RAS_IPolyMaterial *mat);
97
98         list<RAS_MeshMaterial>::iterator GetFirstMaterial();
99         list<RAS_MeshMaterial>::iterator GetLastMaterial();
100
101         //unsigned int          GetLightLayer();
102
103         /* name */
104         void                            SetName(const char *name);
105         STR_String&                     GetName();
106
107         /* modification state */
108         bool                            MeshModified();
109         void                            SetMeshModified(bool v){m_bMeshModified = v;}
110
111         /* original blender mesh */
112         Mesh*                           GetMesh() { return m_mesh; }
113
114         /* mesh construction */
115         
116         virtual RAS_Polygon*    AddPolygon(RAS_MaterialBucket *bucket, int numverts);
117         virtual void                    AddVertex(RAS_Polygon *poly, int i,
118                                                         const MT_Point3& xyz,
119                                                         const MT_Point2& uv,
120                                                         const MT_Point2& uv2,
121                                                         const MT_Vector4& tangent,
122                                                         const unsigned int rgbacolor,
123                                                         const MT_Vector3& normal,
124                                                         bool flat,
125                                                         int origindex);
126
127         void                                    SchedulePolygons(int drawingmode);
128
129         /* vertex and polygon acces */
130         int                                     NumVertices(RAS_IPolyMaterial* mat);
131         RAS_TexVert*            GetVertex(unsigned int matid, unsigned int index);
132         const float*            GetVertexLocation(unsigned int orig_index);
133
134         int                                     NumPolygons();
135         RAS_Polygon*            GetPolygon(int num) const;
136         
137         /* buckets */
138         virtual void            AddMeshUser(void *clientobj, SG_QList *head, RAS_Deformer* deformer);
139
140         void                            RemoveFromBuckets(void *clientobj);
141         void                            EndConversion() {
142 #if 0
143                 m_sharedvertex_map.clear(); // SharedVertex
144                 vector<vector<SharedVertex> >   shared_null(0);
145                 shared_null.swap( m_sharedvertex_map ); /* really free the memory */
146 #endif
147         }
148
149         /* colors */
150         void                            DebugColor(unsigned int abgr);
151         void                            SetVertexColor(RAS_IPolyMaterial* mat,MT_Vector4 rgba);
152         
153         /* polygon sorting by Z for alpha */
154         void                            SortPolygons(RAS_MeshSlot& ms, const MT_Transform &transform);
155
156
157         bool                            HasColliderPolygon() {
158                 int numpolys= NumPolygons();
159                 for (int p=0; p<numpolys; p++)
160                         if (m_Polygons[p]->IsCollider())
161                                 return true;
162                 
163                 return false;
164         }
165
166         /* for construction to find shared vertices */
167         struct SharedVertex {
168                 RAS_DisplayArray *m_darray;
169                 int m_offset;
170         };
171
172         vector<vector<SharedVertex> >   m_sharedvertex_map;
173         
174         
175 #ifdef WITH_CXX_GUARDEDALLOC
176 public:
177         void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:RAS_MeshObject"); }
178         void operator delete( void *mem ) { MEM_freeN(mem); }
179 #endif
180 };
181
182 #endif //__RAS_MESHOBJECT
183