Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/atmospheric_engine/src/lib/graphics/importer/material.cc @ 8267

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

trunk: merged the water back
merged with command
svn merge -r7798:HEAD https://svn.orxonox.net/orxonox/branches/water .

conflicts are all resolved

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