Oolite
Loading...
Searching...
No Matches
OOBasicMaterial.m
Go to the documentation of this file.
1/*
2
3OOBasicMaterial.m
4
5
6Copyright (C) 2007-2013 Jens Ayton
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
28#import "OOBasicMaterial.h"
32#import "OOTexture.h"
33#import "Universe.h"
34
36
37#define FACE GL_FRONT_AND_BACK
38
39@implementation OOBasicMaterial
40
41- (id)initWithName:(NSString*)name
42{
43 self = [super init];
44 if (EXPECT_NOT(self == nil))
45 return nil;
46
47 materialName = [name copy];
48
49 [self setDiffuseRed:1.0f green:1.0f blue:1.0f alpha:1.0f];
50 [self setAmbientRed:1.0f green:1.0f blue:1.0f alpha:1.0f];
51 specular[3] = 1.0;
52 emission[3] = 1.0;
53
54 return self;
55}
56
57- (id)initWithName:(NSString*)name configuration:(NSDictionary*)configuration
58{
59 id colorDesc = nil;
60 int specularExponent;
61
62 self = [self initWithName:name];
63 if (EXPECT_NOT(self == nil))
64 return nil;
65
66 if (configuration == nil)
67 configuration = [NSDictionary dictionary];
68
69 colorDesc = [configuration oo_diffuseColor];
70 if (colorDesc != nil)
72
73 colorDesc = [configuration oo_ambientColor];
74 if (colorDesc != nil)
76 else
77 [self setAmbientColor:[self diffuseColor]];
78
79 colorDesc = [configuration oo_emissionColor];
80 if (colorDesc != nil)
82
83 specularExponent = [configuration oo_specularExponent];
84 if (specularExponent != 0 && [self permitSpecular]) {
85 colorDesc = [configuration oo_specularColor];
86 [self setShininess:specularExponent];
87 if (colorDesc != nil)
89 }
90
91 return self;
92}
93
94- (void)dealloc
95{
96 [super willDealloc];
97 [materialName release];
98
99 [super dealloc];
100}
101
102- (NSString*)name
103{
104 return materialName;
105}
106
107- (BOOL)doApply
108{
109 OOGL(glMaterialfv(FACE, GL_DIFFUSE, diffuse));
110 OOGL(glMaterialfv(FACE, GL_SPECULAR, specular));
111 OOGL(glMaterialfv(FACE, GL_AMBIENT, ambient));
112 OOGL(glMaterialfv(FACE, GL_EMISSION, emission));
113 OOGL(glMateriali(FACE, GL_SHININESS, shininess));
114 if ([self isMemberOfClass:[OOBasicMaterial class]]) {
116 }
117
118 return YES;
119}
120
121- (void)unapplyWithNext:(OOMaterial*)next
122{
123 if (![next isKindOfClass:[OOBasicMaterial class]]) {
125 sDefaultMaterial = [[OOBasicMaterial alloc] initWithName:@"<default material>"];
127 }
128}
129
131{
132 return [OOColor colorWithRed:diffuse[0]
133 green:diffuse[1]
134 blue:diffuse[2]
135 alpha:diffuse[3]];
136}
137
138- (void)setDiffuseColor:(OOColor*)color
139{
140 if (color != nil) {
145 }
146}
147
148- (void)setAmbientAndDiffuseColor:(OOColor*)color
149{
150 [self setAmbientColor:color];
151 [self setDiffuseColor:color];
152}
153
155{
156 return [OOColor colorWithRed:specular[0]
157 green:specular[1]
158 blue:specular[2]
159 alpha:specular[3]];
160}
161
162- (void)setSpecularColor:(OOColor*)color
163{
164 if (color != nil) {
169 }
170}
171
173{
174 return [OOColor colorWithRed:ambient[0]
175 green:ambient[1]
176 blue:ambient[2]
177 alpha:ambient[3]];
178}
179
180- (void)setAmbientColor:(OOColor*)color
181{
182 if (color != nil) {
187 }
188}
189
191{
192 return [OOColor colorWithRed:emission[0]
193 green:emission[1]
194 blue:emission[2]
195 alpha:emission[3]];
196}
197
198- (void)setEmissionColor:(OOColor*)color
199{
200 if (color != nil) {
205 }
206}
207
208- (void)getDiffuseComponents:(GLfloat[4])outComponents
209{
210 memcpy(outComponents, diffuse, 4 * sizeof *outComponents);
211}
212
213- (void)setDiffuseComponents:(const GLfloat[4])components
214{
215 memcpy(diffuse, components, 4 * sizeof *components);
216}
217
218- (void)setAmbientAndDiffuseComponents:(const GLfloat[4])components
219{
220 [self setAmbientComponents:components];
221 [self setDiffuseComponents:components];
222}
223
224- (void)getSpecularComponents:(GLfloat[4])outComponents
225{
226 memcpy(outComponents, specular, 4 * sizeof *outComponents);
227}
228
229- (void)setSpecularComponents:(const GLfloat[4])components
230{
231 memcpy(specular, components, 4 * sizeof *components);
232}
233
234- (void)getAmbientComponents:(GLfloat[4])outComponents
235{
236 memcpy(outComponents, ambient, 4 * sizeof *outComponents);
237}
238
239- (void)setAmbientComponents:(const GLfloat[4])components
240{
241 memcpy(ambient, components, 4 * sizeof *components);
242}
243
244- (void)getEmissionComponents:(GLfloat[4])outComponents
245{
246 memcpy(outComponents, emission, 4 * sizeof *outComponents);
247}
248
249- (void)setEmissionComponents:(const GLfloat[4])components
250{
251 memcpy(emission, components, 4 * sizeof *components);
252}
253
254- (void)setDiffuseRed:(GLfloat)r green:(GLfloat)g blue:(GLfloat)b alpha:(GLfloat)a
255{
256 diffuse[0] = r;
257 diffuse[1] = g;
258 diffuse[2] = b;
259 diffuse[3] = a;
260}
261
262- (void)setAmbientAndDiffuseRed:(GLfloat)r green:(GLfloat)g blue:(GLfloat)b alpha:(GLfloat)a
263{
264 [self setAmbientRed:r green:g blue:b alpha:a];
265 [self setDiffuseRed:r green:g blue:b alpha:a];
266}
267
268- (void)setSpecularRed:(GLfloat)r green:(GLfloat)g blue:(GLfloat)b alpha:(GLfloat)a
269{
270 specular[0] = r;
271 specular[1] = g;
272 specular[2] = b;
273 specular[3] = a;
274}
275
276- (void)setAmbientRed:(GLfloat)r green:(GLfloat)g blue:(GLfloat)b alpha:(GLfloat)a
277{
278 ambient[0] = r;
279 ambient[1] = g;
280 ambient[2] = b;
281 ambient[3] = a;
282}
283
284- (void)setEmissionRed:(GLfloat)r green:(GLfloat)g blue:(GLfloat)b alpha:(GLfloat)a
285{
286 emission[0] = r;
287 emission[1] = g;
288 emission[2] = b;
289 emission[3] = a;
290}
291
292- (uint8_t)shininess
293{
294 return shininess;
295}
296
297- (void)setShininess:(uint8_t)value
298{
299 shininess = MIN(value, 128);
300}
301
303{
304 return ![UNIVERSE reducedDetail];
305}
306
307#ifdef OO_DEBUG_MONITOR_ENABLED
308- (NSSet*)allTextures
309{
310 return [NSSet set];
311}
312#endif
313
314@end
static OOBasicMaterial * sDefaultMaterial
#define FACE
#define EXPECT_NOT(x)
#define MIN(A, B)
Definition OOMaths.h:91
#define OOGL(statement)
Definition OOOpenGL.h:262
return nil
NSString * materialName
void setAmbientRed:green:blue:alpha:(GLfloat r,[green] GLfloat g,[blue] GLfloat b,[alpha] GLfloat a)
void setShininess:(uint8_t value)
void setEmissionColor:(OOColor *color)
void setDiffuseColor:(OOColor *color)
id initWithName:(NSString *name)
OOColor * ambientColor()
void setSpecularColor:(OOColor *color)
GLfloat emission[4]
OOColor * diffuseColor()
OOColor * emmisionColor()
GLfloat specular[4]
void setAmbientComponents:(const GLfloat[4] components)
void setSpecularRed:green:blue:alpha:(GLfloat r,[green] GLfloat g,[blue] GLfloat b,[alpha] GLfloat a)
OOColor * specularColor()
void setDiffuseComponents:(const GLfloat[4] components)
void setDiffuseRed:green:blue:alpha:(GLfloat r,[green] GLfloat g,[blue] GLfloat b,[alpha] GLfloat a)
void setEmissionRed:green:blue:alpha:(GLfloat r,[green] GLfloat g,[blue] GLfloat b,[alpha] GLfloat a)
void setAmbientColor:(OOColor *color)
OOColor * colorWithRed:green:blue:alpha:(float red,[green] float green,[blue] float blue,[alpha] float alpha)
Definition OOColor.m:107
float alphaComponent()
Definition OOColor.m:442
float blueComponent()
Definition OOColor.m:339
OOColor * colorWithDescription:(id description)
Definition OOColor.m:135
float redComponent()
Definition OOColor.m:329
float greenComponent()
Definition OOColor.m:334
void dealloc()
Definition OOMaterial.m:41
void willDealloc()
Definition OOMaterial.m:131
void applyNone()
Definition OOTexture.m:263