-
Notifications
You must be signed in to change notification settings - Fork 8
/
KinemeGLDepthSortSpritePatch.m
279 lines (223 loc) · 7 KB
/
KinemeGLDepthSortSpritePatch.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#import <OpenGL/CGLMacro.h>
#import "KinemeGLDepthSortSpritePatch.h"
#import "KinemeGLDepthSortEnvironmentPatch.h"
@implementation KinemeGLDepthSortLightweightSprite
@synthesize depth;
@synthesize width, height;
@synthesize image;
@synthesize blending, depthTesting, culling;
@synthesize imagePort;
@synthesize blendPort;
@synthesize depthPort;
@synthesize cullingPort;
-(void)setRed:(float)r green:(float)g blue:(float)b alpha:(float)a
{
color[0] = r;
color[1] = g;
color[2] = b;
color[3] = a;
}
-(void)renderOnContext:(QCOpenGLContext*)context
{
float w2 = width/2.0f;
float h2 = height/2.0f;
CGLContextObj cgl_ctx = [context CGLContextObj];
[cullingPort setIndexValue:culling];
[cullingPort setOnOpenGLContext:context];
[depthPort setIndexValue:depthTesting];
[depthPort setOnOpenGLContext:context];
[blendPort setIndexValue:blending];
[blendPort setOnOpenGLContext:context];
[imagePort setImageValue:image];
[imagePort setOnOpenGLContext:context unit:GL_TEXTURE0];
glPushMatrix();
glLoadMatrixd(matrix);
glColor4fv(color);
glNormal3f(0,0,1);
glBegin(GL_QUADS);
{
glTexCoord2f(0,0);
glVertex2f(-w2,-h2);
glTexCoord2f(1,0);
glVertex2f( w2,-h2);
glTexCoord2f(1,1);
glVertex2f( w2, h2);
glTexCoord2f(0,1);
glVertex2f(-w2, h2);
}
glEnd();
glPopMatrix();
[imagePort unsetOnOpenGLContext:context unit:GL_TEXTURE0];
[blendPort unsetOnOpenGLContext:context];
[depthPort unsetOnOpenGLContext:context];
[cullingPort unsetOnOpenGLContext:context];
}
-(void)dealloc
{
[image release];
[super dealloc];
}
- (double*)matrix
{
return matrix;
}
@end
@implementation KinemeGLDepthSortSpritePatch
@synthesize inputX;
@synthesize inputY;
@synthesize inputZ;
@synthesize inputXr;
@synthesize inputYr;
@synthesize inputZr;
@synthesize inputWidth;
@synthesize inputHeight;
@synthesize inputColor;
@synthesize inputImage;
//@synthesize inputMaskImage;
@synthesize inputBlending;
@synthesize inputDepthTesting;
@synthesize inputFaceCulling;
+ (QCPatchExecutionMode)executionModeWithIdentifier:(id)fp8
{
return kQCPatchExecutionModeConsumer;
}
+ (BOOL)allowsSubpatchesWithIdentifier:(id)fp8
{
return NO;
}
+ (BOOL)isSafe
{
return YES;
}
- (id)initWithIdentifier:(id)fp8
{
if(self=[super initWithIdentifier:fp8])
{
[inputWidth setDoubleValue:1];
[inputHeight setDoubleValue:1];
[inputDepthTesting setIndexValue:1]; // Read / Write
[[self userInfo] setObject:@"Kineme GL Depth Sort Sprite" forKey:@"name"];
}
return self;
}
- (BOOL)setup:(QCOpenGLContext *)context
{
KinemeGLDepthSortEnvironmentPatch *p = (KinemeGLDepthSortEnvironmentPatch*)[self parentPatch];
Class KinemeGLDepthSortEnvironmentPatchClass = [KinemeGLDepthSortEnvironmentPatch class];
while( p && ![p isKindOfClass:KinemeGLDepthSortEnvironmentPatchClass] )
p = (KinemeGLDepthSortEnvironmentPatch*)[p parentPatch];
_depthSortEnvironment = p;
// don't alloc helper ports if we're not in the environment
if(_depthSortEnvironment)
{
// initWithNode assigns a parentPatch to the port, so PerformanceInspector can associate image port stats with a patch
// (depthSortEnv in this case)
imagePort = [[QCOpenGLPort_Image allocWithZone:NULL] initWithNode:p arguments:nil];
blendPort = [[QCOpenGLPort_Blending allocWithZone:NULL] initWithNode:p arguments:nil];
depthPort = [[QCOpenGLPort_ZBuffer allocWithZone:NULL] initWithNode:p arguments:nil];
cullingPort = [[QCOpenGLPort_Culling allocWithZone:NULL] initWithNode:p arguments:nil];
}
return YES;
}
- (void)cleanup:(QCOpenGLContext *)context
{
[imagePort release];
[blendPort release];
[depthPort release];
[cullingPort release];
}
- (BOOL)execute:(QCOpenGLContext *)context time:(double)time arguments:(NSDictionary *)arguments
{
CGLContextObj cgl_ctx = [context CGLContextObj];
glPushMatrix();
CGFloat matrix[16];
QCGLMakeTransformationMatrix(matrix,[inputXr doubleValue],[inputYr doubleValue],[inputZr doubleValue],[inputX doubleValue],[inputY doubleValue],[inputZ doubleValue]);
KIGLMultMatrix(matrix);
if(_depthSortEnvironment)
{
KinemeGLDepthSortLightweightSprite *e = [[KinemeGLDepthSortLightweightSprite allocWithZone:NULL] init];
[e setWidth:[inputWidth doubleValue]];
[e setHeight:[inputHeight doubleValue]];
CGFloat r, g, b, a;
[inputColor getRed:&r green:&g blue:&b alpha:&a];
// Effing pre-multiply (holy crap I hate premultiplied alpha... what sissies thought of this?
r *= a;
g *= a;
b *= a;
[e setRed:r green:g blue:b alpha:a];
[e setImage:[inputImage imageValue]];
[e setBlending:[inputBlending indexValue]];
[e setDepthTesting:[inputDepthTesting indexValue]];
[e setCulling:[inputFaceCulling indexValue]];
[e setImagePort:imagePort];
[e setBlendPort:blendPort];
[e setDepthPort:depthPort];
[e setCullingPort:cullingPort];
// preserve the current matrix
{
GLdouble *m = (GLdouble*)[e matrix];
glGetDoublev(GL_MODELVIEW_MATRIX, m);
/*GLdouble *mat = (GLdouble*)[e matrix];
NSLog(@"Matrix: (%x)\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n", self,
mat[0],mat[4],mat[8],mat[12],
mat[1],mat[5],mat[9],mat[13],
mat[2],mat[6],mat[10],mat[14],
mat[3],mat[7],mat[11],mat[15]);*/
// m[12-14] are our x,y,z translation values
double dist = m[14];
[e setDepth:dist];
}
/* opacity's a bit tricky:
if we are wrong, and say we're opaque when we're not, we get wrong output.
if we are wrong, and say we're not opaque when we are, we get correct output, but a performance hit.
Thus, we're conservative on declaring outselves opaque
we're opaque if:
our blend mode is 0 (replace)
our color alpha is 1.0 and our image provider says we don't have alpha
*/
BOOL isOpaque = ([inputBlending indexValue] == 0);
if(!isOpaque)
isOpaque = a >= 1.0 && ![[[inputImage imageValue] provider] hasAlpha];
[_depthSortEnvironment addElement:e isOpaque:isOpaque];
[e release];
}
else
[self renderOnContext:context];
glPopMatrix();
return YES;
}
- (void)renderOnContext:(QCOpenGLContext *)context
{
float w2=[inputWidth doubleValue]/2.0f;
float h2=[inputHeight doubleValue]/2.0f;
CGLContextObj cgl_ctx = [context CGLContextObj];
[inputFaceCulling setOnOpenGLContext: context];
[inputDepthTesting setOnOpenGLContext: context];
[inputBlending setOnOpenGLContext: context];
[inputImage setOnOpenGLContext: context unit:GL_TEXTURE0];
CGFloat color[4];
[inputColor getRed:&color[0] green:&color[1] blue:&color[2] alpha:&color[3]];
// Effing pre-multiply (holy crap I hate premultiplied alpha... what sissies thought of this?
color[0] *= color[3];
color[1] *= color[3];
color[2] *= color[3];
KIGLColor4v(color);
glNormal3f(0,0,1);
glBegin(GL_QUADS);
{
glTexCoord2f(0,0);
glVertex2f(-w2,-h2);
glTexCoord2f(1,0);
glVertex2f( w2,-h2);
glTexCoord2f(1,1);
glVertex2f( w2, h2);
glTexCoord2f(0,1);
glVertex2f(-w2, h2);
}
glEnd();
[inputImage unsetOnOpenGLContext: context unit:GL_TEXTURE0];
[inputBlending unsetOnOpenGLContext: context];
[inputDepthTesting unsetOnOpenGLContext: context];
[inputFaceCulling unsetOnOpenGLContext: context];
}
@end