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 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 | */ |
---|
45 | Material* Material::addMaterial(char* mtlName) |
---|
46 | { |
---|
47 | if (verbose >=2) |
---|
48 | printf ("adding Material %s\n", mtlName); |
---|
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 | |
---|
60 | /** |
---|
61 | \brief initializes a new Material with its default Values |
---|
62 | */ |
---|
63 | void Material::init(void) |
---|
64 | { |
---|
65 | if (verbose >= 3) |
---|
66 | printf ("initializing new Material\n"); |
---|
67 | nextMat = NULL; |
---|
68 | |
---|
69 | setIllum(1); |
---|
70 | setDiffuse(0,0,0); |
---|
71 | setAmbient(0,0,0); |
---|
72 | setSpecular(0,0,0); |
---|
73 | setShininess(2); |
---|
74 | setTransparency(0.0); |
---|
75 | |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | \brief Set the Name of the Material. (Important for searching) |
---|
80 | \param mtlName the Name of the Material to be set. |
---|
81 | */ |
---|
82 | void Material::setName (char* mtlName) |
---|
83 | { |
---|
84 | if (verbose >= 3) |
---|
85 | printf("setting Material Name to %s", mtlName); |
---|
86 | strcpy(name, mtlName); |
---|
87 | // printf ("adding new Material: %s, %p\n", this->getName(), this); |
---|
88 | |
---|
89 | } |
---|
90 | /** |
---|
91 | \returns The Name of The Material |
---|
92 | */ |
---|
93 | char* Material::getName (void) |
---|
94 | { |
---|
95 | return name; |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | \brief Sets the Material Illumination Model. |
---|
100 | \brief illu illumination Model in int form |
---|
101 | */ |
---|
102 | void Material::setIllum (int illum) |
---|
103 | { |
---|
104 | if (verbose >= 3) |
---|
105 | printf("setting illumModel of Material %s to %i", name, illum); |
---|
106 | illumModel = illum; |
---|
107 | // printf ("setting illumModel to: %i\n", illumModel); |
---|
108 | } |
---|
109 | /** |
---|
110 | \brief Sets the Material Illumination Model. |
---|
111 | \brief illu illumination Model in char* form |
---|
112 | */void Material::setIllum (char* illum) |
---|
113 | { |
---|
114 | setIllum (atoi(illum)); |
---|
115 | } |
---|
116 | |
---|
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 | */ |
---|
123 | void Material::setDiffuse (float r, float g, float b) |
---|
124 | { |
---|
125 | if (verbose >= 3) |
---|
126 | printf ("setting Diffuse Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); |
---|
127 | diffuse[0] = r; |
---|
128 | diffuse[1] = g; |
---|
129 | diffuse[2] = b; |
---|
130 | diffuse[3] = 1.0; |
---|
131 | |
---|
132 | } |
---|
133 | /** |
---|
134 | \brief Sets the Material Diffuse Color. |
---|
135 | \param rgb The red, green, blue channel in char format (with spaces between them) |
---|
136 | */ |
---|
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 | |
---|
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 | */ |
---|
150 | void Material::setAmbient (float r, float g, float b) |
---|
151 | { |
---|
152 | if (verbose >=3) |
---|
153 | printf ("setting Ambient Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); |
---|
154 | ambient[0] = r; |
---|
155 | ambient[1] = g; |
---|
156 | ambient[2] = b; |
---|
157 | ambient[3] = 1.0; |
---|
158 | } |
---|
159 | /** |
---|
160 | \brief Sets the Material Ambient Color. |
---|
161 | \param rgb The red, green, blue channel in char format (with spaces between them) |
---|
162 | */ |
---|
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 | |
---|
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 | */ |
---|
176 | void Material::setSpecular (float r, float g, float b) |
---|
177 | { |
---|
178 | if (verbose >= 3) |
---|
179 | printf ("setting Specular Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); |
---|
180 | specular[0] = r; |
---|
181 | specular[1] = g; |
---|
182 | specular[2] = b; |
---|
183 | specular[3] = 1.0; |
---|
184 | } |
---|
185 | /** |
---|
186 | \brief Sets the Material Specular Color. |
---|
187 | \param rgb The red, green, blue channel in char format (with spaces between them) |
---|
188 | */ |
---|
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 | |
---|
196 | /** |
---|
197 | \brief Sets the Material Shininess. |
---|
198 | \param shini stes the Shininess from float. |
---|
199 | */ |
---|
200 | void Material::setShininess (float shini) |
---|
201 | { |
---|
202 | shininess = shini; |
---|
203 | } |
---|
204 | /** |
---|
205 | \brief Sets the Material Shininess. |
---|
206 | \param shini stes the Shininess from char*. |
---|
207 | */ |
---|
208 | void Material::setShininess (char* shini) |
---|
209 | { |
---|
210 | setShininess (atof(shini)); |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | \brief Sets the Material Transparency. |
---|
215 | \param trans stes the Transparency from int. |
---|
216 | */ |
---|
217 | void Material::setTransparency (float trans) |
---|
218 | { |
---|
219 | if (verbose >= 3) |
---|
220 | printf ("setting Transparency of Material %s to %f\n", name, trans); |
---|
221 | transparency = trans; |
---|
222 | } |
---|
223 | /** |
---|
224 | \brief Sets the Material Transparency. |
---|
225 | \param trans stes the Transparency from char*. |
---|
226 | */ |
---|
227 | void Material::setTransparency (char* trans) |
---|
228 | { |
---|
229 | char tr[20]; |
---|
230 | sscanf (trans, "%s", tr); |
---|
231 | setTransparency (atof(tr)); |
---|
232 | } |
---|
233 | |
---|
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 | */ |
---|
239 | Material* Material::search (char* mtlName) |
---|
240 | { |
---|
241 | if (verbose >=3) |
---|
242 | printf ("Searching for material %s", mtlName); |
---|
243 | Material* searcher = this; |
---|
244 | while (searcher != NULL) |
---|
245 | { |
---|
246 | if (verbose >= 3) |
---|
247 | printf ("."); |
---|
248 | if (!strcmp (searcher->getName(), mtlName)) |
---|
249 | { |
---|
250 | if (verbose >= 3) |
---|
251 | printf ("found.\n"); |
---|
252 | return searcher; |
---|
253 | } |
---|
254 | searcher = searcher->nextMat; |
---|
255 | } |
---|
256 | return NULL; |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | \brief sets the material with which the following Faces will be painted |
---|
261 | */ |
---|
262 | bool Material::select (void) |
---|
263 | { |
---|
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); |
---|
273 | |
---|
274 | // setting up Shininess |
---|
275 | glMaterialf(GL_FRONT, GL_SHININESS, shininess); |
---|
276 | |
---|
277 | // setting illumination Model |
---|
278 | if (illumModel == 1) |
---|
279 | glShadeModel(GL_FLAT); |
---|
280 | else if (illumModel >= 2) |
---|
281 | glShadeModel(GL_SMOOTH); |
---|
282 | } |
---|