Merge with -r 22620:23107.
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include "RAS_IPolygonMaterial.h"
30 #include "RAS_IRasterizer.h"
31
32 #include "DNA_image_types.h"
33 #include "DNA_meshdata_types.h"
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 void  RAS_IPolyMaterial::Initialize( 
40                                 const STR_String& texname,
41                                 const STR_String& matname,
42                                 int materialindex,
43                                 int tile,
44                                 int tilexrep,
45                                 int tileyrep,
46                                 int mode,
47                                 int transp,
48                                 bool alpha,
49                                 bool zsort)
50 {
51         m_texturename = texname;
52         m_materialname = matname;
53         m_materialindex = materialindex;
54         m_tile = tile;
55         m_tilexrep = tilexrep;
56         m_tileyrep = tileyrep;
57         m_drawingmode = mode;
58         m_transp = transp;
59         m_alpha = alpha;
60         m_zsort = zsort;
61         m_polymatid = m_newpolymatid++;
62         m_flag = 0;
63         m_multimode = 0;
64         m_shininess = 35.0;
65         m_specular.setValue(0.5,0.5,0.5);
66         m_specularity = 1.0;
67         m_diffuse.setValue(0.5,0.5,0.5);
68 }
69
70 RAS_IPolyMaterial::RAS_IPolyMaterial() 
71                 : m_texturename("__Dummy_Texture_Name__"),
72                 m_materialname("__Dummy_Material_Name__"),
73                 m_tile(0),
74                 m_tilexrep(0),
75                 m_tileyrep(0),
76                 m_drawingmode (0),
77                 m_transp(0),
78                 m_alpha(false),
79                 m_zsort(false),
80                 m_materialindex(0),
81                 m_polymatid(0),
82                 m_flag(0),
83                 m_multimode(0)
84 {
85         m_shininess = 35.0;
86         m_specular = MT_Vector3(0.5,0.5,0.5);
87         m_specularity = 1.0;
88         m_diffuse = MT_Vector3(0.5,0.5,0.5);
89 }
90
91 RAS_IPolyMaterial::RAS_IPolyMaterial(const STR_String& texname,
92                                                                          const STR_String& matname,
93                                                                          int materialindex,
94                                                                          int tile,
95                                                                          int tilexrep,
96                                                                          int tileyrep,
97                                                                          int mode,
98                                                                          int transp,
99                                                                          bool alpha,
100                                                                          bool zsort)
101                 : m_texturename(texname),
102                 m_materialname(matname),
103                 m_tile(tile),
104                 m_tilexrep(tilexrep),
105                 m_tileyrep(tileyrep),
106                 m_drawingmode (mode),
107                 m_transp(transp),
108                 m_alpha(alpha),
109                 m_zsort(zsort),
110                 m_materialindex(materialindex),
111                 m_polymatid(m_newpolymatid++),
112                 m_flag(0),
113                 m_multimode(0)
114 {
115         m_shininess = 35.0;
116         m_specular = MT_Vector3(0.5,0.5,0.5);
117         m_specularity = 1.0;
118         m_diffuse = MT_Vector3(0.5,0.5,0.5);
119 }
120
121
122 bool RAS_IPolyMaterial::Equals(const RAS_IPolyMaterial& lhs) const
123 {
124         if(m_flag &RAS_BLENDERMAT)
125         {
126                 bool test = (
127                         this->m_multimode                       ==              lhs.m_multimode &&
128                         this->m_flag                            ==              lhs.m_flag              &&
129                         this->m_drawingmode                     ==              lhs.m_drawingmode &&
130                         this->m_transp                          ==              lhs.m_transp &&
131                         this->m_texturename.hash()      ==              lhs.m_texturename.hash() &&
132                         this->m_materialname.hash() ==          lhs.m_materialname.hash()
133                 );
134
135                 return test;
136         }
137         else
138         {
139                 return (
140                                 this->m_tile            ==              lhs.m_tile &&
141                                 this->m_tilexrep        ==              lhs.m_tilexrep &&
142                                 this->m_tileyrep        ==              lhs.m_tileyrep &&
143                                 this->m_transp          ==              lhs.m_transp &&
144                                 this->m_alpha           ==              lhs.m_alpha &&
145                                 this->m_zsort           ==              lhs.m_zsort &&
146                                 this->m_drawingmode     ==              lhs.m_drawingmode &&
147                                 this->m_texturename.hash()      ==              lhs.m_texturename.hash() &&
148                                 this->m_materialname.hash() ==          lhs.m_materialname.hash()
149                 );
150         }
151 }
152
153
154 void RAS_IPolyMaterial::GetMaterialRGBAColor(unsigned char *rgba) const
155 {
156         *rgba++ = 0xFF;
157         *rgba++ = 0xFF;
158         *rgba++ = 0xFF;
159         *rgba++ = 0xFF;
160 }
161
162 bool RAS_IPolyMaterial::Less(const RAS_IPolyMaterial& rhs) const
163 {
164         if (Equals(rhs))
165                 return false;
166                 
167         return m_polymatid < rhs.m_polymatid;
168 }
169
170 bool RAS_IPolyMaterial::IsAlpha() const
171 {
172         return m_alpha || m_zsort;
173 }
174
175 bool RAS_IPolyMaterial::IsZSort() const
176 {
177         return m_zsort;
178 }
179
180 unsigned int RAS_IPolyMaterial::hash() const
181 {
182         return m_texturename.hash();
183 }
184
185 int RAS_IPolyMaterial::GetDrawingMode() const
186 {
187         return m_drawingmode;
188 }
189
190 const STR_String& RAS_IPolyMaterial::GetMaterialName() const
191
192         return m_materialname;
193 }
194
195 dword RAS_IPolyMaterial::GetMaterialNameHash() const
196 {
197         return m_materialname.hash();
198 }
199
200 const STR_String& RAS_IPolyMaterial::GetTextureName() const
201 {
202         return m_texturename;
203 }
204
205 int RAS_IPolyMaterial::GetMaterialIndex() const
206 {
207         return m_materialindex;
208 }
209
210 Material *RAS_IPolyMaterial::GetBlenderMaterial() const
211 {
212         return NULL;
213 }
214
215 Scene* RAS_IPolyMaterial::GetBlenderScene() const
216 {
217         return NULL;
218 }
219
220 void RAS_IPolyMaterial::ReleaseMaterial()
221 {
222 }
223
224 unsigned int    RAS_IPolyMaterial::GetFlag() const
225 {
226         return m_flag;
227 }
228
229 bool RAS_IPolyMaterial::UsesLighting(RAS_IRasterizer *rasty) const
230 {
231         bool dolights = false;
232
233         if(m_flag & RAS_BLENDERMAT)
234                 dolights = (m_flag &RAS_MULTILIGHT)!=0;
235         else if(rasty->GetDrawingMode() < RAS_IRasterizer::KX_SOLID);
236         else if(rasty->GetDrawingMode() == RAS_IRasterizer::KX_SHADOW);
237         else
238                 dolights = (m_drawingmode & 16)!=0;
239         
240         return dolights;
241 }
242
243 bool RAS_IPolyMaterial::UsesObjectColor() const
244 {
245         return !(m_flag & RAS_BLENDERGLSL);
246 }
247
248 unsigned int RAS_IPolyMaterial::m_newpolymatid = 0;
249