Camera movement

Rotation around the side-to-side axis is the '_pitch' (y-direction in game), rotation around the vertical axis is called '_yaw' (x-direction in game). Using these values and some angular maths the camera is updated every frame to get smooth movement.


void Camera::UpdateCameraVectors()
{
 _front.x = cos(radians(_yaw)) * cos(radians(_pitch));
 _front.y = sin(radians(_pitch));
 _front.z = sin(radians(_yaw)) * cos(radians(_pitch));
 _front = normalize(_front);
 _right = normalize( cross(_front, _worldUp));
 _up = normalize( cross(_right, _front));
}

Window creation

Function to create the game window. Includes various 'gl' and 'glfw' functions to se the paramaters and various elements of the game window.


void Game::InitWindow(const char* title, GLboolean resizable)
{
 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, GLMAJORVERSION);
 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, GLMINORVERSION);
 glfwWindowHint(GLFW_RESIZABLE, resizable);
 GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, title, NULL, NULL);
 glfwGetFramebufferSize(window, &_frameBufferWidth, &_frameBufferHeight);
 glViewport(0, 0, _frameBufferWidth, _frameBufferHeight);
 glfwMakeContextCurrent(window);
 _window = window;
 if (_window == nullptr)
 {
  cout << "Window could not initialise" << endl;
  glfwTerminate();
 }
}

Shader sending

Using a function built in with 'gl', the user can send information to a shader file (.glsl) which can then be manipulated to produce a realisitc light effect using various equations. The equations use these values as well as other values passed in by the material - ambient, diffuse and specular. The final value calculated is then outputted and applied to the various scene objects.


void Light::SendToShader(Shader& shader)
{
 shader.SetVector3f(_position, "light._position");
 shader.Set1f(_intensity, "light._intensity");
 shader.SetVector3f(_colour, "light._colour");
}
				

Texture loading

This function loads in a texture from a file using a 'SOIL' function. The texture paramaters are then set with a series of 'gl' functions. Next the mipmap is generated, the texture is binded and then the image data is cleared ready for the next texture to be loaded in.


void Texture::LoadFromFile(const char* path)
{
 if (_texture)
 {
  glDeleteTextures(1, &_texture);
 }
 unsigned char* image = SOIL_load_image(path, &_width, &_height, NULL, SOIL_LOAD_RGBA);
 glGenTextures(1, &_texture);
 glBindTexture(_type, _texture);
 glTexParameteri(_type, GL_TEXTURE_WRAP_S, GL_REPEAT);
 glTexParameteri(_type, GL_TEXTURE_WRAP_T, GL_REPEAT);
 glTexParameteri(_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
 glTexParameteri(_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 if (image)
 {
  glTexImage2D(_type, 0, GL_RGBA, _width, _height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
  glGenerateMipmap(GL_TEXTURE_2D);
 }
 else
 {
  cout << "Texture error could not load image: " << path << endl;
 }
 glActiveTexture(0);
 glBindTexture(_type, 0);
 SOIL_free_image_data(image);
}

Feedback

As it is a University assigment I recieved a mark for hitting certain criteria. Here is a breakdown of the marks;

3D Object from File

5/5

Read in at least one scene object from file.

Multiple Scene Objects

3/3

Read in multiple different scene objects from file and store in an appropriate data structure.

Rendering Scene Objects

5/5

Render correctly with appropriate face winding, back face culling and depth testing enabled.

HUD or Text Display

3/3

Render some text to the screen, either for a menu system or in game data.

Camera Movement

3/3

Camera Movement in response to user input or an object tracking camera.

Scene Object Movement

3/3

Animation/Movement of scene objects without continuous user input.

Scene Object Manipulation

4/5

Can manipulate scene objects through user input.

Basic Texturing

2/3

At least one textured object. Can just be a cube.

Advanced Texturing

5/5

A flexible texturing implementation on complex objects.

Materials and Lighting

5/6

Implementation of Materials and Lighting on at least one scene object.

Use standard file formats

6/6

Read in and render 3D objects from standard file formats such as *.obj.

Scene Graph

8/8

Implementation and rendering of a hierarchical scene graph.

Complexity

10/10

Complexity and Inventiveness of scene.

Programming Style

10/10

Use of appropriate structures with clean, tidy and maintainable code.

Gameplay

5/5

Additional Gameplay elements.

Report

11/18

Written report on the project.

Video

2/2

A video capture of the game in action.

Total: 91/100