svn merge ^/trunk/blender -r43124:43160
[blender.git] / source / gameengine / Rasterizer / RAS_IPolygonMaterial.cpp
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 gameengine/Rasterizer/RAS_IPolygonMaterial.cpp
29  *  \ingroup bgerast
30  */
31
32
33 #include "RAS_IPolygonMaterial.h"
34 #include "RAS_IRasterizer.h"
35
36 #include "DNA_image_types.h"
37 #include "DNA_meshdata_types.h"
38 #include "DNA_material_types.h"
39
40 void  RAS_IPolyMaterial::Initialize( 
41                                 const STR_String& texname,
42                                 const STR_String& matname,
43                                 int materialindex,
44                                 int tile,
45                                 int tilexrep,
46                                 int tileyrep,
47                                 int alphablend,
48                                 bool alpha,
49                                 bool zsort,
50                                 bool light,
51                                 bool image,
52                                 struct GameSettings* game)
53 {
54         m_texturename = texname;
55         m_materialname = matname;
56         m_materialindex = materialindex;
57         m_tile = tile;
58         m_tilexrep = tilexrep;
59         m_tileyrep = tileyrep;
60         m_alphablend = alphablend;
61         m_alpha = alpha;
62         m_zsort = zsort;
63         m_light = light;
64         m_polymatid = m_newpolymatid++;
65         m_flag = 0;
66         m_multimode = 0;
67         m_shininess = 35.0;
68         m_specular.setValue(0.5,0.5,0.5);
69         m_specularity = 1.0;
70         m_diffuse.setValue(0.5,0.5,0.5);
71         m_drawingmode = ConvertFaceMode(game, image);
72 }
73
74 RAS_IPolyMaterial::RAS_IPolyMaterial() 
75                 : m_texturename("__Dummy_Texture_Name__"),
76                 m_materialname("__Dummy_Material_Name__"),
77                 m_tile(0),
78                 m_tilexrep(0),
79                 m_tileyrep(0),
80                 m_drawingmode (0),
81                 m_alphablend(0),
82                 m_alpha(false),
83                 m_zsort(false),
84                 m_light(false),
85                 m_materialindex(0),
86                 m_polymatid(0),
87                 m_flag(0),
88                 m_multimode(0)
89 {
90         m_shininess = 35.0;
91         m_specular = MT_Vector3(0.5,0.5,0.5);
92         m_specularity = 1.0;
93         m_diffuse = MT_Vector3(0.5,0.5,0.5);
94 }
95
96 RAS_IPolyMaterial::RAS_IPolyMaterial(const STR_String& texname,
97                                                                          const STR_String& matname,
98                                                                          int materialindex,
99                                                                          int tile,
100                                                                          int tilexrep,
101                                                                          int tileyrep,
102                                                                          int alphablend,
103                                                                          bool alpha,
104                                                                          bool zsort)
105                 : m_texturename(texname),
106                 m_materialname(matname),
107                 m_tile(tile),
108                 m_tilexrep(tilexrep),
109                 m_tileyrep(tileyrep),
110                 m_alphablend(alphablend),
111                 m_alpha(alpha),
112                 m_zsort(zsort),
113                 m_materialindex(materialindex),
114                 m_polymatid(m_newpolymatid++),
115                 m_flag(0),
116                 m_multimode(0)
117 {
118         m_shininess = 35.0;
119         m_specular = MT_Vector3(0.5,0.5,0.5);
120         m_specularity = 1.0;
121         m_diffuse = MT_Vector3(0.5,0.5,0.5);
122 }
123
124
125 bool RAS_IPolyMaterial::Equals(const RAS_IPolyMaterial& lhs) const
126 {
127         if(m_flag &RAS_BLENDERMAT)
128         {
129                 bool test = (
130                         this->m_multimode                       ==              lhs.m_multimode &&
131                         this->m_flag                            ==              lhs.m_flag              &&
132                         this->m_drawingmode                     ==              lhs.m_drawingmode &&
133                         this->m_alphablend                      ==              lhs.m_alphablend &&
134                         this->m_texturename.hash()              ==              lhs.m_texturename.hash() &&
135                         this->m_materialname.hash()             ==              lhs.m_materialname.hash()
136                 );
137
138                 return test;
139         }
140         else
141         {
142                 return (
143                                 this->m_tile            ==              lhs.m_tile &&
144                                 this->m_tilexrep        ==              lhs.m_tilexrep &&
145                                 this->m_tileyrep        ==              lhs.m_tileyrep &&
146                                 this->m_alphablend      ==              lhs.m_alphablend &&
147                                 this->m_alpha           ==              lhs.m_alpha &&
148                                 this->m_zsort           ==              lhs.m_zsort &&
149                                 this->m_light           ==              lhs.m_light &&
150                                 this->m_drawingmode     ==              lhs.m_drawingmode &&
151                                 this->m_texturename.hash()      ==              lhs.m_texturename.hash() &&
152                                 this->m_materialname.hash() ==          lhs.m_materialname.hash()
153                 );
154         }
155 }
156
157 int RAS_IPolyMaterial::ConvertFaceMode(struct GameSettings *game, bool image) const
158 {
159         if (!game) return (image?GEMAT_TEX:0);
160
161         int modefinal = 0;
162
163         int orimode   = game->face_orientation;
164         int alpha_blend = game->alpha_blend;
165         int flags = game->flag & (GEMAT_TEXT | GEMAT_BACKCULL);
166
167         modefinal = orimode | alpha_blend | flags;
168         modefinal |= (image ? GEMAT_TEX : 0);
169
170         return modefinal;
171 }
172
173 void RAS_IPolyMaterial::GetMaterialRGBAColor(unsigned char *rgba) const
174 {
175         *rgba++ = 0xFF;
176         *rgba++ = 0xFF;
177         *rgba++ = 0xFF;
178         *rgba++ = 0xFF;
179 }
180
181 bool RAS_IPolyMaterial::Less(const RAS_IPolyMaterial& rhs) const
182 {
183         if (Equals(rhs))
184                 return false;
185                 
186         return m_polymatid < rhs.m_polymatid;
187 }
188
189 bool RAS_IPolyMaterial::IsAlpha() const
190 {
191         return m_alpha || m_zsort;
192 }
193
194 bool RAS_IPolyMaterial::IsZSort() const
195 {
196         return m_zsort;
197 }
198
199 unsigned int RAS_IPolyMaterial::hash() const
200 {
201         return m_texturename.hash();
202 }
203
204 int RAS_IPolyMaterial::GetDrawingMode() const
205 {
206         return m_drawingmode;
207 }
208
209 const STR_String& RAS_IPolyMaterial::GetMaterialName() const
210
211         return m_materialname;
212 }
213
214 dword RAS_IPolyMaterial::GetMaterialNameHash() const
215 {
216         return m_materialname.hash();
217 }
218
219 const STR_String& RAS_IPolyMaterial::GetTextureName() const
220 {
221         return m_texturename;
222 }
223
224 int RAS_IPolyMaterial::GetMaterialIndex() const
225 {
226         return m_materialindex;
227 }
228
229 Material *RAS_IPolyMaterial::GetBlenderMaterial() const
230 {
231         return NULL;
232 }
233
234 Image *RAS_IPolyMaterial::GetBlenderImage() const
235 {
236         return NULL;
237 }
238
239 Scene* RAS_IPolyMaterial::GetBlenderScene() const
240 {
241         return NULL;
242 }
243
244 void RAS_IPolyMaterial::ReleaseMaterial()
245 {
246 }
247
248 unsigned int    RAS_IPolyMaterial::GetFlag() const
249 {
250         return m_flag;
251 }
252
253 bool RAS_IPolyMaterial::UsesLighting(RAS_IRasterizer *rasty) const
254 {
255         bool dolights = false;
256
257         if(m_flag & RAS_BLENDERMAT)
258                 dolights = (m_flag &RAS_MULTILIGHT)!=0;
259         else if(rasty->GetDrawingMode() < RAS_IRasterizer::KX_SOLID);
260         else if(rasty->GetDrawingMode() == RAS_IRasterizer::KX_SHADOW);
261         else
262                 dolights = m_light;
263         
264         return dolights;
265 }
266
267 bool RAS_IPolyMaterial::CastsShadows() const
268 {
269         return (m_flag & RAS_CASTSHADOW) != 0;
270 }
271
272 bool RAS_IPolyMaterial::UsesObjectColor() const
273 {
274         return !(m_flag & RAS_BLENDERGLSL);
275 }
276
277 unsigned int RAS_IPolyMaterial::m_newpolymatid = 0;
278