Skip to content

Commit f65508f

Browse files
committed
tidied up code and added sample images
1 parent 4c72a56 commit f65508f

13 files changed

Lines changed: 149 additions & 121 deletions

File tree

Building.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ void Building::Init(void) {
3131
}
3232

3333
void Building::Display(void) {
34-
//glPushMatrix();
34+
glPushMatrix();
3535
glBindTexture(GL_TEXTURE_2D, texture); // bind the texture
3636
glCallList(displayList);
37-
//glPopMatrix();
37+
glPopMatrix();
3838
}

Lava.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ Lava::Lava(void) {
1010
Lava::~Lava(void) {
1111
}
1212

13-
14-
/**
15-
* Initialise by loading textures etc.
16-
*/
13+
// Initialise by loading textures etc.
1714
void Lava::Init(void) {
1815
//load textures
1916
RawLoader rawLoader;
@@ -43,11 +40,27 @@ void Lava::Init(void) {
4340
glEndList();
4441
}
4542

43+
// display the lava
4644
void Lava::Display(void) {
4745
glDepthMask(GL_FALSE);
4846
glPushMatrix();
4947
glTranslatef(512.0f, height, 512.0f);
5048
glCallList(displayList);
5149
glPopMatrix();
5250
glDepthMask(GL_TRUE);
51+
}
52+
53+
/** return the lava height */
54+
GLfloat Lava::getHeight(void) {
55+
return height;
56+
}
57+
58+
/** increase the lava height */
59+
void Lava::increaseHeight(void) {
60+
height += 1.0f;
61+
}
62+
63+
/** decrease the lava height */
64+
void Lava::decreaseHeight(void) {
65+
height -= 1.0f;
5366
}

Lava.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ class Lava {
55
private:
66
GLuint lavaTexture;
77
GLuint displayList;
8+
GLfloat height;
89
public:
910
Lava(void);
1011
~Lava(void);
1112

1213
void Init();
1314
void Display();
14-
15-
GLfloat height;
15+
GLfloat getHeight(void);
16+
void increaseHeight();
17+
void decreaseHeight();
1618
};
1719

Main.cpp

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919

2020
float xpos = 512.0f, ypos = 512.0f, zpos = 512.0f, xrot = 758.0f, yrot = 238.0f, angle = 0.0f;
2121
float lastx, lasty;
22-
float bounce;
23-
float cScale = 1.0;
22+
23+
// for fps
2424
int time;
2525
int frame = 0;
2626
int 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

3132
SkyBox skyBox;
3233
Lava 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 */
128130
void 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 */
163166
void 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 */
170174
void 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 */
206211
void 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 */
215221
void switchWireframe() {
216222
glPolygonMode(GL_FRONT_AND_BACK, wireframe ? GL_FILL : GL_LINE);
217223
wireframe = !wireframe;
218224
}
219225

226+
/** keyboard controlled */
220227
void 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 */
286308
void 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) {
294316
int 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);

RawLoader.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
#include "RawLoader.h"
33

44

5-
RawLoader::RawLoader(void)
6-
{
5+
RawLoader::RawLoader(void) {
76
}
87

98

10-
RawLoader::~RawLoader(void)
11-
{
9+
RawLoader::~RawLoader(void) {
1210
}
1311

12+
// load a texture and return its OpengGL mapped integer
1413
GLuint RawLoader::LoadTextureRAW(char * filename, int wrap, int width, int height) {
1514
GLuint texture;
1615
GLubyte * data;
@@ -63,11 +62,7 @@ GLuint RawLoader::LoadTextureRAW(char * filename, int wrap, int width, int heigh
6362
// free buffer
6463
free(data);
6564

66-
std::stringstream ss;
67-
std::string str;
68-
ss << filename << " loaded \n";
69-
str = ss.str();
70-
std::cout << str;
65+
std::cout << filename << " loaded \n";
7166

7267
return texture;
7368
}

Shapes.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
#endif
77

88

9-
Shapes::Shapes(void)
10-
{
9+
Shapes::Shapes(void) {
1110
}
1211

1312

14-
Shapes::~Shapes(void)
15-
{
13+
Shapes::~Shapes(void) {
1614
}
1715

16+
// draw a cube with normals and texture coordinates
1817
void Shapes::cube(void) {
1918
glBegin(GL_QUADS);
2019
// Front Face
@@ -58,14 +57,17 @@ void Shapes::cube(void) {
5857
glEnd();
5958
}
6059

60+
// draws a cuboid, which is just a cube scaled
6161
void Shapes::cuboid(GLfloat width, GLfloat height, GLfloat depth) {
6262
glPushMatrix();
6363
glScalef(width / 2 , height / 2, depth / 2);
6464
cube();
6565
glPopMatrix();
6666
}
6767

68-
/** draw a gear */
68+
/** draw a gear
69+
* Adapted from here http://www.opengl.org/resources/code/samples/glut_examples/mesademos/mesademos.html
70+
*/
6971
void Shapes::gear(GLfloat innerRadius, GLfloat outerRadius, GLfloat width, GLint teeth, GLfloat tooth_depth) {
7072
GLint i; //general purpose incrementer for loops
7173
GLfloat r0, r1, r2, u, v, len, da;
@@ -212,6 +214,7 @@ void Shapes::cylinder(double height, double radiusBase, double radiusTop, int sl
212214
glPopMatrix();
213215
}
214216

217+
/** draw whisk with spindle, part of the refinery */
215218
void Shapes::drawWhisk(int direction, double angle, int smallgear, int whisk, GLUquadricObj* quadric) {
216219
GLdouble whiskangle = direction == 0 ? angle : -angle; // reverse the angle if direction is 0
217220

0 commit comments

Comments
 (0)