Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3657 was 3655, checked in by bensch, 21 years ago

orxonox/trunk: moved protoclass to folder proto
added protosingleton
added resourceManager
modiefied some stuff to work better

File size: 8.5 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 <stdlib.h>
24#include <string.h>
25
26//! \todo check if we are in RESOURCE MANAGER-mode
27#include "resource_manager.h"
28
29using namespace std;
30
31
32/**
33   \brief creates a default Material with no Name
34   normally you call this to create a material List (for an obj-file) and then append with addMaterial()
35*/
36Material::Material()
37{
38  this->init();
39 
40  this->setName ("");
41}
42
43/**
44   \brief creates a Material.
45   \param mtlName Name of the Material to be added to the Material List
46*/
47Material::Material (char* mtlName)
48{
49  this->init();
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    this->diffuseTexture;
64  if (this->nextMat)
65    delete this->nextMat;
66}
67
68/**
69   \brief adds a new Material to the List.
70   this Function will append a new Material to the end of a Material List.
71   \param mtlName The name of the Material to be added.
72*/
73Material* Material::addMaterial(char* mtlName)
74{
75  PRINTF(4)("adding Material %s.\n", mtlName);
76   Material* tmpMat = this;
77  while (tmpMat->nextMat != NULL)
78    {
79      tmpMat = tmpMat->nextMat;
80    }
81  tmpMat->nextMat = new Material(mtlName);
82  return tmpMat->nextMat;
83 
84}
85
86/**
87   \brief initializes a new Material with its default Values
88*/
89void Material::init(void)
90{
91  PRINTF(4)("initializing new Material.\n");
92  this->nextMat = NULL;
93  this->name ="";
94  this->setIllum(1);
95  this->setDiffuse(0,0,0);
96  this->setAmbient(0,0,0);
97  this->setSpecular(.5,.5,.5);
98  this->setShininess(2.0);
99  this->setTransparency(0.0);
100
101
102  this->diffuseTexture = NULL;
103  this->ambientTexture = NULL;
104  this->specularTexture = NULL;
105
106  this->diffuseTextureSet = false;
107  this->ambientTextureSet = false;
108  this->specularTextureSet = false;
109}
110
111/**
112   \brief Search for a Material called mtlName
113   \param mtlName the Name of the Material to search for
114   \returns Material named mtlName if it is found. NULL otherwise.
115*/
116Material* Material::search(char* mtlName)
117{
118  PRINTF(5)("Searching for material %s", mtlName);
119  Material* searcher = this;
120  while (searcher != NULL)
121    {
122      PRINT(5)(".");
123      if (!strcmp (searcher->getName(), mtlName))
124        {
125          PRINT(5)("found.\n");
126          return searcher;
127        }
128      searcher = searcher->nextMat;
129    }
130  PRINT(2)("material %s not found\n", mtlName);
131  return NULL;
132}
133
134/**
135   \brief sets the material with which the following Faces will be painted
136*/
137bool Material::select (void)
138{
139  // setting diffuse color
140  //  glColor3f (diffuse[0], diffuse[1], diffuse[2]);
141  glMaterialfv(GL_FRONT, GL_DIFFUSE, this->diffuse);
142
143  // setting ambient color
144  glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient);
145
146  // setting up Sprecular
147  glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular);
148
149  // setting up Shininess
150  glMaterialf(GL_FRONT, GL_SHININESS, this->shininess);
151 
152  // setting illumination Model
153  if (this->illumModel == 1) //! \todo make this work, if no vertex-normals are read.
154    glShadeModel(GL_FLAT);
155  else if (this->illumModel >= 2)
156    glShadeModel(GL_SMOOTH);
157
158  if (this->diffuseTextureSet)
159    {
160      glEnable(GL_TEXTURE_2D);
161      glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture());
162    }
163  else
164    {
165      glDisable(GL_TEXTURE_2D);
166      glBindTexture(GL_TEXTURE_2D, 0);
167    }
168}
169
170
171/**
172   \brief Set the Name of the Material. (Important for searching)
173   \param mtlName the Name of the Material to be set.
174*/ 
175void Material::setName (char* mtlName)
176{
177  PRINTF(4)("setting Material Name to %s.\n", this->name);
178  this->name = new char [strlen(mtlName)+1];
179  strcpy(this->name, mtlName);
180}
181
182/**
183   \returns The Name of The Material
184*/
185char* Material::getName (void)
186{
187  return this->name;
188}
189
190/**
191   \brief Sets the Material Illumination Model.
192   \brief illu illumination Model in int form
193*/
194void Material::setIllum (int illum)
195{
196  PRINTF(4)("setting illumModel of Material %s to %i\n", this->name, illum);
197  this->illumModel = illum;
198}
199/**
200   \brief Sets the Material Illumination Model.
201   \brief illu illumination Model in char* form
202*/void Material::setIllum (char* illum)
203{
204  this->setIllum (atoi(illum));
205}
206
207/**
208   \brief Sets the Material Diffuse Color.
209   \param r Red Color Channel.
210   \param g Green Color Channel.
211   \param b Blue Color Channel.
212*/
213void Material::setDiffuse (float r, float g, float b)
214{
215  PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b);
216  this->diffuse[0] = r;
217  this->diffuse[1] = g;
218  this->diffuse[2] = b; 
219  this->diffuse[3] = 1.0;
220
221}
222/**
223   \brief Sets the Material Diffuse Color.
224   \param rgb The red, green, blue channel in char format (with spaces between them)
225*/
226void Material::setDiffuse (char* rgb)
227{
228  float r,g,b;
229  sscanf (rgb, "%f %f %f", &r, &g, &b);
230  this->setDiffuse (r, g, b);
231}
232
233/**
234   \brief Sets the Material Ambient Color.
235   \param r Red Color Channel.
236   \param g Green Color Channel.
237   \param b Blue Color Channel.
238*/
239void Material::setAmbient (float r, float g, float b)
240{
241  PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b);
242  this->ambient[0] = r;
243  this->ambient[1] = g;
244  this->ambient[2] = b;
245  this->ambient[3] = 1.0;
246}
247/**
248   \brief Sets the Material Ambient Color.
249   \param rgb The red, green, blue channel in char format (with spaces between them)
250*/
251void Material::setAmbient (char* rgb)
252{
253  float r,g,b;
254  sscanf (rgb, "%f %f %f", &r, &g, &b);
255  this->setAmbient (r, g, b);
256}
257
258/**
259   \brief Sets the Material Specular Color.
260   \param r Red Color Channel.
261   \param g Green Color Channel.
262   \param b Blue Color Channel.
263*/
264void Material::setSpecular (float r, float g, float b)
265{
266  PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b);
267  this->specular[0] = r;
268  this->specular[1] = g;
269  this->specular[2] = b;
270  this->specular[3] = 1.0;
271 }
272/**
273   \brief 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   \brief 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   \brief 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   \brief 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->name, trans);
307  this->transparency = trans;
308}
309/**
310   \brief 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   \brief Adds a Texture Path to the List of already existing Paths
320   \param pathName The Path to add.
321*/
322void Material::addTexturePath(char* pathName)
323{
324  PathList::getInstance()->addPath(pathName);
325}
326
327// MAPPING //
328
329/**
330   \brief Sets the Materials Diffuse Map
331   \param dMap the Name of the Image to Use
332*/
333void Material::setDiffuseMap(char* dMap)
334{
335  PRINTF(4)("setting Diffuse Map %s\n", dMap);
336    diffuseTexture = new Texture();
337    this->diffuseTextureSet = diffuseTexture->loadImage(dMap);
338
339  //! \todo check if RESOURCE MANAGER is availiable
340  //this->diffuseTextureSet = this->diffuseTexture = (Texture*)ResourceManager::load(dMap, IMAGE);
341}
342
343/**
344   \brief Sets the Materials Ambient Map
345   \param aMap the Name of the Image to Use
346   \todo implement this
347*/
348void Material::setAmbientMap(char* aMap)
349{
350  SDL_Surface* ambientMap;
351
352}
353
354/**
355   \brief Sets the Materials Specular Map
356   \param sMap the Name of the Image to Use
357   \todo implement this
358*/
359void Material::setSpecularMap(char* sMap)
360{
361  SDL_Surface* specularMap;
362
363}
364
365/**
366   \brief Sets the Materials Bumpiness
367   \param bump the Name of the Image to Use
368   \todo implemet this
369*/
370void Material::setBump(char* bump)
371{
372
373}
Note: See TracBrowser for help on using the repository browser.