Game Engine: alpha blending and sorting
[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 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 RAS_IPolyMaterial::RAS_IPolyMaterial(const STR_String& texname,
37                                                                          const STR_String& matname,
38                                                                          int tile,
39                                                                          int tilexrep,
40                                                                          int tileyrep,
41                                                                          int mode,
42                                                                          int transp,
43                                                                          bool alpha,
44                                                                          bool zsort,
45                                                                          int lightlayer,
46                                                                          bool bIsTriangle,
47                                                                          void* clientobject=NULL) :
48
49                 m_texturename(texname),
50                 m_materialname(matname),
51                 m_tile(tile),
52                 m_tilexrep(tilexrep),
53                 m_tileyrep(tileyrep),
54                 m_drawingmode (mode),
55                 m_transp(transp),
56                 m_alpha(alpha),
57                 m_zsort(zsort),
58                 m_lightlayer(lightlayer),
59                 m_bIsTriangle(bIsTriangle),
60                 m_polymatid(m_newpolymatid++),
61                 m_flag(0),
62                 m_multimode(0)
63 {
64         m_shininess = 35.0;
65         m_specular = MT_Vector3(0.5,0.5,0.5);
66         m_specularity = 1.0;
67         m_diffuse = MT_Vector3(0.5,0.5,0.5);
68 }
69
70
71 bool RAS_IPolyMaterial::Equals(const RAS_IPolyMaterial& lhs) const
72 {
73         if(m_flag &RAS_BLENDERMAT)
74         {
75                 return (
76                         this->m_multimode                       ==              lhs.m_multimode &&
77                         this->m_flag                            ==              lhs.m_flag              &&
78                         this->m_drawingmode                     ==              lhs.m_drawingmode &&
79                         this->m_transp                          ==              lhs.m_transp &&
80                         this->m_lightlayer                      ==              lhs.m_lightlayer &&
81                         this->m_texturename.hash()      ==              lhs.m_texturename.hash() &&
82                         this->m_materialname.hash() ==          lhs.m_materialname.hash()
83                 );
84         }
85         else
86         {
87                 return (
88                                 this->m_tile            ==              lhs.m_tile &&
89                                 this->m_tilexrep        ==              lhs.m_tilexrep &&
90                                 this->m_tileyrep        ==              lhs.m_tileyrep &&
91                                 this->m_transp          ==              lhs.m_transp &&
92                                 this->m_alpha           ==              lhs.m_alpha &&
93                                 this->m_zsort           ==              lhs.m_zsort &&
94                                 this->m_drawingmode     ==              lhs.m_drawingmode &&
95                                 this->m_bIsTriangle     ==              lhs.m_bIsTriangle &&
96                                 this->m_lightlayer      ==              lhs.m_lightlayer &&
97                                 this->m_texturename.hash()      ==              lhs.m_texturename.hash() &&
98                                 this->m_materialname.hash() ==          lhs.m_materialname.hash()
99                 );
100         }
101 }
102
103 bool RAS_IPolyMaterial::Less(const RAS_IPolyMaterial& rhs) const
104 {
105         if (Equals(rhs))
106                 return false;
107                 
108         return m_polymatid < rhs.m_polymatid;
109 }
110
111 int RAS_IPolyMaterial::GetLightLayer() const
112 {
113         return m_lightlayer;
114 }
115
116 bool RAS_IPolyMaterial::IsAlpha() const
117 {
118         return m_alpha || m_zsort;
119 }
120
121 bool RAS_IPolyMaterial::IsZSort() const
122 {
123         return m_zsort;
124 }
125
126 bool RAS_IPolyMaterial::UsesTriangles() const
127 {
128         return m_bIsTriangle;
129 }
130
131 unsigned int RAS_IPolyMaterial::hash() const
132 {
133         return m_texturename.hash();
134 }
135
136 int RAS_IPolyMaterial::GetDrawingMode() const
137 {
138         return m_drawingmode;
139 }
140
141 const STR_String& RAS_IPolyMaterial::GetMaterialName() const
142
143         return m_materialname;
144 }
145
146 dword RAS_IPolyMaterial::GetMaterialNameHash() const
147 {
148         return m_materialname.hash();
149 }
150
151 const STR_String& RAS_IPolyMaterial::GetTextureName() const
152 {
153         return m_texturename;
154 }
155
156 const unsigned int      RAS_IPolyMaterial::GetFlag() const
157 {
158         return m_flag;
159 }
160
161 bool RAS_IPolyMaterial::UsesLighting(RAS_IRasterizer *rasty) const
162 {
163         bool dolights = false;
164
165         if(m_flag & RAS_BLENDERMAT)
166                 dolights = (m_flag &RAS_MULTILIGHT)!=0;
167         else if(rasty->GetDrawingMode() < RAS_IRasterizer::KX_SOLID);
168         else if(rasty->GetDrawingMode() == RAS_IRasterizer::KX_SHADOW);
169         else
170                 dolights = (m_drawingmode & 16)!=0;
171         
172         return dolights;
173 }
174
175 unsigned int RAS_IPolyMaterial::m_newpolymatid = 0;
176