| 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 | #include "material.h" | 
|---|
| 17 |  | 
|---|
| 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 | */ | 
|---|
| 22 | Material::Material() | 
|---|
| 23 | { | 
|---|
| 24 |   init(); | 
|---|
| 25 |    | 
|---|
| 26 |   setName (""); | 
|---|
| 27 | } | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 |    \brief creates a Material. | 
|---|
| 31 |    \param mtlName Name of the Material to be added to the Material List | 
|---|
| 32 | */ | 
|---|
| 33 | Material::Material (char* mtlName) | 
|---|
| 34 | { | 
|---|
| 35 |   init(); | 
|---|
| 36 |    | 
|---|
| 37 |   setName (mtlName); | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 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 |  | 
|---|
| 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 | */ | 
|---|
| 56 | Material* Material::addMaterial(char* mtlName) | 
|---|
| 57 | { | 
|---|
| 58 |   if (verbose >=2) | 
|---|
| 59 |     printf ("adding Material %s\n", mtlName); | 
|---|
| 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 |  | 
|---|
| 71 | /** | 
|---|
| 72 |    \brief initializes a new Material with its default Values | 
|---|
| 73 | */ | 
|---|
| 74 | void Material::init(void) | 
|---|
| 75 | { | 
|---|
| 76 |   if (verbose >= 3) | 
|---|
| 77 |     printf ("initializing new Material\n"); | 
|---|
| 78 |   nextMat = NULL; | 
|---|
| 79 |  | 
|---|
| 80 |   setIllum(1); | 
|---|
| 81 |   setDiffuse(0,0,0); | 
|---|
| 82 |   setAmbient(0,0,0); | 
|---|
| 83 |   setSpecular(0,0,0); | 
|---|
| 84 |   setShininess(2.0); | 
|---|
| 85 |   setTransparency(0.0); | 
|---|
| 86 |    | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | /** | 
|---|
| 90 |    \brief Set the Name of the Material. (Important for searching) | 
|---|
| 91 |    \param mtlName the Name of the Material to be set. | 
|---|
| 92 | */  | 
|---|
| 93 | void Material::setName (char* mtlName) | 
|---|
| 94 | { | 
|---|
| 95 |   if (verbose >= 3) | 
|---|
| 96 |     printf("setting Material Name to %s", mtlName); | 
|---|
| 97 |   strcpy(name, mtlName); | 
|---|
| 98 |   //  printf ("adding new Material: %s, %p\n", this->getName(), this); | 
|---|
| 99 |  | 
|---|
| 100 | } | 
|---|
| 101 | /** | 
|---|
| 102 |    \returns The Name of The Material | 
|---|
| 103 | */ | 
|---|
| 104 | char* Material::getName (void) | 
|---|
| 105 | { | 
|---|
| 106 |   return name; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | /** | 
|---|
| 110 |    \brief Sets the Material Illumination Model.  | 
|---|
| 111 |    \brief illu illumination Model in int form | 
|---|
| 112 | */ | 
|---|
| 113 | void Material::setIllum (int illum) | 
|---|
| 114 | { | 
|---|
| 115 |   if (verbose >= 3) | 
|---|
| 116 |     printf("setting illumModel of Material %s to %i", name, illum); | 
|---|
| 117 |   illumModel = illum; | 
|---|
| 118 |   //  printf ("setting illumModel to: %i\n", illumModel); | 
|---|
| 119 | } | 
|---|
| 120 | /** | 
|---|
| 121 |    \brief Sets the Material Illumination Model.  | 
|---|
| 122 |    \brief illu illumination Model in char* form | 
|---|
| 123 | */void Material::setIllum (char* illum) | 
|---|
| 124 | { | 
|---|
| 125 |   setIllum (atoi(illum)); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 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 | */ | 
|---|
| 134 | void Material::setDiffuse (float r, float g, float b) | 
|---|
| 135 | { | 
|---|
| 136 |   if (verbose >= 3) | 
|---|
| 137 |     printf ("setting Diffuse Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); | 
|---|
| 138 |   diffuse[0] = r; | 
|---|
| 139 |   diffuse[1] = g; | 
|---|
| 140 |   diffuse[2] = b;   | 
|---|
| 141 |   diffuse[3] = 1.0; | 
|---|
| 142 |  | 
|---|
| 143 | } | 
|---|
| 144 | /** | 
|---|
| 145 |    \brief Sets the Material Diffuse Color. | 
|---|
| 146 |    \param rgb The red, green, blue channel in char format (with spaces between them) | 
|---|
| 147 | */ | 
|---|
| 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 |  | 
|---|
| 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 | */ | 
|---|
| 161 | void Material::setAmbient (float r, float g, float b) | 
|---|
| 162 | { | 
|---|
| 163 |   if (verbose >=3) | 
|---|
| 164 |     printf ("setting Ambient Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); | 
|---|
| 165 |   ambient[0] = r; | 
|---|
| 166 |   ambient[1] = g; | 
|---|
| 167 |   ambient[2] = b; | 
|---|
| 168 |   ambient[3] = 1.0; | 
|---|
| 169 | } | 
|---|
| 170 | /** | 
|---|
| 171 |    \brief Sets the Material Ambient Color. | 
|---|
| 172 |    \param rgb The red, green, blue channel in char format (with spaces between them) | 
|---|
| 173 | */ | 
|---|
| 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 |  | 
|---|
| 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 | */ | 
|---|
| 187 | void Material::setSpecular (float r, float g, float b) | 
|---|
| 188 | { | 
|---|
| 189 |   if (verbose >= 3) | 
|---|
| 190 |     printf ("setting Specular Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); | 
|---|
| 191 |   specular[0] = r; | 
|---|
| 192 |   specular[1] = g; | 
|---|
| 193 |   specular[2] = b; | 
|---|
| 194 |   specular[3] = 1.0; | 
|---|
| 195 |  } | 
|---|
| 196 | /** | 
|---|
| 197 |    \brief Sets the Material Specular Color. | 
|---|
| 198 |    \param rgb The red, green, blue channel in char format (with spaces between them) | 
|---|
| 199 | */ | 
|---|
| 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 |  | 
|---|
| 207 | /** | 
|---|
| 208 |    \brief Sets the Material Shininess. | 
|---|
| 209 |    \param shini stes the Shininess from float. | 
|---|
| 210 | */ | 
|---|
| 211 | void Material::setShininess (float shini) | 
|---|
| 212 | { | 
|---|
| 213 |   shininess = shini; | 
|---|
| 214 | } | 
|---|
| 215 | /** | 
|---|
| 216 |    \brief Sets the Material Shininess. | 
|---|
| 217 |    \param shini stes the Shininess from char*. | 
|---|
| 218 | */ | 
|---|
| 219 | void Material::setShininess (char* shini) | 
|---|
| 220 | { | 
|---|
| 221 |   setShininess (atof(shini)); | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 224 | /** | 
|---|
| 225 |    \brief Sets the Material Transparency. | 
|---|
| 226 |    \param trans stes the Transparency from int. | 
|---|
| 227 | */ | 
|---|
| 228 | void Material::setTransparency (float trans) | 
|---|
| 229 | { | 
|---|
| 230 |   if (verbose >= 3) | 
|---|
| 231 |     printf ("setting Transparency of Material %s to %f\n", name, trans); | 
|---|
| 232 |   transparency = trans; | 
|---|
| 233 | } | 
|---|
| 234 | /** | 
|---|
| 235 |    \brief Sets the Material Transparency. | 
|---|
| 236 |    \param trans stes the Transparency from char*. | 
|---|
| 237 | */ | 
|---|
| 238 | void Material::setTransparency (char* trans) | 
|---|
| 239 | { | 
|---|
| 240 |   char tr[20]; | 
|---|
| 241 |   sscanf (trans, "%s", tr); | 
|---|
| 242 |   setTransparency (atof(tr)); | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 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 | */ | 
|---|
| 250 | Material* Material::search (char* mtlName) | 
|---|
| 251 | { | 
|---|
| 252 |   if (verbose >=3) | 
|---|
| 253 |     printf ("Searching for material %s", mtlName); | 
|---|
| 254 |   Material* searcher = this; | 
|---|
| 255 |   while (searcher != NULL) | 
|---|
| 256 |     { | 
|---|
| 257 |       if (verbose >= 3) | 
|---|
| 258 |         printf ("."); | 
|---|
| 259 |       if (!strcmp (searcher->getName(), mtlName)) | 
|---|
| 260 |         { | 
|---|
| 261 |           if (verbose >= 3) | 
|---|
| 262 |             printf ("found.\n"); | 
|---|
| 263 |           return searcher; | 
|---|
| 264 |         } | 
|---|
| 265 |       searcher = searcher->nextMat; | 
|---|
| 266 |     } | 
|---|
| 267 |   return NULL; | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | /** | 
|---|
| 271 |    \brief sets the material with which the following Faces will be painted | 
|---|
| 272 | */ | 
|---|
| 273 | bool Material::select (void) | 
|---|
| 274 | { | 
|---|
| 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); | 
|---|
| 284 |  | 
|---|
| 285 |   // setting up Shininess | 
|---|
| 286 |   glMaterialf(GL_FRONT, GL_SHININESS, shininess); | 
|---|
| 287 |    | 
|---|
| 288 |   // setting illumination Model  | 
|---|
| 289 |   if (illumModel == 1) | 
|---|
| 290 |     glShadeModel(GL_FLAT); | 
|---|
| 291 |   else if (illumModel >= 2) | 
|---|
| 292 |     glShadeModel(GL_SMOOTH); | 
|---|
| 293 | } | 
|---|