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()
91 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
92 glMatrixMode(GL_MODELVIEW);
94 gluLookAt(eyeRho * sin(eyePhi) * sin(eyeTheta), eyeRho * cos(eyePhi),
95 eyeRho * sin(eyePhi) * cos(eyeTheta),look[0], look[1], look[2], 0, upY, 0);
97 for (int i = 0; i < universe.N; ++i)
99 Body * b = universe.body+i;
100 glColor3f(0.0f, b->mass/1e11*100, 0.0f);
101 glPushMatrix(); // to save the current matrix
102 glTranslated(scale*b->x[0], scale*b->x[1], scale*b->x[2]);
103 glutSolidSphere (BALL_SIZE, 10, 10);
104 glPopMatrix(); // restore the previous matrix
110 * This function is to manipulate with the image
112 void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY)
114 if (theKey == 'x' || theKey == 'X')
116 printf("Kill recieved\n");
120 if (theKey == 'i' || theKey == 'I') {
124 } else if (theKey == 'm' || theKey == 'I') {
126 } else if (theKey == 'j' || theKey == 'J') {
127 eyeTheta -= M_PI / 20;
128 } else if (theKey == 'k' || theKey == 'K') {
129 eyeTheta += M_PI / 20;
130 } else if (theKey == ',') {
132 } else if (theKey == '.' || theKey == 'I') {
134 } else if (theKey == 'w' || theKey == 'W') {
136 } else if (theKey == 'z' || theKey == 'Z') {
138 } else if (theKey == 'a' || theKey == 'A') {
140 } else if (theKey == 's' || theKey == 'S') {
142 } else if (theKey == '+') {
144 } else if (theKey == '-') {
147 if (sin(eyePhi) > 0) upY = 1;
152 * Resize the view window
154 void Graphics_Reshape(int width, int height)
156 double displayRatio = 1.0 * width / height;
158 windowHeight = height;
159 glMatrixMode(GL_PROJECTION);
161 gluPerspective(VIEW_ANGLE, displayRatio, WORLD_NEAR, WORLD_FAR);
162 glViewport(0, 0, windowWidth, windowHeight);