Parallel Programming - Work on pthreaded version
[matches/honours.git] / course / semester2 / pprog / assignment1 / mthread / 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  * NOTE: The graphics functions are run in the main thread.
6  */
7
8 #include "graphics.h" //Function declarations
9 #include "nbody.h" 
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  * Variables
16  */
17 double previousTime, eyeTheta, eyePhi, eyeRho;
18 float look[3];
19 int windowWidth, windowHeight, upY;
20
21 double scale = 1.0;
22
23
24 /**
25  * Initialise the graphics
26  */
27 void Graphics_Run(int argc, char ** argv) 
28 {
29
30         glutInit(&argc, argv);  
31         glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
32         glutInitWindowSize(WIDTH, HEIGHT);
33         glutInitWindowPosition(POSITION_X, POSITION_Y);
34         glutCreateWindow("N-Body : Multi-Threaded (pthread)");
35         glutDisplayFunc(Graphics_Display);
36         glutIdleFunc(Graphics_Display);
37         glutKeyboardFunc(Graphics_Keyboard);
38         glutReshapeFunc(Graphics_Reshape);
39          
40
41         glClearColor(1.0,1.0,1.0,0.0);
42         glColor3f(0.0f, 0.0f, 0.0f);
43         glPointSize(POINT_SIZE);
44         glMatrixMode(GL_PROJECTION);
45         glLoadIdentity();
46
47         /*init lighting */
48
49         GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
50         GLfloat mat_shininess[] = { 50.0 };
51         GLfloat light_position[] = { 1.0, 1.0, 0.0, 0.0 };
52         glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
53         glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
54         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
55
56         glColorMaterial(GL_FRONT,GL_DIFFUSE);                // Set Color Capability
57     
58         glEnable(GL_LIGHTING);
59         glEnable(GL_LIGHT0);
60         glEnable(GL_DEPTH_TEST);
61     
62         glEnable(GL_COLOR_MATERIAL);                   // Enable color
63
64         
65         windowWidth = WIDTH;
66         windowHeight = HEIGHT;
67         previousTime = clock();
68         eyeTheta = 0;
69         eyePhi = 0.5 * M_PI;
70         eyeRho = RHO;
71         upY = 1;
72         look[0] = 0;
73         look[1] = 0;
74         look[2] = 0;
75         gluPerspective(VIEW_ANGLE, (double)WIDTH/(double)HEIGHT, WORLD_NEAR, WORLD_FAR);  
76
77
78         printf("Use:\n X - exit\n I, J, K, M - rotate\n W, Z, A, S - move to view"
79                 " point\n ./, - zoom in/out\n +/- - scaled zoom in/out\n");
80
81         glutMainLoop();   
82 }
83
84 /**
85  * This function redraws the screen after the positions of particles 
86  * have been updated
87  */
88 void Graphics_Display() 
89 {
90
91         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
92         glMatrixMode(GL_MODELVIEW);
93         glLoadIdentity();
94         gluLookAt(eyeRho * sin(eyePhi) * sin(eyeTheta), eyeRho * cos(eyePhi),
95                 eyeRho * sin(eyePhi) * cos(eyeTheta),look[0], look[1], look[2], 0, upY, 0);
96
97         for (int i = 0; i < universe.N; ++i) 
98         {
99                 Body * b = universe.body+i;
100                 glColor3f(0.0f, b->mass/1e11*100, 0.0f);
101                 glPushMatrix(); // to save the current matrix
102                 glTranslated(scale*b->x[0], scale*b->x[1], scale*b->x[2]);
103                 glutSolidSphere (BALL_SIZE, 10, 10);
104                 glPopMatrix(); // restore the previous matrix
105         }
106         glFlush();
107 }
108
109 /**
110  * This function is to manipulate with the image
111  */
112 void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY) 
113 {
114         if (theKey == 'x' || theKey == 'X') 
115         {
116                 printf("Kill recieved\n");
117                 exit(EXIT_SUCCESS);
118         }
119
120                 if (theKey == 'i' || theKey == 'I') {
121                         eyePhi -= M_PI / 20;
122                 if (eyePhi == 0)
123                                 eyePhi = 2 * M_PI;
124                 } else if (theKey == 'm' || theKey == 'I') {
125                         eyePhi += M_PI / 20;
126                 } else if (theKey == 'j' || theKey == 'J') {
127                         eyeTheta -= M_PI / 20;
128                 } else if (theKey == 'k' || theKey == 'K') {
129                         eyeTheta += M_PI / 20;
130                 } else if (theKey == ',') {
131                         eyeRho += 0.5;
132                 } else if (theKey == '.' || theKey == 'I') {
133                         eyeRho -= 0.5;
134                 } else if (theKey == 'w' || theKey == 'W') {
135                         look[1] += 0.5;
136                 } else if (theKey == 'z' || theKey == 'Z') {
137                         look[1] -= 0.5;
138                 } else if (theKey == 'a' || theKey == 'A') {
139                         look[0] -= 0.5;
140                 } else if (theKey == 's' || theKey == 'S') {
141                         look[0] += 0.5;
142                 } else if (theKey == '+') {
143                         scale *= 1.1;
144                 } else if (theKey == '-') {
145                         scale *= 0.9;
146                 }
147                 if (sin(eyePhi) > 0) upY = 1;
148                 else upY = 1;
149 }
150
151 /**
152  * Resize the view window
153  */
154 void Graphics_Reshape(int width, int height) 
155 {
156         double displayRatio = 1.0 * width / height;
157         windowWidth = width;
158         windowHeight = height;
159         glMatrixMode(GL_PROJECTION);
160         glLoadIdentity();
161         gluPerspective(VIEW_ANGLE, displayRatio, WORLD_NEAR, WORLD_FAR);
162         glViewport(0, 0, windowWidth, windowHeight);
163 }
164
165
166

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