TexFace to Material Settings big patch
[blender.git] / source / gameengine / Rasterizer / RAS_IPolygonMaterial.cpp
1 /*
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file gameengine/Rasterizer/RAS_IPolygonMaterial.cpp
30  *  \ingroup bgerast
31  */
32
33
34 #include "RAS_IPolygonMaterial.h"
35 #include "RAS_IRasterizer.h"
36
37 #include "DNA_image_types.h"
38 #include "DNA_meshdata_types.h"
39 #include "DNA_material_types.h"
40
41 void  RAS_IPolyMaterial::Initialize( 
42                                 const STR_String& texname,
43                                 const STR_String& matname,
44                                 int materialindex,
45                                 int tile,
46                                 int tilexrep,
47                                 int tileyrep,
48                                 int alphablend,
49                                 bool alpha,
50                                 bool zsort,
51                                 bool light,
52                                 bool image,
53                                 struct GameSettings* game)
54 {
55         m_texturename = texname;
56         m_materialname = matname;
57         m_materialindex = materialindex;
58         m_tile = tile;
59         m_tilexrep = tilexrep;
60         m_tileyrep = tileyrep;
61         m_alphablend = alphablend;
62         m_alpha = alpha;
63         m_zsort = zsort;
64         m_light = light;
65         m_polymatid = m_newpolymatid++;
66         m_flag = 0;
67         m_multimode = 0;
68         m_shininess = 35.0;
69         m_specular.setValue(0.5,0.5,0.5);
70         m_specularity = 1.0;
71         m_diffuse.setValue(0.5,0.5,0.5);
72         m_drawingmode = ConvertFaceMode(game, image);
73 }
74
75 RAS_IPolyMaterial::RAS_IPolyMaterial() 
76                 : m_texturename("__Dummy_Texture_Name__"),
77                 m_materialname("__Dummy_Material_Name__"),
78                 m_tile(0),
79                 m_tilexrep(0),
80                 m_tileyrep(0),
81                 m_drawingmode (0),
82                 m_alphablend(0),
83                 m_alpha(false),
84                 m_zsort(false),
85                 m_light(false),
86                 m_materialindex(0),
87                 m_polymatid(0),
88                 m_flag(0),
89                 m_multimode(0)
90 {
91         m_shininess = 35.0;
92         m_specular = MT_Vector3(0.5,0.5,0.5);
93         m_specularity = 1.0;
94         m_diffuse = MT_Vector3(0.5,0.5,0.5);
95 }
96
97 RAS_IPolyMaterial::RAS_IPolyMaterial(const STR_String& texname,
98                                                                          const STR_String& matname,
99                                                                          int materialindex,
100                                                                          int tile,
101                                                                          int tilexrep,
102                                                                          int tileyrep,
103                                                                          int alphablend,
104                                                                          bool alpha,
105                                                                          bool zsort)
106                 : m_texturename(texname),
107                 m_materialname(matname),
108                 m_tile(tile),
109                 m_tilexrep(tilexrep),
110                 m_tileyrep(tileyrep),
111                 m_alphablend(alphablend),
112                 m_alpha(alpha),
113                 m_zsort(zsort),
114                 m_materialindex(materialindex),
115                 m_polymatid(m_newpolymatid++),
116                 m_flag(0),
117                 m_multimode(0)
118 {
119         m_shininess = 35.0;
120         m_specular = MT_Vector3(0.5,0.5,0.5);
121         m_specularity = 1.0;
122         m_diffuse = MT_Vector3(0.5,0.5,0.5);
123 }
124
125
126 bool RAS_IPolyMaterial::Equals(const RAS_IPolyMaterial& lhs) const
127 {
128         if(m_flag &RAS_BLENDERMAT)
129         {
130                 bool test = (
131                         this->m_multimode                       ==              lhs.m_multimode &&
132                         this->m_flag                            ==              lhs.m_flag              &&
133                         this->m_drawingmode                     ==              lhs.m_drawingmode &&
134                         this->m_alphablend                      ==              lhs.m_alphablend &&
135                         this->m_texturename.hash()              ==              lhs.m_texturename.hash() &&
136                         this->m_materialname.hash()             ==              lhs.m_materialname.hash()
137                 );
138
139                 return test;
140         }
141         else
142         {
143                 return (
144                                 this->m_tile            ==              lhs.m_tile &&
145                                 this->m_tilexrep        ==              lhs.m_tilexrep &&
146                                 this->m_tileyrep        ==              lhs.m_tileyrep &&
147                                 this->m_alphablend      ==              lhs.m_alphablend &&
148                                 this->m_alpha           ==              lhs.m_alpha &&
149                                 this->m_zsort           ==              lhs.m_zsort &&
150                                 this->m_light           ==              lhs.m_light &&
151                                 this->m_drawingmode     ==              lhs.m_drawingmode &&
152                                 this->m_texturename.hash()      ==              lhs.m_texturename.hash() &&
153                                 this->m_materialname.hash() ==          lhs.m_materialname.hash()
154                 );
155         }
156 }
157
158 int RAS_IPolyMaterial::ConvertFaceMode(struct GameSettings *game, bool image) const
159 {
160         if (!game) return (image?GEMAT_TEX:0);
161
162         int modefinal = 0;
163
164         int orimode   = game->face_orientation;
165         int alpha_blend = game->alpha_blend;
166         int flags = game->flag & (GEMAT_TEXT | GEMAT_BACKCULL);
167
168         modefinal = orimode | alpha_blend | flags;
169         modefinal |= (image ? GEMAT_TEX : 0);
170
171         return modefinal;
172 }
173
174 void RAS_IPolyMaterial::GetMaterialRGBAColor(unsigned char *rgba) const
175 {
176         *rgba++ = 0xFF;
177         *rgba++ = 0xFF;
178         *rgba++ = 0xFF;
179         *rgba++ = 0xFF;
180 }
181
182 bool RAS_IPolyMaterial::Less(const RAS_IPolyMaterial& rhs) const
183 {
184         if (Equals(rhs))
185                 return false;
186                 
187         return m_polymatid < rhs.m_polymatid;
188 }
189
190 bool RAS_IPolyMaterial::IsAlpha() const
191 {
192         return m_alpha || m_zsort;
193 }
194
195 bool RAS_IPolyMaterial::IsZSort() const
196 {
197         return m_zsort;
198 }
199
200 unsigned int RAS_IPolyMaterial::hash() const
201 {
202         return m_texturename.hash();
203 }
204
205 int RAS_IPolyMaterial::GetDrawingMode() const
206 {
207         return m_drawingmode;
208 }
209
210 const STR_String& RAS_IPolyMaterial::GetMaterialName() const
211
212         return m_materialname;
213 }
214
215 dword RAS_IPolyMaterial::GetMaterialNameHash() const
216 {
217         return m_materialname.hash();
218 }
219
220 const STR_String& RAS_IPolyMaterial::GetTextureName() const
221 {
222         return m_texturename;
223 }
224
225 int RAS_IPolyMaterial::GetMaterialIndex() const
226 {
227         return m_materialindex;
228 }
229
230 Material *RAS_IPolyMaterial::GetBlenderMaterial() const
231 {
232         return NULL;
233 }
234
235 Image *RAS_IPolyMaterial::GetBlenderImage() const
236 {
237         return NULL;
238 }
239
240 Scene* RAS_IPolyMaterial::GetBlenderScene() const
241 {
242         return NULL;
243 }
244
245 void RAS_IPolyMaterial::ReleaseMaterial()
246 {
247 }
248
249 unsigned int    RAS_IPolyMaterial::GetFlag() const
250 {
251         return m_flag;
252 }
253
254 bool RAS_IPolyMaterial::UsesLighting(RAS_IRasterizer *rasty) const
255 {
256         bool dolights = false;
257
258         if(m_flag & RAS_BLENDERMAT)
259                 dolights = (m_flag &RAS_MULTILIGHT)!=0;
260         else if(rasty->GetDrawingMode() < RAS_IRasterizer::KX_SOLID);
261         else if(rasty->GetDrawingMode() == RAS_IRasterizer::KX_SHADOW);
262         else
263                 dolights = m_light;
264         
265         return dolights;
266 }
267
268 bool RAS_IPolyMaterial::CastsShadows() const
269 {
270         return (m_flag & RAS_CASTSHADOW) != 0;
271 }
272
273 bool RAS_IPolyMaterial::UsesObjectColor() const
274 {
275         return !(m_flag & RAS_BLENDERGLSL);
276 }
277
278 unsigned int RAS_IPolyMaterial::m_newpolymatid = 0;
279