Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: added folder subprojects.
now one can test all the features one likes in an already handled file in subrojects/testmain

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