-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_main.c
278 lines (238 loc) · 9.03 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
/*========== my_main.c ==========
This is the only file you need to modify in order
to get a working mdl project (for now).
my_main.c will serve as the interpreter for mdl.
When an mdl script goes through a lexer and parser,
the resulting operations will be in the array op[].
Your job is to go through each entry in op and perform
the required action from the list below:
push: push a new origin matrix onto the origin stack
pop: remove the top matrix on the origin stack
move/scale/rotate: create a transformation matrix
based on the provided values, then
multiply the current top of the
origins stack by it.
box/sphere/torus: create a solid object based on the
provided values. Store that in a
temporary matrix, multiply it by the
current top of the origins stack, then
call draw_polygons.
line: create a line based on the provided values. Store
that in a temporary matrix, multiply it by the
current top of the origins stack, then call draw_lines.
save: call save_extension with the provided filename
display: view the screen
=========================*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "parser.h"
#include "symtab.h"
#include "y.tab.h"
#include "matrix.h"
#include "ml6.h"
#include "display.h"
#include "draw.h"
#include "stack.h"
#include "gmath.h"
void my_main() {
int i;
struct matrix *tmp;
struct stack *systems;
screen t;
zbuffer zb;
color g;
double step_3d = 20;
double theta;
//Lighting values here for easy access
color ambient;
double light[2][3];
double view[3];
double areflect[3];
double dreflect[3];
double sreflect[3];
ambient.red = 50;
ambient.green = 50;
ambient.blue = 50;
light[LOCATION][0] = 0.5;
light[LOCATION][1] = 0.75;
light[LOCATION][2] = 1;
light[COLOR][RED] = 0;
light[COLOR][GREEN] = 255;
light[COLOR][BLUE] = 255;
view[0] = 0;
view[1] = 0;
view[2] = 1;
areflect[RED] = 0.1;
areflect[GREEN] = 0.1;
areflect[BLUE] = 0.1;
dreflect[RED] = 0.5;
dreflect[GREEN] = 0.5;
dreflect[BLUE] = 0.5;
sreflect[RED] = 0.5;
sreflect[GREEN] = 0.5;
sreflect[BLUE] = 0.5;
systems = new_stack();
tmp = new_matrix(4, 1000);
clear_screen( t );
clear_zbuffer(zb);
g.red = 0;
g.green = 0;
g.blue = 0;
for (i = 0; i < lastop; i++) {
printf("%d: ",i);
switch (op[i].opcode)
{
case SPHERE:
printf("Sphere: %6.2f %6.2f %6.2f r=%6.2f",
op[i].op.sphere.d[0],op[i].op.sphere.d[1],
op[i].op.sphere.d[2],
op[i].op.sphere.r);
add_sphere(tmp,
op[i].op.sphere.d[0], op[i].op.sphere.d[1], op[i].op.sphere.d[2],
op[i].op.sphere.r,
step_3d);
matrix_mult(peek(systems), tmp);
draw_polygons(tmp, t, zb, view, light, ambient, areflect, dreflect, sreflect);
tmp->lastcol = 0;
if (op[i].op.sphere.constants != NULL)
{
printf("\tconstants: %s",op[i].op.sphere.constants->name);
}
if (op[i].op.sphere.cs != NULL)
{
printf("\tcs: %s",op[i].op.sphere.cs->name);
}
break;
case TORUS:
printf("Torus: %6.2f %6.2f %6.2f r0=%6.2f r1=%6.2f",
op[i].op.torus.d[0],op[i].op.torus.d[1],
op[i].op.torus.d[2],
op[i].op.torus.r0,op[i].op.torus.r1);
add_torus(tmp,
op[i].op.torus.d[0], op[i].op.torus.d[1], op[i].op.torus.d[2],
op[i].op.torus.r0, op[i].op.torus.r1, step_3d);
matrix_mult(peek(systems), tmp);
draw_polygons(tmp, t, zb, view, light, ambient, areflect, dreflect, sreflect);
tmp->lastcol = 0;
if (op[i].op.torus.constants != NULL)
{
printf("\tconstants: %s",op[i].op.torus.constants->name);
}
if (op[i].op.torus.cs != NULL)
{
printf("\tcs: %s",op[i].op.torus.cs->name);
}
break;
case BOX:
printf("Box: d0: %6.2f %6.2f %6.2f d1: %6.2f %6.2f %6.2f",
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]);
add_box(tmp,
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(peek(systems), tmp);
draw_polygons(tmp, t, zb, view, light, ambient, areflect, dreflect, sreflect);
tmp->lastcol = 0;
if (op[i].op.box.constants != NULL)
{
printf("\tconstants: %s",op[i].op.box.constants->name);
}
if (op[i].op.box.cs != NULL)
{
printf("\tcs: %s",op[i].op.box.cs->name);
}
break;
case LINE:
printf("Line: from: %6.2f %6.2f %6.2f to: %6.2f %6.2f %6.2f",
op[i].op.line.p0[0],op[i].op.line.p0[1],
op[i].op.line.p0[2],
op[i].op.line.p1[0],op[i].op.line.p1[1],
op[i].op.line.p1[2]);
add_edge(tmp,
op[i].op.line.p0[0],op[i].op.line.p0[1],op[i].op.line.p0[2],
op[i].op.line.p1[0],op[i].op.line.p1[1],op[i].op.line.p1[2]);
matrix_mult(peek(systems), tmp);
tmp->lastcol = 0;
if (op[i].op.line.constants != NULL)
{
printf("\n\tConstants: %s",op[i].op.line.constants->name);
}
if (op[i].op.line.cs0 != NULL)
{
printf("\n\tCS0: %s",op[i].op.line.cs0->name);
}
if (op[i].op.line.cs1 != NULL)
{
printf("\n\tCS1: %s",op[i].op.line.cs1->name);
}
break;
case MOVE:
printf("Move: %6.2f %6.2f %6.2f",
op[i].op.move.d[0],op[i].op.move.d[1],
op[i].op.move.d[2]);
tmp = make_translate(op[i].op.move.d[0], op[i].op.move.d[1], op[i].op.move.d[2]);
matrix_mult(peek(systems), tmp);
copy_matrix(tmp, peek(systems));
tmp->lastcol = 0;
if (op[i].op.move.p != NULL)
{
printf("\tknob: %s",op[i].op.move.p->name);
}
break;
case SCALE:
printf("Scale: %6.2f %6.2f %6.2f",
op[i].op.scale.d[0],op[i].op.scale.d[1],
op[i].op.scale.d[2]);
tmp = make_scale(op[i].op.scale.d[0], op[i].op.scale.d[1], op[i].op.scale.d[2]);
matrix_mult(peek(systems), tmp);
copy_matrix(tmp, peek(systems));
tmp->lastcol = 0;
if (op[i].op.scale.p != NULL)
{
printf("\tknob: %s",op[i].op.scale.p->name);
}
break;
case ROTATE:
printf("Rotate: axis: %6.2f degrees: %6.2f",
op[i].op.rotate.axis,
op[i].op.rotate.degrees);
theta = op[i].op.rotate.degrees * (M_PI / 180);
if (op[i].op.rotate.axis == 0) {
tmp = make_rotX( theta );
} else if (op[i].op.rotate.axis == 1) {
tmp = make_rotY( theta );
} else {
tmp = make_rotZ( theta );
}
matrix_mult(peek(systems), tmp);
copy_matrix(tmp, peek(systems));
tmp->lastcol = 0;
if (op[i].op.rotate.p != NULL)
{
printf("\tknob: %s",op[i].op.rotate.p->name);
}
break;
case PUSH:
printf("Push");
push(systems);
break;
case POP:
printf("Pop");
pop(systems);
break;
case SAVE:
printf("Save: %s",op[i].op.save.p->name);
save_extension(t, op[i].op.save.p->name);
break;
case DISPLAY:
printf("Display");
display(t);
break;
}
printf("\n");
}
}