-
Notifications
You must be signed in to change notification settings - Fork 8
/
KinemeGLTriangleStructurePatch.m
154 lines (128 loc) · 4.41 KB
/
KinemeGLTriangleStructurePatch.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
#import <OpenGL/gl.h>
#import <OpenGL/OpenGL.h>
#import <OpenGL/CGLMacro.h>
#import "KinemeGLTriangleStructurePatch.h"
@implementation KinemeGLTriangleStructurePatch : QCPatch
+ (QCPatchExecutionMode)executionModeWithIdentifier:(id)fp8
{
return 1;
}
+ (BOOL)allowsSubpatchesWithIdentifier:(id)fp8
{
return NO;
}
+ (BOOL)isSafe
{
return YES;
}
- (id)initWithIdentifier:(id)fp8
{
self = [super initWithIdentifier:fp8];
if(self)
{
[inputDepth setIndexValue:1]; // set normal read/write depth testing
[inputCulling setIndexValue:1]; // set normal backface culling by default
[inputType setMaxIndexValue: 1];
[[self userInfo] setObject:@"Kineme GL Triangle Structure" forKey:@"name"];
}
return self;
}
- (BOOL)execute:(QCOpenGLContext *)context time:(double)time arguments:(NSDictionary *)arguments
{
QCStructure *triangleStruct = [inputTriangles structureValue];
if(triangleStruct == nil)
return YES; // no points, do nothing
if([triangleStruct count] == 0)
return YES;
CGFloat defaultRed,defaultGreen,defaultBlue,defaultAlpha;
[inputColor getRed:&defaultRed green:&defaultGreen blue:&defaultBlue alpha:&defaultAlpha];
CGLContextObj cgl_ctx = [context CGLContextObj];
/* apply environmental changes to our CGL context. */
[inputBlending setOnOpenGLContext: context];
[inputImage setOnOpenGLContext: context unit:GL_TEXTURE0];
[inputDepth setOnOpenGLContext: context];
[inputCulling setOnOpenGLContext: context];
Class QCStructureClass = [QCStructure class];
Class NSArrayClass = [NSArray class];
BOOL keyed = ([[triangleStruct memberAtIndex:0] isKindOfClass:QCStructureClass] && [[triangleStruct memberAtIndex:0] memberForKey:@"X"] != nil);
BOOL hasNormals = ([[triangleStruct memberAtIndex:0] isKindOfClass:QCStructureClass]) &&
([[triangleStruct memberAtIndex:0] memberForKey:@"NX"] != nil) &&
([[triangleStruct memberAtIndex:0] memberForKey:@"NY"] != nil) &&
([[triangleStruct memberAtIndex:0] memberForKey:@"NZ"] != nil);
glPushMatrix();
glTranslated([inputXPosition doubleValue],
[inputYPosition doubleValue],
[inputZPosition doubleValue]);
glRotated([inputXRotation doubleValue],1.0,0.0,0.0);
glRotated([inputYRotation doubleValue],0.0,1.0,0.0);
glRotated([inputZRotation doubleValue],0.0,0.0,1.0);
if([inputType indexValue] == 1)
glBegin(GL_TRIANGLE_STRIP);
else
glBegin(GL_TRIANGLES);
{
if(!keyed)
KIGLColor4(defaultRed,defaultGreen,defaultBlue,defaultAlpha);
for(id point in (GFList*)[triangleStruct _list])
{
if( [point isKindOfClass:QCStructureClass] )
{
if(keyed)
{
{
CGFloat outColor[4] = {defaultRed,defaultGreen,defaultBlue,defaultAlpha};
if( [point memberForKey:@"R"] )
outColor[0] *= [[point memberForKey:@"R"] doubleValue];
if( [point memberForKey:@"G"] )
outColor[1] *= [[point memberForKey:@"G"] doubleValue];
if( [point memberForKey:@"B"] )
outColor[2] *= [[point memberForKey:@"B"] doubleValue];
if( [point memberForKey:@"A"] )
outColor[3] *= [[point memberForKey:@"A"] doubleValue];
KIGLColor4v(outColor);
}
glTexCoord2f
(
[[point memberForKey:@"U"] doubleValue],
[[point memberForKey:@"V"] doubleValue]
);
if(hasNormals)
glNormal3f
(
[[point memberForKey:@"NX"] doubleValue],
[[point memberForKey:@"NY"] doubleValue],
[[point memberForKey:@"NZ"] doubleValue]
);
glVertex3d
(
[[point memberForKey:@"X"] doubleValue],
[[point memberForKey:@"Y"] doubleValue],
[[point memberForKey:@"Z"] doubleValue]
);
}
else
glVertex3d(
[[point memberAtIndex:0] doubleValue],
[[point memberAtIndex:1] doubleValue],
[[point memberAtIndex:2] doubleValue]);
}
else if( [point isKindOfClass:NSArrayClass] )
glVertex3d(
[[(NSArray *)point objectAtIndex:0] doubleValue],
[[(NSArray *)point objectAtIndex:1] doubleValue],
[[(NSArray *)point objectAtIndex:2] doubleValue]);
}
}
glEnd();
glPopMatrix();
/* Here we undo our CGLContextObj changes. Undoing image is mandatory; OpenGL
stack overflows internally otherwise, giving garbage output after a couple frames.
other are just good form.
*/
[inputCulling unsetOnOpenGLContext: context];
[inputDepth unsetOnOpenGLContext: context];
[inputImage unsetOnOpenGLContext: context unit:GL_TEXTURE0];
[inputBlending unsetOnOpenGLContext: context];
return YES;
}
@end