-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjello.cpp
286 lines (214 loc) · 6.67 KB
/
jello.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
/*
USC/Viterbi/Computer Science
"Jello Cube" Assignment 1 starter code
Your name:
Johnson Lien
*/
#include "jello.h"
#include "showCube.h"
#include "input.h"
#include "physics.h"
// camera parameters
double Theta = pi / 6;
double Phi = pi / 6;
double R = 6;
// 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;
int windowWidth, windowHeight;
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0,1.0,0.01,1000.0);
// set background color to grey
glClearColor(0.5, 0.5, 0.5, 0.0);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_LINE_SMOOTH);
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), R * sin(Phi) * cos (Theta), R * sin (Theta),
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[] = { 1.0, 1.0, 1.0, 1.0 };
// light 's ambient, diffuse, specular
GLfloat lKa0[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat lKd0[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat lKs0[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat lKa1[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat lKd1[] = { 1.0, 0.0, 0.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[] = { 1.0, 1.0, 0.0, 1.0 };
GLfloat lKs2[] = { 1.0, 1.0, 0.0, 1.0 };
GLfloat lKa3[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat lKd3[] = { 0.0, 1.0, 1.0, 1.0 };
GLfloat lKs3[] = { 0.0, 1.0, 1.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[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat lKa5[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat lKd5[] = { 1.0, 0.0, 1.0, 1.0 };
GLfloat lKs5[] = { 1.0, 0.0, 1.0, 1.0 };
GLfloat lKa6[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat lKd6[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat lKs6[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat lKa7[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat lKd7[] = { 0.0, 1.0, 1.0, 1.0 };
GLfloat lKs7[] = { 0.0, 1.0, 1.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.3, 0.3, 0.3, 1.0 };
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);
// show the cube
showCube(&jello);
glDisable(GL_LIGHTING);
// show the bounding box
showBoundingBox();
// show the incline plane
showInclinePlane(&jello);
glutSwapBuffers();
}
void doIdle()
{
char s[20]="picxxxx.ppm";
int i;
// 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=1; // save only once, change this if you want continuous 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:
for (i = 1; i <= jello.n; i++) {
if (jello.integrator[0] == 'E') Euler(&jello);
if (jello.integrator[0] == 'R') RK4(&jello);
}
}
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");
/* 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);
/* do initialization */
myinit();
/* forever sink in the black hole */
glutMainLoop();
return(0);
}