Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: no more seg-fault when copying a Texture

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