bf9c0f7b682d7b652153a1f61292dd58f2422e3c
[blender.git] / source / gameengine / Rasterizer / RAS_MeshObject.h
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL 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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29 #ifndef __RAS_MESHOBJECT
30 #define __RAS_MESHOBJECT
31
32 #ifdef WIN32
33 // disable the STL warnings ("debug information length > 255")
34 #pragma warning (disable:4786)
35 #endif
36
37 #include <vector>
38 #include <set>
39 #include <list>
40
41 #include "RAS_Polygon.h"
42 #include "RAS_MaterialBucket.h"
43 #include "MT_Transform.h"
44
45 #include "GEN_HashedPtr.h"
46
47 struct Mesh;
48 class RAS_Deformer;
49
50 /* RAS_MeshObject is a mesh used for rendering. It stores polygons,
51  * but the actual vertices and index arrays are stored in material
52  * buckets, referenced by the list of RAS_MeshMaterials. */
53
54 class RAS_MeshObject
55 {
56 private:
57         unsigned int                            m_debugcolor;
58         //int                                                   m_lightlayer;
59
60         bool                                            m_bModified;
61         bool                                            m_bMeshModified;
62
63         STR_String                                      m_name;
64         static STR_String                       s_emptyname;
65
66         vector<class RAS_Polygon*>      m_Polygons;
67
68         /* polygon sorting */
69         struct polygonSlot;
70         struct backtofront;
71         struct fronttoback;
72
73 protected:
74         list<RAS_MeshMaterial>                  m_materials;
75         Mesh*                                                   m_mesh;
76         bool                                                    m_bDeformed;
77
78 public:
79         // for now, meshes need to be in a certain layer (to avoid sorting on lights in realtime)
80         RAS_MeshObject(Mesh* mesh, int lightlayer);
81         virtual ~RAS_MeshObject();
82
83
84         bool                            IsDeformed() { return m_bDeformed; }
85         
86         /* materials */
87         int                                     NumMaterials();
88         const STR_String&       GetMaterialName(unsigned int matid);
89         const STR_String&       GetTextureName(unsigned int matid);
90
91         RAS_MeshMaterial*       GetMeshMaterial(unsigned int matid);
92         RAS_MeshMaterial*       GetMeshMaterial(RAS_IPolyMaterial *mat);
93         int                                     GetMaterialId(RAS_IPolyMaterial *mat);
94
95         list<RAS_MeshMaterial>::iterator GetFirstMaterial();
96         list<RAS_MeshMaterial>::iterator GetLastMaterial();
97
98         //unsigned int          GetLightLayer();
99
100         /* name */
101         void                            SetName(const char *name);
102         STR_String&                     GetName();
103
104         /* modification state */
105         bool                            MeshModified();
106         void                            SetMeshModified(bool v){m_bMeshModified = v;}
107
108         /* original blender mesh */
109         Mesh*                           GetMesh() { return m_mesh; }
110
111         /* mesh construction */
112         
113         virtual RAS_Polygon*    AddPolygon(RAS_MaterialBucket *bucket, int numverts);
114         virtual void                    AddVertex(RAS_Polygon *poly, int i,
115                                                         const MT_Point3& xyz,
116                                                         const MT_Point2& uv,
117                                                         const MT_Point2& uv2,
118                                                         const MT_Vector4& tangent,
119                                                         const unsigned int rgbacolor,
120                                                         const MT_Vector3& normal,
121                                                         bool flat,
122                                                         int origindex);
123
124         void                                    SchedulePolygons(int drawingmode);
125
126         /* vertex and polygon acces */
127         int                                     NumVertices(RAS_IPolyMaterial* mat);
128         RAS_TexVert*            GetVertex(unsigned int matid, unsigned int index);
129         const float*            GetVertexLocation(unsigned int orig_index);
130
131         int                                     NumPolygons();
132         RAS_Polygon*            GetPolygon(int num) const;
133         
134         /* buckets */
135         virtual void            AddMeshUser(void *clientobj, SG_QList *head, RAS_Deformer* deformer);
136         virtual void            UpdateBuckets(
137                                                         void* clientobj,
138                                                         double* oglmatrix,
139                                                         bool useObjectColor,
140                                                         const MT_Vector4& rgbavec,
141                                                         bool visible,
142                                                         bool culled);
143
144         void                            RemoveFromBuckets(void *clientobj);
145         void                            EndConversion() {
146 #if 0
147                 m_sharedvertex_map.clear(); // SharedVertex
148                 vector<vector<SharedVertex> >   shared_null(0);
149                 shared_null.swap( m_sharedvertex_map ); /* really free the memory */
150 #endif
151         }
152
153         /* colors */
154         void                            DebugColor(unsigned int abgr);
155         void                            SetVertexColor(RAS_IPolyMaterial* mat,MT_Vector4 rgba);
156         
157         /* polygon sorting by Z for alpha */
158         void                            SortPolygons(RAS_MeshSlot& ms, const MT_Transform &transform);
159
160
161         bool                            HasColliderPolygon() {
162                 int numpolys= NumPolygons();
163                 for (int p=0; p<numpolys; p++)
164                         if (m_Polygons[p]->IsCollider())
165                                 return true;
166                 
167                 return false;
168         }
169
170         /* for construction to find shared vertices */
171         struct SharedVertex {
172                 RAS_DisplayArray *m_darray;
173                 int m_offset;
174         };
175
176         vector<vector<SharedVertex> >   m_sharedvertex_map;
177 };
178
179 #endif //__RAS_MESHOBJECT
180