1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | |
---|
15 | */ |
---|
16 | |
---|
17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER |
---|
18 | |
---|
19 | #include "material.h" |
---|
20 | |
---|
21 | #include "texture.h" |
---|
22 | #include "debug.h" |
---|
23 | #include "util/loading/resource_manager.h" |
---|
24 | #include <stdlib.h> |
---|
25 | #include <string.h> |
---|
26 | |
---|
27 | //! @todo check if we are in RESOURCE MANAGER-mode |
---|
28 | #include "util/loading/resource_manager.h" |
---|
29 | |
---|
30 | using namespace std; |
---|
31 | |
---|
32 | /** |
---|
33 | * creates a Material. |
---|
34 | * @param mtlName Name of the Material to be added to the Material List |
---|
35 | */ |
---|
36 | Material::Material (const std::string& mtlName) |
---|
37 | { |
---|
38 | this->setClassID(CL_MATERIAL, "Material"); |
---|
39 | |
---|
40 | this->setIllum(3); |
---|
41 | this->setDiffuse(1,1,1); |
---|
42 | this->setAmbient(0,0,0); |
---|
43 | this->setSpecular(.5,.5,.5); |
---|
44 | this->setShininess(2.0); |
---|
45 | this->setTransparency(1.0); |
---|
46 | |
---|
47 | this->diffuseTexture = NULL; |
---|
48 | this->ambientTexture = NULL; |
---|
49 | this->specularTexture = NULL; |
---|
50 | this->sFactor = GL_SRC_ALPHA; |
---|
51 | this->tFactor = GL_ONE; |
---|
52 | |
---|
53 | this->setName(mtlName); |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * deletes a Material |
---|
58 | */ |
---|
59 | Material::~Material() |
---|
60 | { |
---|
61 | PRINTF(5)("delete Material %s.\n", this->getName()); |
---|
62 | |
---|
63 | if (this->diffuseTexture != NULL) |
---|
64 | { |
---|
65 | ResourceManager::getInstance()->unload(this->diffuseTexture); |
---|
66 | } |
---|
67 | if (this->ambientTexture != NULL) |
---|
68 | ResourceManager::getInstance()->unload(this->ambientTexture); |
---|
69 | if (this->specularTexture != NULL) |
---|
70 | ResourceManager::getInstance()->unload(this->specularTexture); |
---|
71 | } |
---|
72 | |
---|
73 | Material& Material::operator=(const Material& m) |
---|
74 | { |
---|
75 | this->setIllum(m.illumModel); |
---|
76 | this->setDiffuse(m.diffuse[0],m.diffuse[1],m.diffuse[2]); |
---|
77 | this->setAmbient(m.ambient[0],m.ambient[1],m.ambient[2]); |
---|
78 | this->setSpecular(m.specular[0],m.specular[1],m.specular[2]); |
---|
79 | this->setShininess(m.shininess); |
---|
80 | this->setTransparency(m.transparency); |
---|
81 | |
---|
82 | if (this->diffuseTexture != NULL) |
---|
83 | ResourceManager::getInstance()->unload(this->diffuseTexture); |
---|
84 | if (m.diffuseTexture != NULL) |
---|
85 | this->diffuseTexture = dynamic_cast<Texture*>(ResourceManager::getInstance()->copy(m.diffuseTexture)); |
---|
86 | this->ambientTexture = NULL; /// FIXME |
---|
87 | this->specularTexture = NULL; /// FIXME |
---|
88 | |
---|
89 | this->setName(m.getName()); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | /** |
---|
94 | * sets the material with which the following Faces will be painted |
---|
95 | */ |
---|
96 | bool Material::select () const |
---|
97 | { |
---|
98 | // setting diffuse color |
---|
99 | glColor4f (diffuse[0], diffuse[1], diffuse[2], this->transparency); |
---|
100 | // glMaterialfv(GL_FRONT, GL_DIFFUSE, this->diffuse); |
---|
101 | |
---|
102 | // setting ambient color |
---|
103 | glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient); |
---|
104 | |
---|
105 | // setting up Sprecular |
---|
106 | glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular); |
---|
107 | |
---|
108 | // setting up Shininess |
---|
109 | glMaterialf(GL_FRONT, GL_SHININESS, this->shininess); |
---|
110 | |
---|
111 | // setting the transparency |
---|
112 | if (this->transparency < 1.0 || /* This allows alpha blending of 2D textures with the scene */ |
---|
113 | (this->diffuseTexture && this->diffuseTexture->hasAlpha())) |
---|
114 | { |
---|
115 | glEnable(GL_BLEND); |
---|
116 | glBlendFunc(this->sFactor, this->tFactor); |
---|
117 | } |
---|
118 | else |
---|
119 | { |
---|
120 | glDisable(GL_BLEND); |
---|
121 | //glColor4f(*(this->diffuse), *(this->diffuse+1), *(this->diffuse+2), 1); |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | // setting illumination Model |
---|
126 | if (this->illumModel == 1) //! @todo make this work, if no vertex-normals are read. |
---|
127 | glShadeModel(GL_FLAT); |
---|
128 | else if (this->illumModel >= 2) |
---|
129 | glShadeModel(GL_SMOOTH); |
---|
130 | |
---|
131 | if (this->diffuseTexture != NULL) |
---|
132 | { |
---|
133 | glEnable(GL_TEXTURE_2D); |
---|
134 | glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture()); |
---|
135 | } |
---|
136 | else |
---|
137 | { |
---|
138 | glDisable(GL_TEXTURE_2D); |
---|
139 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * Sets the Material Illumination Model. |
---|
145 | * illu illumination Model in int form |
---|
146 | */ |
---|
147 | void Material::setIllum (int illum) |
---|
148 | { |
---|
149 | PRINTF(4)("setting illumModel of Material %s to %i\n", this->getName(), illum); |
---|
150 | this->illumModel = illum; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * Sets the Material Illumination Model. |
---|
155 | * illu illumination Model in char* form |
---|
156 | */ |
---|
157 | void Material::setIllum (char* illum) |
---|
158 | { |
---|
159 | this->setIllum (atoi(illum)); |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * Sets the Material Diffuse Color. |
---|
164 | * @param r Red Color Channel. |
---|
165 | * @param g Green Color Channel. |
---|
166 | * @param b Blue Color Channel. |
---|
167 | */ |
---|
168 | void Material::setDiffuse (float r, float g, float b) |
---|
169 | { |
---|
170 | PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b); |
---|
171 | this->diffuse[0] = r; |
---|
172 | this->diffuse[1] = g; |
---|
173 | this->diffuse[2] = b; |
---|
174 | this->diffuse[3] = 1.0; |
---|
175 | |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | * Sets the Material Diffuse Color. |
---|
180 | * @param rgb The red, green, blue channel in char format (with spaces between them) |
---|
181 | */ |
---|
182 | void Material::setDiffuse (char* rgb) |
---|
183 | { |
---|
184 | float r,g,b; |
---|
185 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
186 | this->setDiffuse (r, g, b); |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * Sets the Material Ambient Color. |
---|
191 | * @param r Red Color Channel. |
---|
192 | * @param g Green Color Channel. |
---|
193 | * @param b Blue Color Channel. |
---|
194 | */ |
---|
195 | void Material::setAmbient (float r, float g, float b) |
---|
196 | { |
---|
197 | PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b); |
---|
198 | this->ambient[0] = r; |
---|
199 | this->ambient[1] = g; |
---|
200 | this->ambient[2] = b; |
---|
201 | this->ambient[3] = 1.0; |
---|
202 | } |
---|
203 | |
---|
204 | /** |
---|
205 | * Sets the Material Ambient Color. |
---|
206 | * @param rgb The red, green, blue channel in char format (with spaces between them) |
---|
207 | */ |
---|
208 | void Material::setAmbient (char* rgb) |
---|
209 | { |
---|
210 | float r,g,b; |
---|
211 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
212 | this->setAmbient (r, g, b); |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * Sets the Material Specular Color. |
---|
217 | * @param r Red Color Channel. |
---|
218 | * @param g Green Color Channel. |
---|
219 | * @param b Blue Color Channel. |
---|
220 | */ |
---|
221 | void Material::setSpecular (float r, float g, float b) |
---|
222 | { |
---|
223 | PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b); |
---|
224 | this->specular[0] = r; |
---|
225 | this->specular[1] = g; |
---|
226 | this->specular[2] = b; |
---|
227 | this->specular[3] = 1.0; |
---|
228 | } |
---|
229 | |
---|
230 | /** |
---|
231 | * Sets the Material Specular Color. |
---|
232 | * @param rgb The red, green, blue channel in char format (with spaces between them) |
---|
233 | */ |
---|
234 | void Material::setSpecular (char* rgb) |
---|
235 | { |
---|
236 | float r,g,b; |
---|
237 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
238 | this->setSpecular (r, g, b); |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * Sets the Material Shininess. |
---|
243 | * @param shini stes the Shininess from float. |
---|
244 | */ |
---|
245 | void Material::setShininess (float shini) |
---|
246 | { |
---|
247 | this->shininess = shini; |
---|
248 | } |
---|
249 | /** |
---|
250 | * Sets the Material Shininess. |
---|
251 | * @param shini stes the Shininess from char*. |
---|
252 | */ |
---|
253 | void Material::setShininess (char* shini) |
---|
254 | { |
---|
255 | this->setShininess (atof(shini)); |
---|
256 | } |
---|
257 | |
---|
258 | /** |
---|
259 | * Sets the Material Transparency. |
---|
260 | * @param trans stes the Transparency from int. |
---|
261 | */ |
---|
262 | void Material::setTransparency (float trans) |
---|
263 | { |
---|
264 | PRINTF(4)("setting Transparency of Material %s to %f.\n", this->getName(), trans); |
---|
265 | this->transparency = trans; |
---|
266 | } |
---|
267 | /** |
---|
268 | * Sets the Material Transparency. |
---|
269 | * @param trans stes the Transparency from char*. |
---|
270 | */ |
---|
271 | void Material::setTransparency (char* trans) |
---|
272 | { |
---|
273 | this->setTransparency (atof(trans)); |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * Adds a Texture Path to the List of already existing Paths |
---|
278 | * @param pathName The Path to add. |
---|
279 | */ |
---|
280 | void Material::addTexturePath(const std::string& pathName) |
---|
281 | { |
---|
282 | ResourceManager::getInstance()->addImageDir(pathName); |
---|
283 | } |
---|
284 | |
---|
285 | // MAPPING // |
---|
286 | |
---|
287 | /** |
---|
288 | * Sets the Materials Diffuse Map |
---|
289 | * @param dMap the Name of the Image to Use |
---|
290 | */ |
---|
291 | void Material::setDiffuseMap(const std::string& dMap, GLenum target) |
---|
292 | { |
---|
293 | PRINTF(5)("setting Diffuse Map %s\n", dMap); |
---|
294 | if (this->diffuseTexture != NULL) |
---|
295 | ResourceManager::getInstance()->unload(this->diffuseTexture); |
---|
296 | |
---|
297 | //! @todo check if RESOURCE MANAGER is availiable |
---|
298 | //! @todo Textures from .mtl-file need special care. |
---|
299 | if (!dMap.empty()) |
---|
300 | this->diffuseTexture = dynamic_cast<Texture*>(ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target)); |
---|
301 | else |
---|
302 | this->diffuseTexture = NULL; |
---|
303 | } |
---|
304 | |
---|
305 | /** |
---|
306 | * Sets the Materials Ambient Map |
---|
307 | * @param aMap the Name of the Image to Use |
---|
308 | @todo implement this |
---|
309 | */ |
---|
310 | void Material::setAmbientMap(const std::string& aMap, GLenum target) |
---|
311 | { |
---|
312 | SDL_Surface* ambientMap; |
---|
313 | |
---|
314 | } |
---|
315 | |
---|
316 | /** |
---|
317 | * Sets the Materials Specular Map |
---|
318 | * @param sMap the Name of the Image to Use |
---|
319 | @todo implement this |
---|
320 | */ |
---|
321 | void Material::setSpecularMap(const std::string& sMap, GLenum target) |
---|
322 | { |
---|
323 | SDL_Surface* specularMap; |
---|
324 | |
---|
325 | } |
---|
326 | |
---|
327 | /** |
---|
328 | * Sets the Materials Bumpiness |
---|
329 | * @param bump the Name of the Image to Use |
---|
330 | @todo implemet this |
---|
331 | */ |
---|
332 | void Material::setBump(const std::string& bump) |
---|
333 | { |
---|
334 | |
---|
335 | } |
---|