5a1b52489b436e7865add869fb77a51473e6ffd7
[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
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 mode,
48                                 int transp,
49                                 bool alpha,
50                                 bool zsort)
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_drawingmode = mode;
59         m_transp = transp;
60         m_alpha = alpha;
61         m_zsort = zsort;
62         m_polymatid = m_newpolymatid++;
63         m_flag = 0;
64         m_multimode = 0;
65         m_shininess = 35.0;
66         m_specular.setValue(0.5,0.5,0.5);
67         m_specularity = 1.0;
68         m_diffuse.setValue(0.5,0.5,0.5);
69 }
70
71 RAS_IPolyMaterial::RAS_IPolyMaterial() 
72                 : m_texturename("__Dummy_Texture_Name__"),
73                 m_materialname("__Dummy_Material_Name__"),
74                 m_tile(0),
75                 m_tilexrep(0),
76                 m_tileyrep(0),
77                 m_drawingmode (0),
78                 m_transp(0),
79                 m_alpha(false),
80                 m_zsort(false),
81                 m_materialindex(0),
82                 m_polymatid(0),
83                 m_flag(0),
84                 m_multimode(0)
85 {
86         m_shininess = 35.0;
87         m_specular = MT_Vector3(0.5,0.5,0.5);
88         m_specularity = 1.0;
89         m_diffuse = MT_Vector3(0.5,0.5,0.5);
90 }
91
92 RAS_IPolyMaterial::RAS_IPolyMaterial(const STR_String& texname,
93                                                                          const STR_String& matname,
94                                                                          int materialindex,
95                                                                          int tile,
96                                                                          int tilexrep,
97                                                                          int tileyrep,
98                                                                          int mode,
99                                                                          int transp,
100                                                                          bool alpha,
101                                                                          bool zsort)
102                 : m_texturename(texname),
103                 m_materialname(matname),
104                 m_tile(tile),
105                 m_tilexrep(tilexrep),
106                 m_tileyrep(tileyrep),
107                 m_drawingmode (mode),
108                 m_transp(transp),
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.0;
117         m_specular = MT_Vector3(0.5,0.5,0.5);
118         m_specularity = 1.0;
119         m_diffuse = MT_Vector3(0.5,0.5,0.5);
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_transp                          ==              lhs.m_transp &&
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_transp          ==              lhs.m_transp &&
145                                 this->m_alpha           ==              lhs.m_alpha &&
146                                 this->m_zsort           ==              lhs.m_zsort &&
147                                 this->m_drawingmode     ==              lhs.m_drawingmode &&
148                                 this->m_texturename.hash()      ==              lhs.m_texturename.hash() &&
149                                 this->m_materialname.hash() ==          lhs.m_materialname.hash()
150                 );
151         }
152 }
153
154
155 void RAS_IPolyMaterial::GetMaterialRGBAColor(unsigned char *rgba) const
156 {
157         *rgba++ = 0xFF;
158         *rgba++ = 0xFF;
159         *rgba++ = 0xFF;
160         *rgba++ = 0xFF;
161 }
162
163 bool RAS_IPolyMaterial::Less(const RAS_IPolyMaterial& rhs) const
164 {
165         if (Equals(rhs))
166                 return false;
167                 
168         return m_polymatid < rhs.m_polymatid;
169 }
170
171 bool RAS_IPolyMaterial::IsAlpha() const
172 {
173         return m_alpha || m_zsort;
174 }
175
176 bool RAS_IPolyMaterial::IsZSort() const
177 {
178         return m_zsort;
179 }
180
181 unsigned int RAS_IPolyMaterial::hash() const
182 {
183         return m_texturename.hash();
184 }
185
186 int RAS_IPolyMaterial::GetDrawingMode() const
187 {
188         return m_drawingmode;
189 }
190
191 const STR_String& RAS_IPolyMaterial::GetMaterialName() const
192
193         return m_materialname;
194 }
195
196 dword RAS_IPolyMaterial::GetMaterialNameHash() const
197 {
198         return m_materialname.hash();
199 }
200
201 const STR_String& RAS_IPolyMaterial::GetTextureName() const
202 {
203         return m_texturename;
204 }
205
206 int RAS_IPolyMaterial::GetMaterialIndex() const
207 {
208         return m_materialindex;
209 }
210
211 Material *RAS_IPolyMaterial::GetBlenderMaterial() const
212 {
213         return NULL;
214 }
215
216 Image *RAS_IPolyMaterial::GetBlenderImage() const
217 {
218         return NULL;
219 }
220
221 Scene* RAS_IPolyMaterial::GetBlenderScene() const
222 {
223         return NULL;
224 }
225
226 void RAS_IPolyMaterial::ReleaseMaterial()
227 {
228 }
229
230 unsigned int    RAS_IPolyMaterial::GetFlag() const
231 {
232         return m_flag;
233 }
234
235 bool RAS_IPolyMaterial::UsesLighting(RAS_IRasterizer *rasty) const
236 {
237         bool dolights = false;
238
239         if(m_flag & RAS_BLENDERMAT)
240                 dolights = (m_flag &RAS_MULTILIGHT)!=0;
241         else if(rasty->GetDrawingMode() < RAS_IRasterizer::KX_SOLID);
242         else if(rasty->GetDrawingMode() == RAS_IRasterizer::KX_SHADOW);
243         else
244                 dolights = (m_drawingmode & RAS_IRasterizer::KX_LIGHT)!=0;
245         
246         return dolights;
247 }
248
249 bool RAS_IPolyMaterial::CastsShadows() const
250 {
251         return (m_flag & RAS_CASTSHADOW) != 0;
252 }
253
254 bool RAS_IPolyMaterial::UsesObjectColor() const
255 {
256         return !(m_flag & RAS_BLENDERGLSL);
257 }
258
259 unsigned int RAS_IPolyMaterial::m_newpolymatid = 0;
260