1919
2020float xpos = 512 .0f , ypos = 512 .0f , zpos = 512 .0f , xrot = 758 .0f , yrot = 238 .0f , angle = 0 .0f ;
2121float lastx, lasty;
22- float bounce;
23- float cScale = 1.0 ;
22+
23+ // for fps
2424int time;
2525int frame = 0 ;
2626int timebase = 0 ;
27- int fps = 0 ;
28- bool wireframe = false ;
29- bool refinerydisplay = false ;
27+ int fps = 0 ;
28+
29+ bool wireframe = false ; // show wireframe?
30+ bool refinerydisplay = false ; // display the refinery?
3031
3132SkyBox skyBox;
3233Lava lava;
@@ -44,7 +45,7 @@ void camera (void) {
4445
4546 if (xrot < -90 ) xrot = -90 ;
4647
47- ypos = lava.height + 65 .0f ; // fix to the height of boating on lava viewpoint
48+ ypos = lava.getHeight () + 65 .0f ; // fix to the height of boating on lava viewpoint
4849
4950 // rotate to the rotation variables
5051 glRotatef (xrot, 1 .0f , 0 .0f , 0 .0f );
@@ -99,7 +100,7 @@ void drawText(void) {
99100 strings[0 ] = " Ross Warren's OpenGL Assignement" ;
100101 strings[1 ] = " Controls:" ;
101102 strings[2 ] = " Movement: WASD, Decrease tree complexity: Z, Increase tree complexity: X, Recalculate trees: C, Relocate trees: T, Raise lava: E, Lower lava: F" ;
102- strings[3 ] = " Increase terrain complexity: M, Decrease terrain complexity: N , Toggle wireframe: V" ;
103+ strings[3 ] = " Decrease terrain complexity: N, Increase terrain complexity: M , Toggle wireframe: V" ;
103104
104105 float complexity = (float )terrain.GetComplexity ();
105106 complexity = complexity / 32 .0f * 100 .0f ;
@@ -125,6 +126,7 @@ void drawText(void) {
125126 glMatrixMode (GL_MODELVIEW);
126127}
127128
129+ /* * opengl display method, draws everything */
128130void display (void ) {
129131 glClearColor (0.0 , 0.0 , 0.0 , 1 );
130132 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -160,13 +162,15 @@ void display (void) {
160162 glutSwapBuffers ();
161163}
162164
165+ /* * Initialise OpenGL extensions for vertex buffer objects */
163166void initExtensions (void ){
164167 glGenBuffersARB = (PFNGLGENBUFFERSARBPROC) wglGetProcAddress (" glGenBuffersARB" );
165168 glBindBufferARB = (PFNGLBINDBUFFERARBPROC) wglGetProcAddress (" glBindBufferARB" );
166169 glBufferDataARB = (PFNGLBUFFERDATAARBPROC) wglGetProcAddress (" glBufferDataARB" );
167170 glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC) wglGetProcAddress (" glDeleteBuffersARB" );
168171}
169172
173+ /* * Initialisation method. Sets up OpenGL and calls initialisations of all the components */
170174void Init (void ) {
171175 initExtensions ();
172176 GLfloat pos[3 ] = {1024 .0f , 800 .0f , 0 .0f };
@@ -198,11 +202,12 @@ void Init(void) {
198202 lava.Init ();
199203 building.Init ();
200204 refinery.Init ();
201- trees.SetUpHeights (terrain, lava.height );
205+ trees.SetUpHeights (terrain, lava.getHeight () );
202206 trees.Init ();
203207 printf (" Welcome to Ross's OpenGL Assigment \n " );
204208}
205209
210+ /* * mouse movement to look around */
206211void mouseMovement (int x, int y) {
207212 float diffx = x-lastx;
208213 float diffy = y-lasty;
@@ -212,77 +217,94 @@ void mouseMovement(int x, int y) {
212217 yrot += diffx;
213218}
214219
220+ /* * toggle wireframe mode */
215221void switchWireframe () {
216222 glPolygonMode (GL_FRONT_AND_BACK, wireframe ? GL_FILL : GL_LINE);
217223 wireframe = !wireframe;
218224}
219225
226+ /* * keyboard controlled */
220227void keyboard (unsigned char key, int x, int y) {
221228 float xrotrad, yrotrad;
222229
223230 switch (key) {
231+ // move forwards
224232 case ' w' :
225233 yrotrad = (yrot / 180 .0f * M_PI);
226234 xrotrad = (xrot / 180 .0f * M_PI);
227- xpos += float (sin (yrotrad)) * cScale;
228- zpos -= float (cos (yrotrad)) * cScale;
229- ypos -= float (sin (xrotrad)) ;
230- bounce += 0 .04f ;
235+ xpos += sin (yrotrad);
236+ zpos -= cos (yrotrad);
237+ ypos -= sin (xrotrad);
231238 break ;
239+ // strafe left
232240 case ' a' :
233241 yrotrad = (yrot / 180 .0f * M_PI);
234- xpos -= float ( cos (yrotrad)) * cScale ;
235- zpos -= float ( sin (yrotrad)) * cScale ;
242+ xpos -= cos (yrotrad);
243+ zpos -= sin (yrotrad);
236244 break ;
245+ // move backwards
237246 case ' s' :
238247 yrotrad = (yrot / 180 .0f * M_PI);
239248 xrotrad = (xrot / 180 .0f * M_PI);
240- xpos -= float (sin (yrotrad)) * cScale;
241- zpos += float (cos (yrotrad)) * cScale;
242- ypos += float (sin (xrotrad));
243- bounce += 0 .04f ;
249+ xpos -= sin (yrotrad);
250+ zpos += cos (yrotrad);
251+ ypos += sin (xrotrad);
244252 break ;
253+ // move right
245254 case ' d' :
246255 yrotrad = (yrot / 180 .0f * M_PI);
247- xpos += float ( cos (yrotrad)) * cScale ;
248- zpos += float ( sin (yrotrad)) * cScale ;
256+ xpos += cos (yrotrad);
257+ zpos += sin (yrotrad);
249258 break ;
259+ // increase lava height
250260 case ' e' :
251- lava.height += 1 . 0f ;
261+ lava.increaseHeight () ;
252262 break ;
263+ // decrease lava height
253264 case ' f' :
254- lava.height -= 1 . 0f ;
265+ lava.decreaseHeight () ;
255266 break ;
267+ // decrease tree complexity
256268 case ' z' :
257269 printf (" Decreasing tree complexity \n " );
258270 trees.DecreaseComplexity ();
259271 break ;
272+ // increase tree complexity
260273 case ' x' :
261274 printf (" Increasing tree complexity \n " );
262275 trees.IncreaseComplexity ();
263276 break ;
277+ // regenerate trees
264278 case ' c' :
265279 printf (" Regenerating trees \n " );
266280 trees.Regen ();
267281 break ;
282+ // decrease terrain complexity
268283 case ' n' :
284+ printf (" Decreasing terrain complexity \n " );
269285 terrain.DecreaseComplexity ();
270286 break ;
287+ // increase terrain complexity
271288 case ' m' :
289+ printf (" Increasing terrain complexity \n " );
272290 terrain.IncreaseComplexity ();
273291 break ;
292+ // toggle wireframe mode
274293 case ' v' :
275294 switchWireframe ();
276295 break ;
296+ // toggle displaying of refinery
277297 case ' r' :
278298 refinerydisplay = !refinerydisplay;
279299 break ;
300+ // set up tree heights
280301 case ' t' :
281- trees.SetUpHeights (terrain, lava.height );
302+ trees.SetUpHeights (terrain, lava.getHeight () );
282303 break ;
283304 }
284305}
285306
307+ /* * reshape called when the window is resized */
286308void reshape (int w, int h) {
287309 glViewport (0 , 0 , (GLsizei)w, (GLsizei)h);
288310 glMatrixMode (GL_PROJECTION);
@@ -294,13 +316,13 @@ void reshape(int w, int h) {
294316int main (int argc, char **argv) {
295317 glutInit (&argc, argv);
296318 glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA | GLUT_MULTISAMPLE);
297- glutInitWindowSize (500 , 500 );
298- glutInitWindowPosition (100 , 100 );
319+ glutInitWindowSize (800 , 640 );
320+ glutInitWindowPosition (400 , 100 );
299321 puts (" Show the refinery? This will not run on slower computers. (Y or N)." );
300322 char answer;
301323 std::cin >> answer;
302- refinerydisplay = (answer == ' y' ) ? true : false ;
303- glutCreateWindow (" Ross Warren's OpenGL Assignment" );
324+ refinerydisplay = (answer == ' y' ) ? true : false ;
325+ glutCreateWindow (" Ross Warren's OpenGL Assignment" );
304326 Init ();
305327 glutDisplayFunc (display);
306328 glutIdleFunc (display);
0 commit comments