Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/graphics/importer/material.cc @ 4369

Last change on this file since 4369 was 4357, checked in by bensch, 20 years ago

orxonox/trunk: now Textures are maped as they should
before this textures where switched upside-down, now this is done in the corresponding model's textureCoordinate

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