Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/bsp_model: Material should support multiple Textures now

File size: 9.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#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
75
76/// TODO FIX THIS
77// Material& Material::operator=(const Material& m)
78// {
79//   this->setIllum(m.illumModel);
80//   this->setDiffuse(m.diffuse[0],m.diffuse[1],m.diffuse[2]);
81//   this->setAmbient(m.ambient[0],m.ambient[1],m.ambient[2]);
82//   this->setSpecular(m.specular[0],m.specular[1],m.specular[2]);
83//   this->setShininess(m.shininess);
84//   this->setTransparency(m.transparency);
85//
86//   if (this->diffuseTexture != NULL)
87//     ResourceManager::getInstance()->unload(this->diffuseTexture);
88//   if (m.diffuseTexture != NULL)
89//     this->diffuseTexture = (Texture*)ResourceManager::getInstance()->copy(m.diffuseTexture);
90//   this->ambientTexture = NULL; /// FIXME
91//   this->specularTexture = NULL; /// FIXME
92//
93//   this->setName(m.getName());
94// }
95
96const GLenum Material::glTextureArbs[] =
97  {
98    GL_TEXTURE0_ARB,
99    GL_TEXTURE1_ARB,
100    GL_TEXTURE2_ARB,
101    GL_TEXTURE3_ARB,
102    GL_TEXTURE4_ARB,
103    GL_TEXTURE5_ARB,
104    GL_TEXTURE6_ARB,
105    GL_TEXTURE7_ARB
106  };
107
108
109/**
110 *  sets the material with which the following Faces will be painted
111 */
112bool Material::select() const
113{
114  // setting diffuse color
115  glColor4f (diffuse[0], diffuse[1], diffuse[2], this->transparency);
116  // setting ambient color
117  glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient);
118  // setting up Sprecular
119  glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular);
120  // setting up Shininess
121  glMaterialf(GL_FRONT, GL_SHININESS, this->shininess);
122
123
124  // setting the transparency
125  if (this->transparency < 1.0 ||       /* This allows alpha blending of 2D textures with the scene */
126      (likely(!this->textures.empty() && this->textures[0] != NULL) && this->textures[0]->hasAlpha()))
127  {
128    glEnable(GL_BLEND);
129    glBlendFunc(this->sFactor, this->tFactor);
130  }
131  else
132  {
133    glDisable(GL_BLEND);
134  }
135
136
137  // setting illumination Model
138  if (this->illumModel == 1) //! @todo make this work, if no vertex-normals are read.
139    glShadeModel(GL_FLAT);
140  else if (this->illumModel >= 2)
141    glShadeModel(GL_SMOOTH);
142
143
144  for(unsigned int i = 0; i < this->textures.size(); ++i)
145  {
146    if (likely(this->textures[i] != NULL))
147    {
148      glActiveTexture(Material::glTextureArbs[i]);
149      glEnable(GL_TEXTURE_2D);
150      glBindTexture(GL_TEXTURE_2D, this->textures[i]->getTexture());
151    }
152  }
153
154  /*  if (this->diffuseTexture != NULL)
155      {
156        glEnable(GL_TEXTURE_2D);
157        glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture());
158      }
159    else
160      {
161        glDisable(GL_TEXTURE_2D);
162        glBindTexture(GL_TEXTURE_2D, 0);
163      }*/
164}
165
166/**
167 *  Sets the Material Illumination Model.
168 *  illu illumination Model in int form
169 */
170void Material::setIllum (int illum)
171{
172  PRINTF(4)("setting illumModel of Material %s to %i\n", this->getName(), illum);
173  this->illumModel = illum;
174}
175
176/**
177 *  Sets the Material Illumination Model.
178 *  illu illumination Model in char* form
179 */
180void Material::setIllum (char* illum)
181{
182  this->setIllum (atoi(illum));
183}
184
185/**
186 *  Sets the Material Diffuse Color.
187 * @param r Red Color Channel.a
188 * @param g Green Color Channel.
189 * @param b Blue Color Channel.
190 */
191void Material::setDiffuse (float r, float g, float b)
192{
193  PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
194  this->diffuse[0] = r;
195  this->diffuse[1] = g;
196  this->diffuse[2] = b;
197  this->diffuse[3] = 1.0;
198
199}
200
201/**
202 *  Sets the Material Diffuse Color.
203 * @param rgb The red, green, blue channel in char format (with spaces between them)
204 */
205void Material::setDiffuse (char* rgb)
206{
207  float r,g,b;
208  sscanf (rgb, "%f %f %f", &r, &g, &b);
209  this->setDiffuse (r, g, b);
210}
211
212/**
213 *  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[0] = r;
222  this->ambient[1] = g;
223  this->ambient[2] = b;
224  this->ambient[3] = 1.0;
225}
226
227/**
228 *  Sets the Material Ambient Color.
229 * @param rgb The red, green, blue channel in char format (with spaces between them)
230 */
231void Material::setAmbient (char* rgb)
232{
233  float r,g,b;
234  sscanf (rgb, "%f %f %f", &r, &g, &b);
235  this->setAmbient (r, g, b);
236}
237
238/**
239 *  Sets the Material Specular Color.
240 * @param r Red Color Channel.
241 * @param g Green Color Channel.
242 * @param b Blue Color Channel.
243 */
244void Material::setSpecular (float r, float g, float b)
245{
246  PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
247  this->specular[0] = r;
248  this->specular[1] = g;
249  this->specular[2] = b;
250  this->specular[3] = 1.0;
251}
252
253/**
254 *  Sets the Material Specular Color.
255 * @param rgb The red, green, blue channel in char format (with spaces between them)
256*/
257void Material::setSpecular (char* rgb)
258{
259  float r,g,b;
260  sscanf (rgb, "%f %f %f", &r, &g, &b);
261  this->setSpecular (r, g, b);
262}
263
264/**
265 *  Sets the Material Shininess.
266 * @param shini stes the Shininess from float.
267*/
268void Material::setShininess (float shini)
269{
270  this->shininess = shini;
271}
272/**
273 *  Sets the Material Shininess.
274 * @param shini stes the Shininess from char*.
275*/
276void Material::setShininess (char* shini)
277{
278  this->setShininess (atof(shini));
279}
280
281/**
282 *  Sets the Material Transparency.
283 * @param trans stes the Transparency from int.
284*/
285void Material::setTransparency (float trans)
286{
287  PRINTF(4)("setting Transparency of Material %s to %f.\n", this->getName(), trans);
288  this->transparency = trans;
289}
290/**
291 *  Sets the Material Transparency.
292 * @param trans stes the Transparency from char*.
293*/
294void Material::setTransparency (char* trans)
295{
296  this->setTransparency (atof(trans));
297}
298
299/**
300 *  Adds a Texture Path to the List of already existing Paths
301 * @param pathName The Path to add.
302*/
303void Material::addTexturePath(const std::string& pathName)
304{
305  ResourceManager::getInstance()->addImageDir(pathName);
306}
307
308// MAPPING //
309
310/**
311 *  Sets the Materials Diffuse Map
312 * @param dMap the Name of the Image to Use
313*/
314void Material::setDiffuseMap(const std::string& dMap, GLenum target, unsigned int textureNumber)
315{
316  assert(textureNumber < Material::getMaxTextureUnits());
317
318  PRINTF(5)("setting Diffuse Map %s\n", dMap);
319  if (this->textures.size() > textureNumber && this->textures[textureNumber] != NULL)
320    ResourceManager::getInstance()->unload(this->textures[textureNumber]);
321
322  if (this->textures.size() <= textureNumber)
323    this->textures.resize(textureNumber+1, NULL);
324
325  //! @todo check if RESOURCE MANAGER is availiable
326  if (!dMap.empty())
327  {
328
329    this->textures[textureNumber] = (Texture*)ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target);
330  }
331  else
332  {
333    this->textures[textureNumber] = NULL;
334  }
335}
336
337/**
338 *  Sets the Materials Ambient Map
339 * @param aMap the Name of the Image to Use
340   @todo implement this
341*/
342void Material::setAmbientMap(const std::string& aMap, GLenum target)
343{
344  SDL_Surface* ambientMap;
345
346}
347
348/**
349 *  Sets the Materials Specular Map
350 * @param sMap the Name of the Image to Use
351   @todo implement this
352*/
353void Material::setSpecularMap(const std::string& sMap, GLenum target)
354{
355  SDL_Surface* specularMap;
356
357}
358
359/**
360 *  Sets the Materials Bumpiness
361 * @param bump the Name of the Image to Use
362   @todo implemet this
363*/
364void Material::setBump(const std::string& bump)
365{
366}
367
368
369
370int Material::getMaxTextureUnits()
371{
372  int maxTexUnits = 0;
373  glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTexUnits);
374  return maxTexUnits;
375}
Note: See TracBrowser for help on using the repository browser.