Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

material updated, so now select first deselects tho old Material

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