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
17 #include <GL/freeglut.h>
19 // --- Variable definitions --- //
20 double previousTime, eyeTheta, eyePhi, eyeRho;
22 int windowWidth, windowHeight, upY;
27 * @function Graphics_Run
28 * @purpose Setup and start the graphics engine
29 * @param argc - Number of arguments to main function, passed to glutInit
30 * @param argv - Argument strings for main function, passed to glutInit
32 void Graphics_Run(int argc, char ** argv)
34 if (options.draw_graphics == false)
42 glutInit(&argc, argv);
43 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
44 glutInitWindowSize(WIDTH, HEIGHT);
45 glutInitWindowPosition(POSITION_X, POSITION_Y);
47 //Set window title based on version of program
48 #ifdef SINGLE_THREADED
49 glutCreateWindow("N-Body : Single-Threaded");
50 #elif defined PTHREADED
51 glutCreateWindow("N-Body : Multi-Threaded (pthread)");
52 #elif defined OMP_THREADED
53 glutCreateWindow("N-Body : Multi-Threaded (OpenMP)");
55 glutCreateWindow("N-Body");
57 glutDisplayFunc(Graphics_Display);
58 glutIdleFunc(Graphics_Display);
59 glutKeyboardFunc(Graphics_Keyboard);
60 glutReshapeFunc(Graphics_Reshape);
63 glClearColor(1.0,1.0,1.0,0.0);
64 glColor3f(0.0f, 0.0f, 0.0f);
65 glPointSize(POINT_SIZE);
66 glMatrixMode(GL_PROJECTION);
71 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
72 GLfloat mat_shininess[] = { 50.0 };
73 GLfloat light_position[] = { 1.0, 1.0, 0.0, 0.0 };
74 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
75 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
76 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
78 glColorMaterial(GL_FRONT,GL_DIFFUSE); // Set Color Capability
80 glEnable(GL_LIGHTING);
82 glEnable(GL_DEPTH_TEST);
84 glEnable(GL_COLOR_MATERIAL); // Enable color
88 windowHeight = HEIGHT;
89 previousTime = clock();
97 gluPerspective(VIEW_ANGLE, (double)WIDTH/(double)HEIGHT, WORLD_NEAR, WORLD_FAR);
103 * @function Graphics_Display
104 * @purpose This function redraws the screen after the positions of particles
106 * It also calls System_Compute, and checks for exit conditions, in the single-threaded version only
108 void Graphics_Display()
110 //Check whether the program should quit due to steps being computed, or a timeout
118 #ifdef SINGLE_THREADED
119 if (options.verbosity != 0 && universe.steps % options.verbosity == 1)
121 System_Compute(&universe);
126 //Check whether the runstate has been set to quit the program
141 if (options.draw_graphics == false)
145 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
146 glMatrixMode(GL_MODELVIEW);
148 gluLookAt(eyeRho * sin(eyePhi) * sin(eyeTheta), eyeRho * cos(eyePhi),
149 eyeRho * sin(eyePhi) * cos(eyeTheta),look[0], look[1], look[2], 0, upY, 0);
151 for (int i = 0; i < universe.N; ++i)
153 Body * b = universe.body+i;
154 glColor3f(0.0f, b->mass/1e11*100, 0.0f);
155 glPushMatrix(); // to save the current matrix
156 glTranslated(scale*b->x[0], scale*b->x[1], scale*b->x[2]);
157 glutSolidSphere (BALL_SIZE, 10, 10);
158 glPopMatrix(); // restore the previous matrix
164 * @function Graphics_Keyboard
165 * @purpose This function is to manipulate with the image
166 * @param theKey key pressed
167 * @param mouseX, mouseY position of the mouse in the window
169 void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY)
171 if (theKey == 'x' || theKey == 'X')
177 if (theKey == 'i' || theKey == 'I') {
181 } else if (theKey == 'm' || theKey == 'I') {
183 } else if (theKey == 'j' || theKey == 'J') {
184 eyeTheta -= M_PI / 20;
185 } else if (theKey == 'k' || theKey == 'K') {
186 eyeTheta += M_PI / 20;
187 } else if (theKey == ',') {
189 } else if (theKey == '.' || theKey == 'I') {
191 } else if (theKey == 'w' || theKey == 'W') {
193 } else if (theKey == 'z' || theKey == 'Z') {
195 } else if (theKey == 'a' || theKey == 'A') {
197 } else if (theKey == 's' || theKey == 'S') {
199 } else if (theKey == '+') {
201 } else if (theKey == '-') {
204 if (sin(eyePhi) > 0) upY = 1;
209 * @function Graphics_Reshape
210 * @purpose Resize the view window
211 * @param width, height - New size of the window
213 void Graphics_Reshape(int width, int height)
215 double displayRatio = 1.0 * width / height;
217 windowHeight = height;
218 glMatrixMode(GL_PROJECTION);
220 gluPerspective(VIEW_ANGLE, displayRatio, WORLD_NEAR, WORLD_FAR);
221 glViewport(0, 0, windowWidth, windowHeight);