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

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