e3722e9c33745d44a0d38ea64c09d4a0e068a643
[matches/honours.git] / course / semester2 / pprog / assignment1 / single-thread / graphics.c
1 /**
2  * @file graphics.c
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
7  */
8
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <time.h> // Needed for clock
13 #include <math.h> // Maths functions (sin, cos)
14
15 #include "graphics.h" //Function declarations
16 #include "nbody.h" //The simulation
17
18 // --- Variable definitions --- //
19 double previousTime, eyeTheta, eyePhi, eyeRho;
20 float look[3];
21 int windowWidth, windowHeight, upY;
22 double scale = 1.0;
23
24
25 /**
26  * @function Graphics_Run
27  * @purpose Setup and start the graphics engine
28  * @param argc - Number of arguments to main function, passed to glutInit
29  * @param argv - Argument strings for main function, passed to glutInit
30  */
31 void Graphics_Run(int argc, char ** argv) 
32 {
33         if (options.draw_graphics == false)
34         {
35                 while (true)
36                         Graphics_Display();
37                         
38                 return;
39         }       
40
41         glutInit(&argc, argv);  
42         glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
43         glutInitWindowSize(WIDTH, HEIGHT);
44         glutInitWindowPosition(POSITION_X, POSITION_Y);
45
46         //Set window title based on version of program
47         #ifdef SINGLE_THREADED
48                 glutCreateWindow("N-Body : Single-Threaded");
49         #elif defined PTHREADED
50                 glutCreateWindow("N-Body : Multi-Threaded (pthread)");
51         #elif defined OMP_THREADED
52                 glutCreateWindow("N-Body : Multi-Threaded (OpenMP)");   
53         #else
54                 glutCreateWindow("N-Body");
55         #endif 
56         glutDisplayFunc(Graphics_Display);
57         glutIdleFunc(Graphics_Display);
58         glutKeyboardFunc(Graphics_Keyboard);
59         glutReshapeFunc(Graphics_Reshape);
60          
61
62         glClearColor(1.0,1.0,1.0,0.0);
63         glColor3f(0.0f, 0.0f, 0.0f);
64         glPointSize(POINT_SIZE);
65         glMatrixMode(GL_PROJECTION);
66         glLoadIdentity();
67
68         /*init lighting */
69
70         GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
71         GLfloat mat_shininess[] = { 50.0 };
72         GLfloat light_position[] = { 1.0, 1.0, 0.0, 0.0 };
73         glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
74         glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
75         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
76
77         glColorMaterial(GL_FRONT,GL_DIFFUSE);                // Set Color Capability
78     
79         glEnable(GL_LIGHTING);
80         glEnable(GL_LIGHT0);
81         glEnable(GL_DEPTH_TEST);
82     
83         glEnable(GL_COLOR_MATERIAL);                   // Enable color
84
85         
86         windowWidth = WIDTH;
87         windowHeight = HEIGHT;
88         previousTime = clock();
89         eyeTheta = 0;
90         eyePhi = 0.5 * M_PI;
91         eyeRho = RHO;
92         upY = 1;
93         look[0] = 0;
94         look[1] = 0;
95         look[2] = 0;
96         gluPerspective(VIEW_ANGLE, (double)WIDTH/(double)HEIGHT, WORLD_NEAR, WORLD_FAR);  
97
98         glutMainLoop();   
99 }
100
101 /**
102  * @function Graphics_Display
103  * @purpose This function redraws the screen after the positions of particles 
104  *              have been updated.
105  *      It also calls System_Compute, and checks for exit conditions, in the single-threaded version only
106  */
107 void Graphics_Display() 
108 {
109
110
111         if (options.verbosity != 0 && universe.steps % options.verbosity == 1)
112                 DisplayStatistics();
113
114         #ifdef SINGLE_THREADED
115                 System_Compute(&universe);
116
117                 //Check whether the program should quit due to steps being computed, or a timeout
118                 if (options.timeout > 0.0)
119                 {
120                         if ((unsigned)(time(NULL) - options.start_time.tv_sec) >= options.timeout)
121                                 exit(EXIT_SUCCESS);
122                 }
123                 if (options.num_steps > 0 && universe.steps > options.num_steps)
124                         exit(EXIT_SUCCESS);
125
126
127         #endif
128
129
130         //Check whether the runstate has been set to quit the program
131         switch (runstate)
132         {
133                 case RUN:
134                         break;
135                 case QUIT:
136                         exit(EXIT_SUCCESS);
137                         break;
138                 case QUIT_ERROR:
139                         exit(EXIT_FAILURE);
140                         break;
141         }
142
143         if (options.draw_graphics == false)
144                 return;
145         
146
147         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
148         glMatrixMode(GL_MODELVIEW);
149         glLoadIdentity();
150         gluLookAt(eyeRho * sin(eyePhi) * sin(eyeTheta), eyeRho * cos(eyePhi),
151                 eyeRho * sin(eyePhi) * cos(eyeTheta),look[0], look[1], look[2], 0, upY, 0);
152
153         for (int i = 0; i < universe.N; ++i) 
154         {
155                 Body * b = universe.body+i;
156                 glColor3f(0.0f, b->mass/1e11*100, 0.0f);
157                 glPushMatrix(); // to save the current matrix
158                 glTranslated(scale*b->x[0], scale*b->x[1], scale*b->x[2]);
159                 glutSolidSphere (BALL_SIZE, 10, 10);
160                 glPopMatrix(); // restore the previous matrix
161         }
162         glFlush();
163 }
164
165 /**
166  * @function Graphics_Keyboard
167  * @purpose This function is to manipulate with the image
168  * @param theKey key pressed
169  * @param mouseX, mouseY position of the mouse in the window
170  */
171 void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY) 
172 {
173         if (theKey == 'x' || theKey == 'X') 
174         {
175                 exit(EXIT_SUCCESS);
176         }
177
178                 if (theKey == 'i' || theKey == 'I') {
179                         eyePhi -= M_PI / 20;
180                 if (eyePhi == 0)
181                                 eyePhi = 2 * M_PI;
182                 } else if (theKey == 'm' || theKey == 'I') {
183                         eyePhi += M_PI / 20;
184                 } else if (theKey == 'j' || theKey == 'J') {
185                         eyeTheta -= M_PI / 20;
186                 } else if (theKey == 'k' || theKey == 'K') {
187                         eyeTheta += M_PI / 20;
188                 } else if (theKey == ',') {
189                         eyeRho += 0.5;
190                 } else if (theKey == '.' || theKey == 'I') {
191                         eyeRho -= 0.5;
192                 } else if (theKey == 'w' || theKey == 'W') {
193                         look[1] += 0.5;
194                 } else if (theKey == 'z' || theKey == 'Z') {
195                         look[1] -= 0.5;
196                 } else if (theKey == 'a' || theKey == 'A') {
197                         look[0] -= 0.5;
198                 } else if (theKey == 's' || theKey == 'S') {
199                         look[0] += 0.5;
200                 } else if (theKey == '+') {
201                         scale *= 1.1;
202                 } else if (theKey == '-') {
203                         scale *= 0.9;
204                 }
205                 if (sin(eyePhi) > 0) upY = 1;
206                 else upY = 1;
207 }
208
209 /**
210  * @function Graphics_Reshape
211  * @purpose Resize the view window
212  * @param width, height - New size of the window
213  */
214 void Graphics_Reshape(int width, int height) 
215 {
216         double displayRatio = 1.0 * width / height;
217         windowWidth = width;
218         windowHeight = height;
219         glMatrixMode(GL_PROJECTION);
220         glLoadIdentity();
221         gluPerspective(VIEW_ANGLE, displayRatio, WORLD_NEAR, WORLD_FAR);
222         glViewport(0, 0, windowWidth, windowHeight);
223 }
224
225
226

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