Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/current_cd/src/lib/graphics/importer/material.cc @ 6903

Last change on this file since 6903 was 6812, checked in by bensch, 19 years ago

trunk: Particles should work again

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