-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.cpp
434 lines (350 loc) · 11.4 KB
/
input.cpp
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
USC/Viterbi/Computer Science
"Jello Cube" Assignment 1 starter code
*/
#include "jello.h"
#include "input.h"
/* Write a screenshot, in the PPM format, to the specified filename, in PPM format */
void saveScreenshot(int windowWidth, int windowHeight, char* filename)
{
if (filename == NULL)
return;
// Allocate a picture buffer
Pic* in = pic_alloc(windowWidth, windowHeight, 3, NULL);
printf("File to save to: %s\n", filename);
for (int i = windowHeight - 1; i >= 0; i--)
{
glReadPixels(0, windowHeight - i - 1, windowWidth, 1, GL_RGB, GL_UNSIGNED_BYTE,
&in->pix[i * in->nx * in->bpp]);
}
if (ppm_write(filename, in))
printf("File saved Successfully\n");
else
printf("Error in Saving\n");
pic_free(in);
}
/* converts mouse drags into information about rotation/translation/scaling */
void mouseMotionDrag(int x, int y)
{
int vMouseDelta[2] = { x - g_vMousePos[0], y - g_vMousePos[1] };
if (g_iRightMouseButton) // handle camera rotations
{
Phi += vMouseDelta[0] * 0.01;
Theta += vMouseDelta[1] * 0.01;
if (Phi > 2 * pi)
Phi -= 2 * pi;
if (Phi < 0)
Phi += 2 * pi;
if (Theta > pi / 2 - 0.01) // dont let the point enter the north pole
Theta = pi / 2 - 0.01;
if (Theta < -pi / 2 + 0.01)
Theta = -pi / 2 + 0.01;
g_vMousePos[0] = x;
g_vMousePos[1] = y;
}
if (g_iLeftMouseButton) // handle mouse dragging forces
{
static GLdouble model[16];
glGetDoublev(GL_MODELVIEW_MATRIX, model);
// if mouse is dragging vertically, then we add forces onto camera's UP axis
// if .... horizontally, then we add forces onto the RIGHT axis
// the matrix is column major
struct point camRight = { 0.0 }, camUp = { 0.0 };
pMAKE(model[1], model[5], model[9], camUp);
pMAKE(model[0], model[4], model[8], camRight);
//pMAKE(model[2], model[6], model[10], camForward);
double right = vMouseDelta[0] * g_mouseDragForceBaseMultiplier * 1;
double up = vMouseDelta[1] * g_mouseDragForceBaseMultiplier * -1;
pMULTIPLY(camUp, up, camUp);
pMULTIPLY(camRight, right, camRight);
pSUM(camRight, camUp, g_pMouseDragForce);
g_vMousePos[0] = x;
g_vMousePos[1] = y;
}
else {
pMAKE(0, 0, 0, g_pMouseDragForce);
}
}
void mouseMotion(int x, int y)
{
g_vMousePos[0] = x;
g_vMousePos[1] = y;
}
void mouseButton(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
g_iLeftMouseButton = (state == GLUT_DOWN);
if (state == GLUT_DOWN)
{
GLdouble model[16];
glGetDoublev(GL_MODELVIEW_MATRIX, model);
GLdouble proj[16];
glGetDoublev(GL_PROJECTION_MATRIX, proj);
GLint view[4];
glGetIntegerv(GL_VIEWPORT, view);
int winX = x;
int winY = view[3] - 1 - y;
float zValue;
glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &zValue);
GLubyte stencilValue;
glReadPixels(winX, winY, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &stencilValue);
GLdouble worldX, worldY, worldZ;
gluUnProject(winX, winY, zValue, model, proj, view, &worldX, &worldY, &worldZ);
if (stencilValue == 1)
{
g_vMousePos[0] = x;
g_vMousePos[1] = y;
struct point pickedPoint;
pMAKE(worldX, worldY, worldZ, pickedPoint);
printf("%.2lf, %.2lf, %.2lf picked\n", worldX, worldY, worldZ);
// find a closest point
double minDistance = 100.0f, distance;
struct point vecDiff;
int pickedPointIndices[3] = { -1, -1, -1 };
for (int i = 0; i <= 7; i++)
for (int j = 0; j <= 7; j++)
for (int k = 0; k <= 7; k++)
{
pDIFFERENCE(pickedPoint, jello.p[i][j][k], vecDiff);
pLENGTH(vecDiff, distance);
if (distance < minDistance)
{
minDistance = distance;
pickedPointIndices[0] = i;
pickedPointIndices[1] = j;
pickedPointIndices[2] = k;
}
}
printf("clicked on vertex: %d, %d, %d\n", pickedPointIndices[0], pickedPointIndices[1], pickedPointIndices[2]);
g_pickedPointIndices[0] = pickedPointIndices[0];
g_pickedPointIndices[1] = pickedPointIndices[1];
g_pickedPointIndices[2] = pickedPointIndices[2];
g_iPickingAMassPoint = 1;
}
}
else
{
g_iPickingAMassPoint = 0;
}
break;
case GLUT_MIDDLE_BUTTON:
g_iMiddleMouseButton = (state == GLUT_DOWN);
break;
case GLUT_RIGHT_BUTTON:
g_iRightMouseButton = (state == GLUT_DOWN);
break;
}
g_vMousePos[0] = x;
g_vMousePos[1] = y;
}
// gets called whenever a key is pressed
void keyboardFunc(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
case 'e':
Theta = pi / 6;
Phi = pi / 6;
viewingMode = 0;
break;
case 'v':
viewingMode = 1 - viewingMode;
break;
case 'h':
shear = 1 - shear;
break;
case 's':
structural = 1 - structural;
break;
case 'b':
bend = 1 - bend;
break;
case 'p':
pause = 1 - pause;
break;
case 'z':
R -= 0.2;
if (R < 0.2)
R = 0.2;
break;
case 'x':
R += 0.2;
break;
case ' ':
saveScreenToFile = 1 - saveScreenToFile;
break;
}
}
/* reads the world parameters from a world file */
/* fileName = string containing the name of the world file, ex: jello1.w */
/* function fills the structure 'jello' with parameters read from file */
/* structure 'jello' will typically be declared (probably statically, not on the heap)
by the caller function */
/* function aborts the program if can't access the file */
void readWorld(char* fileName, struct world* jello)
{
int i, j, k;
FILE* file;
file = fopen(fileName, "r");
if (file == NULL) {
printf("can't open file\n");
exit(1);
}
/*
File should first contain a line specifying the integrator (EULER or RK4).
Example: EULER
Then, follows one line specifying the size of the timestep for the integrator, and
an integer parameter n specifying that every nth timestep will actually be drawn
(the other steps will only be used for internal calculation)
Example: 0.001 5
Now, timestep equals 0.001. Every fifth time point will actually be drawn,
i.e. frame1 <--> t = 0
frame2 <--> t = 0.005
frame3 <--> t = 0.010
frame4 <--> t = 0.015
...
Then, there should be two lines for physical parameters and external acceleration.
Format is:
kElastic dElastic kCollision dCollision
mass
Here
kElastic = elastic coefficient of the spring (same for all springs except collision springs)
dElastic = damping coefficient of the spring (same for all springs except collision springs)
kCollision = elastic coefficient of collision springs (same for all collision springs)
dCollision = damping coefficient of collision springs (same for all collision springs)
mass = mass in kilograms for each of the 512 mass points
(mass assumed to be the same for all the points; total mass of the jello cube = 512 * mass)
Example:
10000 25 10000 15
0.002
Then, there should be one or two lines for the inclined plane, with the obvious syntax.
If there is no inclined plane, there should be only one line with a 0 value. There
is no line for the coefficient. Otherwise, there are two lines, first one containing 1,
and the second one containing the coefficients.
Note: there is no inclined plane in this assignment (always 0).
Example:
1
0.31 -0.78 0.5 5.39
Next is the forceField block, first with the resolution and then the data, one point per row.
Example:
30
<here 30 * 30 * 30 = 27 000 lines follow, each containing 3 real numbers>
After this, there should be 1024 lines, each containing three floating-point numbers.
The first 512 lines correspond to initial point locations.
The last 512 lines correspond to initial point velocities.
There should no blank lines anywhere in the file.
*/
/* read integrator algorithm */
fscanf(file, "%s\n", &jello->integrator);
/* read timestep size and render */
fscanf(file, "%lf %d\n", &jello->dt, &jello->n);
/* read physical parameters */
fscanf(file, "%lf %lf %lf %lf\n",
&jello->kElastic, &jello->dElastic, &jello->kCollision, &jello->dCollision);
/* read mass of each of the 512 points */
fscanf(file, "%lf\n", &jello->mass);
/* read info about the plane */
fscanf(file, "%d\n", &jello->incPlanePresent);
if (jello->incPlanePresent == 1)
fscanf(file, "%lf %lf %lf %lf\n", &jello->a, &jello->b, &jello->c, &jello->d);
/* read info about the force field */
fscanf(file, "%d\n", &jello->resolution);
jello->forceField =
(struct point*)malloc(jello->resolution * jello->resolution * jello->resolution * sizeof(struct point));
if (jello->resolution != 0)
for (i = 0; i <= jello->resolution - 1; i++)
for (j = 0; j <= jello->resolution - 1; j++)
for (k = 0; k <= jello->resolution - 1; k++)
fscanf(file, "%lf %lf %lf\n",
&jello->forceField[i * jello->resolution * jello->resolution + j * jello->resolution + k].x,
&jello->forceField[i * jello->resolution * jello->resolution + j * jello->resolution + k].y,
&jello->forceField[i * jello->resolution * jello->resolution + j * jello->resolution + k].z);
/* read initial point positions */
for (i = 0; i <= 7; i++)
{
for (j = 0; j <= 7; j++)
{
for (k = 0; k <= 7; k++)
fscanf(file, "%lf %lf %lf\n",
&jello->p[i][j][k].x, &jello->p[i][j][k].y, &jello->p[i][j][k].z);
}
}
/* read initial point velocities */
for (i = 0; i <= 7; i++)
{
for (j = 0; j <= 7; j++)
{
for (k = 0; k <= 7; k++)
fscanf(file, "%lf %lf %lf\n",
&jello->v[i][j][k].x, &jello->v[i][j][k].y, &jello->v[i][j][k].z);
}
}
fclose(file);
return;
}
/* writes the world parameters to a world file on disk*/
/* fileName = string containing the name of the output world file, ex: jello1.w */
/* function creates the output world file and then fills it corresponding to the contents
of structure 'jello' */
/* function aborts the program if can't access the file */
void writeWorld(char* fileName, struct world* jello)
{
int i, j, k;
FILE* file;
file = fopen(fileName, "w");
if (file == NULL) {
printf("can't open file\n");
exit(1);
}
/* write integrator algorithm */
fprintf(file, "%s\n", jello->integrator);
/* write timestep */
fprintf(file, "%lf %d\n", jello->dt, jello->n);
/* write physical parameters */
fprintf(file, "%lf %lf %lf %lf\n",
jello->kElastic, jello->dElastic, jello->kCollision, jello->dCollision);
/* write mass */
fprintf(file, "%lf\n",
jello->mass);
/* write info about the plane */
fprintf(file, "%d\n", jello->incPlanePresent);
if (jello->incPlanePresent == 1)
fprintf(file, "%lf %lf %lf %lf\n", jello->a, jello->b, jello->c, jello->d);
/* write info about the force field */
fprintf(file, "%d\n", jello->resolution);
if (jello->resolution != 0)
for (i = 0; i <= jello->resolution - 1; i++)
for (j = 0; j <= jello->resolution - 1; j++)
for (k = 0; k <= jello->resolution - 1; k++)
fprintf(file, "%lf %lf %lf\n",
jello->forceField[i * jello->resolution * jello->resolution + j * jello->resolution + k].x,
jello->forceField[i * jello->resolution * jello->resolution + j * jello->resolution + k].y,
jello->forceField[i * jello->resolution * jello->resolution + j * jello->resolution + k].z);
/* write initial point positions */
for (i = 0; i <= 7; i++)
{
for (j = 0; j <= 7; j++)
{
for (k = 0; k <= 7; k++)
fprintf(file, "%lf %lf %lf\n",
jello->p[i][j][k].x, jello->p[i][j][k].y, jello->p[i][j][k].z);
}
}
/* write initial point velocities */
for (i = 0; i <= 7; i++)
{
for (j = 0; j <= 7; j++)
{
for (k = 0; k <= 7; k++)
fprintf(file, "%lf %lf %lf\n",
jello->v[i][j][k].x, jello->v[i][j][k].y, jello->v[i][j][k].z);
}
}
fclose(file);
return;
}