Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/atmospheric_engine/src/lib/graphics/importer/material.cc @ 7909

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

orxonox/trunk: Material handles references… lets see if this works

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