Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: removed stupid included in base_object.h
this should lead to faster compile-times

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