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

Started shader implementation

parent 0a8ebf30
......@@ -43,6 +43,7 @@ set( INCLUDE
include/VertexArray.h
include/VertexBuffer.h
include/VertexBufferLayout.h
include/Texture.h
include/GLErrorHandler.h
include/stb_image.h
......@@ -60,6 +61,7 @@ set( SRCS
src/Shader.cpp
src/VertexArray.cpp
src/VertexBuffer.cpp
src/Texture.cpp
src/GLErrorHandler.cpp
src/stb_image.cpp
......
......@@ -4,6 +4,8 @@
#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>
......@@ -12,8 +14,9 @@
#include "SceneObject.hpp"
#include "../../glm-master/glm/glm.hpp"
#define POS_ARRAY_SIZE 48
#define IND_ARRAY_SIZE 12
// 4 points with 3D position and 2D texture position
#define POS_ARRAY_SIZE 4 * (3 + 2)
#define IND_ARRAY_SIZE 4
class Landscape : public SceneObject
{
......@@ -36,39 +39,42 @@ private:
float positions_[POS_ARRAY_SIZE] = {
// Left plane
-100.0f, -50.0f, 50.0f,
0.0f, -50.0f, 50.0f,
0.0f, -50.0f, -50.0f,
-100.0f, -50.0f, -50.0f,
-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,
100.0f, -50.0f, 50.0f,
100.0f, -50.0f, -50.0f,
0.0f, -50.0f, -50.0f,
// // Middle plane
// 0.0f, -50.0f, 50.0f,
// 100.0f, -50.0f, 50.0f,
// 100.0f, -50.0f, -50.0f,
// 0.0f, -50.0f, -50.0f,
// Right plane
100.0f, -50.0f, 50.0f,
200.0f, -50.0f, 50.0f,
200.0f, -50.0f, -50.0f,
100.0f, -50.0f, -50.0f,
// // Right plane
// 100.0f, -50.0f, 50.0f,
// 200.0f, -50.0f, 50.0f,
// 200.0f, -50.0f, -50.0f,
// 100.0f, -50.0f, -50.0f,
// Background
-1000.0f, -1000.0f, -500.0f,
1000.0f, -1000.0f, -500.0f,
1000.0f, 1000.0f, -500.0f,
-1000.0f, 1000.0f, -500.0f
// // Background
// -1000.0f, -1000.0f, -500.0f,
// 1000.0f, -1000.0f, -500.0f,
// 1000.0f, 1000.0f, -500.0f,
// -1000.0f, 1000.0f, -500.0f
};
unsigned int indices_[IND_ARRAY_SIZE] = {
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11
// 4, 5, 6, 7,
// 8, 9, 10, 11,
// 12, 13, 14, 15
};
VertexArray vao;
VertexBuffer vbo = VertexBuffer(positions_, POS_ARRAY_SIZE * 3 * sizeof(float));
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");
};
......@@ -5,33 +5,35 @@
#include "../../glm-master/glm/glm.hpp"
struct ShaderProgramSource {
std::string VertexSource;
std::string FragmentSource;
std::string VertexSource;
std::string FragmentSource;
};
class Shader {
private:
std::string m_FilePath;
unsigned int m_RendererID;
std::unordered_map<std::string, int> m_UniformLocationCache;
std::string m_VertexShaderFilePath;
std::string m_FragmentShaderFilePath;
unsigned int m_RendererID;
std::unordered_map<std::string, int> m_UniformLocationCache;
public:
Shader(const std::string& filepath);
~Shader();
Shader(const std::string& vertexShaderFilepath, const std::string& fragmentShaderFilepath);
~Shader();
void Bind() const;
void Unbind() const;
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);
void SetUniformMat4f(const std::string& name, const glm::mat4& matrix);
// Set uniforms
void SetUniform1i(const std::string& name, int value);
void SetUniform4f(const std::string& name, float v0, float v1, float v2, float v3);
void SetUniformMat4f(const std::string& name, const glm::mat4& matrix);
private:
int GetUniformLocation(const std::string& name);
unsigned int CompileShader(unsigned int type, const std::string& source);
ShaderProgramSource ParseShader(const std::string& filepath);
unsigned int CreateShader(const std::string& vertexShaderCode, const std::string& fragmentShaderCode);
int GetUniformLocation(const std::string& name);
unsigned int CompileShader(unsigned int type, const std::string& source);
std::string ParseShader(const std::string& filepath);
unsigned int CreateShader(const std::string& vertexShaderCode, const std::string& fragmentShaderCode);
};
#pragma once
#include "GLErrorHandler.h"
class Texture {
private:
unsigned int m_RendererID;
std::string m_FilePath;
unsigned char* m_LocalBuffer;
int m_Width, m_Height, m_BPP;
public:
Texture(const std::string& filepath);
~Texture();
void Bind(unsigned int slot = 0) const;
void Unbind() const;
inline int GetWidth() const { return m_Width; }
inline int GetHeight() const { return m_Height; }
};
\ No newline at end of file
......@@ -350,7 +350,7 @@ void Character::setUpHBTextures()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
int width, height, nrChannels;
unsigned char *data = stbi_load("C:/dev/DTE-3609/start_code/resources/HP.jpg", &width, &height, &nrChannels, 0);
unsigned char *data = stbi_load("C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/HP.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
......
......@@ -10,9 +10,20 @@ Landscape::~Landscape()
void Landscape::privateInit()
{
// 3 floats for position
layout.Push<float>(3);
// 2 floats for texture position
layout.Push<float>(2);
vao.AddBuffer(vbo, layout);
shader.Bind();
shader.SetUniform4f("u_Color", 0.1f, 0.5f, 0.5f, 1.0f);
unsigned int textureSlot = 0;
texture.Bind(textureSlot);
shader.SetUniform1i("u_Texture", textureSlot);
vao.Unbind();
vbo.Unbind();
ibo.Unbind();
......
//#include <windows.h>
//#include "GL/glew.h"
//#include <GL/glut.h>
//#include <GL/gl.h>
#include "../include/Minimap.hpp"
//#include "../include/GLErrorHandler.h"
//GLuint g_fbo; // frame buffer object
//GLuint g_pbo; // pixel buffer object
//GLuint g_texture; // texture object
//int g_iTexWidth = 512; // texture width
//int g_iTexHeight = 512; // texture height
Minimap::Minimap() {
......
......@@ -6,11 +6,12 @@
#include <string>
#include <sstream>
Shader::Shader(const std::string& filepath)
: m_FilePath(filepath), m_RendererID(0)
Shader::Shader(const std::string& vertexShaderFilepath, const std::string& fragmentShaderFilepath)
: m_VertexShaderFilePath(vertexShaderFilepath), m_FragmentShaderFilePath(fragmentShaderFilepath), m_RendererID(0)
{
ShaderProgramSource source = ParseShader(filepath);
m_RendererID = CreateShader(source.VertexSource, source.FragmentSource);
const std::string vertexSource = ParseShader(vertexShaderFilepath);
const std::string fragmentSource = ParseShader(fragmentShaderFilepath);
m_RendererID = CreateShader(vertexSource, fragmentSource);
}
Shader::~Shader()
......@@ -56,36 +57,24 @@ int Shader::GetUniformLocation(const std::string& name)
}
m_UniformLocationCache[name] = location;
return location;
}
ShaderProgramSource Shader::ParseShader(const std::string& filepath) {
std::string Shader::ParseShader(const std::string& filepath) {
std::ifstream stream(filepath);
enum class ShaderType {
NONE = -1, VERTEX = 0, FRAGMENT = 1
};
std::string line;
std::stringstream stringStream[2];
ShaderType type = ShaderType::NONE;
std::string result;
while (getline(stream, line)) {
if (line.find("shader") != std::string::npos) {
if (line.find("vertex") != std::string::npos) {
type = ShaderType::VERTEX;
}
else if (line.find("fragment") != std::string::npos) {
type = ShaderType::FRAGMENT;
}
}
else if (line.find("//") == std::string::npos) {
stringStream[(int)type] << line << '\n';
if (line.find("shader") != std::string::npos || line.find("//") != std::string::npos) {
continue;
}
result += line + '\n';
}
return { stringStream[0].str(), stringStream[1].str() };
return result;
}
unsigned int Shader::CompileShader(unsigned int type, const std::string& source) {
......
......@@ -33,7 +33,7 @@ void Snow::privateInit() {
}
void Snow::privateRender() {
/*
glLoadIdentity(); // Reset The ModelView Matrix
for (loop=0;loop<MAX_PARTICLES;loop++) // Loop Through All The Particles
......@@ -81,7 +81,7 @@ void Snow::privateRender() {
}
}
}
*/
}
void Snow::privateUpdate() {
......@@ -89,11 +89,6 @@ void Snow::privateUpdate() {
}
void Snow::setUpTexture() {
// textureArray_.push_back(glm::vec2(0.0f, 1.0f));
// textureArray_.push_back(glm::vec2(1.0f, 1.0f));
// textureArray_.push_back(glm::vec2(1.0f, 0.0f));
// textureArray_.push_back(glm::vec2(0.0f, 0.0f));
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texture);
......@@ -105,7 +100,7 @@ void Snow::setUpTexture() {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
int width, height, nrChannels;
unsigned char *data = stbi_load("C:/dev/DTE-3609/start_code/resources/snowflake.jpg", &width, &height, &nrChannels, 0);
unsigned char *data = stbi_load("C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/snowflake.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
......
#include "../include/Texture.h"
#include "../include/stb_image.h"
Texture::Texture(const std::string& filepath)
: m_FilePath(filepath), m_LocalBuffer(nullptr),
m_Width(0), m_Height(0), m_BPP(0)
{
stbi_set_flip_vertically_on_load(1);
m_LocalBuffer = stbi_load(filepath.c_str(), &m_Width, &m_Height, &m_BPP, 4);
GLCall(glGenTextures(1, &m_RendererID));
GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer));
GLCall(glBindTexture(GL_TEXTURE_2D, 0));
if (m_LocalBuffer) {
stbi_image_free(m_LocalBuffer);
}
}
Texture::~Texture()
{
GLCall(glDeleteTextures(1, &m_RendererID));
}
void Texture::Bind(unsigned int slot) const
{
GLCall(glActiveTexture(GL_TEXTURE0 + slot));
GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
}
void Texture::Unbind() const
{
GLCall(glBindTexture(GL_TEXTURE_2D, 0));
}
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