-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathKinemeGLGridEditorPatch.m
216 lines (192 loc) · 7.74 KB
/
KinemeGLGridEditorPatch.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
/*
* KinemeGLGridEditorPatch.m
* GLTools
*
* Created by Christopher Wright on 8/16/09.
* Copyright (c) 2009 Kosada Incorporated. All rights reserved.
*
*/
#import "KinemeGLGridEditorPatch.h"
@implementation KinemeGLGridEditorPatch : QCPatch
+ (QCPatchExecutionMode)executionModeWithIdentifier:(id)fp8
{
return 0;
}
+ (BOOL)allowsSubpatchesWithIdentifier:(id)fp8
{
return NO;
}
- (id)initWithIdentifier:(id)fp8
{
if(self=[super initWithIdentifier:fp8])
{
[[self userInfo] setObject:@"Kineme GL Grid Editor" forKey:@"name"];
}
return self;
}
- (void)cleanup:(QCOpenGLContext *)context
{
if(vertex)
free(vertex);
vertex = NULL;
}
- (BOOL)execute:(QCOpenGLContext *)context time:(double)time arguments:(NSDictionary *)arguments
{
unsigned int x, y;
if(vertex == NULL || [inputGrid wasUpdated] || [inputResetGrid booleanValue])
{
selected = 0;
QCStructure *grid = [inputGrid structureValue];
width = [[grid memberForKey:@"width"] intValue];
height = [[grid memberForKey:@"height"] intValue];
if(width == 0 || height == 0)
{
[outputGrid setStructureValue:nil];
return YES;
}
vertex = (KinemeVertex*)realloc(vertex, sizeof(KinemeVertex)*(width+1)*(height+1));
CGFloat r, g, b, a;
[inputColor getRed:&r green:&g blue:&b alpha:&a];
QCStructure *vertexStruct;
for(y = 0; y <= height; ++y)
for(x = 0; x <= width; ++x)
{
id key = [[NSString allocWithZone:NULL] initWithFormat:@"vertex_%i",1+y*(width+1)+x];
vertexStruct = [grid memberForKey:key];
[key release]; // don't flood the autorelease pool
vertex[y*(width+1)+x].x = [[vertexStruct memberForKey:@"x"] doubleValue];
vertex[y*(width+1)+x].y = [[vertexStruct memberForKey:@"y"] doubleValue];
vertex[y*(width+1)+x].z = [[vertexStruct memberForKey:@"z"] doubleValue];
vertex[y*(width+1)+x].r = r;
vertex[y*(width+1)+x].g = g;
vertex[y*(width+1)+x].b = b;
vertex[y*(width+1)+x].a = a;
vertex[y*(width+1)+x].u = [[vertexStruct memberForKey:@"u"] doubleValue];
vertex[y*(width+1)+x].v = [[vertexStruct memberForKey:@"v"] doubleValue];
}
}
if(width == 0 || height == 0)
{
[outputGrid setStructureValue:nil];
return YES;
}
if([inputAutoIncrementOnCommit booleanValue] == FALSE)
selected = [inputSelection indexValue] % ((width+1)*(height+1));
/* modification mode */
if([inputSelectionEnabled booleanValue] == TRUE)
{
//NSLog(@"selection is enabled");
// This is a bit different: we want falling edge so mouse input is intuitive (drag-and-release)
if([inputCommitChanges booleanValue] == TRUE && oldCommitValue == FALSE)
{
//NSLog(@"Commiting Changes to %i",selected);
//selected = [inputSelection indexValue];
//selected %= (width+1)*(height+1);
vertex[selected].x = [inputX doubleValue];
vertex[selected].y = [inputY doubleValue];
vertex[selected].z = [inputZ doubleValue];
if([inputU doubleValue] >= 0.0)
{
//NSLog(@"u is %f, updating",[inputU doubleValue]);
vertex[selected].u = [inputU doubleValue];
}
if([inputV doubleValue] >= 0.0)
{
//NSLog(@"v is %f, updating",[inputV doubleValue]);
vertex[selected].v = [inputV doubleValue];
}
[inputColor getRed:&vertex[selected].r
green:&vertex[selected].g
blue:&vertex[selected].b
alpha:&vertex[selected].a];
/*if([inputAutoIncrementOnCommit booleanValue] == TRUE)
{
++selected;
[inputSelection setIndexValue: selected];
oldSelectionValue = FALSE; // need the next block to execute to fill in proper values
}*/
{
unsigned int nextSelected = (selected + 1) % ((width+1) * (height+1));
[inputX setDoubleValue:vertex[nextSelected].x];
[inputY setDoubleValue:vertex[nextSelected].y];
[inputZ setDoubleValue:vertex[nextSelected].z];
[inputU setDoubleValue:vertex[nextSelected].u];
[inputV setDoubleValue:vertex[nextSelected].v];
[inputColor setRed:vertex[nextSelected].r
green:vertex[nextSelected].g
blue:vertex[nextSelected].b
alpha:vertex[nextSelected].a];
}
}
if(oldSelectionValue == FALSE) // rising edge. populate inputs
{
[inputX setDoubleValue:vertex[selected].x];
[inputY setDoubleValue:vertex[selected].y];
[inputZ setDoubleValue:vertex[selected].z];
[inputU setDoubleValue:vertex[selected].u];
[inputV setDoubleValue:vertex[selected].v];
[inputColor setRed:vertex[selected].r
green:vertex[selected].g
blue:vertex[selected].b
alpha:vertex[selected].a];
}
}
/* generate the grid structure */
NSMutableArray *meshArray = [[NSMutableArray allocWithZone:NULL] initWithCapacity:(width+1)*(height+1)];
for(y = 0; y <= height; ++y)
{
for(x = 0; x <= width; ++x)
{
QCStructure *vertexStructure = [[QCStructure allocWithZone:NULL] init];
// this hammers the autorelease pool -- nsnumber is pricey :/
if( y*(width+1)+x == selected && [inputSelectionEnabled booleanValue] == TRUE)
{
//NSLog(@"overriding %i",selected);
[vertexStructure setMember:[NSNumber numberWithDouble: [inputX doubleValue]] forKey:@"x"];
[vertexStructure setMember:[NSNumber numberWithDouble: [inputY doubleValue]] forKey:@"y"];
[vertexStructure setMember:[NSNumber numberWithDouble: [inputZ doubleValue]] forKey:@"z"];
if([inputU doubleValue] >= 0.0)
[vertexStructure setMember:[NSNumber numberWithDouble: [inputU doubleValue]] forKey:@"u"];
else
[vertexStructure setMember:[NSNumber numberWithDouble: vertex[y*(width+1)+x].u] forKey:@"u"];
if([inputV doubleValue] >= 0.0)
[vertexStructure setMember:[NSNumber numberWithDouble: [inputV doubleValue]] forKey:@"v"];
else
[vertexStructure setMember:[NSNumber numberWithDouble: vertex[y*(width+1)+x].v] forKey:@"v"];
[vertexStructure setMember:[NSNumber numberWithDouble: [inputColor redComponent]] forKey:@"r"];
[vertexStructure setMember:[NSNumber numberWithDouble: [inputColor greenComponent]] forKey:@"g"];
[vertexStructure setMember:[NSNumber numberWithDouble: [inputColor blueComponent]] forKey:@"b"];
[vertexStructure setMember:[NSNumber numberWithDouble: [inputColor alphaComponent]] forKey:@"a"];
}
else
{
[vertexStructure setMember:[NSNumber numberWithDouble: vertex[y*(width+1)+x].x] forKey:@"x"];
[vertexStructure setMember:[NSNumber numberWithDouble: vertex[y*(width+1)+x].y] forKey:@"y"];
[vertexStructure setMember:[NSNumber numberWithDouble: vertex[y*(width+1)+x].z] forKey:@"z"];
[vertexStructure setMember:[NSNumber numberWithDouble: vertex[y*(width+1)+x].u] forKey:@"u"];
[vertexStructure setMember:[NSNumber numberWithDouble: vertex[y*(width+1)+x].v] forKey:@"v"];
[vertexStructure setMember:[NSNumber numberWithDouble: vertex[y*(width+1)+x].r] forKey:@"r"];
[vertexStructure setMember:[NSNumber numberWithDouble: vertex[y*(width+1)+x].g] forKey:@"g"];
[vertexStructure setMember:[NSNumber numberWithDouble: vertex[y*(width+1)+x].b] forKey:@"b"];
[vertexStructure setMember:[NSNumber numberWithDouble: vertex[y*(width+1)+x].a] forKey:@"a"];
}
[meshArray addObject:vertexStructure];
[vertexStructure release];
}
}
QCStructure *meshStructure = [[QCStructure allocWithZone:NULL] initWithMembers:meshArray keyPrefix:@"vertex_"];
[meshArray release];
[meshStructure setMember:[NSNumber numberWithInt:width] forKey:@"width"];
[meshStructure setMember:[NSNumber numberWithInt:height] forKey:@"height"];
[outputGrid setStructureValue: meshStructure];
[meshStructure release];
// it's safe to auto increment now
if( [inputAutoIncrementOnCommit booleanValue] == TRUE &&
[inputCommitChanges booleanValue] == TRUE && oldCommitValue == FALSE)
selected = (selected + 1) % ((width+1) * (height+1));
[outputSelection setIndexValue:selected];
oldSelectionValue = [inputSelectionEnabled booleanValue];
oldCommitValue = [inputCommitChanges booleanValue];
return YES;
}
@end