-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlab5.c
249 lines (218 loc) · 7.6 KB
/
lab5.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
#include <FPT.h>
#include <D3d_matrix.h>
double centerx, centery, centerz;
double halfangle = 40;
double x[100][100], y[100][100], z[100][100]; //x, y, z coordinates
int points[100]; //number of points in whatever
int polys[10];
int reverse = 1;
int shapeorder[100][100][100]; //where things connect to
int shapes[100][100]; //the shapes[what argument][which shape]
int WIDTH = 600; int HEIGHT = 600; int DEPTH = 600;
void printarray(double *a, int size) {
int i;
for (i = 0; i < size; i++) {
printf("%.2lf\n", a[i]);
}
printf("\n");
}
//reads in file
//Modified from lab2; does not read in colors
void readobject(FILE *g, int i) {
int k, j;
fscanf(g, "%d", &points[i]);
// printf("there are %d points\n", points[i]);
for (k = 0; k < points[i]; k++) {
fscanf(g, "%lf %lf %lf", &x[i][k], &y[i][k], &z[i][k]);
// printf("(%lf,%lf,%lf)\n", x[i][k], y[i][k], z[i][k]);
}
fscanf(g, "%d", &polys[i]);
// printf("there are %d polygons in this whatever\n", polys[i]);
for (k = 0; k < polys[i]; k++) {
fscanf(g, "%d", &shapes[i][k]);
// printf("%d: ", shapes[i][k]);
for (j = 0; j < shapes[i][k]; j++) {
fscanf(g, "%d", &shapeorder[i][k][j]);
// printf("%d ", shapeorder[i][k][j]);
}
// printf("\n");
}
}
int check_if_behind(double *plane_x, double *plane_y, double *plane_z,
int size) {
double p_vect[3] = {0.0};
double vect1[3] = {plane_x[0] - plane_x[1],
plane_y[0] - plane_y[1],
plane_z[0] - plane_z[1],
};
double vect2[3] = {plane_x[0] - plane_x[2],
plane_y[0] - plane_y[2],
plane_z[0] - plane_z[2],
};
double e_vect[3] = {plane_x[0] * reverse, plane_y[0] * reverse,
plane_z[0] * reverse};
D3d_x_product(p_vect, vect1, vect2);
int dot_product = (p_vect[0] * e_vect[0]) + (p_vect[1] *
e_vect[1]) + (p_vect[2] * e_vect[2]);
return (dot_product > 0) ? 1 : 0;
}
void drawit(int i) {
int k, j;
double tempx[points[i]], tempy[points[i]];
double plane_x[points[i]], plane_y[points[i]], plane_z[points[i]];
double mod = (HEIGHT / 2) / tan(halfangle * (M_PI / 180));
for (k = 0; k < polys[i]; k++) {
j = 0;
while (j < shapes[i][k]) {
// printf("%d", shapeorder[i][k][j]);
if (fabs(z[i][shapeorder[i][k][j]]) > 10e-7) { //Checks if z == 0
tempx[j] = mod * (x[i][shapeorder[i][k][j]] /
z[i][shapeorder[i][k][j]]) + (WIDTH / 2);
tempy[j] = mod * (y[i][shapeorder[i][k][j]] /
z[i][shapeorder[i][k][j]]) + (HEIGHT / 2);
plane_x[j] = x[i][shapeorder[i][k][j]];
plane_y[j] = y[i][shapeorder[i][k][j]];
plane_z[j] = z[i][shapeorder[i][k][j]];
j++;
} else {
j++;
}
}
if (check_if_behind(plane_x, plane_y, plane_z, j) == 1) {
G_rgb(1, 0, .3);
G_polygon(tempx, tempy, j);
}
}
}
//finds the largest/smallest value in array
int findextrema(double *y, int z, int swatch) {
int position = 0; int i = 0;
double temp = 0 + (swatch * HEIGHT);
for (i = 0; i < z; i++) {
if (swatch == 0 && y[i] > temp) { //finds largest
position = i;
temp = y[i];
} else if (swatch == 1 && y[i] < temp) { //finds smallest
position = i;
temp = y[i];
}
}
return position;
}
//finds the scale factor of the object
//also reveals the center of the object
double scale_n_fit(int i) {
int k;
double smallx, bigx, smally, bigy, smallz, bigz;
bigx = x[i][findextrema(x[i], points[i], 0)];
smallx = x[i][findextrema(x[i], points[i], 1)];
bigy = y[i][findextrema(y[i], points[i], 0)];
smally = y[i][findextrema(y[i], points[i], 1)];
bigz = z[i][findextrema(z[i], points[i], 0)];
smallz = z[i][findextrema(z[i], points[i], 1)];
double boxheight = bigy - smally;
double boxwidth = bigx - smallx;
double boxdepth = bigz - smallz;
centerx = (bigx + smallx) / 2;
centery = (bigy + smally) / 2;
centerz = (bigz + smallz) / 2;
double centers[3] = {centerx, centery, centerz};
return (findextrema(centers, 3, 1) == 1) ? WIDTH / boxwidth :
(findextrema(centers, 3, 1) == 2) ? HEIGHT / boxheight :
DEPTH / boxdepth;
}
//changes the message based on how many objects are inputted via command line
void welcome(int i) {
printf((i == 1) ? "Press 1 to start: " :
(i == 2) ? "Press 1 or 2 to start: " :
"Press 1 through %d to start ", i);
}
int main (int argc, char **argv)
{
char q, action;
double mat[4][4], minv[4][4], scaleFactor;
double radians = 3 * (M_PI / 180);
FILE *g;
int cc, sign, currentObj, k, h;
int increment = 15;
int xcounter, ycounter, zcounter = 0;
for (cc = 1; cc < argc; cc++) {
g = fopen(argv[cc], "r"); //opens a file; r = read only
if (g == NULL) { //if the file is empty, it will let me know
printf("can't open (1)\n");
exit(1);
} else {
readobject(g, cc);
D3d_make_identity(mat); D3d_make_identity(minv);
scaleFactor = scale_n_fit(cc);
D3d_translate(mat, minv, -centerx, -centery, -centerz);
D3d_scale(mat, minv, scaleFactor, scaleFactor, scaleFactor);
D3d_mat_mult_points(x[cc], y[cc], z[cc], mat,
x[cc], y[cc], z[cc], points[cc]);
// printarray(z[cc], points[cc]);
}
}
welcome(argc - 1);
scanf("%c", &q);
currentObj = q - '0';
sign = 1 ;
action = 't' ;
if (currentObj < argc && currentObj > 0) {
G_init_graphics(WIDTH, HEIGHT);
while (1) {
G_rgb(0, 0, 0);
G_clear();
drawit(currentObj);
D3d_make_identity (mat) ;
D3d_make_identity (minv) ;
q = G_wait_key() ;
if (q == 'q') {
exit(0) ;
} else if (q == 'c') {
sign = -sign ;
} else if (q == 't') {
action = q ;
} else if (q == 's') {
reverse = -reverse;
} else if (q == 'r') {
action = q ;
} else if (('0' <= q) && (q <= '9')) {
k = q - '0' ;
if (h != currentObj) {
currentObj = k;
}
} else if ((q == 'x') && (action == 't')) {
D3d_translate (mat, minv, sign * increment, 0, 0);
xcounter = xcounter + (sign * increment);
} else if ((q == 'y') && (action == 't')) {
D3d_translate (mat, minv, 0, sign * increment, 0);
ycounter = ycounter + (sign * increment);
} else if ((q == 'z') && (action == 't')) {
D3d_translate(mat, minv, 0, 0, sign * increment);
zcounter = zcounter + (sign * increment);
} else if ((q == 'x') && (action == 'r')) {
D3d_translate(mat, minv, -xcounter, -ycounter, -zcounter);
D3d_rotate_x(mat, minv, sign * radians);
D3d_translate(mat, minv, xcounter, ycounter, zcounter);
} else if ((q == 'y') && (action == 'r')) {
D3d_translate(mat, minv, -xcounter, -ycounter, -zcounter);
D3d_rotate_y(mat, minv, sign * radians);
D3d_translate(mat, minv, xcounter, ycounter, zcounter);
} else if ((q == 'z') && (action == 'r')) {
D3d_translate(mat, minv, -xcounter, -ycounter, -zcounter);
D3d_rotate_z(mat, minv, sign * radians);
D3d_translate(mat, minv, xcounter, ycounter, zcounter);
} else {
printf("no action\n") ;
}
D3d_mat_mult_points (x[currentObj], y[currentObj], z[currentObj],
mat, x[currentObj], y[currentObj],
z[currentObj], points[currentObj] + 1) ;
//the numpoints[currentObj]+1 is because we have stored
//the center of the object at the arrays' end
G_rgb(0, 0, 0);
G_clear();
drawit(currentObj);
}
}
}