Oolite
Loading...
Searching...
No Matches
OOShaderMaterial.h
Go to the documentation of this file.
1/*
2
3OOShaderMaterial.h
4
5Managers a combination of a shader program, textures and uniforms.
6
7
8Copyright (C) 2007-2013 Jens Ayton
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files (the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26SOFTWARE.
27
28*/
29
30#import "OOBasicMaterial.h"
31#import "OOMaths.h"
32#import "OOWeakReference.h"
33
34#if OO_SHADERS
35
36@class OOShaderProgram, OOTexture;
37
38enum {
39 // Conversion settings for uniform bindings
40 kOOUniformConvertClamp = 0x0001U,
41 kOOUniformConvertNormalize = 0x0002U,
42 kOOUniformConvertToMatrix = 0x0004U,
43 kOOUniformBindToSuperTarget = 0x0008U,
44
45 kOOUniformConvertDefaults = kOOUniformConvertToMatrix | kOOUniformBindToSuperTarget
46};
47typedef uint16_t OOUniformConvertOptions;
48
49@interface OOShaderMaterial : OOBasicMaterial {
50@private
51 OOShaderProgram* shaderProgram;
52 NSMutableDictionary* uniforms;
53
54 uint32_t texCount;
55 OOTexture** textures;
56
57 OOWeakReference* bindingTarget;
58}
59
60+ (BOOL)configurationDictionarySpecifiesShaderMaterial:(NSDictionary*)configuration;
61
62/* Set up an OOShaderMaterial.
63
64 Configuration should be a dictionary equivalent to an entry in a
65 shipdata.plist "shaders" dictionary. Specifically, keys OOShaderMaterial
66 will look for are currently:
67 textures array of texture file names.
68 vertex_shader name of vertex shader file.
69 fragment_shader name of fragment shader file.
70 uniforms dictionary of uniforms. Values are either reals or
71 dictionaries containing:
72 type "int", "texture" or "float"
73 value number
74 gloss gloss value of material, float between 0.0 and 1.0, defaults to 0.5
75
76 Macros is a dictionary which is converted to macro definitions and
77 prepended to shader source code. It should be used to specify the
78 availability if uniforms you tend to register, and other macros such as
79 bug fix identifiers. For example, the
80 dictionary:
81 { "OO_ENGINE_LEVEL" = 1; }
82
83 will be transformed into:
84 #define OO_ENGINE_LEVEL 1
85*/
86+ (instancetype)shaderMaterialWithName:(NSString*)name
87 configuration:(NSDictionary*)configuration
88 macros:(NSDictionary*)macros
89 bindingTarget:(id<OOWeakReferenceSupport>)target;
90
91- (id)initWithName:(NSString*)name
92 configuration:(NSDictionary*)configuration
93 macros:(NSDictionary*)macros
94 bindingTarget:(id<OOWeakReferenceSupport>)target;
95
96/* Bind a uniform to a property of an object.
97
98 SelectorName should specify a method of source which returns the desired
99 value; it will be called every time -apply is, assuming uniformName is
100 used in the shader. (If not, OOShaderMaterial will not track the binding.)
101
102 A bound method must not take any parameters, and must return one of the
103 following types:
104 * Any integer or float type.
105 * NSNumber.
106 * Vector.
107 * Quaternion.
108 * OOMatrix.
109 * OOColor.
110
111 The "convert" flag has different meanings for different types:
112 * For int, float or NSNumber, it clamps to the range [0..1].
113 * For Vector, it normalizes.
114 * For Quaternion, it converts to a rotation matrix (instead of a vector).
115
116 NOTE: this method *does not* check against the whitelist. See
117 -bindSafeUniform:toObject:propertyNamed:convertOptions: below.
118*/
119- (BOOL)bindUniform:(NSString*)uniformName
120 toObject:(id<OOWeakReferenceSupport>)target
121 property:(SEL)selector
122 convertOptions:(OOUniformConvertOptions)options;
123
124/* Bind a uniform to a property of an object.
125
126 This is similar to -bindUniform:toObject:property:convertOptions:, except
127 that it checks against OOUniformBindingPermitted().
128*/
129- (BOOL)bindSafeUniform:(NSString*)uniformName
130 toObject:(id<OOWeakReferenceSupport>)target
131 propertyNamed:(NSString*)property
132 convertOptions:(OOUniformConvertOptions)options;
133
134/* Set a uniform value.
135 */
136- (void)setUniform:(NSString*)uniformName intValue:(int)value;
137- (void)setUniform:(NSString*)uniformName floatValue:(float)value;
138- (void)setUniform:(NSString*)uniformName vectorValue:(GLfloat[4])value;
139- (void)setUniform:(NSString*)uniformName vectorObjectValue:(id)value; // Array of four numbers, or something that can be OOVectorFromObject()ed.
140- (void)setUniform:(NSString*)uniformName quaternionValue:(Quaternion)value asMatrix:(BOOL)asMatrix;
141
142/* Add constant uniforms. Same format as uniforms dictionary of configuration
143 parameter to -initWithConfiguration:macros:. The target parameter is used
144 for bindings.
145
146 Additionally, the target may implement the following method, used to seed
147 any random bindings:
148- (uint32_t) randomSeedForShaders;
149*/
150- (void)addUniformsFromDictionary:(NSDictionary*)uniformDefs withBindingTarget:(id<OOWeakReferenceSupport>)target;
151
152@end
153
154@interface NSObject (ShaderBindingHierarchy)
155
156/* Informal protocol for objects to "forward" their shader bindings up a
157 hierarchy (for instance, subentities to parent entities).
158*/
159- (id<OOWeakReferenceSupport>)superShaderBindingTarget;
160
161@end
162
163enum {
164 /* ID of vertex attribute used for tangents. A fixed ID is used for
165 simplicty.
166 NOTE: on Nvidia hardware, attribute 15 is aliased to
167 gl_MultiTexCoord7. This is not expected to become a problem.
168 */
169 kTangentAttributeIndex = 15
170};
171
172/* OOUniformBindingPermitted()
173
174 Predicate determining whether a given property may be used as a binding.
175 Client code is responsible for implementing this.
176*/
177BOOL OOUniformBindingPermitted(NSString* propertyName, id bindingTarget);
178
179@interface NSObject (OOShaderMaterialTargetOptional)
180
181- (uint32_t)randomSeedForShaders;
182
183@end
184
185// Material specifier dictionary keys.
186extern NSString* const kOOVertexShaderSourceKey;
187extern NSString* const kOOVertexShaderNameKey;
188extern NSString* const kOOFragmentShaderSourceKey;
189extern NSString* const kOOFragmentShaderNameKey;
190extern NSString* const kOOTexturesKey;
191extern NSString* const kOOTextureObjectsKey;
192extern NSString* const kOOUniformsKey;
193extern NSString* const kOOIsSynthesizedMaterialConfigurationKey;
194extern NSString* const kOOIsSynthesizedMaterialMacrosKey;
195
196#endif // OO_SHADERS
BOOL OOUniformBindingPermitted(NSString *propertyName, id bindingTarget)