-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
407 lines (331 loc) · 10.2 KB
/
main.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
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
#ifdef _WIN32
#include "GL/freeglut.h"
#else
#include <GL/glut.h>
#endif
#include <vecmath.h>
#include "camera.h"
///include more headers if necessary
#include <string>
#include "TimeStepper.hpp"
#include "SPHSystem.h"
#include "Box.h"
#include "Material.h"
using namespace std;
// Globals here.
namespace
{
SPHSystem * system;
//use RK4 as integrator
TimeStepper * timeStepper = new ForwardEuler();
float h = 0.005f;
bool pause = true;
bool boxDraw = false;
bool fSphereDraw = false;
Material* m = new Ferro(1000);
// initialize particle system
void initSystem()
{
// seed the random number generator with the current time
srand( time( NULL ) );
system = new SPHSystem(m, false);
}
// Take a step forward for the particle shower
///and switch between different timeSteppers
void stepSystem()
{
///TODO The stepsize should change according to commandline arguments
//const float h = 0.04f;
if(timeStepper!=0 && !pause){
timeStepper->takeStep(system,h);
}
}
// Draw the current particle positions
void drawSystem()
{
// Base material colors (they don't change)
GLfloat particleColor[] = {0.4f, 0.7f, 1.0f, 1.0f};
GLfloat floorColor[] = {1.0f, 0.0f, 0.0f, 1.0f};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, particleColor);
//glutSolidSphere(0.1f,10.0f,10.0f);
system->draw();
if (boxDraw) {system->getBox().draw();}
if (fSphereDraw) {system->getForceSphere()->draw();}
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, floorColor);
glPushMatrix();
glTranslatef(0.0f,-5.0f,0.0f);
glScaled(50.0f,0.01f,50.0f);
glutSolidCube(1);
glPopMatrix();
}
void resetSystem() {
delete system;
initSystem();
}
//-------------------------------------------------------------------
// This is the camera
Camera camera;
// These are state variables for the UI
bool g_mousePressed = false;
// Declarations of functions whose implementations occur later.
void arcballRotation(int endX, int endY);
void keyboardFunc( unsigned char key, int x, int y);
void specialFunc( int key, int x, int y );
void mouseFunc(int button, int state, int x, int y);
void motionFunc(int x, int y);
void reshapeFunc(int w, int h);
void drawScene(void);
void initRendering();
// This function is called whenever a "Normal" key press is
// received.
void keyboardFunc( unsigned char key, int x, int y )
{
switch ( key )
{
case 'a':
system->AddHundredParticles();
break;
case 'b':
if (boxDraw) {boxDraw = false;} else {boxDraw = true;}
break;
case 'c':
system->clear();
break;
case 'f':
if (fSphereDraw) {fSphereDraw= false;} else {fSphereDraw = true;}
break;
case 'h': // DOWN
//printf("DOWN");
system->getForceSphere()->move(Vector3f(0,-0.01,0));
break;
case 'u': //LEFT
//printf("LEFT");
system->getForceSphere()->move(Vector3f(-0.01,0,0));
break;
case 'j': //RIGHT
//printf("RIGHT");
system->getForceSphere()->move(Vector3f(0.01,0,0));
break;
case 'i': //LEFT
//printf("LEFT");
system->getForceSphere()->move(Vector3f(0,0,0.01));
break;
case 'k': //RIGHT
//printf("RIGHT");
system->getForceSphere()->move(Vector3f(0,0,-0.01));
break;
case 'r':
resetSystem();
break;
case 'p':
if (pause) {pause = false;} else {pause = true;}
break;
case 's':
if (timeStepper!=0 && pause) {
timeStepper->takeStep(system,h);
}
break;
case 'm':
if (system->mesh) {
system->mesh = false;
} else {
system->mesh = true;
}
break;
case 'y': // UP
//printf("UP");
system->getForceSphere()->move(Vector3f(0,0.01,0));
break;
case 27: // Escape key
exit(0);
break;
case ' ':
{
Matrix4f eye = Matrix4f::identity();
camera.SetRotation( eye );
camera.SetCenter( Vector3f::ZERO );
break;
}
default:
cout << "Unhandled key press " << key << "." << endl;
}
glutPostRedisplay();
}
// This function is called whenever a "Special" key press is
// received. Right now, it's handling the arrow keys.
void specialFunc( int key, int x, int y )
{
switch ( key )
{
case 101: // UP
system->getForceSphere()->setAmplitude(500.0);
printf("Amp: %f \n",system->getForceSphere()->getAmplitude());
break;
case 103: // DOWN
//printf("DOWN");
system->getForceSphere()->setAmplitude(-500.0);
printf("Amp: %f \n",system->getForceSphere()->getAmplitude());
break;
case 100: //LEFT
//printf("LEFT");
break;
case 102: //LEFT
//printf("RIGHT");
break;
default:
cout << "Unhandled key press " << key << "." << endl;
}
//glutPostRedisplay();
}
// Called when mouse button is pressed.
void mouseFunc(int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
g_mousePressed = true;
switch (button)
{
case GLUT_LEFT_BUTTON:
camera.MouseClick(Camera::LEFT, x, y);
break;
case GLUT_MIDDLE_BUTTON:
camera.MouseClick(Camera::MIDDLE, x, y);
break;
case GLUT_RIGHT_BUTTON:
camera.MouseClick(Camera::RIGHT, x,y);
default:
break;
}
}
else
{
camera.MouseRelease(x,y);
g_mousePressed = false;
}
glutPostRedisplay();
}
// Called when mouse is moved while button pressed.
void motionFunc(int x, int y)
{
camera.MouseDrag(x,y);
glutPostRedisplay();
}
// Called when the window is resized
// w, h - width and height of the window in pixels.
void reshapeFunc(int w, int h)
{
camera.SetDimensions(w,h);
camera.SetViewport(0,0,w,h);
camera.ApplyViewport();
// Set up a perspective view, with square aspect ratio
glMatrixMode(GL_PROJECTION);
camera.SetPerspective(50);
glLoadMatrixf( camera.projectionMatrix() );
}
// Initialize OpenGL's rendering modes
void initRendering()
{
glEnable(GL_DEPTH_TEST); // Depth testing must be turned on
glEnable(GL_LIGHTING); // Enable lighting calculations
glEnable(GL_LIGHT0); // Turn on light #0.
glEnable(GL_NORMALIZE);
// Setup polygon drawing
glShadeModel(GL_SMOOTH);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//We do not cull as cloth utilizes both faces
//glEnable(GL_CULL_FACE);
//glCullFace(GL_BACK);
// Clear to black
glClearColor(0,0,0,1);
}
// This function is responsible for displaying the object.
void drawScene(void)
{
// Clear the rendering window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// Light color (RGBA)
GLfloat Lt0diff[] = {1.0,1.0,1.0,1.0};
GLfloat Lt0pos[] = {3.0,3.0,5.0,1.0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, Lt0diff);
glLightfv(GL_LIGHT0, GL_POSITION, Lt0pos);
glLoadMatrixf( camera.viewMatrix() );
// THIS IS WHERE THE DRAW CODE GOES.
drawSystem();
// This draws the coordinate axes when you're rotating, to
// keep yourself oriented.
if( g_mousePressed )
{
glPushMatrix();
Vector3f eye = camera.GetCenter();
glTranslatef( eye[0], eye[1], eye[2] );
// Save current state of OpenGL
glPushAttrib(GL_ALL_ATTRIB_BITS);
// This is to draw the axes when the mouse button is down
glDisable(GL_LIGHTING);
glLineWidth(3);
glPushMatrix();
glScaled(5.0,5.0,5.0);
glBegin(GL_LINES);
glColor4f(1,0.5,0.5,1); glVertex3f(0,0,0); glVertex3f(1,0,0);
glColor4f(0.5,1,0.5,1); glVertex3f(0,0,0); glVertex3f(0,1,0);
glColor4f(0.5,0.5,1,1); glVertex3f(0,0,0); glVertex3f(0,0,1);
glColor4f(0.5,0.5,0.5,1);
glVertex3f(0,0,0); glVertex3f(-1,0,0);
glVertex3f(0,0,0); glVertex3f(0,-1,0);
glVertex3f(0,0,0); glVertex3f(0,0,-1);
glEnd();
glPopMatrix();
glPopAttrib();
glPopMatrix();
}
// Dump the image to the screen.
glutSwapBuffers();
}
void timerFunc(int t)
{
stepSystem();
glutPostRedisplay();
glutTimerFunc(t, &timerFunc, t);
}
}
// Main routine.
// Set up OpenGL, define the callbacks and start the main loop
int main( int argc, char* argv[] )
{
glutInit( &argc, argv );
// We're going to animate it, so double buffer
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
// Initial parameters for window position and size
glutInitWindowPosition( 60, 60 );
glutInitWindowSize( 600, 600 );
camera.SetDimensions( 600, 600 );
camera.SetDistance( 5 );
camera.SetCenter( Vector3f::ZERO );
glutCreateWindow("Ferro");
// Initialize OpenGL parameters.
initRendering();
// Setup particle system
initSystem();
// Set up callback functions for key presses
glutKeyboardFunc(keyboardFunc); // Handles "normal" ascii symbols
glutSpecialFunc(specialFunc); // Handles "special" keyboard keys
// Set up callback functions for mouse
glutMouseFunc(mouseFunc);
glutMotionFunc(motionFunc);
// Set up the callback function for resizing windows
glutReshapeFunc( reshapeFunc );
// Call this whenever window needs redrawing
glutDisplayFunc( drawScene );
// Trigger timerFunc every 20 msec
glutTimerFunc(20, timerFunc, 20);
// Start the main loop. glutMainLoop never returns.
glutMainLoop();
return 0; // This line is never reached.
}