Commit edb8bf1a authored by Håkon Berg Borhaug's avatar Håkon Berg Borhaug
Browse files

Final(?) product

parent 5f0bcaee
......@@ -38,7 +38,6 @@ set( INCLUDE
include/Snow.hpp
include/Text.hpp
include/ScoreText.hpp
include/Minimap.hpp
include/IndexBuffer.h
include/Shader.h
include/VertexArray.h
......@@ -48,6 +47,7 @@ set( INCLUDE
include/Projection.h
include/Skybox.h
include/Obstacle.h
include/Billboard.h
include/GLErrorHandler.h
include/stb_image.h
......@@ -61,7 +61,6 @@ set( SRCS
src/Snow.cpp
src/Text.cpp
src/ScoreText.cpp
src/Minimap.cpp
src/IndexBuffer.cpp
src/Shader.cpp
src/VertexArray.cpp
......@@ -69,6 +68,7 @@ set( SRCS
src/Texture.cpp
src/Skybox.cpp
src/Obstacle.cpp
src/Billboard.cpp
src/GLErrorHandler.cpp
src/stb_image.cpp
......
......@@ -25,31 +25,132 @@ float moveX, moveY;
int upgradeCoolDown = 100;
int timeSinceUpgrade = 0;
/* ------- Minimap -------- */
//float vertices[4 * 3 * 2] = {
// -1.0f, 1.0f, 0.0f, 1.0f,
// -1.0f, 0.5f, 0.0f, 0.0f,
// -0.5f, 0.5f, 1.0f, 0.0f,
// -1.0f, 1.0f, 0.0f, 1.0f,
// -0.5f, 0.5f, 1.0f, 0.0f,
// -0.5f, 1.0f, 1.0f, 1.0f
//};
//unsigned int indices[6] = {
// 0, 1, 2,
// 3, 4, 5
//};
//unsigned int fbo;
//unsigned int rbo;
//unsigned int texture;
//std::shared_ptr<VertexBuffer> vbo;
//std::shared_ptr<VertexBufferLayout> layout;
//std::shared_ptr<VertexArray> vao;
//std::shared_ptr<IndexBuffer> ibo;
//std::shared_ptr<Shader> shader;
/* ----------------------- */
void init()
{
glewInit();
glClearColor(1.0, 1.0, 1.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
GLCall(glClearColor(1.0, 1.0, 1.0, 0.0));
counter.start();
/* ------- Minimap -------- */
// vbo.reset(new VertexBuffer(vertices, 4 * 3 * 2 * sizeof(float)));
// layout.reset(new VertexBufferLayout());
// vao.reset(new VertexArray());
// ibo.reset(new IndexBuffer(indices, 6));
// shader.reset(new Shader("C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/shaders/Minimap_vertex.shader",
// "C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/shaders/Minimap_fragment.shader"));
/* ----------------------- */
gm.reset(new GameManager());
gm->init();
/* ------- Minimap -------- */
// GLCall(glGenFramebuffers(1, &fbo));
// GLCall(glBindFramebuffer(GL_FRAMEBUFFER, fbo));
// // Create a texture for the framebuffer
// GLCall(glGenTextures(1, &texture));
// GLCall(glBindTexture(GL_TEXTURE_2D, texture));
// GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 960, 540, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL));
// GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
// GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
// // Attach to frambuffer
// GLCall(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0));
// GLCall(glBindTexture(GL_TEXTURE_2D, 0));
// // Renderbuffer
// GLCall(glGenRenderbuffers(1, &rbo));
// GLCall(glBindRenderbuffer(GL_RENDERBUFFER, rbo));
// GLCall(glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 960, 540));
// glBindRenderbuffer(GL_RENDERBUFFER, 0);
// GLCall(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo));
// if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
// std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << std::endl;
// glBindFramebuffer(GL_FRAMEBUFFER, 0);
// layout->Push<float>(2);
// layout->Push<float>(2);
// vao->AddBuffer(*vbo, *layout);
// shader->Bind();
// shader->SetUniform1i("u_Texture", 0);
// vao->Unbind();
// vbo->Unbind();
// ibo->Unbind();
// shader->Unbind();
/* ----------------------- */
GLCall(glEnable(GL_BLEND));
GLCall(glShadeModel(GL_SMOOTH));
GLCall(glEnable(GL_DEPTH_TEST));
GLCall(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
for(int i=0; i<30; i++)
keyPressed[i]=false;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Render screen
GLCall(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
gm->update(counter.fps());
gm->render();
/* ------- Minimap -------- */
// // Render fbo to texture
// GLCall(glBindFramebuffer(GL_FRAMEBUFFER, fbo)); //fbo stuff
// GLCall(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
// gm->render();
// GLCall(glBindFramebuffer(GL_FRAMEBUFFER, 0));
// shader->Bind();
// vao->Bind();
// ibo->Bind();
// GLCall(glBindTexture(GL_TEXTURE_2D, texture));
// GLCall(glDrawElements(GL_TRIANGLES, ibo->GetCount(), GL_UNSIGNED_INT, nullptr));
// GLCall(glBindTexture(GL_TEXTURE_2D, 0));
// ibo->Unbind();
// vao->Unbind();
// shader->Unbind();
/* ----------------------- */
timeSinceUpgrade++;
if(keyPressed[KEY_ID_W]==true) gm->getCam()->moveForward();
......@@ -82,7 +183,6 @@ void keyDown(unsigned char key, int x, int y)
case 'q':
case 27:
glutDestroyWindow(window);
exit(0);
#ifndef _WIN32
// Must use this with regular glut, since it never returns control to main().
exit(0);
......
#pragma once
#include "../include/VertexArray.h"
#include "../include/VertexBuffer.h"
#include "../include/VertexBufferLayout.h"
#include "../include/IndexBuffer.h"
#include "../include/Shader.h"
#include "../include/Texture.h"
#include <windows.h>
#include <GL/freeglut.h>
#include <GL/glu.h>
#include "SceneObject.hpp"
#include "../../glm-master/glm/glm.hpp"
// 4 points with 2D position and 2D texture position
#define BB_POS_ARRAY_SIZE 4 * (3+2) //(2 + 2)
#define BB_IND_ARRAY_SIZE 4
class Billboard : public SceneObject
{
public:
Billboard();
~Billboard();
void setState(bool toDraw) { draw_ = toDraw; }
protected:
virtual void privateInit();
virtual void privateRender();
virtual void privateUpdate();
private:
// const float size_ = 50.0f;
float positions_[BB_POS_ARRAY_SIZE] = {
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
unsigned int indices_[BB_IND_ARRAY_SIZE] = {
0, 1, 2, 3,
};
VertexArray vao;
VertexBuffer vbo = VertexBuffer(positions_, BB_POS_ARRAY_SIZE * sizeof(float));
VertexBufferLayout layout;
IndexBuffer ibo = IndexBuffer(indices_, BB_IND_ARRAY_SIZE);
Shader shader = Shader("C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/shaders/Billboard_vertex.shader",
"C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/shaders/Billboard_fragment.shader");
Texture texture = Texture("C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/textures/gameover.jpg");
unsigned int textureSlot = 0;
bool draw_ = false;
};
......@@ -9,9 +9,9 @@
#include "Snow.hpp"
#include "ScoreText.hpp"
#include "Text.hpp"
#include "Minimap.hpp"
#include "Skybox.h"
#include "Obstacle.h"
#include "Billboard.h"
#include <GL/gl.h>
#include <GL/glu.h>
......@@ -43,7 +43,7 @@ class GameManager : public SceneObject
std::shared_ptr<Text> controlsText_;
std::shared_ptr<Text> upgradeText1_;
std::shared_ptr<Text> upgradeText2_;
std::shared_ptr<Minimap> minimap_;
std::shared_ptr<Billboard> gameoverBillboard_;
void checkCollisions();
void handleCollision(std::shared_ptr<Obstacle> obs);
......
#pragma once
#include <windows.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <iostream>
#include "SceneObject.hpp"
class Minimap : public SceneObject {
public:
Minimap();
~Minimap();
protected:
void privateInit();
void privateRender();
void privateUpdate();
void CheckFrameBufferStatus();
private:
unsigned int g_fbo; // frame buffer object
unsigned int g_pbo; // pixel buffer object
unsigned int g_texture; // texture object
int g_iTexWidth = 512; // texture width
int g_iTexHeight = 512; // texture height
};
......@@ -15,7 +15,7 @@
#include "../../glm-master/glm/glm.hpp"
// 4 points with 3D position and 2D texture position
#define OB_POS_ARRAY_SIZE 6 * 4 * 3
#define OB_POS_ARRAY_SIZE 6 * 4 * (3+3)
#define OB_IND_ARRAY_SIZE 6 * 4
class Obstacle : public SceneObject
......@@ -37,40 +37,40 @@ private:
float positions_[OB_POS_ARRAY_SIZE] = {
// Right face
size_, -size_, size_,
size_, -size_, -size_,
size_, size_, -size_,
size_, size_, size_,
size_, -size_, size_, 1.0f, 0.0f, 0.0f,
size_, -size_, -size_, 1.0f, 0.0f, 0.0f,
size_, size_, -size_, 1.0f, 0.0f, 0.0f,
size_, size_, size_, 1.0f, 0.0f, 0.0f,
// Left face
-size_, -size_, -size_,
-size_, -size_, size_,
-size_, size_, size_,
-size_, size_, -size_,
-size_, -size_, -size_, -1.0f, 0.0f, 0.0f,
-size_, -size_, size_, -1.0f, 0.0f, 0.0f,
-size_, size_, size_, -1.0f, 0.0f, 0.0f,
-size_, size_, -size_, -1.0f, 0.0f, 0.0f,
// Top face
-size_, size_, size_,
size_, size_, size_,
size_, size_, -size_,
-size_, size_, -size_,
-size_, size_, size_, 0.0f, 1.0f, 0.0f,
size_, size_, size_, 0.0f, 1.0f, 0.0f,
size_, size_, -size_, 0.0f, 1.0f, 0.0f,
-size_, size_, -size_, 0.0f, 1.0f, 0.0f,
// Bottom face
-size_, -size_, -size_,
size_, -size_, -size_,
size_, -size_, size_,
-size_, -size_, size_,
-size_, -size_, -size_, 0.0f, -1.0f, 0.0f,
size_, -size_, -size_, 0.0f, -1.0f, 0.0f,
size_, -size_, size_, 0.0f, -1.0f, 0.0f,
-size_, -size_, size_, 0.0f, -1.0f, 0.0f,
// North face
size_, -size_, -size_,
-size_, -size_, -size_,
-size_, size_, -size_,
size_, size_, -size_,
size_, -size_, -size_, 0.0f, 0.0f, -1.0f,
-size_, -size_, -size_, 0.0f, 0.0f, -1.0f,
-size_, size_, -size_, 0.0f, 0.0f, -1.0f,
size_, size_, -size_, 0.0f, 0.0f, -1.0f,
// South Face
-size_, -size_, size_,
size_, -size_, size_,
size_, size_, size_,
-size_, size_, size_,
-size_, -size_, size_, 0.0f, 0.0f, 1.0f,
size_, -size_, size_, 0.0f, 0.0f, 1.0f,
size_, size_, size_, 0.0f, 0.0f, 1.0f,
-size_, size_, size_, 0.0f, 0.0f, 1.0f,
};
unsigned int indices_[OB_IND_ARRAY_SIZE] = {
......@@ -101,6 +101,6 @@ private:
float scaling;
glm::vec3 position_;
float speed_ = -6.0f;
float speed_ = -3.0f;
};
......@@ -39,7 +39,6 @@ private:
std::vector< glm::vec3 > textureArray_;
float positions_[LS_POS_ARRAY_SIZE] = {
// Left plane
-100.0f,0.0f, 100.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
100.0f, 0.0f, 100.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
100.0f, 0.0f, -100.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
......@@ -56,8 +55,10 @@ private:
IndexBuffer ibo = IndexBuffer(indices_, LS_IND_ARRAY_SIZE);
Shader shader = Shader("C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/shaders/Plane_vertex.shader",
"C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/shaders/Plane_fragment.shader");
Texture texture = Texture("C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/textures/road.jpg");
unsigned int textureSlot = 0;
Texture texture1 = Texture("C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/textures/road.jpg");
Texture texture2 = Texture("C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/textures/dirt.jpg");
unsigned int textureSlot1 = 0;
unsigned int textureSlot2 = 1;
float scaling;
};
......@@ -81,6 +81,8 @@ class SceneObject
// Projection matrix
glm::mat4 projMatrix_ = glm::perspective(glm::radians(proj::PoV), proj::aspectRatio, proj::nearClip, proj::farClip);
float globalLightPosition_[3] = {100.0f, 2000.0f, 2000.0f};
private:
// List of all SceneObjects that belong to the current object.
std::vector<std::shared_ptr<SceneObject> > children_;
......
......@@ -16,6 +16,7 @@ class ScoreText : public SceneObject
unsigned int getScore() { return score_; }
void setScore(unsigned int newScore) { score_ = newScore; }
void setIncrement(unsigned int newIncrement) { increment_ = newIncrement; }
protected:
void privateInit();
......@@ -31,6 +32,7 @@ class ScoreText : public SceneObject
unsigned int stringLength_;
bool constant_;
unsigned int score_ = 0;
unsigned int increment_ = 1;
};
11994
\ No newline at end of file
13227
\ No newline at end of file
// FRAGMENT SHADER
#shader fragment
#version 330 core
layout(location = 0) out vec4 color;
in vec2 v_TexCoord;
uniform sampler2D u_Texture;
void main() {
vec4 texColor = texture(u_Texture, v_TexCoord);
color = texColor;
};
// VERTEX SHADER
#shader vertex
#version 330 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 texCoord;
out vec2 v_TexCoord;
void main() {
gl_Position = vec4(position, 1.0f);
v_TexCoord = texCoord;
};
......@@ -10,7 +10,6 @@ in vec3 v_FragmentPosition;
uniform vec4 u_Color;
//uniform sampler2D u_Texture;
uniform sampler2D u_DiffuseMaterial;
uniform vec3 u_LightPosition;
uniform vec3 u_CameraPosition;
......
......@@ -5,11 +5,40 @@
layout(location = 0) out vec4 color;
in vec3 v_TexCoord;
in vec3 v_Normal;
//in vec3 v_LightPosition;
in vec3 v_FragmentPosition;
//uniform vec4 u_Color;
uniform samplerCube u_Texture;
uniform vec3 u_LightPosition;
uniform vec3 u_CameraPosition;
void main() {
vec4 texColor = texture(u_Texture, v_TexCoord);
color = texColor;
// color = texColor;
vec3 norm = normalize(v_Normal);
vec3 lightDir = normalize(u_LightPosition - v_FragmentPosition);
// Ambient light
vec4 ambientLight = vec4(0.1f, 0.1f, 0.1f, 1.0f);
vec4 ambient = ambientLight * texColor;
// Diffuse light
float diff = max(dot(norm, lightDir), 0.0);
vec4 light_diffuse = vec4(0.8f,0.8f,0.8f, 1.0f);
vec4 diffuse = light_diffuse * diff * texColor;
// Specular light
vec3 viewDir = normalize(u_CameraPosition - v_FragmentPosition);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0f);
vec4 material_specular = vec4(0.2f,0.2f,0.2f, 1.0f);
vec4 light_specular = vec4(1.0f,1.0f,1.0f, 1.0f);
vec4 specular = light_specular * (spec * material_specular);
vec4 result = ambient + diffuse + specular;
color = mix(texColor, result, 0.9);
};
......@@ -3,14 +3,23 @@
#version 330 core
layout(location = 0) in vec3 position;
out vec3 v_TexCoord;
layout(location = 1) in vec3 normal;
uniform mat4 u_ProjectionMatrix;
uniform mat4 u_ViewMatrix;
uniform mat4 u_ModelMatrix;
out vec3 v_TexCoord;
out vec3 v_Normal;
//out vec3 v_LightPositon;
out vec3 v_FragmentPosition;
void main() {
gl_Position = u_ProjectionMatrix * u_ViewMatrix * u_ModelMatrix * vec4(position, 1.0);
v_Normal = mat3(transpose(inverse(u_ModelMatrix))) * normal;
v_FragmentPosition = vec3(u_ModelMatrix * vec4(position, 1.0f));
v_TexCoord = position;
};
......@@ -10,13 +10,13 @@ in vec3 v_Normal;
in vec3 v_FragmentPosition;
//uniform vec4 u_Color;
uniform sampler2D u_Texture;
uniform sampler2D u_DiffuseMaterial;
uniform sampler2D u_Texture1;
uniform sampler2D u_Texture2;
uniform vec3 u_LightPosition;
uniform vec3 u_CameraPosition;
void main() {
vec4 texColor = texture(u_Texture, v_TexCoord);
vec4 texColor = mix(texture(u_Texture1, v_TexCoord), texture(u_Texture2, v_TexCoord), 0.5);
//color = texColor;
vec3 norm = normalize(v_Normal);
......@@ -24,12 +24,12 @@ void main() {
// Ambient light
vec3 ambientLight = vec3(0.1f, 0.1f, 0.1f);
vec3 ambient = ambientLight * texture(u_DiffuseMaterial, v_TexCoord).rgb;
vec3 ambient = ambientLight * texColor.rgb;
// Diffuse light
float diff = max(dot(norm, lightDir), 0.0);
vec3 light_diffuse = vec3(0.8f,0.8f,0.8f);
vec3 diffuse = light_diffuse * diff * texture(u_DiffuseMaterial, v_TexCoord).rgb;
vec3 diffuse = light_diffuse * diff * texColor.rgb;
// Specular light
vec3 viewDir = normalize(u_CameraPosition - v_FragmentPosition);
......
#include "../include/Billboard.h"
#include "../../glm-master/glm/glm.hpp"
#include "../../glm-master/glm/gtc/type_ptr.hpp"
Billboard::Billboard()
{
}
Billboard::~Billboard()
{
}
void Billboard::privateInit()
{
/* VERTEX BUFFER */
// Is set up in initialization of object attribute
/* LAYOUT */
// 2 floats for world position
layout.Push<float>(3);
// 2 floats for texture position
layout.Push<float>(2);
/* VERTEX ARRAY */
vao.AddBuffer(vbo, layout);
/* INDEX BUFFER */
// Is set up in initialization of object attribute
/* SHADER */
shader.Bind();
/* TEXTURE */
texture.Bind(textureSlot);
shader.SetUniform1i("u_Texture", textureSlot);
/* UNBINDING */
texture.Unbind();
vao.Unbind();
vbo.Unbind();
ibo.Unbind();
shader.Unbind();
}
void Billboard::privateRender()
{
if (draw_) {
shader.Bind();
vao.Bind();