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

Started implementing the use of custom shaders

parent 25b9ca70
......@@ -15,8 +15,8 @@
#include "../../glm-master/glm/glm.hpp"
// 4 points with 3D position and 2D texture position
#define POS_ARRAY_SIZE 4 * (3 + 2)
#define IND_ARRAY_SIZE 4
#define POS_ARRAY_SIZE 4 * 3 //4 * 4 * (3 + 2)
#define IND_ARRAY_SIZE 4 //4 * 4
class Landscape : public SceneObject
{
......@@ -38,11 +38,15 @@ private:
std::vector< glm::vec3 > textureArray_;
float positions_[POS_ARRAY_SIZE] = {
// Left plane
-100.0f, -50.0f, 100.0f, 0.0f, 0.0f,
100.0f, -50.0f, 100.0f, 1.0f, 0.0f,
100.0f, -50.0f, -100.0f, 1.0f, 1.0f,
-100.0f, -50.0f, -100.0f, 0.0f, 1.0f
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 3.0f,
0.5f, 0.5f, 3.0f,
-0.5f, 0.5f, 0.0f
// // Left plane
// -100.0f, -50.0f, 100.0f, 0.0f, 0.0f,
// 100.0f, -50.0f, 100.0f, 1.0f, 0.0f,
// 100.0f, -50.0f, -100.0f, 1.0f, 1.0f,
// -100.0f, -50.0f, -100.0f, 0.0f, 1.0f,
// // Middle plane
// 0.0f, -50.0f, 50.0f,
......@@ -64,7 +68,7 @@ private:
};
unsigned int indices_[IND_ARRAY_SIZE] = {
0, 1, 2, 3,
0, 1, 2, 3
// 4, 5, 6, 7,
// 8, 9, 10, 11,
// 12, 13, 14, 15
......@@ -74,7 +78,8 @@ private:
VertexBuffer vbo = VertexBuffer(positions_, POS_ARRAY_SIZE * sizeof(float));
VertexBufferLayout layout;
IndexBuffer ibo = IndexBuffer(indices_, IND_ARRAY_SIZE);
Shader shader = Shader("../resources/shaders/Vertex.shader", "../resources/shaders/Fragment.shader");
Texture texture = Texture("../resources/color.jpg");
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");
Texture texture = Texture("C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/textures/color.jpg");
};
......@@ -5,19 +5,14 @@
#include "../../glm-master/glm/glm.hpp"
struct ShaderProgramSource {
std::string VertexSource;
std::string FragmentSource;
};
class Shader {
private:
std::string m_VertexShaderFilePath;
std::string m_FragmentShaderFilePath;
unsigned int m_RendererID;
std::unordered_map<std::string, int> m_UniformLocationCache;
std::string m_VertexShaderSource;
std::string m_FragmentShaderSource;
public:
Shader(const std::string& vertexShaderFilepath, const std::string& fragmentShaderFilepath);
......@@ -26,6 +21,7 @@ public:
void Bind() const;
void Unbind() const;
// Set uniforms
void SetUniform1i(const std::string& name, int value);
void SetUniform4f(const std::string& name, float v0, float v1, float v2, float v3);
......
// FRAGMENT SHADER
#shader fragment
#version 330 core
layout(location = 0) out vec4 color;
//in vec2 v_TexCoord;
uniform vec4 u_Color;
//uniform sampler2D u_Texture;
void main() {
//vec4 texColor = texture(u_Texture, v_TexCoord);
color = u_Color;
};
// VERTEX SHADER
#shader vertex
#version 330 core
layout(location = 0) in vec4 position;
//layout(location = 1) in vec2 texCoord;
//out vec2 v_TexCoord;
uniform mat4 u_MVP;
void main() {
gl_Position = u_MVP * position;
// v_TexCoord = texCoord;
};
......@@ -350,14 +350,15 @@ void Character::setUpHBTextures()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
int width, height, nrChannels;
unsigned char *data = stbi_load("../resources/textures/HP.jpg", &width, &height, &nrChannels, 0);
std::string filepath = "C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/textures/HP.jpg";
unsigned char *data = stbi_load(filepath.c_str(), &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
else
{
std::cout << "FAILED TO LOAD TEXTURE: " << stbi_failure_reason() << std::endl;
std::cout << "FAILED TO LOAD TEXTURE FROM " << filepath <<"\nFAILURE REASON: "<< stbi_failure_reason() << std::endl;
}
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(data);
......@@ -368,6 +369,7 @@ void Character::privateUpdate()
auto translation = glm::translate(glm::mat4(1.0f), glm::vec3(0.2f * speed_, 0.0f, 0.0f));
auto rotation = glm::rotate(matrix_, 0.01f, glm::vec3(0.0f,1.0f,0.0f));
matrix_ = translation * rotation;
}
void Character::increaseSpeed()
......
#include "../include/Landscape.hpp"
#include "../../glm-master/glm/glm.hpp"
#include "../../glm-master/glm/gtc/type_ptr.hpp"
Landscape::Landscape()
{
......@@ -10,34 +12,57 @@ Landscape::~Landscape()
void Landscape::privateInit()
{
// 3 floats for position
/* VERTEX BUFFER */
// Is set up in initialization of object attribute
/* LAYOUT */
// 3 floats for world position
layout.Push<float>(3);
// 2 floats for texture position
layout.Push<float>(2);
// // 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", 0.1f, 0.5f, 0.5f, 1.0f);
shader.SetUniform4f("u_Color", 0.0f, 0.0f, 1.0f, 1.0f);
unsigned int textureSlot = 0;
texture.Bind(textureSlot);
shader.SetUniform1i("u_Texture", textureSlot);
/* TEXTURE */
// unsigned int textureSlot = 0;
// texture.Bind(textureSlot);
// shader.SetUniform1i("u_Texture", textureSlot);
/* UNBINDING */
vao.Unbind();
vbo.Unbind();
ibo.Unbind();
shader.Unbind();
}
void Landscape::privateRender()
{
shader.Bind();
vao.Bind();
ibo.Bind();
// shader.SetUniformMat4f("u_MVP", matrix_);
glm::mat4 trans = glm::mat4(1.0f);
// trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0.0f));
// trans = glm::rotate(trans, 45.0f, glm::vec3(0.0f, 0.0f, 1.0f));
matrix_ = matrix_ * trans;
shader.SetUniformMat4f("u_MVP", matrix_);
GLCall(glDrawElements(GL_QUADS, ibo.GetCount(), GL_UNSIGNED_INT, nullptr));
vao.Unbind();
ibo.Unbind();
vao.Unbind();
shader.Unbind();
// // Draw filled polygons
// glColor3f(0.0f, 1.0f, 0.0f);
......
#include "../include/Shader.h"
#include "../include/GLErrorHandler.h"
#include "../../glm-master/glm/glm.hpp"
#include "../../glm-master/glm/gtc/type_ptr.hpp"
#include <iostream>
#include <fstream>
......@@ -9,9 +11,9 @@
Shader::Shader(const std::string& vertexShaderFilepath, const std::string& fragmentShaderFilepath)
: m_VertexShaderFilePath(vertexShaderFilepath), m_FragmentShaderFilePath(fragmentShaderFilepath), m_RendererID(0)
{
const std::string vertexSource = ParseShader(vertexShaderFilepath);
const std::string fragmentSource = ParseShader(fragmentShaderFilepath);
m_RendererID = CreateShader(vertexSource, fragmentSource);
m_VertexShaderSource = ParseShader(vertexShaderFilepath);
m_FragmentShaderSource = ParseShader(fragmentShaderFilepath);
m_RendererID = CreateShader(m_VertexShaderSource, m_FragmentShaderSource);
}
Shader::~Shader()
......@@ -41,7 +43,7 @@ void Shader::SetUniform4f(const std::string& name, float v0, float v1, float v2,
void Shader::SetUniformMat4f(const std::string& name, const glm::mat4& matrix)
{
GLCall(glUniformMatrix4fv(GetUniformLocation(name), 1, GL_FALSE, &matrix[0][0]));
GLCall(glUniformMatrix4fv(GetUniformLocation(name), 1, GL_FALSE, glm::value_ptr(matrix)));
}
int Shader::GetUniformLocation(const std::string& name)
......
......@@ -100,14 +100,15 @@ void Snow::setUpTexture() {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
int width, height, nrChannels;
unsigned char *data = stbi_load("../resources/textures/snowflake.jpg", &width, &height, &nrChannels, 0);
std::string filepath = "C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/textures/snowflake.jpg";
unsigned char *data = stbi_load(filepath.c_str(), &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
else
{
std::cout << "FAILED TO LOAD TEXTURE: " << stbi_failure_reason() << std::endl;
std::cout << "FAILED TO LOAD TEXTURE FROM " << filepath <<"\nFAILURE REASON: "<< stbi_failure_reason() << std::endl;
}
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(data);
......
......@@ -8,6 +8,10 @@ Texture::Texture(const std::string& filepath)
{
stbi_set_flip_vertically_on_load(1);
m_LocalBuffer = stbi_load(filepath.c_str(), &m_Width, &m_Height, &m_BPP, 4);
if (!m_LocalBuffer)
{
std::cout << "FAILED TO LOAD TEXTURE FROM " << filepath <<"\nFAILURE REASON: "<< stbi_failure_reason() << std::endl;
}
GLCall(glGenTextures(1, &m_RendererID));
GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
......@@ -32,7 +36,7 @@ Texture::~Texture()
void Texture::Bind(unsigned int slot) const
{
GLCall(glActiveTexture(GL_TEXTURE0 + slot));
GLCall(glActiveTexture(GL_TEXTURE0 + slot));
GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment