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
113 //printf("Leave graphics loop\n");
118 #ifdef SINGLE_THREADED
119 if (options.verbosity != 0 && universe.steps % options.verbosity == 1)
121 System_Compute(&universe);
124 if (options.draw_graphics == false)
128 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
129 glMatrixMode(GL_MODELVIEW);
131 gluLookAt(eyeRho * sin(eyePhi) * sin(eyeTheta), eyeRho * cos(eyePhi),
132 eyeRho * sin(eyePhi) * cos(eyeTheta),look[0], look[1], look[2], 0, upY, 0);
134 for (int i = 0; i < universe.N; ++i)
136 Body * b = universe.body+i;
137 glColor3f(0.0f, b->mass/1e11*100, 0.0f);
138 glPushMatrix(); // to save the current matrix
139 glTranslated(scale*b->x[0], scale*b->x[1], scale*b->x[2]);
140 glutSolidSphere (BALL_SIZE, 10, 10);
141 glPopMatrix(); // restore the previous matrix
147 * @function Graphics_Keyboard
148 * @purpose This function is to manipulate with the image
149 * @param theKey key pressed
150 * @param mouseX, mouseY position of the mouse in the window
152 void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY)
154 if (theKey == 'x' || theKey == 'X')
160 if (theKey == 'i' || theKey == 'I') {
164 } else if (theKey == 'm' || theKey == 'I') {
166 } else if (theKey == 'j' || theKey == 'J') {
167 eyeTheta -= M_PI / 20;
168 } else if (theKey == 'k' || theKey == 'K') {
169 eyeTheta += M_PI / 20;
170 } else if (theKey == ',') {
172 } else if (theKey == '.' || theKey == 'I') {
174 } else if (theKey == 'w' || theKey == 'W') {
176 } else if (theKey == 'z' || theKey == 'Z') {
178 } else if (theKey == 'a' || theKey == 'A') {
180 } else if (theKey == 's' || theKey == 'S') {
182 } else if (theKey == '+') {
184 } else if (theKey == '-') {
187 if (sin(eyePhi) > 0) upY = 1;
192 * @function Graphics_Reshape
193 * @purpose Resize the view window
194 * @param width, height - New size of the window
196 void Graphics_Reshape(int width, int height)
198 double displayRatio = 1.0 * width / height;
200 windowHeight = height;
201 glMatrixMode(GL_PROJECTION);
203 gluPerspective(VIEW_ANGLE, displayRatio, WORLD_NEAR, WORLD_FAR);
204 glViewport(0, 0, windowWidth, windowHeight);