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 "resource_manager.h" |
---|
24 | #include <stdlib.h> |
---|
25 | #include <string.h> |
---|
26 | |
---|
27 | //! \todo check if we are in RESOURCE MANAGER-mode |
---|
28 | #include "resource_manager.h" |
---|
29 | |
---|
30 | using namespace std; |
---|
31 | |
---|
32 | /** |
---|
33 | \brief creates a Material. |
---|
34 | \param mtlName Name of the Material to be added to the Material List |
---|
35 | */ |
---|
36 | Material::Material (char* mtlName) |
---|
37 | { |
---|
38 | PRINTF(4)("initializing new Material.\n"); |
---|
39 | this->nextMat = NULL; |
---|
40 | this->name =""; |
---|
41 | this->setIllum(3); |
---|
42 | this->setDiffuse(0,0,0); |
---|
43 | this->setAmbient(0,0,0); |
---|
44 | this->setSpecular(.5,.5,.5); |
---|
45 | this->setShininess(2.0); |
---|
46 | this->setTransparency(1.0); |
---|
47 | |
---|
48 | |
---|
49 | this->diffuseTexture = NULL; |
---|
50 | this->ambientTexture = NULL; |
---|
51 | this->specularTexture = NULL; |
---|
52 | |
---|
53 | this->diffuseTextureSet = false; |
---|
54 | this->ambientTextureSet = false; |
---|
55 | this->specularTextureSet = false; |
---|
56 | |
---|
57 | if (mtlName) |
---|
58 | this->setName (mtlName); |
---|
59 | else |
---|
60 | this->setName(""); |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | \brief deletes a Material |
---|
65 | */ |
---|
66 | Material::~Material() |
---|
67 | { |
---|
68 | PRINTF(4)("delete Material %s.\n", this->name); |
---|
69 | if (this->name) |
---|
70 | delete []this->name; |
---|
71 | if (this->diffuseTexture) |
---|
72 | ResourceManager::getInstance()->unload(this->diffuseTexture); |
---|
73 | if (this->nextMat) |
---|
74 | delete this->nextMat; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | \brief adds a new Material to the List. |
---|
79 | this Function will append a new Material to the end of a Material List. |
---|
80 | \param mtlName The name of the Material to be added. |
---|
81 | */ |
---|
82 | Material* Material::addMaterial(char* mtlName) |
---|
83 | { |
---|
84 | PRINTF(4)("adding Material %s.\n", mtlName); |
---|
85 | Material* tmpMat = this; |
---|
86 | while (tmpMat->nextMat != NULL) |
---|
87 | { |
---|
88 | tmpMat = tmpMat->nextMat; |
---|
89 | } |
---|
90 | tmpMat->nextMat = new Material(mtlName); |
---|
91 | return tmpMat->nextMat; |
---|
92 | |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | \brief Search for a Material called mtlName |
---|
97 | \param mtlName the Name of the Material to search for |
---|
98 | \returns Material named mtlName if it is found. NULL otherwise. |
---|
99 | */ |
---|
100 | Material* Material::search(char* mtlName) |
---|
101 | { |
---|
102 | PRINTF(5)("Searching for material %s", mtlName); |
---|
103 | Material* searcher = this; |
---|
104 | while (searcher != NULL) |
---|
105 | { |
---|
106 | PRINT(5)("."); |
---|
107 | if (!strcmp (searcher->getName(), mtlName)) |
---|
108 | { |
---|
109 | PRINT(5)("found.\n"); |
---|
110 | return searcher; |
---|
111 | } |
---|
112 | searcher = searcher->nextMat; |
---|
113 | } |
---|
114 | PRINT(2)("material %s not found\n", mtlName); |
---|
115 | return NULL; |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | \brief sets the material with which the following Faces will be painted |
---|
120 | */ |
---|
121 | bool Material::select (void) |
---|
122 | { |
---|
123 | // setting diffuse color |
---|
124 | // glColor3f (diffuse[0], diffuse[1], diffuse[2]); |
---|
125 | glMaterialfv(GL_FRONT, GL_DIFFUSE, this->diffuse); |
---|
126 | |
---|
127 | // setting ambient color |
---|
128 | glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient); |
---|
129 | |
---|
130 | // setting up Sprecular |
---|
131 | glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular); |
---|
132 | |
---|
133 | // setting up Shininess |
---|
134 | glMaterialf(GL_FRONT, GL_SHININESS, this->shininess); |
---|
135 | |
---|
136 | // setting the transparency |
---|
137 | if (this->transparency == 1.0) |
---|
138 | { |
---|
139 | glDisable(GL_BLEND); |
---|
140 | } |
---|
141 | else |
---|
142 | { |
---|
143 | glEnable(GL_BLEND); |
---|
144 | glColor4f(1.0f, 1.0f, 1.0f, this->transparency); |
---|
145 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
146 | } |
---|
147 | |
---|
148 | // setting illumination Model |
---|
149 | if (this->illumModel == 1) //! \todo make this work, if no vertex-normals are read. |
---|
150 | glShadeModel(GL_FLAT); |
---|
151 | else if (this->illumModel >= 2) |
---|
152 | glShadeModel(GL_SMOOTH); |
---|
153 | |
---|
154 | if (this->diffuseTextureSet) |
---|
155 | { |
---|
156 | glEnable(GL_TEXTURE_2D); |
---|
157 | glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture()); |
---|
158 | } |
---|
159 | else |
---|
160 | { |
---|
161 | glDisable(GL_TEXTURE_2D); |
---|
162 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | \brief Set the Name of the Material. (Important for searching) |
---|
168 | \param mtlName the Name of the Material to be set. |
---|
169 | */ |
---|
170 | void Material::setName (char* mtlName) |
---|
171 | { |
---|
172 | PRINTF(4)("setting Material Name to %s.\n", this->name); |
---|
173 | this->name = new char [strlen(mtlName)+1]; |
---|
174 | strcpy(this->name, mtlName); |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | \returns The Name of The Material |
---|
179 | */ |
---|
180 | char* Material::getName (void) |
---|
181 | { |
---|
182 | return this->name; |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | \brief Sets the Material Illumination Model. |
---|
187 | \brief illu illumination Model in int form |
---|
188 | */ |
---|
189 | void Material::setIllum (int illum) |
---|
190 | { |
---|
191 | PRINTF(4)("setting illumModel of Material %s to %i\n", this->name, illum); |
---|
192 | this->illumModel = illum; |
---|
193 | } |
---|
194 | /** |
---|
195 | \brief Sets the Material Illumination Model. |
---|
196 | \brief illu illumination Model in char* form |
---|
197 | */void Material::setIllum (char* illum) |
---|
198 | { |
---|
199 | this->setIllum (atoi(illum)); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | \brief Sets the Material Diffuse Color. |
---|
204 | \param r Red Color Channel. |
---|
205 | \param g Green Color Channel. |
---|
206 | \param b Blue Color Channel. |
---|
207 | */ |
---|
208 | void Material::setDiffuse (float r, float g, float b) |
---|
209 | { |
---|
210 | PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b); |
---|
211 | this->diffuse[0] = r; |
---|
212 | this->diffuse[1] = g; |
---|
213 | this->diffuse[2] = b; |
---|
214 | this->diffuse[3] = 1.0; |
---|
215 | |
---|
216 | } |
---|
217 | /** |
---|
218 | \brief Sets the Material Diffuse Color. |
---|
219 | \param rgb The red, green, blue channel in char format (with spaces between them) |
---|
220 | */ |
---|
221 | void Material::setDiffuse (char* rgb) |
---|
222 | { |
---|
223 | float r,g,b; |
---|
224 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
225 | this->setDiffuse (r, g, b); |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | \brief Sets the Material Ambient Color. |
---|
230 | \param r Red Color Channel. |
---|
231 | \param g Green Color Channel. |
---|
232 | \param b Blue Color Channel. |
---|
233 | */ |
---|
234 | void Material::setAmbient (float r, float g, float b) |
---|
235 | { |
---|
236 | PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b); |
---|
237 | this->ambient[0] = r; |
---|
238 | this->ambient[1] = g; |
---|
239 | this->ambient[2] = b; |
---|
240 | this->ambient[3] = 1.0; |
---|
241 | } |
---|
242 | /** |
---|
243 | \brief Sets the Material Ambient Color. |
---|
244 | \param rgb The red, green, blue channel in char format (with spaces between them) |
---|
245 | */ |
---|
246 | void Material::setAmbient (char* rgb) |
---|
247 | { |
---|
248 | float r,g,b; |
---|
249 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
250 | this->setAmbient (r, g, b); |
---|
251 | } |
---|
252 | |
---|
253 | /** |
---|
254 | \brief Sets the Material Specular Color. |
---|
255 | \param r Red Color Channel. |
---|
256 | \param g Green Color Channel. |
---|
257 | \param b Blue Color Channel. |
---|
258 | */ |
---|
259 | void Material::setSpecular (float r, float g, float b) |
---|
260 | { |
---|
261 | PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b); |
---|
262 | this->specular[0] = r; |
---|
263 | this->specular[1] = g; |
---|
264 | this->specular[2] = b; |
---|
265 | this->specular[3] = 1.0; |
---|
266 | } |
---|
267 | /** |
---|
268 | \brief Sets the Material Specular Color. |
---|
269 | \param rgb The red, green, blue channel in char format (with spaces between them) |
---|
270 | */ |
---|
271 | void Material::setSpecular (char* rgb) |
---|
272 | { |
---|
273 | float r,g,b; |
---|
274 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
275 | this->setSpecular (r, g, b); |
---|
276 | } |
---|
277 | |
---|
278 | /** |
---|
279 | \brief Sets the Material Shininess. |
---|
280 | \param shini stes the Shininess from float. |
---|
281 | */ |
---|
282 | void Material::setShininess (float shini) |
---|
283 | { |
---|
284 | this->shininess = shini; |
---|
285 | } |
---|
286 | /** |
---|
287 | \brief Sets the Material Shininess. |
---|
288 | \param shini stes the Shininess from char*. |
---|
289 | */ |
---|
290 | void Material::setShininess (char* shini) |
---|
291 | { |
---|
292 | this->setShininess (atof(shini)); |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | \brief Sets the Material Transparency. |
---|
297 | \param trans stes the Transparency from int. |
---|
298 | */ |
---|
299 | void Material::setTransparency (float trans) |
---|
300 | { |
---|
301 | PRINTF(4)("setting Transparency of Material %s to %f.\n", this->name, trans); |
---|
302 | this->transparency = trans; |
---|
303 | } |
---|
304 | /** |
---|
305 | \brief Sets the Material Transparency. |
---|
306 | \param trans stes the Transparency from char*. |
---|
307 | */ |
---|
308 | void Material::setTransparency (char* trans) |
---|
309 | { |
---|
310 | this->setTransparency (atof(trans)); |
---|
311 | } |
---|
312 | |
---|
313 | /** |
---|
314 | \brief Adds a Texture Path to the List of already existing Paths |
---|
315 | \param pathName The Path to add. |
---|
316 | */ |
---|
317 | void Material::addTexturePath(char* pathName) |
---|
318 | { |
---|
319 | ResourceManager::getInstance()->addImageDir(pathName); |
---|
320 | } |
---|
321 | |
---|
322 | // MAPPING // |
---|
323 | |
---|
324 | /** |
---|
325 | \brief Sets the Materials Diffuse Map |
---|
326 | \param dMap the Name of the Image to Use |
---|
327 | */ |
---|
328 | void Material::setDiffuseMap(const char* dMap) |
---|
329 | { |
---|
330 | PRINTF(4)("setting Diffuse Map %s\n", dMap); |
---|
331 | // diffuseTexture = new Texture(); |
---|
332 | // this->diffuseTextureSet = diffuseTexture->loadImage(dMap); |
---|
333 | |
---|
334 | //! \todo check if RESOURCE MANAGER is availiable |
---|
335 | //! \todo Textures from .mtl-file need special care. |
---|
336 | this->diffuseTextureSet = this->diffuseTexture = (Texture*)ResourceManager::getInstance()->load(dMap, IMAGE); |
---|
337 | } |
---|
338 | |
---|
339 | /** |
---|
340 | \brief Sets the Materials Ambient Map |
---|
341 | \param aMap the Name of the Image to Use |
---|
342 | \todo implement this |
---|
343 | */ |
---|
344 | void Material::setAmbientMap(const char* aMap) |
---|
345 | { |
---|
346 | SDL_Surface* ambientMap; |
---|
347 | |
---|
348 | } |
---|
349 | |
---|
350 | /** |
---|
351 | \brief Sets the Materials Specular Map |
---|
352 | \param sMap the Name of the Image to Use |
---|
353 | \todo implement this |
---|
354 | */ |
---|
355 | void Material::setSpecularMap(const char* sMap) |
---|
356 | { |
---|
357 | SDL_Surface* specularMap; |
---|
358 | |
---|
359 | } |
---|
360 | |
---|
361 | /** |
---|
362 | \brief Sets the Materials Bumpiness |
---|
363 | \param bump the Name of the Image to Use |
---|
364 | \todo implemet this |
---|
365 | */ |
---|
366 | void Material::setBump(const char* bump) |
---|
367 | { |
---|
368 | |
---|
369 | } |
---|