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
18 // --- Variable definitions --- //
19 double previousTime, eyeTheta, eyePhi, eyeRho;
21 int windowWidth, windowHeight, upY;
26 * @function Graphics_Run
27 * @purpose Setup and start the graphics engine
28 * @param argc - Number of arguments to main function, passed to glutInit
29 * @param argv - Argument strings for main function, passed to glutInit
31 void Graphics_Run(int argc, char ** argv)
33 if (options.draw_graphics == false)
41 glutInit(&argc, argv);
42 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
43 glutInitWindowSize(WIDTH, HEIGHT);
44 glutInitWindowPosition(POSITION_X, POSITION_Y);
46 //Set window title based on version of program
47 #ifdef SINGLE_THREADED
48 glutCreateWindow("N-Body : Single-Threaded");
49 #elif defined PTHREADED
50 glutCreateWindow("N-Body : Multi-Threaded (pthread)");
51 #elif defined OMP_THREADED
52 glutCreateWindow("N-Body : Multi-Threaded (OpenMP)");
54 glutCreateWindow("N-Body");
56 glutDisplayFunc(Graphics_Display);
57 glutIdleFunc(Graphics_Display);
58 glutKeyboardFunc(Graphics_Keyboard);
59 glutReshapeFunc(Graphics_Reshape);
62 glClearColor(1.0,1.0,1.0,0.0);
63 glColor3f(0.0f, 0.0f, 0.0f);
64 glPointSize(POINT_SIZE);
65 glMatrixMode(GL_PROJECTION);
70 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
71 GLfloat mat_shininess[] = { 50.0 };
72 GLfloat light_position[] = { 1.0, 1.0, 0.0, 0.0 };
73 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
74 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
75 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
77 glColorMaterial(GL_FRONT,GL_DIFFUSE); // Set Color Capability
79 glEnable(GL_LIGHTING);
81 glEnable(GL_DEPTH_TEST);
83 glEnable(GL_COLOR_MATERIAL); // Enable color
87 windowHeight = HEIGHT;
88 previousTime = clock();
96 gluPerspective(VIEW_ANGLE, (double)WIDTH/(double)HEIGHT, WORLD_NEAR, WORLD_FAR);
102 * @function Graphics_Display
103 * @purpose This function redraws the screen after the positions of particles
105 * It also calls System_Compute, and checks for exit conditions, in the single-threaded version only
107 void Graphics_Display()
111 if (options.verbosity != 0 && universe.steps % options.verbosity == 1)
114 #ifdef SINGLE_THREADED
115 System_Compute(&universe);
117 //Check whether the program should quit due to steps being computed, or a timeout
118 if (options.timeout > 0.0)
120 if ((unsigned)(time(NULL) - options.start_time.tv_sec) >= options.timeout)
123 if (options.num_steps > 0 && universe.steps > options.num_steps)
130 //Check whether the runstate has been set to quit the program
143 if (options.draw_graphics == false)
147 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
148 glMatrixMode(GL_MODELVIEW);
150 gluLookAt(eyeRho * sin(eyePhi) * sin(eyeTheta), eyeRho * cos(eyePhi),
151 eyeRho * sin(eyePhi) * cos(eyeTheta),look[0], look[1], look[2], 0, upY, 0);
153 for (int i = 0; i < universe.N; ++i)
155 Body * b = universe.body+i;
156 glColor3f(0.0f, b->mass/1e11*100, 0.0f);
157 glPushMatrix(); // to save the current matrix
158 glTranslated(scale*b->x[0], scale*b->x[1], scale*b->x[2]);
159 glutSolidSphere (BALL_SIZE, 10, 10);
160 glPopMatrix(); // restore the previous matrix
166 * @function Graphics_Keyboard
167 * @purpose This function is to manipulate with the image
168 * @param theKey key pressed
169 * @param mouseX, mouseY position of the mouse in the window
171 void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY)
173 if (theKey == 'x' || theKey == 'X')
178 if (theKey == 'i' || theKey == 'I') {
182 } else if (theKey == 'm' || theKey == 'I') {
184 } else if (theKey == 'j' || theKey == 'J') {
185 eyeTheta -= M_PI / 20;
186 } else if (theKey == 'k' || theKey == 'K') {
187 eyeTheta += M_PI / 20;
188 } else if (theKey == ',') {
190 } else if (theKey == '.' || theKey == 'I') {
192 } else if (theKey == 'w' || theKey == 'W') {
194 } else if (theKey == 'z' || theKey == 'Z') {
196 } else if (theKey == 'a' || theKey == 'A') {
198 } else if (theKey == 's' || theKey == 'S') {
200 } else if (theKey == '+') {
202 } else if (theKey == '-') {
205 if (sin(eyePhi) > 0) upY = 1;
210 * @function Graphics_Reshape
211 * @purpose Resize the view window
212 * @param width, height - New size of the window
214 void Graphics_Reshape(int width, int height)
216 double displayRatio = 1.0 * width / height;
218 windowHeight = height;
219 glMatrixMode(GL_PROJECTION);
221 gluPerspective(VIEW_ANGLE, displayRatio, WORLD_NEAR, WORLD_FAR);
222 glViewport(0, 0, windowWidth, windowHeight);