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