Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3542 was 3536, checked in by bensch, 20 years ago

orxonox/trunk: GL_TEXTURE_2D-patch: now only enabled and disabled through Material

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 <stdlib.h>
21#include <string.h>
22
23using namespace std;
24
25
26/**
27   \brief creates a default Material with no Name
28   normally you call this to create a material List (for an obj-file) and then append with addMaterial()
29*/
30Material::Material()
31{
32  this->init();
33 
34  this->setName ("");
35}
36
37/**
38   \brief creates a Material.
39   \param mtlName Name of the Material to be added to the Material List
40*/
41Material::Material (char* mtlName)
42{
43  this->init();
44 
45  this->setName (mtlName);
46}
47
48/**
49    \brief deletes a Material
50*/
51Material::~Material()
52{
53  PRINTF(2)("delete Material %s.\n", this->name);
54  if (this->name)
55    delete []this->name;
56  if (this->diffuseTexture)
57    this->diffuseTexture;
58  if (this->nextMat)
59    delete this->nextMat;
60}
61
62/**
63   \brief adds a new Material to the List.
64   this Function will append a new Material to the end of a Material List.
65   \param mtlName The name of the Material to be added.
66*/
67Material* Material::addMaterial(char* mtlName)
68{
69  PRINTF(2)("adding Material %s.\n", mtlName);
70   Material* tmpMat = this;
71  while (tmpMat->nextMat != NULL)
72    {
73      tmpMat = tmpMat->nextMat;
74    }
75  tmpMat->nextMat = new Material(mtlName);
76  return tmpMat->nextMat;
77 
78}
79
80/**
81   \brief initializes a new Material with its default Values
82*/
83void Material::init(void)
84{
85  PRINTF(2)("initializing new Material.\n");
86  this->nextMat = NULL;
87  this->name ="";
88  this->setIllum(1);
89  this->setDiffuse(0,0,0);
90  this->setAmbient(0,0,0);
91  this->setSpecular(.5,.5,.5);
92  this->setShininess(2.0);
93  this->setTransparency(0.0);
94
95
96  this->diffuseTexture = NULL;
97  this->ambientTexture = NULL;
98  this->specularTexture = NULL;
99
100  this->diffuseTextureSet = false;
101  this->ambientTextureSet = false;
102  this->specularTextureSet = false;
103
104 
105}
106
107/**
108   \brief Search for a Material called mtlName
109   \param mtlName the Name of the Material to search for
110   \returns Material named mtlName if it is found. NULL otherwise.
111*/
112Material* Material::search(char* mtlName)
113{
114  PRINTF(2)("Searching for material %s", mtlName);
115  Material* searcher = this;
116  while (searcher != NULL)
117    {
118      PRINT(2)(".");
119      if (!strcmp (searcher->getName(), mtlName))
120        {
121          PRINT(2)("found.\n");
122          return searcher;
123        }
124      searcher = searcher->nextMat;
125    }
126  PRINT(2)("not found\n");
127  return NULL;
128}
129
130/**
131   \brief sets the material with which the following Faces will be painted
132*/
133bool Material::select (void)
134{
135  // setting diffuse color
136  //  glColor3f (diffuse[0], diffuse[1], diffuse[2]);
137  glMaterialfv(GL_FRONT, GL_DIFFUSE, this->diffuse);
138
139  // setting ambient color
140  glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient);
141
142  // setting up Sprecular
143  glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular);
144
145  // setting up Shininess
146  glMaterialf(GL_FRONT, GL_SHININESS, this->shininess);
147 
148  // setting illumination Model
149  if (this->illumModel == 1) //! \todo make this work, if no vertex-normals are read.
150    glShadeModel(GL_FLAT);
151  else if (this->illumModel >= 2)
152    glShadeModel(GL_SMOOTH);
153
154  if (this->diffuseTextureSet)
155    {
156      glEnable(GL_TEXTURE_2D);
157      glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture());
158    }
159  else
160    {
161      glDisable(GL_TEXTURE_2D);
162      glBindTexture(GL_TEXTURE_2D, 0);
163    }
164}
165
166
167/**
168   \brief Set the Name of the Material. (Important for searching)
169   \param mtlName the Name of the Material to be set.
170*/ 
171void Material::setName (char* mtlName)
172{
173  PRINTF(3)("setting Material Name to %s.\n", this->name);
174  this->name = new char [strlen(mtlName)+1];
175  strcpy(this->name, mtlName);
176}
177
178/**
179   \returns The Name of The Material
180*/
181char* Material::getName (void)
182{
183  return this->name;
184}
185
186/**
187   \brief Sets the Material Illumination Model.
188   \brief illu illumination Model in int form
189*/
190void Material::setIllum (int illum)
191{
192  PRINTF(3)("setting illumModel of Material %s to %i\n", this->name, illum);
193  this->illumModel = illum;
194  //  PRINTF(3)("setting illumModel to: %i\n", illumModel);
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(3)("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(3)("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(3)("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(3)("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(3)("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.