-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_main.c
381 lines (344 loc) · 10.1 KB
/
my_main.c
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "z_buffer.h"
#include "my_main.h"
void print_knobs(){
int i;
printf("--------------------------------------\nID\t\tName\t\tValue\n");
for(i = 0; i < lastsym; i++)
if(symtab[i].type == SYM_VALUE)
printf("%d\t\t%s\t\t%6.2f\n", i, symtab[i].name, symtab[i].s.value);
}
void move(stack *hay, int i, double knobby){
matrix_mult_reverse(&hay -> matrices[hay -> top], make_translate(op[i].op.move.d[0] * knobby, op[i].op.move.d[1] * knobby, op[i].op.move.d[2] * knobby));
}
void scale(stack *hay, int i, double knobby){
matrix_mult_reverse(&hay -> matrices[hay -> top], make_scale(op[i].op.scale.d[0] * knobby, op[i].op.scale.d[1] * knobby, op[i].op.scale.d[2] * knobby));
}
void rotate(stack *hay, int i, double knobby){
if(op[i].op.rotate.axis == 0)
matrix_mult_reverse(&hay -> matrices[hay -> top], make_rotX(op[i].op.rotate.degrees * knobby));
else if(op[i].op.rotate.axis == 1)
matrix_mult_reverse(&hay -> matrices[hay -> top], make_rotY(op[i].op.rotate.degrees * knobby));
else
matrix_mult_reverse(&hay -> matrices[hay -> top], make_rotZ(op[i].op.rotate.degrees * knobby));
}
void pass_one(){
int i, b = 0, f = 0, v = 0;
for(i = 0; i < lastop; i++)
switch (op[i].opcode){
case BASENAME:
strncpy(basename, op[i].op.basename.p -> name, sizeof(basename));
b++;
break;
case FRAMES:
frames = op[i].op.frames.num_frames;
f++;
break;
case VARY:
v++;
varies++;
break;
}
if(v > 0){
if(b == 0)
printf("Error: No Basename\n");
if(f == 0)
printf("Error: No Frames\n");
}
if(b > 1)
printf("Error: Conflicting Basenames\n");
if(f > 1)
printf("Error: Conflicting Numbers of Frames\n");
//printf("Frames in pass1 = %d\n", frames);
}
struct knob_value * add_knob_value(char k_name[64], double k_value, struct knob_value *array, int type){
struct knob_value *new = (struct knob_value *)malloc(sizeof(struct knob_value));
new -> value = k_value;
strcpy (new -> name, k_name);
new -> next = array;
new -> animagus = type;
array = new;
return array;
}
struct knob_value * get_knob(char k_name[64], int frame){
struct knob_value *temp;
temp = frame_values[frame];
while(temp != NULL){
if(!strncmp(temp -> name, k_name, 64))
return temp;
temp = temp -> next;
}
return temp;
}
void pass_two(){
int i, j, start, end;
double step, curr_val;
frame_values = (struct knob_value **)malloc(frames * sizeof(struct knob_value *));
for(i = 0; i < frames; i++)
frame_values[i] = NULL;
struct knob_value *temp;
for(i = 0; i < lastop; i++)
switch(op[i].opcode){
case VARY:
start = op[i].op.vary.start_frame;
end = op[i].op.vary.end_frame;
step = (op[i].op.vary.end_val - op[i].op.vary.start_val) / (end - start);
curr_val = op[i].op.vary.start_val;
//Preceding Frames
for(j = start - 1; j >= 0; j--){
temp = get_knob(op[i].op.vary.p -> name, j);
if(temp == NULL)
frame_values[j] = add_knob_value(op[i].op.vary.p -> name, curr_val, frame_values[j], 2);
else if(temp -> animagus == 2)
temp -> value = curr_val;
else
break;
//printf("Value at frame %d is %6.2f for knob %s\n", j, curr_val, op[i].op.vary.p -> name);
}
//printf("Current Value: %6.2f\n", curr_val);
//Varied Frames
for(j = start; j <= end; j++){
temp = get_knob(op[i].op.vary.p -> name, j);
if(temp == NULL)
frame_values[j] = add_knob_value(op[i].op.vary.p -> name, curr_val, frame_values[j], 0);
else if(temp -> animagus == 0){
printf("Error: Overlapping Varies\n");
break;
}
else{
temp -> value = curr_val;
temp -> animagus = 0;
}
//printf("Value at frame %d is %6.2f for knob %s\n", j, curr_val, op[i].op.vary.p -> name);
curr_val += step;
}
//printf("Current Value: %6.2f\n", curr_val);
//Proceding Frames
curr_val -= step;
for(j = end + 1; j < frames; j++){
temp = get_knob(op[i].op.vary.p -> name, j);
if(temp == NULL)
frame_values[j] = add_knob_value(op[i].op.vary.p -> name, curr_val, frame_values[j], 1);
else if(temp -> animagus != 0){
temp -> value = curr_val;
temp -> animagus = 1;
}
else
break;
//printf("Value at frame %d is %6.2f for knob %s\n", j, curr_val, op[i].op.vary.p -> name);
}
break;
}
}
void set_knobs(){
int i = 0;
double j;
char kid[8];
while(i != -1){
print_knobs();
printf("Enter ID of Knob to Set (-1 to Stop): ");
fgets(kid, sizeof(kid), stdin);
*(strchr(kid, '\n')) = 0;
sscanf(kid, "%d", &i);
printf("Scanned\n");
if(i != -1){
printf("Enter New Value for %s: \n", symtab[i].name);
fgets(kid, sizeof(kid), stdin);
*(strchr(kid, '\n')) = 0;
sscanf(kid, "%lf", &j);
set_value(lookup_symbol(symtab[i].name), j);
}
}
}
void box(stack *hay, struct matrix *shape, screen *s, color *c, int i, struct zbuff *buff){
struct constants *constant = (struct constants *)malloc(sizeof(struct constants));
int j;
poxy_box(shape, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]);
matrix_mult(&hay -> matrices[hay -> top], shape);
if(op[i].op.box.constants == NULL)
for(j = 0; j < 3; j++){
constant -> r[j] = 1;
constant -> g[j] = 1;
constant -> b[j] = 1;
constant -> red = 0;
constant -> green = 0;
constant -> blue = 0;
}
else
constant = lookup_symbol(op[i].op.box.constants -> name) -> s.c;
draw_polygons(shape, *s, *c, constant, buff);
}
void torus(stack *hay, struct matrix *shape, screen *s, color *c, int i, struct zbuff *buff){
struct constants *constant = (struct constants *)malloc(sizeof(struct constants));
int j;
render_polygonal_torus(shape, op[i].op.torus.r0, op[i].op.torus.r1, op[i].op.torus.d[0], op[i].op.torus.d[1], op[i].op.torus.d[2], .05);
matrix_mult(&hay -> matrices[hay -> top], shape);
if(op[i].op.torus.constants == NULL)
for(j = 0; j < 3; j++){
constant -> r[j] = 1;
constant -> g[j] = 1;
constant -> b[j] = 1;
constant -> red = 0;
constant -> green = 0;
constant -> blue = 0;
}
else
constant = lookup_symbol(op[i].op.torus.constants -> name) -> s.c;
draw_polygons(shape, *s, *c, constant, buff);
}
void sphere(stack *hay, struct matrix *shape, screen *s, color *c, int i, struct zbuff *buff){
struct constants *constant = (struct constants *)malloc(sizeof(struct constants));
int j;
render_polygonal_sphere(shape, op[i].op.sphere.r, op[i].op.sphere.d[0], op[i].op.sphere.d[1], op[i].op.sphere.d[2], .05);
matrix_mult(&hay -> matrices[hay -> top], shape);
if(op[i].op.sphere.constants == NULL)
for(j = 0; j < 3; j++){
constant -> r[j] = 1;
constant -> g[j] = 1;
constant -> b[j] = 1;
constant -> red = 0;
constant -> green = 0;
constant -> blue = 0;
}
else
constant = lookup_symbol(op[i].op.sphere.constants -> name) -> s.c;
draw_polygons(shape, *s, *c, constant, buff);
}
void print_frames(struct knob_value **array){
int i;
struct knob_value *temp;
for(i = 0; i < frames; i++){
printf("Frame: %d\n", i);
temp = array[i];
while(temp){
printf("Knob %s has value %6.2f\n", temp -> name, temp -> value);
temp = temp -> next;
}
}
}
void set_frame(struct knob_value **array, int frame){
struct knob_value *temp;
temp = array[frame];
while(temp){
set_value(lookup_symbol(temp -> name), temp -> value);
temp = temp -> next;
}
}
void my_main(){
stack *hay = sinit();
struct matrix *temp = new_matrix(4, 1);
int i, j, k, l;
screen s;
clear_screen(s);
color c;
c.red = 85;
c.blue = 85;
c.green = 85;
char name[64];
struct zbuff *buff = (struct zbuff *)malloc(sizeof(struct zbuff));
for(k = 0; k < XRES; k++){
for(l = 0; l < YRES; l++){
buff -> arr[k][l].c.red = 0;
buff -> arr[k][l].c.blue = 0;
buff -> arr[k][l].c.green = 0;
buff -> arr[k][l].z = -1 * pow(2, 10);
}
}
pass_one();
int temp2 = frames;
//printf("Frames = %d\n", frames);
if(varies)
pass_two();
else
set_knobs();
//printf("Frames: %d\nBasename: %s\n", frames, basename);
//print_frames(frame_values);
if(frames == -1)
frames = 1;
for(j = 0; j < frames; j++){
for(k = 0; k < XRES; k++){
for(l = 0; l < YRES; l++){
buff -> arr[k][l].c.red = 0;
buff -> arr[k][l].c.blue = 0;
buff -> arr[k][l].c.green = 0;
buff -> arr[k][l].z = -1 * pow(2, 10);
}
}
if(varies)
set_frame(frame_values, j);
for(i = 0; i < lastop; i++){
switch (op[i].opcode){
case POP:
pop(hay);
break;
case PUSH:
push(hay);
break;
case MOVE:
if(op[i].op.move.p != NULL)
move(hay, i, (lookup_symbol(op[i].op.move.p -> name) -> s).value);
else
move(hay, i, 1);
//printf("Moved: %d\n", hay -> top);
break;
case SCALE:
if(op[i].op.scale.p != NULL)
scale(hay, i, (lookup_symbol(op[i].op.scale.p -> name) -> s).value);
else
scale(hay, i, 1);
//printf("Scaled: %d\n", hay -> top);
break;
case ROTATE:
if(op[i].op.rotate.p != NULL)
rotate(hay, i, (lookup_symbol(op[i].op.rotate.p -> name) -> s).value);
else
rotate(hay, i, 1);
//printf("Rotated: %d\n", hay -> top);
break;
case SPHERE:
//printf("Sphere\n");
sphere(hay, temp, &s, &c, i, buff);
free_matrix(temp);
temp = new_matrix(4, 1);
break;
case TORUS:
//printf("Torus\n");
torus(hay, temp, &s, &c, i, buff);
free_matrix(temp);
temp = new_matrix(4, 1);
break;
case BOX:
//printf("Box\n");
box(hay, temp, &s, &c, i, buff);
free_matrix(temp);
temp = new_matrix(4, 1);
break;
case DISPLAY:
//printf("Displayed\n");
display(s);
break;
case SAVE:
//printf("Saved\n", j);
save_extension(s, op[i].op.save.p -> name);
break;
case SET:
set_value(lookup_symbol(op[i].op.set.p -> name), op[i].op.set.p -> s.value);
case LIGHT:
break;
case AMBIENT:
break;
case CONSTANTS:
break;
}
}
if(temp2 != -1){
sprintf(name, "%s%04d", basename, j);
save_extension(s, name);
printf("SAVING FRAME %s\n", name);
}
clear_screen(s);
}
}