Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

doxy

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 "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/**
176 * @brief Deselect Material (if one is selected).
177 */
178void Material::unselect()
179{
180  Material::selectedMaterial = NULL;
181    for(unsigned int i = 0; i < 8; ++i)
182    {
183        glActiveTexture(Material::glTextureArbs[i]);
184        glBindTexture(GL_TEXTURE_2D, 0);
185        glDisable(GL_TEXTURE_2D);
186    }
187}
188
189/**
190 * @brief Sets the Material Illumination Model.
191 * @param illu illumination Model in int form
192 */
193void Material::setIllum (int illum)
194{
195  PRINTF(4)("setting illumModel of Material %s to %i\n", this->getName(), illum);
196  this->illumModel = illum;
197}
198
199/**
200 * @brief Sets the Material Diffuse Color.
201 * @param r Red Color Channel.a
202 * @param g Green Color Channel.
203 * @param b Blue Color Channel.
204 */
205void Material::setDiffuse (float r, float g, float b)
206{
207  PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
208  this->diffuse[0] = r;
209  this->diffuse[1] = g;
210  this->diffuse[2] = b;
211  this->diffuse[3] = 1.0;
212
213}
214
215
216/**
217 * @brief Sets the Material Ambient Color.
218 * @param r Red Color Channel.
219 * @param g Green Color Channel.
220 * @param b Blue Color Channel.
221*/
222void Material::setAmbient (float r, float g, float b)
223{
224  PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
225  this->ambient[0] = r;
226  this->ambient[1] = g;
227  this->ambient[2] = b;
228  this->ambient[3] = 1.0;
229}
230
231/**
232 * @brief Sets the Material Specular Color.
233 * @param r Red Color Channel.
234 * @param g Green Color Channel.
235 * @param b Blue Color Channel.
236 */
237void Material::setSpecular (float r, float g, float b)
238{
239  PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
240  this->specular[0] = r;
241  this->specular[1] = g;
242  this->specular[2] = b;
243  this->specular[3] = 1.0;
244}
245
246/**
247 * @brief Sets the Material Shininess.
248 * @param shini stes the Shininess from float.
249*/
250void Material::setShininess (float shini)
251{
252  this->shininess = shini;
253}
254
255/**
256 * @brief Sets the Material Transparency.
257 * @param trans stes the Transparency from int.
258*/
259void Material::setTransparency (float trans)
260{
261  PRINTF(4)("setting Transparency of Material %s to %f.\n", this->getName(), trans);
262  this->transparency = trans;
263}
264
265/**
266 * @brief Adds a Texture Path to the List of already existing Paths
267 * @param pathName The Path to add.
268*/
269void Material::addTexturePath(const std::string& pathName)
270{
271  ResourceManager::getInstance()->addImageDir(pathName);
272}
273
274// MAPPING //
275
276
277/**
278 * @brief Sets the Diffuse map of this Texture.
279 * @param texture The Texture to load
280 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS
281 */
282void Material::setDiffuseMap(const Texture& texture, unsigned int textureNumber)
283{
284  assert(textureNumber < Material::getMaxTextureUnits());
285
286  if (this->textures.size() <= textureNumber)
287    this->textures.resize(textureNumber+1, Texture());
288
289  //! @todo check if RESOURCE MANAGER is availiable
290  this->textures[textureNumber] = texture;
291}
292
293
294/**
295 * @brief Sets the Materials Diffuse Map
296 * @param dMap the Name of the Image to Use
297 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS
298 */
299void Material::setDiffuseMap(const std::string& dMap, GLenum target, unsigned int textureNumber)
300{
301  assert(textureNumber < Material::getMaxTextureUnits());
302
303  PRINTF(5)("setting Diffuse Map %s\n", dMap.c_str());
304  if (this->textures.size() <= textureNumber)
305    this->textures.resize(textureNumber+1, Texture());
306
307  //! @todo check if RESOURCE MANAGER is availiable
308  if (!dMap.empty())
309  {
310    Texture* tex = dynamic_cast<Texture*>(ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target));
311    if (tex != NULL)
312    this->textures[textureNumber] = *tex;
313    else
314      this->textures[textureNumber] = Texture();
315  }
316  else
317  {
318    this->textures[textureNumber] = Texture();
319  }
320}
321
322/**
323 * @brief Sets the Materials Diffuse Map
324 * @param surface pointer to SDL_Surface which shall be used as texture.
325 * @param target the GL-Target to load the Surface to.
326 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS.
327 */
328void Material::setSDLDiffuseMap(SDL_Surface *surface, GLenum target, unsigned int textureNumber)
329{
330  assert(textureNumber < Material::getMaxTextureUnits());
331
332
333  if (this->textures.size() <= textureNumber)
334    this->textures.resize(textureNumber+1, Texture());
335
336  if(surface != NULL)
337  {
338    this->textures[textureNumber] = Texture(surface, GL_TEXTURE_2D);
339  }
340  else
341  {
342    this->textures[textureNumber] = Texture();
343  }
344
345}
346
347/**
348 * @brief Renders to a Texture.
349 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS.
350 * @param target The GL-Target.
351 * @param level the MipMap-Level to render to.
352 * @param xoffset The offset in the Source from the left.
353 * @param yoffset The offset in the Source from the top (or bottom).
354 * @param x The Offset in the Destination from the left.
355 * @param y The Offset in the Destination from the top (or bottom).
356 * @param width The width of the region to copy.
357 * @param height The height of the region to copy.
358 */
359void Material::renderToTexture(unsigned int textureNumber, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height)
360{
361  assert(textureNumber < Material::getMaxTextureUnits());
362  assert(textureNumber < this->textures.size());
363
364  // HACK
365  glActiveTexture(textureNumber);
366   glEnable(GL_TEXTURE_2D);
367   glBindTexture(GL_TEXTURE_2D, this->textures[textureNumber].getTexture());
368  glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height);
369
370}
371
372/**
373 * @brief Sets the Materials Ambient Map
374 * @todo implement this
375*/
376void Material::setAmbientMap(const std::string& aMap, GLenum target)
377{
378  /// FIXME SDL_Surface* ambientMap;
379
380}
381
382/**
383 * @brief Sets the Materials Specular Map
384 * @param sMap the Name of the Image to Use
385   @todo implement this
386*/
387void Material::setSpecularMap(const std::string& sMap, GLenum target)
388{
389  /// FIXME SDL_Surface* specularMap;
390
391}
392
393/**
394 * @brief Sets the Materials Bumpiness
395 * @param bump the Name of the Image to Use
396 * @todo implemet this
397*/
398void Material::setBump(const std::string& bump)
399{
400}
401
402
403/**
404 * @returns the Maximim Texture Unit the users OpenGL-Implementation supports.
405 */
406unsigned int Material::getMaxTextureUnits()
407{
408  int maxTexUnits = 0;
409  glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTexUnits);
410  return maxTexUnits;
411}
Note: See TracBrowser for help on using the repository browser.