614c2bfdc6b4f7f6a77116f842311a42195738ed
[matches/honours.git] / course / semester2 / pprog / assignment1 / graphics.c
1 /**
2  * @file graphics.c
3  * @author Sam Moore (20503628) 2012 - adapted from template program provided by UWA
4  * @purpose Definition of graphics related functions
5  */
6
7 #include "graphics.h" //Function declarations
8 #include <time.h> // Needed for clock
9 #include <math.h> // Maths functions (sin, cos)
10 /**
11  * Variables
12  */
13 double previousTime, eyeTheta, eyePhi, eyeRho;
14 float look[3];
15 int windowWidth, windowHeight, upY;
16
17 double scale = 1.0;
18
19
20 /**
21  * Initialise the graphics
22  */
23 void Graphics_Init(void) 
24 {
25     
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);
30         glLoadIdentity();
31
32         /*init lighting */
33
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);
40
41         glColorMaterial(GL_FRONT,GL_DIFFUSE);                // Set Color Capability
42     
43         glEnable(GL_LIGHTING);
44         glEnable(GL_LIGHT0);
45         glEnable(GL_DEPTH_TEST);
46     
47         glEnable(GL_COLOR_MATERIAL);                   // Enable color
48
49         
50         windowWidth = WIDTH;
51         windowHeight = HEIGHT;
52         previousTime = clock();
53         eyeTheta = 0;
54         eyePhi = 0.5 * M_PI;
55         eyeRho = RHO;
56         upY = 1;
57         look[0] = 0;
58         look[1] = 0;
59         look[2] = 0;
60         gluPerspective(VIEW_ANGLE, (double)WIDTH/(double)HEIGHT, WORLD_NEAR, WORLD_FAR);  
61 }
62
63 /**
64  * This function redraws the screen after the positions of particles 
65  * have been updated
66  */
67 void Graphics_Display(System * s) 
68 {
69         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
70         glMatrixMode(GL_MODELVIEW);
71         glLoadIdentity();
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);
74
75         for (int i = 0; i < s->N; ++i) 
76         {
77                 Body * b = s->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
83         }
84         glFlush();
85 }
86
87 /**
88  * This function is to manipulate with the image
89  */
90 void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY) 
91 {
92         if (theKey == 'x' || theKey == 'X') 
93         {
94                 exit(EXIT_SUCCESS);
95         }
96
97                 if (theKey == 'i' || theKey == 'I') {
98                         eyePhi -= M_PI / 20;
99                 if (eyePhi == 0)
100                                 eyePhi = 2 * M_PI;
101                 } else if (theKey == 'm' || theKey == 'I') {
102                         eyePhi += M_PI / 20;
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 == ',') {
108                         eyeRho += 0.5;
109                 } else if (theKey == '.' || theKey == 'I') {
110                         eyeRho -= 0.5;
111                 } else if (theKey == 'w' || theKey == 'W') {
112                         look[1] += 0.5;
113                 } else if (theKey == 'z' || theKey == 'Z') {
114                         look[1] -= 0.5;
115                 } else if (theKey == 'a' || theKey == 'A') {
116                         look[0] -= 0.5;
117                 } else if (theKey == 's' || theKey == 'S') {
118                         look[0] += 0.5;
119                 } else if (theKey == '+') {
120                         scale *= 1.1;
121                 } else if (theKey == '-') {
122                         scale *= 0.9;
123                 }
124                 if (sin(eyePhi) > 0) upY = 1;
125                 else upY = 1;
126 }
127
128 /**
129  * Resize the view window
130  */
131 void Graphics_Reshape(int width, int height) 
132 {
133         double displayRatio = 1.0 * width / height;
134         windowWidth = width;
135         windowHeight = height;
136         glMatrixMode(GL_PROJECTION);
137         glLoadIdentity();
138         gluPerspective(VIEW_ANGLE, displayRatio, WORLD_NEAR, WORLD_FAR);
139         glViewport(0, 0, windowWidth, windowHeight);
140 }
141
142
143 /**
144  * This function is called repeatedly by graphics library. You can consider 
145  * it as main loop in the program.
146  */
147 void Graphics_Animate(void) 
148 {
149
150         System_Compute(&universe); //Compute and update new positions for the time step
151         Graphics_Display(&universe); // Display needs to be called to redraw the screen
152 }

UCC git Repository :: git.ucc.asn.au