Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/graphics/importer/material.cc @ 9785

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

some implementations of the new self sustained Resources. It works, but does not yet load the correct stuff

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