1 | #include "material.h" |
---|
2 | |
---|
3 | Material::Material() |
---|
4 | { |
---|
5 | init(); |
---|
6 | |
---|
7 | setName (""); |
---|
8 | } |
---|
9 | |
---|
10 | Material::Material (char* mtlName) |
---|
11 | { |
---|
12 | init(); |
---|
13 | |
---|
14 | setName (mtlName); |
---|
15 | } |
---|
16 | |
---|
17 | Material* Material::addMaterial(char* mtlName) |
---|
18 | { |
---|
19 | if (verbose >=2) |
---|
20 | printf ("adding Material %s\n", mtlName); |
---|
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 | |
---|
32 | void Material::init(void) |
---|
33 | { |
---|
34 | if (verbose >= 3) |
---|
35 | printf ("initializing new Material\n"); |
---|
36 | nextMat = NULL; |
---|
37 | |
---|
38 | setIllum(1); |
---|
39 | setDiffuse(0,0,0); |
---|
40 | setAmbient(0,0,0); |
---|
41 | setSpecular(0,0,0); |
---|
42 | setTransparency(0.0); |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | void Material::setName (char* mtlName) |
---|
47 | { |
---|
48 | if (verbose >= 3) |
---|
49 | printf("setting Material Name to %s", mtlName); |
---|
50 | strcpy(name, mtlName); |
---|
51 | // printf ("adding new Material: %s, %p\n", this->getName(), this); |
---|
52 | |
---|
53 | } |
---|
54 | char* Material::getName (void) |
---|
55 | { |
---|
56 | return name; |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | void Material::setIllum (int illum) |
---|
61 | { |
---|
62 | if (verbose >= 3) |
---|
63 | printf("setting illumModel of Material %s to %i", name, illum); |
---|
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 | { |
---|
74 | if (verbose >= 3) |
---|
75 | printf ("setting Diffuse Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); |
---|
76 | diffuse[0] = r; |
---|
77 | diffuse[1] = g; |
---|
78 | diffuse[2] = b; |
---|
79 | diffuse[3] = 1.0; |
---|
80 | |
---|
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 | { |
---|
91 | if (verbose >=3) |
---|
92 | printf ("setting Ambient Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); |
---|
93 | ambient[0] = r; |
---|
94 | ambient[1] = g; |
---|
95 | ambient[2] = b; |
---|
96 | ambient[3] = 1.0; |
---|
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 | { |
---|
107 | if (verbose >= 3) |
---|
108 | printf ("setting Specular Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); |
---|
109 | specular[0] = r; |
---|
110 | specular[1] = g; |
---|
111 | specular[2] = b; |
---|
112 | specular[3] = 1.0; |
---|
113 | } |
---|
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 | { |
---|
124 | if (verbose >= 3) |
---|
125 | printf ("setting Transparency of Material %s to %f\n", name, trans); |
---|
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 | } |
---|
134 | |
---|
135 | |
---|
136 | Material* Material::search (char* mtlName) |
---|
137 | { |
---|
138 | if (verbose >=3) |
---|
139 | printf ("Searching for material %s", mtlName); |
---|
140 | Material* searcher = this; |
---|
141 | while (searcher != NULL) |
---|
142 | { |
---|
143 | if (verbose >= 3) |
---|
144 | printf ("."); |
---|
145 | if (!strcmp (searcher->getName(), mtlName)) |
---|
146 | { |
---|
147 | if (verbose >= 3) |
---|
148 | printf ("found.\n"); |
---|
149 | return searcher; |
---|
150 | } |
---|
151 | searcher = searcher->nextMat; |
---|
152 | } |
---|
153 | return NULL; |
---|
154 | } |
---|
155 | |
---|
156 | bool Material::select (void) |
---|
157 | { |
---|
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); |
---|
173 | } |
---|