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