Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7465 was 7465, checked in by bottac, 18 years ago

Experimental lightmapping. Screenshots: http://people.ee.ethz.ch/~bottac/lightmap_test/

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