3 * @author Sam Moore (20503628) 2012 - adapted from template program provided by UWA
4 * @purpose Definition of graphics related functions
7 #include "graphics.h" //Function declarations
8 #include <time.h> // Needed for clock
9 #include <math.h> // Maths functions (sin, cos)
13 double previousTime, eyeTheta, eyePhi, eyeRho;
15 int windowWidth, windowHeight, upY;
21 * Initialise the graphics
23 void Graphics_Init(void)
26 glClearColor(1.0,1.0,1.0,0.0);
27 glColor3f(0.0f, 0.0f, 0.0f);
28 glPointSize(POINT_SIZE);
29 glMatrixMode(GL_PROJECTION);
34 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
35 GLfloat mat_shininess[] = { 50.0 };
36 GLfloat light_position[] = { 1.0, 1.0, 0.0, 0.0 };
37 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
38 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
39 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
41 glColorMaterial(GL_FRONT,GL_DIFFUSE); // Set Color Capability
43 glEnable(GL_LIGHTING);
45 glEnable(GL_DEPTH_TEST);
47 glEnable(GL_COLOR_MATERIAL); // Enable color
51 windowHeight = HEIGHT;
52 previousTime = clock();
60 gluPerspective(VIEW_ANGLE, (double)WIDTH/(double)HEIGHT, WORLD_NEAR, WORLD_FAR);
64 * This function redraws the screen after the positions of particles
67 void Graphics_Display()
69 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
70 glMatrixMode(GL_MODELVIEW);
72 gluLookAt(eyeRho * sin(eyePhi) * sin(eyeTheta), eyeRho * cos(eyePhi),
73 eyeRho * sin(eyePhi) * cos(eyeTheta),look[0], look[1], look[2], 0, upY, 0);
75 for (int i = 0; i < universe.N; ++i)
77 Body * b = universe.body+i;
78 glColor3f(0.0f, b->mass/1e11*100, 0.0f);
79 glPushMatrix(); // to save the current matrix
80 glTranslated(scale*b->x[0], scale*b->x[1], scale*b->x[2]);
81 glutSolidSphere (BALL_SIZE, 10, 10);
82 glPopMatrix(); // restore the previous matrix
88 * This function is to manipulate with the image
90 void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY)
92 if (theKey == 'x' || theKey == 'X')
97 if (theKey == 'i' || theKey == 'I') {
101 } else if (theKey == 'm' || theKey == 'I') {
103 } else if (theKey == 'j' || theKey == 'J') {
104 eyeTheta -= M_PI / 20;
105 } else if (theKey == 'k' || theKey == 'K') {
106 eyeTheta += M_PI / 20;
107 } else if (theKey == ',') {
109 } else if (theKey == '.' || theKey == 'I') {
111 } else if (theKey == 'w' || theKey == 'W') {
113 } else if (theKey == 'z' || theKey == 'Z') {
115 } else if (theKey == 'a' || theKey == 'A') {
117 } else if (theKey == 's' || theKey == 'S') {
119 } else if (theKey == '+') {
121 } else if (theKey == '-') {
124 if (sin(eyePhi) > 0) upY = 1;
129 * Resize the view window
131 void Graphics_Reshape(int width, int height)
133 double displayRatio = 1.0 * width / height;
135 windowHeight = height;
136 glMatrixMode(GL_PROJECTION);
138 gluPerspective(VIEW_ANGLE, displayRatio, WORLD_NEAR, WORLD_FAR);
139 glViewport(0, 0, windowWidth, windowHeight);
144 * This function is called repeatedly by graphics library. You can consider
145 * it as main loop in the program.
147 void Graphics_Animate(void)
149 System_Compute(&universe); //Compute and update new positions for the time step
150 Graphics_Display(); // Display needs to be called to redraw the screen