Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/importer/material.cc @ 8728

Last change on this file since 8728 was 8619, checked in by bensch, 18 years ago

trunk: merged the gui-branche back.
merged with command:
svn merge -r8520:HEAD https://svn.orxonox.net/orxonox/branches/gui
no conflicts

File size: 13.2 KB
Line 
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 "compiler.h"
24
25#include "loading/load_param.h"
26
27#include "util/loading/resource_manager.h"
28
29/**
30 * @brief creates a Material.
31 * @param mtlName Name of the Material to be added to the Material List
32 */
33Material::Material (const std::string& mtlName)
34{
35  this->setClassID(CL_MATERIAL, "Material");
36
37  this->setIllum(3);
38  this->setDiffuse(1,1,1);
39  this->setAmbient(0,0,0);
40  this->setSpecular(.5,.5,.5);
41  this->setShininess(2.0);
42  this->setTransparency(1.0);
43
44  this->ambientTexture = NULL;
45  this->specularTexture = NULL;
46  this->sFactor = GL_SRC_ALPHA;
47  this->tFactor = GL_ONE;
48
49  this->setName(mtlName);
50}
51
52Material& Material::operator=(const Material& material)
53{
54  this->illumModel = material.illumModel;
55  this->diffuse = material.diffuse;
56  this->specular = material.specular;
57  this->ambient = material.ambient;
58  this->shininess = material.shininess;
59
60  this->textures = material.textures;
61  this->sFactor = material.sFactor;
62  this->tFactor = material.tFactor;
63  this->setName(material.getName());
64
65  return *this;
66}
67
68
69
70void Material::loadParams(const TiXmlElement* root)
71{
72  LoadParam(root, "illum", this, Material, setIllum);
73
74  LoadParam(root, "diffuse-color", this, Material , setDiffuse);
75  LoadParam(root, "ambient-color", this, Material , setAmbient);
76  LoadParam(root, "specular-color", this, Material , setSpecular);
77  LoadParam(root, "transparency", this, Material , setTransparency);
78
79  LoadParam(root, "tex", this, Material, setDiffuseMap);
80  LoadParam(root, "blendfunc", this, Material, setBlendFuncS)
81      .defaultValues("ZERO", "ZERO");
82}
83
84
85/**
86 * @brief deletes a Material
87 */
88Material::~Material()
89{
90  PRINTF(5)("delete Material %s.\n", this->getName());
91
92  if (this->ambientTexture != NULL)
93    ResourceManager::getInstance()->unload(this->ambientTexture);
94  if (this->specularTexture != NULL)
95    ResourceManager::getInstance()->unload(this->specularTexture);
96
97  if (this == Material::selectedMaterial)
98    Material::selectedMaterial = NULL;
99}
100
101
102const Material* Material::selectedMaterial = NULL;
103
104
105/// TODO FIX THIS
106// Material& Material::operator=(const Material& m)
107// {
108//   this->setIllum(m.illumModel);
109//   this->setDiffuse(m.diffuse[0],m.diffuse[1],m.diffuse[2]);
110//   this->setAmbient(m.ambient[0],m.ambient[1],m.ambient[2]);
111//   this->setSpecular(m.specular[0],m.specular[1],m.specular[2]);
112//   this->setShininess(m.shininess);
113//   this->setTransparency(m.transparency);
114//
115//   if (this->diffuseTexture != NULL)
116//     ResourceManager::getInstance()->unload(this->diffuseTexture);
117//   if (m.diffuseTexture != NULL)
118//     this->diffuseTexture = (Texture*)ResourceManager::getInstance()->copy(m.diffuseTexture);
119//   this->ambientTexture = NULL; /// FIXME
120//   this->specularTexture = NULL; /// FIXME
121//
122//   this->setName(m.getName());
123// }
124
125
126
127/**
128 * @brief sets the material with which the following Faces will be painted
129 */
130bool Material::select() const
131{
132  if (unlikely(this == Material::selectedMaterial))
133    return true;
134
135  /// !!  HACK !!! FIX THIS IN MODEL ///
136  else if (likely(Material::selectedMaterial != NULL))
137  {
138    Material::unselect();
139    //     for(unsigned int i = 0; i < Material::selectedMaterial->textures.size(); ++i)
140    //     {
141    //         glActiveTexture(Material::glTextureArbs[i]);
142    //         glBindTexture(GL_TEXTURE_2D, 0);
143    //         glDisable(GL_TEXTURE_2D);
144    //     }
145  }
146
147  if (likely(Material::selectedMaterial != NULL))
148  {
149    for(unsigned int i = 0; i < Material::selectedMaterial->textures.size(); ++i)
150    {
151      glActiveTexture(Material::glTextureArbs[i]);
152      //glBindTexture(GL_TEXTURE_2D, 0);
153      glDisable(GL_TEXTURE_2D);
154    }
155  }
156
157  // setting diffuse color
158  glColor4f (diffuse.r(), diffuse.g(), diffuse.b(), diffuse.a());
159  // setting ambient color
160  glMaterialfv(GL_FRONT, GL_AMBIENT, &this->ambient[0]);
161  // setting up Sprecular
162  glMaterialfv(GL_FRONT, GL_SPECULAR, &this->specular[0]);
163  // setting up Shininess
164  glMaterialf(GL_FRONT, GL_SHININESS, this->shininess);
165
166  // setting the transparency
167  if (this->diffuse.a() < 1.0 ||       /* This allows alpha blending of 2D textures with the scene */
168      (likely(!this->textures.empty() && this->textures[0].hasAlpha())))
169  {
170    glEnable(GL_BLEND);
171    glBlendFunc(this->sFactor, this->tFactor);
172  }
173  else
174  {
175    glDisable(GL_BLEND);
176  }
177
178
179  // setting illumination Model
180  if (this->illumModel == 1) //! @todo make this work, if no vertex-normals are read.
181    glShadeModel(GL_FLAT);
182  else if (this->illumModel >= 2)
183    glShadeModel(GL_SMOOTH);
184
185  for(unsigned int i = 0; i < this->textures.size(); ++i)
186  {
187    glActiveTexture(Material::glTextureArbs[i]);
188    glEnable(GL_TEXTURE_2D);
189    if(this->textures[i].hasAlpha())
190    {
191      glEnable(GL_BLEND);
192    }
193    glBindTexture(GL_TEXTURE_2D, this->textures[i].getTexture());
194  }
195  Material::selectedMaterial = this;
196
197  return true;
198}
199/**
200 * @brief Deselect Material (if one is selected).
201 */
202void Material::unselect()
203{
204  Material::selectedMaterial = NULL;
205  for(unsigned int i = 0; i < 8; ++i)
206  {
207    glActiveTexture(Material::glTextureArbs[i]);
208    glBindTexture(GL_TEXTURE_2D, 0);
209    glDisable(GL_TEXTURE_2D);
210  }
211}
212
213/**
214 * @brief Sets the Material Illumination Model.
215 * @param illu illumination Model in int form
216 */
217void Material::setIllum (int illum)
218{
219  PRINTF(4)("setting illumModel of Material %s to %i\n", this->getName(), illum);
220  this->illumModel = illum;
221}
222
223/**
224 * @brief Sets the Material Diffuse Color.
225 * @param r Red Color Channel.a
226 * @param g Green Color Channel.
227 * @param b Blue Color Channel.
228 */
229void Material::setDiffuse (float r, float g, float b)
230{
231  PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
232  this->diffuse = Color(r, g, b, this->diffuse.a() );
233}
234
235
236/**
237 * @brief Sets the Material Ambient Color.
238 * @param r Red Color Channel.
239 * @param g Green Color Channel.
240 * @param b Blue Color Channel.
241*/
242void Material::setAmbient (float r, float g, float b)
243{
244  PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
245  this->ambient = Color(r, g, b, 1.0);
246}
247
248/**
249 * @brief Sets the Material Specular Color.
250 * @param r Red Color Channel.
251 * @param g Green Color Channel.
252 * @param b Blue Color Channel.
253 */
254void Material::setSpecular (float r, float g, float b)
255{
256  PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
257  this->specular = Color (r, g, b, 1.0);
258}
259
260/**
261 * @brief Sets the Material Shininess.
262 * @param shini stes the Shininess from float.
263*/
264void Material::setShininess (float shini)
265{
266  this->shininess = shini;
267}
268
269/**
270 * @brief Sets the Material Transparency.
271 * @param trans stes the Transparency from int.
272*/
273void Material::setTransparency (float trans)
274{
275  PRINTF(4)("setting Transparency of Material %s to %f.\n", this->getName(), trans);
276  this->diffuse.a() = trans;
277}
278
279/**
280 * @brief sets the Blend-Function Parameters
281 * @param sFactor the Source Parameter.
282 * @param tFactor the Desitnation Parameter.
283 */
284void Material::setBlendFuncS(const std::string& sFactor, const std::string& tFactor)
285{
286  this->setBlendFunc(Material::stringToBlendFunc(sFactor), Material::stringToBlendFunc(tFactor));
287}
288
289
290
291/**
292 * @brief Adds a Texture Path to the List of already existing Paths
293 * @param pathName The Path to add.
294*/
295void Material::addTexturePath(const std::string& pathName)
296{
297  ResourceManager::getInstance()->addImageDir(pathName);
298}
299
300// MAPPING //
301
302
303/**
304 * @brief Sets the Diffuse map of this Texture.
305 * @param texture The Texture to load
306 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS
307 */
308void Material::setDiffuseMap(const Texture& texture, unsigned int textureNumber)
309{
310  assert(textureNumber < Material::getMaxTextureUnits());
311
312  if (this->textures.size() <= textureNumber)
313    this->textures.resize(textureNumber+1, Texture());
314
315  //! @todo check if RESOURCE MANAGER is availiable
316  this->textures[textureNumber] = texture;
317}
318
319
320/**
321 * @brief Sets the Materials Diffuse Map
322 * @param dMap the Name of the Image to Use
323 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS
324 */
325void Material::setDiffuseMap(const std::string& dMap, GLenum target, unsigned int textureNumber)
326{
327  assert(textureNumber < Material::getMaxTextureUnits());
328
329  PRINTF(5)("setting Diffuse Map %s\n", dMap.c_str());
330  if (this->textures.size() <= textureNumber)
331    this->textures.resize(textureNumber+1, Texture());
332
333  //! @todo check if RESOURCE MANAGER is availiable
334  if (!dMap.empty())
335  {
336    Texture* tex = dynamic_cast<Texture*>(ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target));
337    if (tex != NULL)
338      this->textures[textureNumber] = *tex;
339    else
340      this->textures[textureNumber] = Texture();
341  }
342  else
343  {
344    this->textures[textureNumber] = Texture();
345  }
346}
347
348/**
349 * @brief Sets the Materials Diffuse Map
350 * @param surface pointer to SDL_Surface which shall be used as texture.
351 * @param target the GL-Target to load the Surface to.
352 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS.
353 */
354void Material::setSDLDiffuseMap(SDL_Surface *surface, GLenum target, unsigned int textureNumber)
355{
356  assert(textureNumber < Material::getMaxTextureUnits());
357
358
359  if (this->textures.size() <= textureNumber)
360    this->textures.resize(textureNumber+1, Texture());
361
362  if(surface != NULL)
363  {
364    this->textures[textureNumber] = Texture(surface, GL_TEXTURE_2D);
365  }
366  else
367  {
368    this->textures[textureNumber] = Texture();
369  }
370
371}
372
373/**
374 * @brief Renders to a Texture.
375 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS.
376 * @param target The GL-Target.
377 * @param level the MipMap-Level to render to.
378 * @param xoffset The offset in the Source from the left.
379 * @param yoffset The offset in the Source from the top (or bottom).
380 * @param x The Offset in the Destination from the left.
381 * @param y The Offset in the Destination from the top (or bottom).
382 * @param width The width of the region to copy.
383 * @param height The height of the region to copy.
384 */
385void Material::renderToTexture(unsigned int textureNumber, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height)
386{
387  assert(textureNumber < Material::getMaxTextureUnits());
388  assert(textureNumber < this->textures.size());
389
390  // HACK
391  glActiveTexture(textureNumber);
392  glEnable(GL_TEXTURE_2D);
393  glBindTexture(GL_TEXTURE_2D, this->textures[textureNumber].getTexture());
394  glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height);
395
396}
397
398/**
399 * @brief Sets the Materials Ambient Map
400 * @todo implement this
401 */
402void Material::setAmbientMap(const std::string& aMap, GLenum target)
403{
404  /// FIXME SDL_Surface* ambientMap;
405
406}
407
408/**
409 * @brief Sets the Materials Specular Map
410 * @param sMap the Name of the Image to Use
411 * @todo implement this
412 */
413void Material::setSpecularMap(const std::string& sMap, GLenum target)
414{
415  /// FIXME SDL_Surface* specularMap;
416
417}
418
419/**
420 * @brief Sets the Materials Bumpiness
421 * @param bump the Name of the Image to Use
422 * @todo implemet this
423 */
424void Material::setBump(const std::string& bump)
425{}
426
427
428/**
429 * @returns the Maximim Texture Unit the users OpenGL-Implementation supports.
430 */
431unsigned int Material::getMaxTextureUnits()
432{
433  int maxTexUnits = 0;
434  glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTexUnits);
435  return maxTexUnits;
436}
437
438const GLenum Material::glTextureArbs[] =
439{
440  GL_TEXTURE0,
441  GL_TEXTURE1,
442  GL_TEXTURE2,
443  GL_TEXTURE3,
444  GL_TEXTURE4,
445  GL_TEXTURE5,
446  GL_TEXTURE6,
447  GL_TEXTURE7
448};
449
450
451/**
452 * @param blendFunc the Enumerator to convert to a String.
453 * @returns the matching String if found
454 */
455const std::string& Material::blendFuncToString(GLenum blendFunc)
456{
457  for (unsigned int i = 0; i < 9; ++i)
458    if (blendFunc == Material::glBlendFuncParams[i])
459      return Material::blendFuncNames[i];
460  PRINTF(2)("Supplied an Invalid Enumerator to blendfunc %d\n", blendFunc);
461  return Material::blendFuncNames[0];
462}
463
464/**
465 * @param blendFuncString the String to convert into a gl-enumeration
466 * @returns the matching GL-enumeration if found.
467 */
468GLenum Material::stringToBlendFunc(const std::string& blendFuncString)
469{
470  for (unsigned int i = 0; i < 9; ++i)
471    if (blendFuncString == Material::blendFuncNames[i])
472      return Material::glBlendFuncParams[i];
473  PRINTF(2)("BlendFunction %s not recognized using %s\n", blendFuncString.c_str(), Material::blendFuncNames[0].c_str());
474  return Material::glBlendFuncParams[0];
475}
476
477
478const GLenum Material::glBlendFuncParams[] =
479  {
480    GL_ZERO,
481    GL_ONE,
482    GL_DST_COLOR,
483    GL_ONE_MINUS_DST_COLOR,
484    GL_SRC_ALPHA,
485    GL_ONE_MINUS_SRC_ALPHA,
486    GL_DST_ALPHA,
487    GL_ONE_MINUS_DST_ALPHA,
488    GL_SRC_ALPHA_SATURATE
489  };
490
491const std::string Material::blendFuncNames[] =
492  {
493    "ZERO",
494    "ONE",
495    "DST_COLOR",
496    "ONE_MINUS_DST_COLOR",
497    "SRC_ALPHA",
498    "ONE_MINUS_SRC_ALPHA",
499    "DST_ALPHA",
500    "ONE_MINUS_DST_ALPHA",
501    "SRC_ALPHA_SATURATE"
502
503  };
Note: See TracBrowser for help on using the repository browser.