[2776] | 1 | #include "material.h" |
---|
| 2 | |
---|
| 3 | Material::Material() |
---|
| 4 | { |
---|
| 5 | init(); |
---|
[2778] | 6 | |
---|
| 7 | setName (""); |
---|
[2776] | 8 | } |
---|
| 9 | |
---|
| 10 | Material::Material (char* mtlName) |
---|
| 11 | { |
---|
| 12 | init(); |
---|
| 13 | |
---|
| 14 | setName (mtlName); |
---|
| 15 | } |
---|
| 16 | |
---|
[2778] | 17 | Material* Material::addMaterial(char* mtlName) |
---|
| 18 | { |
---|
[2810] | 19 | if (verbose >=2) |
---|
| 20 | printf ("adding Material %s\n", mtlName); |
---|
[2778] | 21 | Material* newMat = new Material(mtlName); |
---|
| 22 | Material* tmpMat = this; |
---|
| 23 | while (tmpMat->nextMat != NULL) |
---|
| 24 | { |
---|
| 25 | tmpMat = tmpMat->nextMat; |
---|
| 26 | } |
---|
| 27 | tmpMat->nextMat = newMat; |
---|
| 28 | return newMat; |
---|
| 29 | |
---|
| 30 | } |
---|
| 31 | |
---|
[2776] | 32 | void Material::init(void) |
---|
| 33 | { |
---|
[2810] | 34 | if (verbose >= 3) |
---|
| 35 | printf ("initializing new Material\n"); |
---|
[2776] | 36 | nextMat = NULL; |
---|
[2778] | 37 | |
---|
| 38 | setIllum(1); |
---|
| 39 | setDiffuse(0,0,0); |
---|
| 40 | setAmbient(0,0,0); |
---|
| 41 | setSpecular(0,0,0); |
---|
| 42 | setTransparency(0.0); |
---|
[2776] | 43 | } |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | void Material::setName (char* mtlName) |
---|
| 47 | { |
---|
[2810] | 48 | if (verbose >= 3) |
---|
| 49 | printf("setting Material Name to %s", mtlName); |
---|
[2778] | 50 | strcpy(name, mtlName); |
---|
| 51 | // printf ("adding new Material: %s, %p\n", this->getName(), this); |
---|
[2776] | 52 | |
---|
| 53 | } |
---|
[2778] | 54 | char* Material::getName (void) |
---|
| 55 | { |
---|
| 56 | return name; |
---|
| 57 | } |
---|
[2776] | 58 | |
---|
| 59 | |
---|
| 60 | void Material::setIllum (int illum) |
---|
| 61 | { |
---|
[2810] | 62 | if (verbose >= 3) |
---|
| 63 | printf("setting illumModel of Material %s to %i", name, illum); |
---|
[2776] | 64 | illumModel = illum; |
---|
| 65 | // printf ("setting illumModel to: %i\n", illumModel); |
---|
| 66 | } |
---|
| 67 | void Material::setIllum (char* illum) |
---|
| 68 | { |
---|
| 69 | setIllum (atoi(illum)); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | void Material::setDiffuse (float r, float g, float b) |
---|
| 73 | { |
---|
[2810] | 74 | if (verbose >= 3) |
---|
| 75 | printf ("setting Diffuse Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); |
---|
[2776] | 76 | diffuse[0] = r; |
---|
| 77 | diffuse[1] = g; |
---|
[2780] | 78 | diffuse[2] = b; |
---|
| 79 | diffuse[3] = 1.0; |
---|
| 80 | |
---|
[2776] | 81 | } |
---|
| 82 | void Material::setDiffuse (char* rgb) |
---|
| 83 | { |
---|
| 84 | char r[20],g[20],b[20]; |
---|
| 85 | sscanf (rgb, "%s %s %s", r, g, b); |
---|
| 86 | setDiffuse (atof(r), atof(g), atof(b)); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | void Material::setAmbient (float r, float g, float b) |
---|
| 90 | { |
---|
[2810] | 91 | if (verbose >=3) |
---|
| 92 | printf ("setting Ambient Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); |
---|
[2776] | 93 | ambient[0] = r; |
---|
| 94 | ambient[1] = g; |
---|
| 95 | ambient[2] = b; |
---|
[2780] | 96 | ambient[3] = 1.0; |
---|
[2776] | 97 | } |
---|
| 98 | void Material::setAmbient (char* rgb) |
---|
| 99 | { |
---|
| 100 | char r[20],g[20],b[20]; |
---|
| 101 | sscanf (rgb, "%s %s %s", r, g, b); |
---|
| 102 | setAmbient (atof(r), atof(g), atof(b)); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | void Material::setSpecular (float r, float g, float b) |
---|
| 106 | { |
---|
[2810] | 107 | if (verbose >= 3) |
---|
| 108 | printf ("setting Specular Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); |
---|
[2776] | 109 | specular[0] = r; |
---|
| 110 | specular[1] = g; |
---|
| 111 | specular[2] = b; |
---|
[2780] | 112 | specular[3] = 1.0; |
---|
[2810] | 113 | } |
---|
[2776] | 114 | void Material::setSpecular (char* rgb) |
---|
| 115 | { |
---|
| 116 | char r[20],g[20],b[20]; |
---|
| 117 | sscanf (rgb, "%s %s %s", r, g, b); |
---|
| 118 | setSpecular (atof(r), atof(g), atof(b)); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | void Material::setTransparency (float trans) |
---|
| 123 | { |
---|
[2810] | 124 | if (verbose >= 3) |
---|
| 125 | printf ("setting Transparency of Material %s to %f\n", name, trans); |
---|
[2776] | 126 | transparency = trans; |
---|
| 127 | } |
---|
| 128 | void Material::setTransparency (char* trans) |
---|
| 129 | { |
---|
| 130 | char tr[20]; |
---|
| 131 | sscanf (trans, "%s", tr); |
---|
| 132 | setTransparency (atof(tr)); |
---|
| 133 | } |
---|
[2778] | 134 | |
---|
| 135 | |
---|
| 136 | Material* Material::search (char* mtlName) |
---|
| 137 | { |
---|
[2810] | 138 | if (verbose >=3) |
---|
| 139 | printf ("Searching for material %s", mtlName); |
---|
[2778] | 140 | Material* searcher = this; |
---|
| 141 | while (searcher != NULL) |
---|
| 142 | { |
---|
[2810] | 143 | if (verbose >= 3) |
---|
| 144 | printf ("."); |
---|
[2778] | 145 | if (!strcmp (searcher->getName(), mtlName)) |
---|
[2810] | 146 | { |
---|
| 147 | if (verbose >= 3) |
---|
| 148 | printf ("found.\n"); |
---|
| 149 | return searcher; |
---|
| 150 | } |
---|
[2778] | 151 | searcher = searcher->nextMat; |
---|
| 152 | } |
---|
| 153 | return NULL; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | bool Material::select (void) |
---|
| 157 | { |
---|
[2780] | 158 | // setting diffuse color |
---|
| 159 | // glColor3f (diffuse[0], diffuse[1], diffuse[2]); |
---|
| 160 | glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse); |
---|
| 161 | |
---|
| 162 | // setting ambient color |
---|
| 163 | glMaterialfv(GL_FRONT, GL_AMBIENT, ambient); |
---|
| 164 | |
---|
| 165 | // setting up Sprecular |
---|
| 166 | glMaterialfv(GL_FRONT, GL_SPECULAR, specular); |
---|
| 167 | |
---|
| 168 | // setting illumination Model |
---|
| 169 | if (illumModel == 1) |
---|
| 170 | glShadeModel(GL_FLAT); |
---|
| 171 | else if (illumModel >= 2) |
---|
| 172 | glShadeModel(GL_SMOOTH); |
---|
[2778] | 173 | } |
---|