zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Material.cpp
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib 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 Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <zeni_graphics.h>
19 
20 #include <cmath>
21 
22 #ifndef DISABLE_DX9
23 #include <d3dx9.h>
24 #endif
25 
26 #ifndef DISABLE_GL
27 #if defined(REQUIRE_GL_ES)
28 #include <GLES/gl.h>
29 #else
30 #include <GL/glew.h>
31 #endif
32 #endif
33 
34 #include <Zeni/Define.h>
35 
36 namespace Zeni {
37 
38  Material::Material(const Color &ambient_, const Color &diffuse_, const Color &specular_, const Color &emissive_, const float &power, const String &texture)
39  : diffuse(diffuse_),
40  ambient(ambient_),
41  specular(specular_),
42  emissive(emissive_),
43  m_power(power),
44  m_texture(""),
45  m_texture_id(0),
46  m_optimization(0)
47  {
48  set_Texture(texture);
49  }
50 
51  Material::Material(const String &texture, const Color &ambient_and_diffuse)
52  : diffuse(ambient_and_diffuse),
53  ambient(ambient_and_diffuse),
54  specular(ZENI_DIFFUSE_TO_SPECULAR(ambient_and_diffuse)),
57  m_texture(""),
58  m_texture_id(0),
59  m_optimization(0)
60  {
61  set_Texture(texture);
62  }
63 
64  float Material::get_shininess() const {
65  return 0.1f * float(log(m_power)/log(2.0f));
66  }
67 
68  void Material::set_shininess(const float &shininess) {
69  m_power = float(pow(2.0f, 10.0f * shininess));
70  if(m_power > 128.0f)
71  m_power = 128.0f;
72  }
73 
75  m_texture = texture;
76  if(texture.empty())
77  m_texture_id = 0;
78  else
79  m_texture_id = get_Textures().get_id(texture);
80  }
81 
82 #ifndef DISABLE_GL_FIXED
83  void Material::set(Video_GL_Fixed &vgl) const {
84  if(vgl.get_lighting()) {
86 
87  if(!(m_optimization & (1 << 0)))
88  glMaterialfv(face, GL_AMBIENT, reinterpret_cast<const GLfloat *>(&ambient));
89  if(!(m_optimization & (1 << 1)))
90  glMaterialfv(face, GL_DIFFUSE, reinterpret_cast<const GLfloat *>(&diffuse));
91  if(!(m_optimization & (1 << 2)))
92  glMaterialfv(face, GL_SPECULAR, reinterpret_cast<const GLfloat *>(&specular));
93  if(!(m_optimization & (1 << 3)))
94  glMaterialfv(face, GL_EMISSION, reinterpret_cast<const GLfloat *>(&emissive));
95  if(!(m_optimization & (1 << 4)))
96  glMaterialfv(face, GL_SHININESS, &m_power);
97  }
98  else
99  vgl.set_Color(diffuse);
100 
101  if(!(m_optimization & (1 << 5)) &&
102  !m_texture.empty()) {
103  try {
104  vgl.apply_Texture(m_texture_id);
105  }
106  catch(Database_Entry_Not_Found &) {
107  m_texture_id = get_Textures().get_id(m_texture);
108  if(!m_texture_id)
109  throw;
110  vgl.apply_Texture(m_texture_id);
111  }
112  }
113  }
114 
115  void Material::unset(Video_GL_Fixed &vgl) const {
116  if(!(m_optimization & (1 << 11)) &&
117  !m_texture.empty())
118  vgl.unapply_Texture();
119  }
120 #endif
121 
122 #ifndef DISABLE_GL_SHADER
123  void Material::set(Video_GL_Shader &vgl) const {
124  if(vgl.get_lighting()) {
125  const GLenum face = GL_FRONT_AND_BACK;
126 
127  if(!(m_optimization & (1 << 0)))
128  glMaterialfv(face, GL_AMBIENT, reinterpret_cast<const GLfloat *>(&ambient));
129  if(!(m_optimization & (1 << 1)))
130  glMaterialfv(face, GL_DIFFUSE, reinterpret_cast<const GLfloat *>(&diffuse));
131  if(!(m_optimization & (1 << 2)))
132  glMaterialfv(face, GL_SPECULAR, reinterpret_cast<const GLfloat *>(&specular));
133  if(!(m_optimization & (1 << 3)))
134  glMaterialfv(face, GL_EMISSION, reinterpret_cast<const GLfloat *>(&emissive));
135  if(!(m_optimization & (1 << 4)))
136  glMaterialfv(face, GL_SHININESS, &m_power);
137  }
138  else
139  vgl.set_Color(diffuse);
140 
141  if(!(m_optimization & (1 << 5)) &&
142  !m_texture.empty()) {
143  try {
144  vgl.apply_Texture(m_texture_id);
145  }
146  catch(Database_Entry_Not_Found &) {
147  m_texture_id = get_Textures().get_id(m_texture);
148  if(!m_texture_id)
149  throw;
150  vgl.apply_Texture(m_texture_id);
151  }
152  }
153  }
154 
155  void Material::unset(Video_GL_Shader &vgl) const {
156  if(!(m_optimization & (1 << 11)) &&
157  !m_texture.empty())
158  vgl.unapply_Texture();
159  }
160 #endif
161 
162 #ifndef DISABLE_DX9
163  void Material::set(Video_DX9 &vdx) const {
164  if(vdx.get_lighting()) {
165  if((m_optimization & ((1 << 5) - 1)) != 0x1F)
166  vdx.get_d3d_device()->SetMaterial(reinterpret_cast<const D3DMATERIAL9 *>(this));
167  }
168  else
169  vdx.set_Color(diffuse);
170 
171  if(!(m_optimization & (1 << 5)) &&
172  !m_texture.empty())
173  vdx.apply_Texture(m_texture);
174  }
175 
176  void Material::unset(Video_DX9 &vdx) const {
177  if(!(m_optimization & (1 << 11)) &&
178  !m_texture.empty())
179  vdx.unapply_Texture();
180  }
181 #endif
182 
183  bool Material::operator<(const Material &rhs) const {
184  return m_texture < rhs.m_texture || (m_texture == rhs.m_texture &&
185  (diffuse < rhs.diffuse || (diffuse == rhs.diffuse &&
186  (ambient < rhs.ambient || (ambient == rhs.ambient &&
187  (specular < rhs.specular || (specular == rhs.specular &&
188  (emissive < rhs.emissive || (emissive == rhs.emissive &&
189  m_power < rhs.m_power)))))))));
190  }
191 
192  bool Material::operator==(const Material &rhs) const {
193  return m_texture == rhs.m_texture &&
194  diffuse == rhs.diffuse &&
195  ambient == rhs.ambient &&
196  specular == rhs.specular &&
197  emissive == rhs.emissive &&
198  m_power == rhs.m_power;
199  }
200 
202  m_optimization &= 0xFFFFFFC0;
203 
204  if(ambient == rhs.ambient)
205  m_optimization |= (1 << 0);
206  if(diffuse == rhs.diffuse)
207  m_optimization |= (1 << 1);
208  if(specular == rhs.specular)
209  m_optimization |= (1 << 2);
210  if(emissive == rhs.emissive)
211  m_optimization |= (1 << 3);
212  if(m_power == rhs.m_power)
213  m_optimization |= (1 << 4);
214  if(m_texture == rhs.m_texture)
215  m_optimization |= (1 << 5);
216  }
217 
219  m_optimization &= 0xFFFFF03F;
220 
221  if(ambient == rhs.ambient)
222  m_optimization |= (1 << 6);
223  if(diffuse == rhs.diffuse)
224  m_optimization |= (1 << 7);
225  if(specular == rhs.specular)
226  m_optimization |= (1 << 8);
227  if(emissive == rhs.emissive)
228  m_optimization |= (1 << 9);
229  if(m_power == rhs.m_power)
230  m_optimization |= (1 << 10);
231  if(m_texture == rhs.m_texture)
232  m_optimization |= (1 << 11);
233  }
234 
236  m_optimization = 0;
237  }
238 
239 }
240 
241 #include <Zeni/Undefine.h>
void set_Color(const Color &color)
Set the current color.
void optimize_to_follow(const Material &rhs)
If this Material will be set regularly after another Material, and only after that other Material...
Definition: Material.cpp:201
Definition: Database.h:142
The Direct3D9 Rendering System.
Definition: Video_DX9.h:62
LPDIRECT3DDEVICE9 & get_d3d_device()
See DirectX Documentation for details.
Definition: Video_DX9.hxx:48
unsigned int GLenum
Definition: gl2.h:23
#define ZENI_DEFAULT_MATERIAL_EMISSIVE
Definition: Define.h:46
Color specular
The specular Color.
Definition: Material.h:104
GLclampf f
Definition: glew.h:3390
#define GL_DIFFUSE
Definition: glew_head.h:539
#define glMaterialfv
Definition: gl_mangle.h:1040
bool empty() const
Definition: String.cpp:325
static bool get_lighting()
Determine whether dynamic lighting is enabled.
Definition: Video.hxx:41
void apply_Texture(const String &name)
Apply a texture by name.
#define GL_EMISSION
Definition: glew_head.h:577
#define ZENI_DIFFUSE_TO_SPECULAR(d)
Definition: Define.h:103
An Abstraction of a Material.
Definition: Material.h:56
float get_shininess() const
Get the shininess of the Material (indicates the focus of the specular highlights - logarithmically t...
Definition: Material.cpp:64
unsigned long get_id(const String &name) const
Get an id by name, possibly throwing an Error.
Definition: Database.hxx:172
#define GL_FRONT_AND_BACK
Definition: gl2.h:132
bool operator==(const Material &rhs) const
A simple equality test. Close hits are misses.
Definition: Material.cpp:192
void unset(Video_GL_Fixed &screen) const
Definition: Material.cpp:115
The OpenGL Rendering System.
void apply_Texture(const String &name)
Apply a texture by name.
void unapply_Texture()
Unapply a texture.
Definition: Video_DX9.cpp:371
void unapply_Texture()
Unapply a texture.
GLenum face
Definition: gl2ext.h:1490
Color ambient
The ambient Color.
Definition: Material.h:103
The OpenGL Rendering System.
Color diffuse
The diffuse Color.
Definition: Material.h:102
Color emissive
The emissive Color.
Definition: Material.h:105
void optimize_to_precede(const Material &rhs)
If this Material will be set regularly before another Material, and only before that other Material...
Definition: Material.cpp:218
#define GL_AMBIENT
Definition: glew_head.h:538
void set_Color(const Color &)
Set the current color.
Definition: Video_DX9.cpp:349
Material(const Color &ambient=ZENI_DEFAULT_MATERIAL_DIFFUSE, const Color &diffuse=ZENI_DEFAULT_MATERIAL_DIFFUSE, const Color &specular=ZENI_DEFAULT_MATERIAL_SPECULAR, const Color &emissive=ZENI_DEFAULT_MATERIAL_EMISSIVE, const float &power=ZENI_DEFAULT_MATERIAL_POWER, const String &texture="")
An Alternative to the numerous setter functions.
Definition: Material.cpp:38
void set(Video_GL_Fixed &screen) const
Definition: Material.cpp:83
Textures & get_Textures()
Get access to the singleton.
Definition: Textures.cpp:64
void set_shininess(const float &shininess)
Set the shininess of the Material (indicates the focus of the specular highlights - logarithmically t...
Definition: Material.cpp:68
GLenum GLenum GLuint texture
Definition: gl2ext.h:850
bool operator<(const Material &rhs) const
To provide an arbitrary total ordering. Do not depend on it remaining the same in the future...
Definition: Material.cpp:183
void set_Color(const Color &color)
Set the current color.
#define ZENI_DEFAULT_MATERIAL_POWER
Definition: Define.h:47
#define GL_SPECULAR
Definition: glew_head.h:540
#define GL_SHININESS
Definition: glew_head.h:578
void clear_optimization()
Simply undo any previous optimizations.
Definition: Material.cpp:235
void unapply_Texture()
Unapply a texture.
void apply_Texture(const String &name)
Apply a texture by name.
Definition: Video_DX9.h:146
Color.
Definition: Color.h:41
void set_Texture(const String &texture)
Set the texture identifier.
Definition: Material.cpp:74