Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/graphics/importer/material.cc @ 7992

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

trunk: reverted change

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