Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8325 was 8316, checked in by bensch, 19 years ago

trunk: fixed most -Wall warnings… but there are still many missing :/

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