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

Updated UI and added upgrade feature

parent e06974ab
......@@ -37,6 +37,7 @@ set( INCLUDE
include/Utils.hpp
include/Snow.hpp
include/Text.hpp
include/ScoreText.hpp
include/Minimap.hpp
include/IndexBuffer.h
include/Shader.h
......@@ -59,6 +60,7 @@ set( SRCS
src/Character.cpp
src/Snow.cpp
src/Text.cpp
src/ScoreText.cpp
src/Minimap.cpp
src/IndexBuffer.cpp
src/Shader.cpp
......
......@@ -22,6 +22,10 @@ bool keyPressed[30];
int mousePosX, mousePosY;
float moveX, moveY;
int upgradeCoolDown = 100;
int timeSinceUpgrade = 0;
void init()
{
glewInit();
......@@ -46,6 +50,8 @@ void display()
gm->update(counter.fps());
gm->render();
timeSinceUpgrade++;
if(keyPressed[KEY_ID_W]==true) gm->getCam()->moveForward();
if(keyPressed[KEY_ID_A]==true) gm->getCam()->moveLeft();
if(keyPressed[KEY_ID_D]==true) gm->getCam()->moveRight();
......@@ -56,7 +62,14 @@ void display()
if(keyPressed[KEY_ID_J]==true) gm->getCam()->rotateRight();
if(keyPressed[KEY_ID_O]==true) gm->getCharacter()->moveLeft();
if(keyPressed[KEY_ID_P]==true) gm->getCharacter()->moveRight();
if(keyPressed[KEY_ID_U]==true) {
unsigned int currentScore = gm->getScoreText()->getScore();
if (gm->getScoreText()->getScore() > 1000 && timeSinceUpgrade > upgradeCoolDown) {
gm->getCharacter()->upgradeAndHeal();
gm->getScoreText()->setScore(currentScore - 1000);
timeSinceUpgrade = 0;
}
}
glutSwapBuffers();
glutPostRedisplay();
......@@ -106,6 +119,9 @@ void keyDown(unsigned char key, int x, int y)
case 'p':
keyPressed[KEY_ID_P] = true;
break;
case 'u':
keyPressed[KEY_ID_U] = true;
break;
default:
......@@ -147,6 +163,9 @@ void keyUp(unsigned char key, int x, int y)
case 'p':
keyPressed[KEY_ID_P] = false;
break;
case 'u':
keyPressed[KEY_ID_U] = false;
break;
}
}
......
#pragma once
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
//#include <GL/gl.h>
//#include <GL/glu.h>
#include "SceneObject.hpp"
#include "../include/VertexArray.h"
#include "../include/VertexBuffer.h"
#include "../include/VertexBufferLayout.h"
#include "../include/IndexBuffer.h"
#include "../include/Shader.h"
enum characterStates {
normal = 0,
colliding = 1,
};
#define CH_POS_ARRAY_SIZE 6 * 4 * (3 + 3)
#define CH_IND_ARRAY_SIZE 6 * 4
class Character : public SceneObject
{
public:
......@@ -22,10 +30,12 @@ class Character : public SceneObject
float getSize() { return size_; }
int getState() { return state_; }
float getCurrentLife() { return life_; }
float getMaxLife() { return maxLife_; }
void setColor(float r, float g, float b) { color_[0] = r, color_[1] = g, color_[2] = b; }
void setState(int newState) { state_ = newState; }
void reduceLife() { life_ -= 0.3f; }
void upgradeAndHeal();
protected:
void privateInit();
......@@ -47,6 +57,62 @@ class Character : public SceneObject
void renderHealthbar();
private:
const float size_ = 30.0f;
float positions_[CH_POS_ARRAY_SIZE] = {
// Right face
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_, -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_, 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_, 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_, 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_, 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_[CH_IND_ARRAY_SIZE] = {
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15,
16, 17, 18, 19,
20, 21, 22, 23
};
VertexArray vao;
VertexBuffer vbo = VertexBuffer(positions_, CH_POS_ARRAY_SIZE * sizeof(float));
VertexBufferLayout layout;
IndexBuffer ibo = IndexBuffer(indices_, CH_IND_ARRAY_SIZE);
Shader shader = Shader("C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/shaders/Character_vertex.shader",
"C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/shaders/Character_fragment.shader");
std::vector< glm::vec3 > vertexArray_;
std::vector< glm::vec3 > normalArray_;
std::vector< glm::vec3 > sharedNormalArray_;
......@@ -58,15 +124,13 @@ class Character : public SceneObject
unsigned int texture;
glm::vec3 position_;
float size_ = 30.0f;
int state_ = characterStates::normal;
int list_id_;
float speed_ = 0;
float life_ = 1.0f;
float armor_;
float color_[3] = {0.0f, life_, 0.0f};
float maxLife_ = 1.0f;
float color_[3] = {0.0f, 1.0f, 0.0f};
};
......@@ -7,6 +7,7 @@
#include "Character.hpp"
#include "Camera.hpp"
#include "Snow.hpp"
#include "ScoreText.hpp"
#include "Text.hpp"
#include "Minimap.hpp"
#include "Skybox.h"
......@@ -15,9 +16,6 @@
#include <GL/gl.h>
#include <GL/glu.h>
class GameManager : public SceneObject
{
public:
......@@ -26,6 +24,7 @@ class GameManager : public SceneObject
std::shared_ptr<Camera> getCam();
std::shared_ptr<Character> getCharacter();
std::shared_ptr<ScoreText> getScoreText();
protected:
virtual void privateInit();
......@@ -39,14 +38,17 @@ class GameManager : public SceneObject
std::vector<std::shared_ptr<Obstacle>> obstacles_;
std::shared_ptr<Camera> cam_;
std::shared_ptr<Snow> snow_;
std::shared_ptr<Text> scoreText_;
std::shared_ptr<ScoreText> scoreText_;
std::shared_ptr<Text> highscoreText_;
std::shared_ptr<Text> controlsText_;
std::shared_ptr<Text> upgradeText1_;
std::shared_ptr<Text> upgradeText2_;
std::shared_ptr<Minimap> minimap_;
void checkCollisions();
void handleCollision(std::shared_ptr<Obstacle> obs);
void resetCharacterAfterCollision();
int highscore_ = 0;
unsigned int highscore_ = 0;
};
......@@ -10,5 +10,6 @@ const int KEY_ID_H = 6;
const int KEY_ID_J = 7;
const int KEY_ID_O = 8;
const int KEY_ID_P = 9;
const int KEY_ID_U = 10;
const int MOUSE_LEFT_BUTTON_DOWN = 20;
......@@ -101,6 +101,6 @@ private:
float scaling;
glm::vec3 position_;
float speed_ = -3.0f;
float speed_ = -6.0f;
};
......@@ -54,8 +54,8 @@ private:
VertexBuffer vbo = VertexBuffer(positions_, LS_POS_ARRAY_SIZE * sizeof(float));
VertexBufferLayout layout;
IndexBuffer ibo = IndexBuffer(indices_, LS_IND_ARRAY_SIZE);
Shader shader = Shader("C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/shaders/Vertex.shader",
"C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/shaders/Fragment.shader");
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;
float scaling;
......
#pragma once
#include <windows.h>
//#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <string>
#include "SceneObject.hpp"
class ScoreText : public SceneObject
{
public:
ScoreText(std::string string, float x, float y, float z, bool constant = false, unsigned int score = 0);
~ScoreText();
unsigned int getScore() { return score_; }
void setScore(unsigned int newScore) { score_ = newScore; }
protected:
void privateInit();
void privateRender();
void privateUpdate();
private:
float pos_x;
float pos_y;
float pos_z;
std::string string_;
unsigned int stringLength_;
bool constant_;
unsigned int score_ = 0;
};
......@@ -24,6 +24,7 @@ public:
// Set uniforms
void SetUniform1i(const std::string& name, int value);
void SetUniform3f(const std::string& name, float v0, float v1, float v2);
void SetUniform4f(const std::string& name, float v0, float v1, float v2, float v3);
void SetUniformMat4f(const std::string& name, const glm::mat4& matrix);
......
......@@ -10,13 +10,9 @@
class Text : public SceneObject
{
public:
Text(std::string string, float x, float y, float z, bool constant = false, unsigned int score = 0);
Text(std::string string, float x, float y, float z);
~Text();
unsigned int getScore() { return score_; }
void setScore(unsigned int newScore) { score_ = newScore; }
protected:
void privateInit();
void privateRender();
......@@ -28,9 +24,6 @@ class Text : public SceneObject
float pos_y;
float pos_z;
std::string string_;
unsigned int stringLength_;
bool constant_;
unsigned int score_ = 0;
};
5125
\ No newline at end of file
11994
\ No newline at end of file
// FRAGMENT SHADER
#shader fragment
#version 330 core
layout(location = 0) out vec4 color;
in vec3 v_Normal;
//in vec3 v_LightPosition;
in vec3 v_FragmentPosition;
uniform vec4 u_Color;
//uniform sampler2D u_Texture;
uniform sampler2D u_DiffuseMaterial;
uniform vec3 u_LightPosition;
uniform vec3 u_CameraPosition;
void main() {
// vec4 texColor = texture(u_Texture, v_TexCoord);
// color = texColor;
vec3 norm = normalize(v_Normal);
vec3 lightDir = normalize(u_LightPosition - v_FragmentPosition);
// Ambient light
vec3 ambientLight = vec3(0.1f, 0.1f, 0.1f);
vec3 ambient = ambientLight * u_Color.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 * u_Color.rgb;
// Specular light
vec3 viewDir = normalize(u_CameraPosition - v_FragmentPosition);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 16.0f);
vec3 material_specular = vec3(0.5f,0.5f,0.5f);
vec3 light_specular = vec3(1.0f,1.0f,1.0f);
vec3 specular = light_specular * (spec * material_specular);
vec3 result = ambient + diffuse + specular;
color = mix(u_Color, vec4(result, 1.0f), 0.9);
};
// VERTEX SHADER
#shader vertex
#version 330 core
layout(location = 0) in vec4 position;
layout(location = 1) in vec3 normal;
uniform mat4 u_ProjectionMatrix;
uniform mat4 u_ViewMatrix;
uniform mat4 u_ModelMatrix;
out vec3 v_Normal;
//out vec3 v_LightPositon;
out vec3 v_FragmentPosition;
void main() {
gl_Position = u_ProjectionMatrix * u_ViewMatrix * u_ModelMatrix * position;
v_Normal = mat3(transpose(inverse(u_ModelMatrix))) * normal;
// v_LightPositon = vec3(u_ViewMatrix * vec4(10.0f, 20000.0f, 2.0f, 1.0f));
v_FragmentPosition = vec3(u_ModelMatrix * position);
};
......@@ -6,37 +6,40 @@ layout(location = 0) out vec4 color;
in vec2 v_TexCoord;
in vec3 v_Normal;
in vec3 v_LightPosition;
//in vec3 v_LightPosition;
in vec3 v_FragmentPosition;
//uniform vec4 u_Color;
uniform sampler2D u_Texture;
uniform sampler2D u_DiffuseMaterial;
uniform vec3 u_LightPosition;
uniform vec3 u_CameraPosition;
void main() {
vec4 texColor = texture(u_Texture, v_TexCoord);
//color = texColor;
vec3 norm = normalize(v_Normal);
vec3 lightDir = normalize(u_LightPosition - v_FragmentPosition);
// Ambient light
vec3 ambientLightPos = vec3(0.2f, 0.2f, 0.2f);
vec3 ambient = ambientLightPos * texture(u_DiffuseMaterial, v_TexCoord).rgb;
vec3 ambientLight = vec3(0.1f, 0.1f, 0.1f);
vec3 ambient = ambientLight * texture(u_DiffuseMaterial, v_TexCoord).rgb;
// Diffuse light
vec3 norm = normalize(v_Normal);
vec3 lightDir = normalize(v_LightPosition - v_FragmentPosition);
float diff = max(dot(norm, lightDir), 0.0);
vec3 light_diffuse = vec3(0.5f,0.5f,0.5f);
vec3 light_diffuse = vec3(0.8f,0.8f,0.8f);
vec3 diffuse = light_diffuse * diff * texture(u_DiffuseMaterial, v_TexCoord).rgb;
// Specular light
vec3 viewDir = normalize( - v_FragmentPosition);
vec3 viewDir = normalize(u_CameraPosition - v_FragmentPosition);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 64.0f);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 16.0f);
vec3 material_specular = vec3(0.5f,0.5f,0.5f);
vec3 light_specular = vec3(1.0f,1.0f,1.0f);
vec3 specular = light_specular * (spec * material_specular);
vec3 result = ambient + diffuse + specular;
color = mix(texColor, vec4(result, 1.0f), 0.1);
color = mix(texColor, vec4(result, 1.0f), 0.9);
};
......@@ -9,18 +9,18 @@ layout(location = 2) in vec2 texCoord;
uniform mat4 u_ProjectionMatrix;
uniform mat4 u_ViewMatrix;
uniform mat4 u_ModelMatrix;
//uniform vec4 u_LightPosition;
out vec2 v_TexCoord;
out vec3 v_Normal;
out vec3 v_LightPositon;
//out vec3 v_LightPositon;
out vec3 v_FragmentPosition;
void main() {
gl_Position = u_ProjectionMatrix * u_ViewMatrix * u_ModelMatrix * position;
v_Normal = normal;
v_LightPositon = vec3(u_ViewMatrix * vec4(1.0f, 1.0f, 2.0f, 1.0f));
v_FragmentPosition = vec3(u_ViewMatrix * u_ModelMatrix * position);
v_Normal = mat3(transpose(inverse(u_ModelMatrix))) * normal;
// v_LightPositon = vec3(u_ViewMatrix * vec4(10.0f, 20000.0f, 2.0f, 1.0f));
v_FragmentPosition = vec3(u_ModelMatrix * position);
v_TexCoord = texCoord;
};
......@@ -24,7 +24,45 @@ Character::~Character()
void Character::privateInit()
{
setUpVertices();
// Shader implemetation
/* VERTEX BUFFER */
// Is set up in initialization of object attribute
/* LAYOUT */
// 3 floats for world position
layout.Push<float>(3);
// 3 floats for normals
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();
shader.SetUniform4f("u_Color", color_[0], color_[1], color_[2], 1.0f);
shader.SetUniform3f("u_LightPosition", -200.0f, 500.0f, -200.0f);
/* TEXTURE */
// texture.BindCubemap(textureSlot);
// shader.SetUniform1i("u_Texture", textureSlot);
/* UNBINDING */
// texture.UnbindCubemap();
vao.Unbind();
vbo.Unbind();
ibo.Unbind();
shader.Unbind();
/***************************************************************************/
// Fixed function pipeline implemetation
// setUpVertices();
// setUpNormals();
......@@ -36,10 +74,10 @@ void Character::privateInit()
// setUpHealthbar();
GLenum err = glGetError();
if (err != GL_NO_ERROR)
std::cout<< "OpenGL error: " << gluErrorString(err) << std::endl;
std::cout.flush();
// GLenum err = glGetError();
// if (err != GL_NO_ERROR)
// std::cout<< "OpenGL error: " << gluErrorString(err) << std::endl;
// std::cout.flush();
// char *buff = (char*)glGetString(GL_VERSION);
// std::cout<< buff << std::endl;
......@@ -55,6 +93,29 @@ void Character::privateInit()
void Character::privateRender()
{
// Shader implemetation
shader.Bind();
vao.Bind();
ibo.Bind();
// texture.BindCubemap(textureSlot);
shader.SetUniformMat4f("u_ViewMatrix", viewMatrix_);
shader.SetUniformMat4f("u_ProjectionMatrix", projMatrix_);
shader.SetUniformMat4f("u_ModelMatrix", matrix_);
shader.SetUniform4f("u_Color", color_[0], color_[1], color_[2], 1.0f);
GLCall(glDrawElements(GL_QUADS, ibo.GetCount(), GL_UNSIGNED_INT, nullptr));
// texture.UnbindCubemap();
ibo.Unbind();
vao.Unbind();
shader.Unbind();
/***************************************************************************/
// Fixed function pipeline implemetation
// Enable stuff
// glEnable(GL_LIGHTING);
// glEnable(GL_LIGHT0);
......@@ -62,7 +123,7 @@ void Character::privateRender()
// glEnable(GL_COLOR_MATERIAL);
// Draw cube
renderCube();
// renderCube();
// Draw normal lines of cube
// renderNormals();
......@@ -77,10 +138,10 @@ void Character::privateRender()
// glDisable(GL_COLOR_MATERIAL);
// glDisable(GL_FOG);
GLenum err = glGetError();
if (err != GL_NO_ERROR)
std::cout<< "OpenGL error: " << gluErrorString(err) << std::endl;
std::cout.flush();
// GLenum err = glGetError();
// if (err != GL_NO_ERROR)
// std::cout<< "OpenGL error: " << gluErrorString(err) << std::endl;
// std::cout.flush();
}
......@@ -331,9 +392,6 @@ void Character::setUpTextures()
void Character::setUpHealthbar()
{
life_ = 100.0f;
armor_ = 50.0f;
setUpHBVertices();
setUpHBTextures();
}
......@@ -405,3 +463,18 @@ void Character::moveRight()
}
}
void Character::upgradeAndHeal() {
maxLife_ += 0.5f;
life_ = std::min(life_ + maxLife_ / 3, maxLife_);
if (life_ / maxLife_ > 0.5f) {
setColor(0.0f, 1.0f, 0.0f);
}
else if (life_ / maxLife_ > 0.25f) {
setColor(1.0f, 1.0f, 0.0f);
}