3 * @author Sam Moore (20503628) 2012
4 * - Adapted from template program provided by UWA
5 * @purpose Definition of graphics related functions
6 * NOTE: This file is identical for both the single-threaded and multi-threaded versions of the program
12 #include <time.h> // Needed for clock
13 #include <math.h> // Maths functions (sin, cos)
15 #include "graphics.h" //Function declarations
16 #include "nbody.h" //The simulation
19 // --- Variable definitions --- //
20 double previousTime, eyeTheta, eyePhi, eyeRho;
22 int windowWidth, windowHeight, upY;
27 #endif //FLYING_CAMERA
30 * @function Graphics_Run
31 * @purpose Setup and start the graphics engine
32 * @param argc - Number of arguments to main function, passed to glutInit
33 * @param argv - Argument strings for main function, passed to glutInit
35 void Graphics_Run(int argc, char ** argv)
37 if (options.draw_graphics == false)
39 // This message is left here for when I inevitably accidentally call the function
40 fprintf(stderr, "Graphics_Run should not be called if graphics are disabled!\n");
44 glutInit(&argc, argv);
45 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
46 glutInitWindowSize(WIDTH, HEIGHT);
47 glutInitWindowPosition(POSITION_X, POSITION_Y);
49 //Set window title based on version of program
50 #ifdef SINGLE_THREADED
51 glutCreateWindow("N-Body : Single-Threaded");
52 #elif defined PTHREADED
53 glutCreateWindow("N-Body : Multi-Threaded (pthread)");
54 #elif defined OMP_THREADED
55 glutCreateWindow("N-Body : Multi-Threaded (OpenMP)");
57 glutCreateWindow("N-Body");
59 glutDisplayFunc(Graphics_Display);
60 glutIdleFunc(Graphics_Display);
61 glutKeyboardFunc(Graphics_Keyboard);
62 glutReshapeFunc(Graphics_Reshape);
65 glClearColor(1.0,1.0,1.0,0.0);
66 glColor3f(0.0f, 0.0f, 0.0f);
67 glPointSize(POINT_SIZE);
68 glMatrixMode(GL_PROJECTION);
73 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
74 GLfloat mat_shininess[] = { 50.0 };
75 GLfloat light_position[] = { 1.0, 1.0, 0.0, 0.0 };
76 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
77 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
78 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
80 glColorMaterial(GL_FRONT,GL_DIFFUSE); // Set Color Capability
82 glEnable(GL_LIGHTING);
84 glEnable(GL_DEPTH_TEST);
86 glEnable(GL_COLOR_MATERIAL); // Enable color
90 windowHeight = HEIGHT;
91 previousTime = clock();
99 gluPerspective(VIEW_ANGLE, (double)WIDTH/(double)HEIGHT, WORLD_NEAR, WORLD_FAR);
102 eye.x[0] = 0; eye.x[1] = 0; eye.x[2] = 1;
103 eye.y[0] = 0; eye.y[1] = 1; eye.y[2] = 0;
104 eye.z[0] = 1; eye.z[1] = 0; eye.z[2] = 0;
105 eye.p[0] = 0; eye.p[1] = 0; eye.p[2] = -50;
106 #endif //FLYING_CAMERA
109 //This option must be set, or when glut leaves the main loop. the exit(3) function is called... annoying
110 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
117 * @function Graphics_Display
118 * @purpose This function redraws the screen after the positions of particles
120 * It also calls System_Compute, and checks for exit conditions, in the single-threaded version only
122 void Graphics_Display()
125 if (options.draw_graphics == false)
127 // This message is left here for when I inevitably accidentally call the function
128 fprintf(stderr, "Graphics_Display should not be called if graphics are disabled!\n");
132 //Check whether the graphics thread should exit for any reason
135 glutLeaveMainLoop(); // original glut does not have this, which makes "nicely" exiting a bit annoying
139 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
140 glMatrixMode(GL_MODELVIEW);
145 gluLookAt(eye.p[0], eye.p[1], eye.p[2],
146 eye.p[0] + eye.x[0], eye.p[1] + eye.x[1], eye.p[2] + eye.x[2],
147 eye.z[0], eye.z[1], eye.z[2]);
149 gluLookAt(eyeRho * sin(eyePhi) * sin(eyeTheta), eyeRho * cos(eyePhi),
150 eyeRho * sin(eyePhi) * cos(eyeTheta),look[0], look[1], look[2], 0, upY, 0);
151 #endif //FLYING_CAMERA
153 BeforeDraw(); // Stuff to do before graphics is allowed to draw
154 // Single-thread - perform a computation step, obviously! It's not done anywhere else
155 // Multiple threads - ensure that all body positions are updated (ie: not halfway through step).
156 // (We don't care about the force and velocity variables though)
158 for (int i = 0; i < universe.N; ++i)
161 Body * b = universe.body+i;
162 glColor3f(0.0f, b->mass/1e11*100, 0.0f);
163 //glColor3f(1.0f, 0.0f, 0.0f);
164 glPushMatrix(); // to save the current matrix
165 glTranslated(scale*b->x[0], scale*b->x[1], scale*b->x[2]);
166 glutSolidSphere (BALL_SIZE, 10, 10);
167 glPopMatrix(); // restore the previous matrix
171 AfterDraw(); // Stuff to do after graphics is done drawing
172 // Single-thread - Nothing at all
173 // Multiple threads - signal threads it is safe to change position variables
178 * @function Graphics_Keyboard
179 * @purpose This function is to manipulate with the image
180 * @param theKey key pressed
181 * @param mouseX, mouseY position of the mouse in the window
183 void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY)
185 if (theKey == 'x' || theKey == 'X')
194 // Translate in direction of camera
197 for (unsigned i = 0; i < DIMENSIONS; ++i)
198 eye.p[i] += eye.x[i];
200 // Translate backwards from camera direction
203 for (unsigned i = 0; i < DIMENSIONS; ++i)
204 eye.p[i] -= eye.x[i];
206 // Translate left from camera direction
209 for (unsigned i = 0; i < DIMENSIONS; ++i)
210 eye.p[i] -= eye.y[i];
212 // Translate right from camera direction
215 for (unsigned i = 0; i < DIMENSIONS; ++i)
216 eye.p[i] += eye.y[i];
218 // Translate up from camera direction
221 for (unsigned i = 0; i < DIMENSIONS; ++i)
222 eye.p[i] += eye.z[i];
224 // Translate down from camera direction
227 for (unsigned i = 0; i < DIMENSIONS; ++i)
228 eye.p[i] -= eye.z[i];
231 // Rotate camera direction "down"
236 float theta = M_PI/80.0;
239 for (unsigned i = 0; i < DIMENSIONS; ++i)
241 eye.z[i] = old.z[i] * cos(theta) + old.x[i] * sin(theta);
242 eye.x[i] = old.x[i] * cos(theta) - old.z[i] * sin(theta);
246 // Rotate camera direction "up"
251 float theta = -M_PI/80.0;
254 for (unsigned i = 0; i < DIMENSIONS; ++i)
256 eye.z[i] = old.z[i] * cos(theta) + old.x[i] * sin(theta);
257 eye.x[i] = old.x[i] * cos(theta) - old.z[i] * sin(theta);
261 // Rotate camera direction "left"
266 float theta = +M_PI/80.0;
269 for (unsigned i = 0; i < DIMENSIONS; ++i)
271 eye.y[i] = old.y[i] * cos(theta) + old.x[i] * sin(theta);
272 eye.x[i] = old.x[i] * cos(theta) - old.y[i] * sin(theta);
276 // Rotate camera direction "right"
281 float theta = -M_PI/80.0;
284 for (unsigned i = 0; i < DIMENSIONS; ++i)
286 eye.y[i] = old.y[i] * cos(theta) + old.x[i] * sin(theta);
287 eye.x[i] = old.x[i] * cos(theta) - old.y[i] * sin(theta);
291 // Rotate camera direction CCW about its axis
296 float theta = -M_PI/80.0;
299 for (unsigned i = 0; i < DIMENSIONS; ++i)
301 eye.z[i] = old.z[i] * cos(theta) + old.y[i] * sin(theta);
302 eye.y[i] = old.y[i] * cos(theta) - old.z[i] * sin(theta);
306 // Rotate camera direction CW about its axis
311 float theta = +M_PI/80.0;
314 for (unsigned i = 0; i < DIMENSIONS; ++i)
316 eye.z[i] = old.z[i] * cos(theta) + old.y[i] * sin(theta);
317 eye.y[i] = old.y[i] * cos(theta) - old.z[i] * sin(theta);
323 /* Code used for debugging the camera
325 printf("Camera status:\n");
326 printf("Position: %f %f %f\n", eye.p[0], eye.p[1], eye.p[2]);
327 printf("x: %f %f %f (%f)\n", eye.x[0], eye.x[1], eye.x[2], sqrt(eye.x[0]*eye.x[0] + eye.x[1]*eye.x[1] + eye.x[2]*eye.x[2]));
328 printf("y: %f %f %f (%f)\n", eye.y[0], eye.y[1], eye.y[2], sqrt(eye.y[0]*eye.y[0] + eye.y[1]*eye.y[1] + eye.y[2]*eye.y[2]));
329 printf("z: %f %f %f (%f)\n", eye.z[0], eye.z[1], eye.z[2], sqrt(eye.z[0]*eye.z[0] + eye.z[1]*eye.z[1] + eye.z[2]*eye.z[2]));
331 #else // The original view code
333 // I like how 'I' is used three times.
335 if (theKey == 'i' || theKey == 'I') {
339 } else if (theKey == 'm' || theKey == 'I') {
341 } else if (theKey == 'j' || theKey == 'J') {
342 eyeTheta -= M_PI / 20;
343 } else if (theKey == 'k' || theKey == 'K') {
344 eyeTheta += M_PI / 20;
345 } else if (theKey == ',') {
347 } else if (theKey == '.' || theKey == 'I') {
349 } else if (theKey == 'w' || theKey == 'W') {
351 } else if (theKey == 'z' || theKey == 'Z') {
353 } else if (theKey == 'a' || theKey == 'A') {
355 } else if (theKey == 's' || theKey == 'S') {
357 } else if (theKey == '+') {
359 } else if (theKey == '-') {
362 if (sin(eyePhi) > 0) upY = 1;
364 #endif //FLYING_CAMERA
368 * @function Graphics_Reshape
369 * @purpose Resize the view window
370 * @param width, height - New size of the window
372 void Graphics_Reshape(int width, int height)
374 double displayRatio = 1.0 * width / height;
376 windowHeight = height;
377 glMatrixMode(GL_PROJECTION);
379 gluPerspective(VIEW_ANGLE, displayRatio, WORLD_NEAR, WORLD_FAR);
380 glViewport(0, 0, windowWidth, windowHeight);