Parallel Programming - Trivial
[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                 //printf("Leave graphics loop\n");
114                 glutLeaveMainLoop();    
115                 return;
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         if (options.draw_graphics == false)
125                 return;
126         
127
128         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
129         glMatrixMode(GL_MODELVIEW);
130         glLoadIdentity();
131         gluLookAt(eyeRho * sin(eyePhi) * sin(eyeTheta), eyeRho * cos(eyePhi),
132                 eyeRho * sin(eyePhi) * cos(eyeTheta),look[0], look[1], look[2], 0, upY, 0);
133
134         for (int i = 0; i < universe.N; ++i) 
135         {
136                 Body * b = universe.body+i;
137                 glColor3f(0.0f, b->mass/1e11*100, 0.0f);
138                 glPushMatrix(); // to save the current matrix
139                 glTranslated(scale*b->x[0], scale*b->x[1], scale*b->x[2]);
140                 glutSolidSphere (BALL_SIZE, 10, 10);
141                 glPopMatrix(); // restore the previous matrix
142         }
143         glFlush();
144 }
145
146 /**
147  * @function Graphics_Keyboard
148  * @purpose This function is to manipulate with the image
149  * @param theKey key pressed
150  * @param mouseX, mouseY position of the mouse in the window
151  */
152 void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY) 
153 {
154         if (theKey == 'x' || theKey == 'X') 
155         {
156                 QuitProgram(false);
157                 return;
158         }
159
160                 if (theKey == 'i' || theKey == 'I') {
161                         eyePhi -= M_PI / 20;
162                 if (eyePhi == 0)
163                                 eyePhi = 2 * M_PI;
164                 } else if (theKey == 'm' || theKey == 'I') {
165                         eyePhi += M_PI / 20;
166                 } else if (theKey == 'j' || theKey == 'J') {
167                         eyeTheta -= M_PI / 20;
168                 } else if (theKey == 'k' || theKey == 'K') {
169                         eyeTheta += M_PI / 20;
170                 } else if (theKey == ',') {
171                         eyeRho += 0.5;
172                 } else if (theKey == '.' || theKey == 'I') {
173                         eyeRho -= 0.5;
174                 } else if (theKey == 'w' || theKey == 'W') {
175                         look[1] += 0.5;
176                 } else if (theKey == 'z' || theKey == 'Z') {
177                         look[1] -= 0.5;
178                 } else if (theKey == 'a' || theKey == 'A') {
179                         look[0] -= 0.5;
180                 } else if (theKey == 's' || theKey == 'S') {
181                         look[0] += 0.5;
182                 } else if (theKey == '+') {
183                         scale *= 1.1;
184                 } else if (theKey == '-') {
185                         scale *= 0.9;
186                 }
187                 if (sin(eyePhi) > 0) upY = 1;
188                 else upY = 1;
189 }
190
191 /**
192  * @function Graphics_Reshape
193  * @purpose Resize the view window
194  * @param width, height - New size of the window
195  */
196 void Graphics_Reshape(int width, int height) 
197 {
198         double displayRatio = 1.0 * width / height;
199         windowWidth = width;
200         windowHeight = height;
201         glMatrixMode(GL_PROJECTION);
202         glLoadIdentity();
203         gluPerspective(VIEW_ANGLE, displayRatio, WORLD_NEAR, WORLD_FAR);
204         glViewport(0, 0, windowWidth, windowHeight);
205 }
206
207
208

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