3 * @author Sam Moore (20503628) 2012 - adapted from template program provided by UWA
4 * @purpose Definition of graphics related functions
5 * NOTE: The graphics functions are run in the main thread.
8 #include "graphics.h" //Function declarations
12 #include <time.h> // Needed for clock
13 #include <math.h> // Maths functions (sin, cos)
17 double previousTime, eyeTheta, eyePhi, eyeRho;
19 int windowWidth, windowHeight, upY;
25 * Initialise the graphics
27 void Graphics_Run(int argc, char ** argv)
30 glutInit(&argc, argv);
31 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
32 glutInitWindowSize(WIDTH, HEIGHT);
33 glutInitWindowPosition(POSITION_X, POSITION_Y);
34 glutCreateWindow("N-Body : Multi-Threaded (pthread)");
35 glutDisplayFunc(Graphics_Display);
36 glutIdleFunc(Graphics_Display);
37 glutKeyboardFunc(Graphics_Keyboard);
38 glutReshapeFunc(Graphics_Reshape);
41 glClearColor(1.0,1.0,1.0,0.0);
42 glColor3f(0.0f, 0.0f, 0.0f);
43 glPointSize(POINT_SIZE);
44 glMatrixMode(GL_PROJECTION);
49 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
50 GLfloat mat_shininess[] = { 50.0 };
51 GLfloat light_position[] = { 1.0, 1.0, 0.0, 0.0 };
52 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
53 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
54 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
56 glColorMaterial(GL_FRONT,GL_DIFFUSE); // Set Color Capability
58 glEnable(GL_LIGHTING);
60 glEnable(GL_DEPTH_TEST);
62 glEnable(GL_COLOR_MATERIAL); // Enable color
66 windowHeight = HEIGHT;
67 previousTime = clock();
75 gluPerspective(VIEW_ANGLE, (double)WIDTH/(double)HEIGHT, WORLD_NEAR, WORLD_FAR);
78 printf("Use:\n X - exit\n I, J, K, M - rotate\n W, Z, A, S - move to view"
79 " point\n ./, - zoom in/out\n +/- - scaled zoom in/out\n");
85 * This function redraws the screen after the positions of particles
88 void Graphics_Display()
90 //Check whether the runstate has been set to quit the program
103 //Check if window exists, quit if it doesn't
104 if (glutGetWindow() == 0)
110 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
111 glMatrixMode(GL_MODELVIEW);
113 gluLookAt(eyeRho * sin(eyePhi) * sin(eyeTheta), eyeRho * cos(eyePhi),
114 eyeRho * sin(eyePhi) * cos(eyeTheta),look[0], look[1], look[2], 0, upY, 0);
116 for (int i = 0; i < universe.N; ++i)
118 Body * b = universe.body+i;
119 glColor3f(0.0f, b->mass/1e11*100, 0.0f);
120 glPushMatrix(); // to save the current matrix
121 glTranslated(scale*b->x[0], scale*b->x[1], scale*b->x[2]);
122 glutSolidSphere (BALL_SIZE, 10, 10);
123 glPopMatrix(); // restore the previous matrix
129 * This function is to manipulate with the image
131 void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY)
133 if (theKey == 'x' || theKey == 'X')
138 if (theKey == 'i' || theKey == 'I') {
142 } else if (theKey == 'm' || theKey == 'I') {
144 } else if (theKey == 'j' || theKey == 'J') {
145 eyeTheta -= M_PI / 20;
146 } else if (theKey == 'k' || theKey == 'K') {
147 eyeTheta += M_PI / 20;
148 } else if (theKey == ',') {
150 } else if (theKey == '.' || theKey == 'I') {
152 } else if (theKey == 'w' || theKey == 'W') {
154 } else if (theKey == 'z' || theKey == 'Z') {
156 } else if (theKey == 'a' || theKey == 'A') {
158 } else if (theKey == 's' || theKey == 'S') {
160 } else if (theKey == '+') {
162 } else if (theKey == '-') {
165 if (sin(eyePhi) > 0) upY = 1;
170 * Resize the view window
172 void Graphics_Reshape(int width, int height)
174 double displayRatio = 1.0 * width / height;
176 windowHeight = height;
177 glMatrixMode(GL_PROJECTION);
179 gluPerspective(VIEW_ANGLE, displayRatio, WORLD_NEAR, WORLD_FAR);
180 glViewport(0, 0, windowWidth, windowHeight);