| [2823] | 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 |  | 
|---|
| [2776] | 16 | #include "material.h" | 
|---|
 | 17 |  | 
|---|
| [2842] | 18 | /** | 
|---|
 | 19 |    \brief creates a default Material with no Name | 
|---|
 | 20 |    normally you call this to create a material List (for an obj-file) and then append with addMaterial() | 
|---|
 | 21 | */ | 
|---|
| [2776] | 22 | Material::Material() | 
|---|
 | 23 | { | 
|---|
 | 24 |   init(); | 
|---|
| [2778] | 25 |    | 
|---|
 | 26 |   setName (""); | 
|---|
| [2776] | 27 | } | 
|---|
 | 28 |  | 
|---|
| [2842] | 29 | /** | 
|---|
 | 30 |    \brief creates a Material. | 
|---|
 | 31 |    \param mtlName Name of the Material to be added to the Material List | 
|---|
 | 32 | */ | 
|---|
| [2776] | 33 | Material::Material (char* mtlName) | 
|---|
 | 34 | { | 
|---|
 | 35 |   init(); | 
|---|
 | 36 |    | 
|---|
 | 37 |   setName (mtlName); | 
|---|
 | 38 | } | 
|---|
 | 39 |  | 
|---|
| [2847] | 40 | /**  | 
|---|
 | 41 |     \brief deletes a Material | 
|---|
 | 42 | */ | 
|---|
 | 43 | Material::~Material() | 
|---|
 | 44 | { | 
|---|
 | 45 |   if (verbose >= 2) | 
|---|
 | 46 |     printf ("delete Material %s\n", name); | 
|---|
 | 47 |   if (nextMat != NULL) | 
|---|
 | 48 |     delete nextMat; | 
|---|
 | 49 | } | 
|---|
 | 50 |  | 
|---|
| [2842] | 51 | /** | 
|---|
 | 52 |    \brief adds a new Material to the List. | 
|---|
 | 53 |    this Function will append a new Material to the end of a Material List. | 
|---|
 | 54 |    \param mtlName The name of the Material to be added. | 
|---|
 | 55 | */ | 
|---|
| [2778] | 56 | Material* Material::addMaterial(char* mtlName) | 
|---|
 | 57 | { | 
|---|
| [2804] | 58 |   if (verbose >=2) | 
|---|
 | 59 |     printf ("adding Material %s\n", mtlName); | 
|---|
| [2778] | 60 |   Material* newMat = new Material(mtlName); | 
|---|
 | 61 |   Material* tmpMat = this; | 
|---|
 | 62 |   while (tmpMat->nextMat != NULL) | 
|---|
 | 63 |     { | 
|---|
 | 64 |       tmpMat = tmpMat->nextMat; | 
|---|
 | 65 |     } | 
|---|
 | 66 |       tmpMat->nextMat = newMat; | 
|---|
 | 67 |       return newMat; | 
|---|
 | 68 |    | 
|---|
 | 69 | } | 
|---|
 | 70 |  | 
|---|
| [2842] | 71 | /** | 
|---|
 | 72 |    \brief initializes a new Material with its default Values | 
|---|
 | 73 | */ | 
|---|
| [2776] | 74 | void Material::init(void) | 
|---|
 | 75 | { | 
|---|
| [2804] | 76 |   if (verbose >= 3) | 
|---|
 | 77 |     printf ("initializing new Material\n"); | 
|---|
| [2776] | 78 |   nextMat = NULL; | 
|---|
| [2778] | 79 |  | 
|---|
 | 80 |   setIllum(1); | 
|---|
 | 81 |   setDiffuse(0,0,0); | 
|---|
 | 82 |   setAmbient(0,0,0); | 
|---|
 | 83 |   setSpecular(0,0,0); | 
|---|
| [2850] | 84 |   setShininess(2.0); | 
|---|
| [2778] | 85 |   setTransparency(0.0); | 
|---|
| [2836] | 86 |    | 
|---|
| [2776] | 87 | } | 
|---|
 | 88 |  | 
