/
parser.c
232 lines (215 loc) · 7.56 KB
/
parser.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "ml6.h"
#include "display.h"
#include "draw.h"
#include "matrix.h"
#include "parser.h"
/*======== void parse_file () ==========
Inputs: char * filename
struct matrix * transform,
struct matrix * pm,
screen s
Returns:
Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
Every command is a single character that takes up a line
Any command that requires arguments must have those arguments in the second line.
The commands are as follows:
line: add a line to the edge matrix -
takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
circle: add a circle to the edge matrix -
takes 3 arguments (cx, cy, r)
hermite: add a hermite curve to the edge matrix -
takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
bezier: add a bezier curve to the edge matrix -
takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
sphere: add a sphere to the edge matrix -
takes 3 arguemnts (cx, cy, r)
torus: add a torus to the edge matrix -
takes 4 arguemnts (cx, cy, r1, r2)
box: add a rectangular prism to the edge matrix -
takes 6 arguemnts (x, y, z, width, height, depth)
clear: clear the currnt edge matrix -
takes 0 arguments
ident: set the transform matrix to the identity matrix -
scale: create a scale matrix,
then multiply the transform matrix by the scale matrix -
takes 3 arguments (sx, sy, sz)
translate: create a translation matrix,
then multiply the transform matrix by the translation matrix -
takes 3 arguments (tx, ty, tz)
xrotate: create an x-axis rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 1 argument (theta)
yrotate: create an y-axis rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 1 argument (theta)
zrotate: create an z-axis rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 1 argument (theta)
apply: apply the current transformation matrix to the
edge matrix
display: draw the lines of the edge matrix to the screen
display the screen
save: draw the lines of the edge matrix to the screen
save the screen to a file -
takes 1 argument (file name)
quit: end parsing
See the file script for an example of the file format
IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)
====================*/
void parse_file ( char * filename,
struct matrix * transform,
struct matrix * pm,
screen s) {
FILE *f;
char line[256];
struct matrix * tmp;
double angle;
color g;
g.red = 0;
g.green = 255;
g.blue = 0;
clear_screen(s);
if ( strcmp(filename, "stdin") == 0 )
f = stdin;
else
f = fopen(filename, "r");
while ( fgets(line, 255, f) != NULL ) {
line[strlen(line)-1]='\0';
//printf(":%s:\n",line);
double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4;
if ( strncmp(line, "line", strlen(line)) == 0 ) {
// printf("LINE!\n");
fgets(line, 255, f);
// printf("\t%s", line);
//line[strlen(line)-1]='\0';
sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
add_edge(pm, x, y, z, x1, y1, z1);
// printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
}
else if ( strncmp(line, "circle", strlen(line)) == 0 ) {
//printf("CIRCLE\n");
fgets(line, 255, f);
sscanf(line, "%lf %lf %lf", &x, &y, &z);
add_circle(pm, x, y, z, 0.01);
//printf( "%lf %lf %lf\n", x, y, z);
}
else if ( strncmp(line, "bezier", strlen(line)) == 0 ) {
//printf("BEZIER\n");
fgets(line, 255, f);
sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE );
//printf( "%lf %lf %lf\n", x, y, z);
}
else if ( strncmp(line, "hermite", strlen(line)) == 0 ) {
//printf("HERMITE\n");
fgets(line, 255, f);
sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE );
//printf( "%lf %lf %lf\n", x, y, z);
}
else if ( strncmp(line, "box", strlen(line)) == 0 ) {
fgets(line, 255, f);
sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
add_box(pm, x, y, z, x1, y1, z1);
// printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
}
else if (strncmp(line, "sphere", strlen(line)) == 0 ) {
fgets(line, 255, f);
sscanf(line, "%lf %lf %lf", &x, &y, &z);
add_sphere(pm, x, y, z, 1);
//printf( "%lf %lf %lf\n", x, y, z);
}
else if (strncmp(line, "torus", strlen(line)) == 0 ) {
fgets(line, 255, f);
sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1);
add_torus(pm, x, y, z, z1, 1);
//printf( "%lf %lf %lf\n", x, y, z);
}
else if ( strncmp(line, "scale", strlen(line)) == 0 ) {
//printf("SCALE\n");
fgets(line, 255, f);
//line[strlen(line)-1]='\0';
sscanf(line, "%lf %lf %lf", &x, &y, &z);
tmp = make_scale(x, y, z);
matrix_mult(tmp, transform);
//print_matrix(transform);
}
else if ( strncmp(line, "translate", strlen(line)) == 0 ) {
//printf("TRANSLATE\n");
fgets(line, 255, f);
// line[strlen(line)-1]='\0';
sscanf(line, "%lf %lf %lf", &x, &y, &z);
tmp = make_translate(x, y, z);
matrix_mult(tmp, transform);
//print_matrix(transform);
}
else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) {
//printf("ROTATE!\n");
fgets(line, 255, f);
sscanf(line, "%lf", &angle);
angle = angle * (M_PI / 180);
tmp = make_rotX( angle);
matrix_mult(tmp, transform);
}
else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) {
//printf("ROTATE!\n");
fgets(line, 255, f);
sscanf(line, "%lf", &angle);
angle = angle * (M_PI / 180);
tmp = make_rotY( angle);
matrix_mult(tmp, transform);
}
else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) {
//printf("ROTATE!\n");
fgets(line, 255, f);
sscanf(line, "%lf", &angle);
angle = angle * (M_PI / 180);
tmp = make_rotZ( angle);
matrix_mult(tmp, transform);
}
else if ( strncmp(line, "ident", strlen(line)) == 0 ) {
ident(transform);
}
else if ( strncmp(line, "apply", strlen(line)) == 0 ) {
//printf("APPLY!\n");
//print_matrix( transform );
// print_matrix(pm);
matrix_mult(transform, pm);
}
else if ( strncmp(line, "display", strlen(line)) == 0 ) {
clear_screen(s);
draw_polygons(pm, s, g);
display(s);
}
else if ( strncmp(line, "save", strlen(line)) == 0 ) {
fgets(line, 255, f);
// line[strlen(line)-1] = '\0';
clear_screen(s);
draw_polygons(pm, s, g);
save_extension(s, line);
}
else if ( strncmp(line, "clear", strlen(line)) == 0 ) {
pm->lastcol = 0;
}
else if ( strncmp(line, "quit", strlen(line)) == 0 ) {
return;
}
else if ( line[0] != '#' ) {
printf("Invalid command\n");
}
}
free_matrix(tmp);
fclose(f);
//printf("END PARSE\n");
}