Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/water/src/lib/graphics/importer/material.cc @ 8034

Last change on this file since 8034 was 8034, checked in by stefalie, 18 years ago

water: refraction doesnt work with reflection

File size: 10.7 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  else if (likely(Material::selectedMaterial != NULL))
110  {
111  Material::unselect();
112//     for(unsigned int i = 0; i < Material::selectedMaterial->textures.size(); ++i)
113//     {
114//         glActiveTexture(Material::glTextureArbs[i]);
115//         glBindTexture(GL_TEXTURE_2D, 0);
116//         glDisable(GL_TEXTURE_2D);
117//     }
118  }
119
120
121  // setting diffuse color
122  glColor4f (diffuse[0], diffuse[1], diffuse[2], this->transparency);
123  // setting ambient color
124  glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient);
125  // setting up Sprecular
126  glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular);
127  // setting up Shininess
128  glMaterialf(GL_FRONT, GL_SHININESS, this->shininess);
129
130
131  // setting the transparency
132  if (this->transparency < 1.0 ||       /* This allows alpha blending of 2D textures with the scene */
133      (likely(!this->textures.empty() && this->textures[0].hasAlpha())))
134  {
135    glEnable(GL_BLEND);
136    glBlendFunc(this->sFactor, this->tFactor);
137  }
138  else
139  {
140    glDisable(GL_BLEND);
141  }
142
143
144  // setting illumination Model
145  if (this->illumModel == 1) //! @todo make this work, if no vertex-normals are read.
146    glShadeModel(GL_FLAT);
147  else if (this->illumModel >= 2)
148    glShadeModel(GL_SMOOTH);
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
174void Material::unselect()
175{
176  Material::selectedMaterial = NULL;
177    for(unsigned int i = 0; i < 8; ++i)
178    {
179        glActiveTexture(Material::glTextureArbs[i]);
180        glBindTexture(GL_TEXTURE_2D, 0);
181        glDisable(GL_TEXTURE_2D);
182    }
183}
184
185/**
186 *  Sets the Material Illumination Model.
187 *  illu illumination Model in int form
188 */
189void Material::setIllum (int illum)
190{
191  PRINTF(4)("setting illumModel of Material %s to %i\n", this->getName(), illum);
192  this->illumModel = illum;
193}
194
195/**
196 *  Sets the Material Illumination Model.
197 *  illu illumination Model in char* form
198 */
199void Material::setIllum (char* illum)
200{
201  this->setIllum (atoi(illum));
202}
203
204/**
205 *  Sets the Material Diffuse Color.
206 * @param r Red Color Channel.a
207 * @param g Green Color Channel.
208 * @param b Blue Color Channel.
209 */
210void Material::setDiffuse (float r, float g, float b)
211{
212  PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
213  this->diffuse[0] = r;
214  this->diffuse[1] = g;
215  this->diffuse[2] = b;
216  this->diffuse[3] = 1.0;
217
218}
219
220/**
221 *  Sets the Material Diffuse Color.
222 * @param rgb The red, green, blue channel in char format (with spaces between them)
223 */
224void Material::setDiffuse (char* rgb)
225{
226  float r,g,b;
227  sscanf (rgb, "%f %f %f", &r, &g, &b);
228  this->setDiffuse (r, g, b);
229}
230
231/**
232 *  Sets the Material Ambient Color.
233 * @param r Red Color Channel.
234 * @param g Green Color Channel.
235 * @param b Blue Color Channel.
236*/
237void Material::setAmbient (float r, float g, float b)
238{
239  PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
240  this->ambient[0] = r;
241  this->ambient[1] = g;
242  this->ambient[2] = b;
243  this->ambient[3] = 1.0;
244}
245
246/**
247 *  Sets the Material Ambient Color.
248 * @param rgb The red, green, blue channel in char format (with spaces between them)
249 */
250void Material::setAmbient (char* rgb)
251{
252  float r,g,b;
253  sscanf (rgb, "%f %f %f", &r, &g, &b);
254  this->setAmbient (r, g, b);
255}
256
257/**
258 *  Sets the Material Specular Color.
259 * @param r Red Color Channel.
260 * @param g Green Color Channel.
261 * @param b Blue Color Channel.
262 */
263void Material::setSpecular (float r, float g, float b)
264{
265  PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
266  this->specular[0] = r;
267  this->specular[1] = g;
268  this->specular[2] = b;
269  this->specular[3] = 1.0;
270}
271
272/**
273 *  Sets the Material Specular Color.
274 * @param rgb The red, green, blue channel in char format (with spaces between them)
275*/
276void Material::setSpecular (char* rgb)
277{
278  float r,g,b;
279  sscanf (rgb, "%f %f %f", &r, &g, &b);
280  this->setSpecular (r, g, b);
281}
282
283/**
284 *  Sets the Material Shininess.
285 * @param shini stes the Shininess from float.
286*/
287void Material::setShininess (float shini)
288{
289  this->shininess = shini;
290}
291/**
292 *  Sets the Material Shininess.
293 * @param shini stes the Shininess from char*.
294*/
295void Material::setShininess (char* shini)
296{
297  this->setShininess (atof(shini));
298}
299
300/**
301 *  Sets the Material Transparency.
302 * @param trans stes the Transparency from int.
303*/
304void Material::setTransparency (float trans)
305{
306  PRINTF(4)("setting Transparency of Material %s to %f.\n", this->getName(), trans);
307  this->transparency = trans;
308}
309/**
310 *  Sets the Material Transparency.
311 * @param trans stes the Transparency from char*.
312*/
313void Material::setTransparency (char* trans)
314{
315  this->setTransparency (atof(trans));
316}
317
318/**
319 *  Adds a Texture Path to the List of already existing Paths
320 * @param pathName The Path to add.
321*/
322void Material::addTexturePath(const std::string& pathName)
323{
324  ResourceManager::getInstance()->addImageDir(pathName);
325}
326
327// MAPPING //
328
329void Material::setDiffuseMap(const Texture& texture, unsigned int textureNumber)
330{
331  assert(textureNumber < Material::getMaxTextureUnits());
332
333  if (this->textures.size() <= textureNumber)
334    this->textures.resize(textureNumber+1, Texture());
335
336  //! @todo check if RESOURCE MANAGER is availiable
337  this->textures[textureNumber] = texture;
338}
339
340
341/**
342 * @brief Sets the Materials Diffuse Map
343 * @param dMap the Name of the Image to Use
344*/
345void Material::setDiffuseMap(const std::string& dMap, GLenum target, unsigned int textureNumber)
346{
347  assert(textureNumber < Material::getMaxTextureUnits());
348
349  PRINTF(5)("setting Diffuse Map %s\n", dMap.c_str());
350  if (this->textures.size() <= textureNumber)
351    this->textures.resize(textureNumber+1, Texture());
352
353  //! @todo check if RESOURCE MANAGER is availiable
354  if (!dMap.empty())
355  {
356
357    this->textures[textureNumber] = *(Texture*)ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target);
358  }
359  else
360  {
361    this->textures[textureNumber] = Texture();
362  }
363}
364
365/**
366 * @brief Sets the Materials Diffuse Map
367 * @param surface pointer to SDL_Surface which shall be used as texture
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 viewport buffer (?? or another buffer ;-)) to a texture
390 * @param textureNumber select the texture unit that will be overwritten
391*/
392void Material::renderToTexture(unsigned int textureNumber, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height)
393{
394  glActiveTexture(Material::glTextureArbs[textureNumber]);
395  glEnable(GL_TEXTURE_2D);
396  glBindTexture(GL_TEXTURE_2D, this->textures[textureNumber].getTexture());
397  glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height);
398}
399
400/**
401 * @brief Sets the Materials Ambient Map
402 * @todo implement this
403*/
404void Material::setAmbientMap(const std::string& aMap, GLenum target)
405{
406  SDL_Surface* ambientMap;
407
408}
409
410/**
411 * @brief Sets the Materials Specular Map
412 * @param sMap the Name of the Image to Use
413   @todo implement this
414*/
415void Material::setSpecularMap(const std::string& sMap, GLenum target)
416{
417  SDL_Surface* specularMap;
418
419}
420
421/**
422 * @brief Sets the Materials Bumpiness
423 * @param bump the Name of the Image to Use
424 * @todo implemet this
425*/
426void Material::setBump(const std::string& bump)
427{
428}
429
430
431
432int Material::getMaxTextureUnits()
433{
434  int maxTexUnits = 0;
435  glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTexUnits);
436  return maxTexUnits;
437}
Note: See TracBrowser for help on using the repository browser.