|---|
| [2842] | 89 | /** | 
|---|
 | 90 |    \brief Set the Name of the Material. (Important for searching) | 
|---|
 | 91 |    \param mtlName the Name of the Material to be set. | 
|---|
 | 92 | */  | 
|---|
| [2776] | 93 | void Material::setName (char* mtlName) | 
|---|
 | 94 | { | 
|---|
| [2804] | 95 |   if (verbose >= 3) | 
|---|
 | 96 |     printf("setting Material Name to %s", mtlName); | 
|---|
| [2778] | 97 |   strcpy(name, mtlName); | 
|---|
 | 98 |   //  printf ("adding new Material: %s, %p\n", this->getName(), this); | 
|---|
| [2776] | 99 |  | 
|---|
 | 100 | } | 
|---|
| [2842] | 101 | /** | 
|---|
 | 102 |    \returns The Name of The Material | 
|---|
 | 103 | */ | 
|---|
| [2778] | 104 | char* Material::getName (void) | 
|---|
 | 105 | { | 
|---|
 | 106 |   return name; | 
|---|
 | 107 | } | 
|---|
| [2776] | 108 |  | 
|---|
| [2842] | 109 | /** | 
|---|
 | 110 |    \brief Sets the Material Illumination Model.  | 
|---|
 | 111 |    \brief illu illumination Model in int form | 
|---|
 | 112 | */ | 
|---|
| [2776] | 113 | void Material::setIllum (int illum) | 
|---|
 | 114 | { | 
|---|
| [2804] | 115 |   if (verbose >= 3) | 
|---|
 | 116 |     printf("setting illumModel of Material %s to %i", name, illum); | 
|---|
| [2776] | 117 |   illumModel = illum; | 
|---|
 | 118 |   //  printf ("setting illumModel to: %i\n", illumModel); | 
|---|
 | 119 | } | 
|---|
| [2842] | 120 | /** | 
|---|
 | 121 |    \brief Sets the Material Illumination Model.  | 
|---|
 | 122 |    \brief illu illumination Model in char* form | 
|---|
 | 123 | */void Material::setIllum (char* illum) | 
|---|
| [2776] | 124 | { | 
|---|
 | 125 |   setIllum (atoi(illum)); | 
|---|
 | 126 | } | 
|---|
 | 127 |  | 
|---|
| [2842] | 128 | /** | 
|---|
 | 129 |    \brief Sets the Material Diffuse Color. | 
|---|
 | 130 |    \param r Red Color Channel. | 
|---|
 | 131 |    \param g Green Color Channel. | 
|---|
 | 132 |    \param b Blue Color Channel. | 
|---|
 | 133 | */ | 
