-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjello.cpp
More file actions
356 lines (274 loc) · 8.97 KB
/
Copy pathjello.cpp
File metadata and controls
356 lines (274 loc) · 8.97 KB
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
/*
USC/Viterbi/Computer Science
"Jello Cube" Assignment 1 starter code
Your name:
<write your name here>
*/
#include "jello.h"
#include "showCube.h"
#include "input.h"
#include "physics.h"
#include <vector>
#include <iostream>
using namespace std;
// camera parameters
double Theta = pi / 6;
double Phi = pi / 6;
double R = 6;
double horiTranslate = 0;
double vertTranslate = 0;
// mouse control
int g_iMenuId;
int g_vMousePos[2];
int g_iLeftMouseButton,g_iMiddleMouseButton,g_iRightMouseButton;
// number of images saved to disk so far
int sprite=0;
// these variables control what is displayed on screen
int shear=0, bend=0, structural=1, pause=0, viewingMode=0, saveScreenToFile=0;
struct world jello;
vector<spring> structuralSprings;
vector<spring> shearSprings;
vector<spring> bendSprings;
// used determin inclined plane side
// first component stores unit normal vector to plane
// second component is a flag about if cube is on negative side
pair<point, bool> inclinedPlaneNormal;
point dragForceDirection;
double dragForceMagnitude;
int windowWidth, windowHeight;
// calculate unit normal vector to inclined plane and check which side is the cube on
pair<point,bool> getPlaneNormalBasedOnCubeSide (struct world * jello) {
point n;
double length, currValueOfEq;
// get normal vector of the plane
pMAKE(jello->a,jello->b,jello->c,n);
pNORMALIZE(n);
bool negSide = false;
// check side of the cube
for (int i=0; i<=7; i++)
for (int j=0; j<=7; j++)
for (int k=0; k<=7; k++)
{
currValueOfEq = jello->a * jello->p[i][j][k].x +
jello->b * jello->p[i][j][k].y +
jello->c * jello->p[i][j][k].z + jello->d;
if (currValueOfEq < 0) {
negSide = true;
pNEG(n,n);
}
}
return make_pair(n, negSide);
}
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0,1.0,0.01,1000.0);
// set background color to white
glClearColor(1, 1, 1, 0.0);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_LINE_SMOOTH);
// call functions in physics.cpp to create springs
// passed by reference to allow changes
createStructuralSprings(structuralSprings);
createShearSprings(shearSprings);
createBendSprings(bendSprings);
// check cube side if inclined plane is present
if (jello.incPlanePresent == 1) inclinedPlaneNormal = getPlaneNormalBasedOnCubeSide(&jello);
// for dragging the cube to a direction
// they are extern variables and will be used in physics.cpp
dragForceMagnitude = 0;
pMAKE(0,0,0, dragForceDirection);
return;
}
void reshape(int w, int h)
{
// Prevent a divide by zero, when h is zero.
// You can't make a window of zero height.
if(h == 0)
h = 1;
glViewport(0, 0, w, h);
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the perspective
double aspectRatio = 1.0 * w / h;
gluPerspective(60.0f, aspectRatio, 0.01f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
windowWidth = w;
windowHeight = h;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// camera parameters are Phi, Theta, R
gluLookAt(R * cos(Phi) * cos (Theta)+horiTranslate,
R * sin(Phi) * cos (Theta)+horiTranslate,
R * sin (Theta)+vertTranslate,
0.0,0.0,0.0, 0.0,0.0,1.0);
/* Lighting */
/* You are encouraged to change lighting parameters or make improvements/modifications
to the lighting model .
This way, you will personalize your assignment and your assignment will stick out.
*/
// global ambient light
GLfloat aGa[] = { 0.0, 0.0, 0.0, 0.0 };
// light 's ambient, diffuse, specular
GLfloat lKa0[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat lKd0[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat lKs0[] = { 1.0, 0.5, 0.0, 1.0 };
GLfloat lKa1[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat lKd1[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat lKs1[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat lKa2[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat lKd2[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat lKs2[] = { 1.0, 0.5, 0.0, 1.0 };
GLfloat lKa3[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat lKd3[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat lKs3[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat lKa4[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat lKd4[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat lKs4[] = { 1.0, 0.5, 0.0, 1.0 };
GLfloat lKa5[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat lKd5[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat lKs5[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat lKa6[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat lKd6[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat lKs6[] = { 1.0, 0.5, 0.0, 1.0 };
GLfloat lKa7[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat lKd7[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat lKs7[] = { 1.0, 0.0, 0.0, 1.0 };
// light positions and directions
GLfloat lP0[] = { -1.999, -1.999, -1.999, 1.0 };
GLfloat lP1[] = { 1.999, -1.999, -1.999, 1.0 };
GLfloat lP2[] = { 1.999, 1.999, -1.999, 1.0 };
GLfloat lP3[] = { -1.999, 1.999, -1.999, 1.0 };
GLfloat lP4[] = { -1.999, -1.999, 1.999, 1.0 };
GLfloat lP5[] = { 1.999, -1.999, 1.999, 1.0 };
GLfloat lP6[] = { 1.999, 1.999, 1.999, 1.0 };
GLfloat lP7[] = { -1.999, 1.999, 1.999, 1.0 };
// jelly material color
GLfloat mKa[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat mKd[] = { 0, 0, 0.7, 0.7 };
GLfloat mKs[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mKe[] = { 0.0, 0.0, 0.0, 1.0 };
/* set up lighting */
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, aGa);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
// set up cube color
glMaterialfv(GL_FRONT, GL_AMBIENT, mKa);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mKd);
glMaterialfv(GL_FRONT, GL_SPECULAR, mKs);
glMaterialfv(GL_FRONT, GL_EMISSION, mKe);
glMaterialf(GL_FRONT, GL_SHININESS, 120);
// macro to set up light i
#define LIGHTSETUP(i)\
glLightfv(GL_LIGHT##i, GL_POSITION, lP##i);\
glLightfv(GL_LIGHT##i, GL_AMBIENT, lKa##i);\
glLightfv(GL_LIGHT##i, GL_DIFFUSE, lKd##i);\
glLightfv(GL_LIGHT##i, GL_SPECULAR, lKs##i);\
glEnable(GL_LIGHT##i)
LIGHTSETUP (0);
LIGHTSETUP (1);
LIGHTSETUP (2);
LIGHTSETUP (3);
LIGHTSETUP (4);
LIGHTSETUP (5);
LIGHTSETUP (6);
LIGHTSETUP (7);
// enable lighting
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
// glEnable(GL_BLEND);
// glBlendFunc(GL_SRC_COLOR, GL_DST_ALPHA);
// show the cube
showCube(&jello);
glDisable(GL_LIGHTING);
// show the bounding box
showBoundingBox();
// if inclined plane is present, show it
if (jello.incPlanePresent == 1) showInclinedPlane(&jello);
glutSwapBuffers();
}
// #define TURNONOFFLIGHTS(i)\
// if (i%2==0) {\
// glDisable(GL_LIGHT##i+1);glDisable(GL_LIGHT##i-1);glDisable(GL_LIGHT##i+2);glDisable(GL_LIGHT##i+3);\
// }
void doIdle()
{
// TURNONOFFLIGHTS(1);
for (int i=1; i<=jello.n; i++) {
if (jello.integrator[0]=='E') Euler(&jello, &structuralSprings, &shearSprings, &bendSprings);
if (jello.integrator[0]=='R') RK4(&jello, &structuralSprings, &shearSprings, &bendSprings);
}
char s[20]="picxxxx.ppm";
// save screen to file
s[3] = 48 + (sprite / 1000);
s[4] = 48 + (sprite % 1000) / 100;
s[5] = 48 + (sprite % 100 ) / 10;
s[6] = 48 + sprite % 10;
if (saveScreenToFile==1)
{
saveScreenshot(windowWidth, windowHeight, s);
saveScreenToFile=0; // save only once, change this if you want continuos image generation (i.e. animation)
sprite++;
}
if (sprite >= 300) // allow only 300 snapshots
{
exit(0);
}
if (pause == 0)
{
// insert code which appropriately performs one step of the cube simulation:
}
glutPostRedisplay();
}
int main (int argc, char ** argv)
{
if (argc<2)
{
printf ("Oops! You didn't say the jello world file!\n");
printf ("Usage: %s [worldfile]\n", argv[0]);
exit(0);
}
readWorld(argv[1],&jello);
glutInit(&argc,argv);
/* double buffered window, use depth testing, 640x480 */
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
windowWidth = 640;
windowHeight = 480;
glutInitWindowSize (windowWidth, windowHeight);
glutInitWindowPosition (0,0);
glutCreateWindow ("Jello cube");
glutReshapeWindow(641, 481);
printf("Press 'i' to turn on interactive mode.\n\n");
/* tells glut to use a particular display function to redraw */
glutDisplayFunc(display);
/* replace with any animate code */
glutIdleFunc(doIdle);
/* callback for mouse drags */
glutMotionFunc(mouseMotionDrag);
/* callback for window size changes */
glutReshapeFunc(reshape);
/* callback for mouse movement */
glutPassiveMotionFunc(mouseMotion);
/* callback for mouse button changes */
glutMouseFunc(mouseButton);
/* register for keyboard events */
glutKeyboardFunc(keyboardFunc);
/* register for arrow key interaction function */
glutSpecialFunc(arrowKeyFunc);
/* do initialization */
myinit();
/* forever sink in the black hole */
glutMainLoop();
return(0);
}