Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/graphics/importer/material.cc @ 7525

Last change on this file since 7525 was 7525, checked in by stefalie, 18 years ago

branches/bsp_model: setSDLSurfaceDiffuseMap added to Material

File size: 10.2 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#include <stdlib.h>
25#include <string.h>
26
27//! @todo check if we are in RESOURCE MANAGER-mode
28#include "util/loading/resource_manager.h"
29
30using namespace std;
31
32/**
33 * creates a Material.
34 * @param mtlName Name of the Material to be added to the Material List
35 */
36Material::Material (const std::string& mtlName)
37{
38  this->setClassID(CL_MATERIAL, "Material");
39
40  this->setIllum(3);
41  this->setDiffuse(1,1,1);
42  this->setAmbient(0,0,0);
43  this->setSpecular(.5,.5,.5);
44  this->setShininess(2.0);
45  this->setTransparency(1.0);
46
47  this->ambientTexture = NULL;
48  this->specularTexture = NULL;
49  this->sFactor = GL_SRC_ALPHA;
50  this->tFactor = GL_ONE;
51
52  this->setName(mtlName);
53}
54
55/**
56  *  deletes a Material
57*/
58Material::~Material()
59{
60  PRINTF(5)("delete Material %s.\n", this->getName());
61
62  while(!this->textures.empty())
63  {
64    if (this->textures.back() != NULL)
65      ResourceManager::getInstance()->unload(this->textures.back());
66    this->textures.pop_back();
67  }
68
69  if (this->ambientTexture != NULL)
70    ResourceManager::getInstance()->unload(this->ambientTexture);
71  if (this->specularTexture != NULL)
72    ResourceManager::getInstance()->unload(this->specularTexture);
73
74  if (this == Material::selectedMaterial)
75    Material::selectedMaterial = NULL;
76}
77
78
79Material* Material::selectedMaterial = NULL;
80
81const GLenum Material::glTextureArbs[] =
82{
83  GL_TEXTURE0_ARB,
84  GL_TEXTURE1_ARB,
85  GL_TEXTURE2_ARB,
86  GL_TEXTURE3_ARB,
87  GL_TEXTURE4_ARB,
88  GL_TEXTURE5_ARB,
89  GL_TEXTURE6_ARB,
90  GL_TEXTURE7_ARB
91};
92
93
94/// TODO FIX THIS
95// Material& Material::operator=(const Material& m)
96// {
97//   this->setIllum(m.illumModel);
98//   this->setDiffuse(m.diffuse[0],m.diffuse[1],m.diffuse[2]);
99//   this->setAmbient(m.ambient[0],m.ambient[1],m.ambient[2]);
100//   this->setSpecular(m.specular[0],m.specular[1],m.specular[2]);
101//   this->setShininess(m.shininess);
102//   this->setTransparency(m.transparency);
103//
104//   if (this->diffuseTexture != NULL)
105//     ResourceManager::getInstance()->unload(this->diffuseTexture);
106//   if (m.diffuseTexture != NULL)
107//     this->diffuseTexture = (Texture*)ResourceManager::getInstance()->copy(m.diffuseTexture);
108//   this->ambientTexture = NULL; /// FIXME
109//   this->specularTexture = NULL; /// FIXME
110//
111//   this->setName(m.getName());
112// }
113
114
115
116/**
117 *  sets the material with which the following Faces will be painted
118 */
119bool Material::select() const
120{
121  if (unlikely(this == Material::selectedMaterial))
122      return true;
123
124
125  // setting diffuse color
126  glColor4f (diffuse[0], diffuse[1], diffuse[2], this->transparency);
127  // setting ambient color
128  glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient);
129  // setting up Sprecular
130  glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular);
131  // setting up Shininess
132  glMaterialf(GL_FRONT, GL_SHININESS, this->shininess);
133
134
135  // setting the transparency
136  if (this->transparency < 1.0 ||       /* This allows alpha blending of 2D textures with the scene */
137      (likely(!this->textures.empty() && this->textures[0] != NULL) && this->textures[0]->hasAlpha()))
138  {
139    glEnable(GL_BLEND);
140    glBlendFunc(this->sFactor, this->tFactor);
141  }
142  else
143  {
144    glDisable(GL_BLEND);
145  }
146
147
148  // setting illumination Model
149  if (this->illumModel == 1) //! @todo make this work, if no vertex-normals are read.
150    glShadeModel(GL_FLAT);
151  else if (this->illumModel >= 2)
152    glShadeModel(GL_SMOOTH);
153
154  if (likely(Material::selectedMaterial != NULL))
155  {
156    for(unsigned int i = 0; i < Material::selectedMaterial->textures.size(); ++i)
157    {
158        glActiveTexture(Material::glTextureArbs[i]);
159        glBindTexture(GL_TEXTURE_2D, 0);
160    }
161  }
162
163  for(unsigned int i = 0; i < this->textures.size(); ++i)
164  {
165    if (likely(this->textures[i] != NULL))
166    {
167      glActiveTexture(Material::glTextureArbs[i]);
168      glEnable(GL_TEXTURE_2D);
169      if(this->textures[i]->hasAlpha())
170      {
171        glEnable(GL_BLEND);
172      }
173      glBindTexture(GL_TEXTURE_2D, this->textures[i]->getTexture());
174    }
175  }
176
177  /*  if (this->diffuseTexture != NULL)
178      {
179        glEnable(GL_TEXTURE_2D);
180        glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture());
181      }
182    else
183      {
184        glDisable(GL_TEXTURE_2D);
185        glBindTexture(GL_TEXTURE_2D, 0);
186      }*/
187}
188
189/**
190 *  Sets the Material Illumination Model.
191 *  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 *  Sets the Material Illumination Model.
201 *  illu illumination Model in char* form
202 */
203void Material::setIllum (char* illum)
204{
205  this->setIllum (atoi(illum));
206}
207
208/**
209 *  Sets the Material Diffuse Color.
210 * @param r Red Color Channel.a
211 * @param g Green Color Channel.
212 * @param b Blue Color Channel.
213 */
214void Material::setDiffuse (float r, float g, float b)
215{
216  PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
217  this->diffuse[0] = r;
218  this->diffuse[1] = g;
219  this->diffuse[2] = b;
220  this->diffuse[3] = 1.0;
221
222}
223
224/**
225 *  Sets the Material Diffuse Color.
226 * @param rgb The red, green, blue channel in char format (with spaces between them)
227 */
228void Material::setDiffuse (char* rgb)
229{
230  float r,g,b;
231  sscanf (rgb, "%f %f %f", &r, &g, &b);
232  this->setDiffuse (r, g, b);
233}
234
235/**
236 *  Sets the Material Ambient Color.
237 * @param r Red Color Channel.
238 * @param g Green Color Channel.
239 * @param b Blue Color Channel.
240*/
241void Material::setAmbient (float r, float g, float b)
242{
243  PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
244  this->ambient[0] = r;
245  this->ambient[1] = g;
246  this->ambient[2] = b;
247  this->ambient[3] = 1.0;
248}
249
250/**
251 *  Sets the Material Ambient Color.
252 * @param rgb The red, green, blue channel in char format (with spaces between them)
253 */
254void Material::setAmbient (char* rgb)
255{
256  float r,g,b;
257  sscanf (rgb, "%f %f %f", &r, &g, &b);
258  this->setAmbient (r, g, b);
259}
260
261/**
262 *  Sets the Material Specular Color.
263 * @param r Red Color Channel.
264 * @param g Green Color Channel.
265 * @param b Blue Color Channel.
266 */
267void Material::setSpecular (float r, float g, float b)
268{
269  PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
270  this->specular[0] = r;
271  this->specular[1] = g;
272  this->specular[2] = b;
273  this->specular[3] = 1.0;
274}
275
276/**
277 *  Sets the Material Specular Color.
278 * @param rgb The red, green, blue channel in char format (with spaces between them)
279*/
280void Material::setSpecular (char* rgb)
281{
282  float r,g,b;
283  sscanf (rgb, "%f %f %f", &r, &g, &b);
284  this->setSpecular (r, g, b);
285}
286
287/**
288 *  Sets the Material Shininess.
289 * @param shini stes the Shininess from float.
290*/
291void Material::setShininess (float shini)
292{
293  this->shininess = shini;
294}
295/**
296 *  Sets the Material Shininess.
297 * @param shini stes the Shininess from char*.
298*/
299void Material::setShininess (char* shini)
300{
301  this->setShininess (atof(shini));
302}
303
304/**
305 *  Sets the Material Transparency.
306 * @param trans stes the Transparency from int.
307*/
308void Material::setTransparency (float trans)
309{
310  PRINTF(4)("setting Transparency of Material %s to %f.\n", this->getName(), trans);
311  this->transparency = trans;
312}
313/**
314 *  Sets the Material Transparency.
315 * @param trans stes the Transparency from char*.
316*/
317void Material::setTransparency (char* trans)
318{
319  this->setTransparency (atof(trans));
320}
321
322/**
323 *  Adds a Texture Path to the List of already existing Paths
324 * @param pathName The Path to add.
325*/
326void Material::addTexturePath(const std::string& pathName)
327{
328  ResourceManager::getInstance()->addImageDir(pathName);
329}
330
331// MAPPING //
332
333/**
334 *  Sets the Materials Diffuse Map
335 * @param dMap the Name of the Image to Use
336*/
337void Material::setDiffuseMap(const std::string& dMap, GLenum target, unsigned int textureNumber)
338{
339  assert(textureNumber < Material::getMaxTextureUnits());
340
341  PRINTF(5)("setting Diffuse Map %s\n", dMap);
342  if (this->textures.size() > textureNumber && this->textures[textureNumber] != NULL)
343    ResourceManager::getInstance()->unload(this->textures[textureNumber]);
344
345  if (this->textures.size() <= textureNumber)
346    this->textures.resize(textureNumber+1, NULL);
347
348  //! @todo check if RESOURCE MANAGER is availiable
349  if (!dMap.empty())
350  {
351
352    this->textures[textureNumber] = (Texture*)ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target);
353  }
354  else
355  {
356    this->textures[textureNumber] = NULL;
357  }
358}
359
360/**
361 *  Sets the Materials Diffuse Map
362 * @param surface pointer to SDL_Surface which shall be used as texture
363*/
364void Material::setSDLDiffuseMap(SDL_Surface *surface, GLenum target, unsigned int textureNumber)
365{
366  assert(textureNumber < Material::getMaxTextureUnits());
367
368 
369  if (this->textures.size() > textureNumber && this->textures[textureNumber] != NULL)
370    ResourceManager::getInstance()->unload(this->textures[textureNumber]);
371
372  if (this->textures.size() <= textureNumber)
373    this->textures.resize(textureNumber+1, NULL);
374
375  if(surface != NULL)
376  {
377    this->textures[textureNumber] = new Texture(surface, GL_TEXTURE_2D);
378  }
379  else
380  {
381    this->textures[textureNumber] = NULL;
382  }
383
384}
385
386
387/**
388 *  Sets the Materials Ambient Map
389 * @param aMap the Name of the Image to Use
390   @todo implement this
391*/
392void Material::setAmbientMap(const std::string& aMap, GLenum target)
393{
394  SDL_Surface* ambientMap;
395
396}
397
398/**
399 *  Sets the Materials Specular Map
400 * @param sMap the Name of the Image to Use
401   @todo implement this
402*/
403void Material::setSpecularMap(const std::string& sMap, GLenum target)
404{
405  SDL_Surface* specularMap;
406
407}
408
409/**
410 *  Sets the Materials Bumpiness
411 * @param bump the Name of the Image to Use
412   @todo implemet this
413*/
414void Material::setBump(const std::string& bump)
415{
416}
417
418
419
420int Material::getMaxTextureUnits()
421{
422  int maxTexUnits = 0;
423  glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTexUnits);
424  return maxTexUnits;
425}
Note: See TracBrowser for help on using the repository browser.