|---|
| [2776] | 134 | void Material::setDiffuse (float r, float g, float b) | 
|---|
 | 135 | { | 
|---|
| [2804] | 136 |   if (verbose >= 3) | 
|---|
 | 137 |     printf ("setting Diffuse Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); | 
|---|
| [2776] | 138 |   diffuse[0] = r; | 
|---|
 | 139 |   diffuse[1] = g; | 
|---|
| [2780] | 140 |   diffuse[2] = b;   | 
|---|
 | 141 |   diffuse[3] = 1.0; | 
|---|
 | 142 |  | 
|---|
| [2776] | 143 | } | 
|---|
| [2842] | 144 | /** | 
|---|
 | 145 |    \brief Sets the Material Diffuse Color. | 
|---|
 | 146 |    \param rgb The red, green, blue channel in char format (with spaces between them) | 
|---|
 | 147 | */ | 
|---|
| [2776] | 148 | void Material::setDiffuse (char* rgb) | 
|---|
 | 149 | { | 
|---|
 | 150 |   char r[20],g[20],b[20]; | 
|---|
 | 151 |   sscanf (rgb, "%s %s %s", r, g, b); | 
|---|
 | 152 |   setDiffuse (atof(r), atof(g), atof(b)); | 
|---|
 | 153 | } | 
|---|
 | 154 |  | 
|---|
| [2842] | 155 | /** | 
|---|
 | 156 |    \brief Sets the Material Ambient Color.  | 
|---|
 | 157 |    \param r Red Color Channel. | 
|---|
 | 158 |    \param g Green Color Channel. | 
|---|
 | 159 |    \param b Blue Color Channel. | 
|---|
 | 160 | */ | 
|---|
| [2776] | 161 | void Material::setAmbient (float r, float g, float b) | 
|---|
 | 162 | { | 
|---|
| [2804] | 163 |   if (verbose >=3) | 
|---|
 | 164 |     printf ("setting Ambient Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); | 
|---|
| [2776] | 165 |   ambient[0] = r; | 
|---|
 | 166 |   ambient[1] = g; | 
|---|
 | 167 |   ambient[2] = b; | 
|---|
| [2780] | 168 |   ambient[3] = 1.0; | 
|---|
| [2776] | 169 | } | 
|---|
| [2842] | 170 | /** | 
|---|
 | 171 |    \brief Sets the Material Ambient Color. | 
|---|
 | 172 |    \param rgb The red, green, blue channel in char format (with spaces between them) | 
|---|
 | 173 | */ | 
|---|
| [2776] | 174 | void Material::setAmbient (char* rgb) | 
|---|
 | 175 | { | 
|---|
 | 176 |   char r[20],g[20],b[20]; | 
|---|
 | 177 |   sscanf (rgb, "%s %s %s", r, g, b); | 
|---|
 | 178 |   setAmbient (atof(r), atof(g), atof(b)); | 
|---|
 | 179 | } | 
|---|
 | 180 |  | 
|---|
| [2842] | 181 | /** | 
|---|
 | 182 |    \brief Sets the Material Specular Color.  | 
|---|
 | 183 |    \param r Red Color Channel. | 
|---|
 | 184 |    \param g Green Color Channel. | 
|---|
 | 185 |    \param b Blue Color Channel. | 
|---|
 | 186 | */ | 
|---|
| [2776] | 187 | void Material::setSpecular (float r, float g, float b) | 
|---|
 | 188 | { | 
|---|
| [2804] | 189 |   if (verbose >= 3) | 
|---|
 | 190 |     printf ("setting Specular Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); | 
|---|
| [2776] | 191 |   specular[0] = r; | 
|---|
 | 192 |   specular[1] = g; | 
|---|
 | 193 |   specular[2] = b; | 
|---|
| [2780] | 194 |   specular[3] = 1.0; | 
|---|
| [2804] | 195 |  } | 
|---|
| [2842] | 196 | /** | 
|---|
 | 197 |    \brief Sets the Material Specular Color. | 
|---|
 | 198 |    \param rgb The red, green, blue channel in char format (with spaces between them) | 
|---|
 | 199 | */ | 
|---|
| [2776] | 200 | void Material::setSpecular (char* rgb) | 
|---|
 | 201 | { | 
|---|
 | 202 |   char r[20],g[20],b[20]; | 
|---|
 | 203 |   sscanf (rgb, "%s %s %s", r, g, b); | 
|---|
 | 204 |   setSpecular (atof(r), atof(g), atof(b)); | 
|---|
 | 205 | } | 
|---|
 | 206 |  | 
|---|
| [2842] | 207 | /** | 
|---|
 | 208 |    \brief Sets the Material Shininess. | 
|---|
 | 209 |    \param shini stes the Shininess from float. | 
|---|
 | 210 | */ | 
|---|
| [2836] | 211 | void Material::setShininess (float shini) | 
|---|
 | 212 | { | 
|---|
 | 213 |   shininess = shini; | 
|---|
 | 214 | } | 
|---|
| [2842] | 215 | /** | 
|---|
 | 216 |    \brief Sets the Material Shininess. | 
|---|
 | 217 |    \param shini stes the Shininess from char*. | 
|---|
 | 218 | */ | 
|---|
| [2836] | 219 | void Material::setShininess (char* shini) | 
|---|
 | 220 | { | 
|---|
 | 221 |   setShininess (atof(shini)); | 
|---|
 | 222 | } | 
|---|
| [2776] | 223 |  | 
|---|
| [2842] | 224 | /** | 
|---|
 | 225 |    \brief Sets the Material Transparency. | 
|---|
 | 226 |    \param trans stes the Transparency from int. | 
|---|
 | 227 | */ | 
|---|
| [2776] | 228 | void Material::setTransparency (float trans) | 
|---|
 | 229 | { | 
|---|
| [2804] | 230 |   if (verbose >= 3) | 
|---|
 | 231 |     printf ("setting Transparency of Material %s to %f\n", name, trans); | 
|---|
| [2776] | 232 |   transparency = trans; | 
|---|
 | 233 | } | 
|---|
| [2842] | 234 | /** | 
|---|
 | 235 |    \brief Sets the Material Transparency. | 
|---|
 | 236 |    \param trans stes the Transparency from char*. | 
|---|
 | 237 | */ | 
|---|
| [2776] | 238 | void Material::setTransparency (char* trans) | 
|---|
 | 239 | { | 
|---|
 | 240 |   char tr[20]; | 
|---|
 | 241 |   sscanf (trans, "%s", tr); | 
|---|
 | 242 |   setTransparency (atof(tr)); | 
|---|
 | 243 | } | 
|---|
| [2778] | 244 |  | 
|---|
| [2842] | 245 | /** | 
|---|
 | 246 |    \brief Search for a Material called mtlName | 
|---|
 | 247 |    \param mtlName the Name of the Material to search for | 
|---|
 | 248 |    \returns Material named mtlName if it is found. NULL otherwise. | 
|---|
 | 249 | */ | 
|---|
| [2778] | 250 | Material* Material::search (char* mtlName) | 
|---|
 | 251 | { | 
|---|
| [2804] | 252 |   if (verbose >=3) | 
|---|
 | 253 |     printf ("Searching for material %s", mtlName); | 
|---|
| [2778] | 254 |   Material* searcher = this; | 
|---|
 | 255 |   while (searcher != NULL) | 
|---|
 | 256 |     { | 
|---|
| [2804] | 257 |       if (verbose >= 3) | 
|---|
 | 258 |         printf ("."); | 
|---|
| [2778] | 259 |       if (!strcmp (searcher->getName(), mtlName)) | 
|---|
| [2804] | 260 |         { | 
|---|
 | 261 |           if (verbose >= 3) | 
|---|
 | 262 |             printf ("found.\n"); | 
|---|
 | 263 |           return searcher; | 
|---|
 | 264 |         } | 
|---|
| [2778] | 265 |       searcher = searcher->nextMat; | 
|---|
 | 266 |     } | 
|---|
 | 267 |   return NULL; | 
|---|
 | 268 | } | 
|---|
 | 269 |  | 
|---|
| [2842] | 270 | /** | 
|---|
 | 271 |    \brief sets the material with which the following Faces will be painted | 
|---|
 | 272 | */ | 
|---|
| [2778] | 273 | bool Material::select (void) | 
|---|
 | 274 | { | 
|---|
| [2780] | 275 |   // setting diffuse color | 
|---|
 | 276 |   //  glColor3f (diffuse[0], diffuse[1], diffuse[2]); | 
|---|
 | 277 |   glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse); | 
|---|
 | 278 |  | 
|---|
 | 279 |   // setting ambient color | 
|---|
 | 280 |   glMaterialfv(GL_FRONT, GL_AMBIENT, ambient); | 
|---|
 | 281 |  | 
|---|
 | 282 |   // setting up Sprecular | 
|---|
 | 283 |   glMaterialfv(GL_FRONT, GL_SPECULAR, specular); | 
|---|
| [2836] | 284 |  | 
|---|
 | 285 |   // setting up Shininess | 
|---|
 | 286 |   glMaterialf(GL_FRONT, GL_SHININESS, shininess); | 
|---|
| [2780] | 287 |    | 
|---|
 | 288 |   // setting illumination Model  | 
|---|
 | 289 |   if (illumModel == 1) | 
|---|
 | 290 |     glShadeModel(GL_FLAT); | 
|---|
 | 291 |   else if (illumModel >= 2) | 
|---|
 | 292 |     glShadeModel(GL_SMOOTH); | 
|---|
| [2778] | 293 | } | 
|---|