Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Blue/src/IndexBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ IndexBuffer::~IndexBuffer()
glDeleteBuffers(1, &m_RendererID);
}

void IndexBuffer::Bind()
void IndexBuffer::Bind() const
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);
}

void IndexBuffer::Unbind()
void IndexBuffer::Unbind() const
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
4 changes: 2 additions & 2 deletions Blue/src/IndexBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class IndexBuffer
IndexBuffer(const unsigned *data, unsigned int count);
~IndexBuffer();

void Bind();
void Unbind();
void Bind() const;
void Unbind() const;

inline unsigned int GetCount() const { return m_Count; }
};
14 changes: 13 additions & 1 deletion Blue/src/Renderer.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
#include "Renderer.h"
#include "Renderer.h"

void Renderer::Clear() const
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Renderer::Draw(const VertexArray &va, const IndexBuffer &ib, const Shader &shader) const
{
shader.Bind();
va.Bind();
ib.Bind();
glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr);
}
14 changes: 13 additions & 1 deletion Blue/src/Renderer.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
#pragma once

#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "GLFW/glfw3.h"
#include "VertexArray.h"
#include "IndexBuffer.h"
#include "Shader.h"

class Renderer
{

private:
public:
void Clear() const;
void Draw(const VertexArray &va, const IndexBuffer &ib, const Shader &shader) const;
};
121 changes: 121 additions & 0 deletions Blue/src/Shader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include "Shader.h"
#include <iostream>
#include <string.h>
#include <fstream>
#include <sstream>

Shader::Shader(const std::string &filepath)
: m_FilePath(filepath), m_RendererID(0)
{
struct ShaderProgramSource shaderSource = ParseShader(filepath);
m_RendererID = CreateShader(shaderSource.VertexSource, shaderSource.FragmentSource);
}

Shader::~Shader()
{
glDeleteProgram(m_RendererID);
}

struct ShaderProgramSource Shader::ParseShader(std::string filePath)
{
enum class ShaderType
{
NONE = -1,
VERTEX = 0,
FRAGMENT = 1
};
std::ifstream stream(filePath);
std::string line;
std::stringstream ss[2];
ShaderType type = ShaderType::NONE;

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
ss[(int)type] << line << "\n";
}
return {ss[0].str(), ss[1].str()};
}

unsigned int Shader::CreateShader(const std::string vertexShader, const std::string fragmentShader)
{
unsigned int program = glCreateProgram();

unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);

glAttachShader(program, fs);
glAttachShader(program, vs);
glLinkProgram(program);

return program;
}

unsigned int Shader::CompileShader(unsigned int type, const std::string &source)
{
unsigned int id = glCreateShader(type);
const char *src = source.c_str();
glShaderSource(id, 1, &src, NULL);
glCompileShader(id);

int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char message[length];
glGetShaderInfoLog(id, length, &length, message);
std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << "shader" << std::endl;
std::cout << message << std::endl;
glDeleteShader(id);
return 0;
}

return id;
}

void Shader::Bind() const
{
glUseProgram(m_RendererID);
}

void Shader::Unbind() const
{
glUseProgram(0);
}

void Shader::SetUniform4f(const std::string &name, float v0, float v1, float v2, float v3)
{
glUniform4f(GetUniformLocation(name), v0, v1, v2, v3);
}

int Shader::GetUniformLocation(const std::string &name)
{
if (m_UniformLocationCache.find(name) != m_UniformLocationCache.end())
return m_UniformLocationCache[name];

int location = glGetUniformLocation(m_RendererID, name.c_str());
if (location == -1)
std::cout << "Warning: Uniform " << name << " doesn't exist !" << std::endl;
else
m_UniformLocationCache[name] = location;
return location;
}

void Shader::SetUniform1i(const std::string &name, int value)
{
glUniform1i(GetUniformLocation(name), value);
}

void Shader::SetUniform1f(const std::string &name, float value)
{
glUniform1f(GetUniformLocation(name), value);
}
36 changes: 36 additions & 0 deletions Blue/src/Shader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once
#include <string>
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include <unordered_map>

struct ShaderProgramSource
{
std::string VertexSource;
std::string FragmentSource;
};

class Shader
{
private:
unsigned int m_RendererID;
std::string m_FilePath;
std::unordered_map<std::string, int> m_UniformLocationCache;

public:
Shader(const std::string &filepath);
~Shader();

void Bind() const;
void Unbind() const;

void SetUniform1i(const std::string &name, int value);
void SetUniform1f(const std::string &name, float value);
void SetUniform4f(const std::string &name, float v0, float v1, float v2, float v3);

private:
unsigned int CompileShader(unsigned int type, const std::string &source);
int GetUniformLocation(const std::string &name);
unsigned int CreateShader(const std::string vertexShader, const std::string fragmentShader);
struct ShaderProgramSource ParseShader(std::string filePath);
};
36 changes: 36 additions & 0 deletions Blue/src/Texture.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "Texture.h"
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "stb_image.h"

Texture::Texture(const std::string &filePath)
: m_RendererID(0), 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);
glGenTextures(1, &m_RendererID);
glBindTexture(GL_TEXTURE_2D, m_RendererID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer);
glBindTexture(GL_TEXTURE_2D, 0);
if (m_LocalBuffer)
stbi_image_free(m_LocalBuffer);
}

Texture::~Texture()
{
glDeleteTextures(1, &m_RendererID);
}
void Texture::Bind(unsigned int slot) const
{
glActiveTexture(GL_TEXTURE0 + slot);
glBindTexture(GL_TEXTURE_2D, m_RendererID);
}

void Texture::Unbind() const
{
glBindTexture(GL_TEXTURE_2D, 0);
}
21 changes: 21 additions & 0 deletions Blue/src/Texture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include <string>

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; }
};
2 changes: 1 addition & 1 deletion Blue/src/VertexArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ class VertexArray

void AddBuffer(const VertexBuffer &vb, const VertexBufferLayout &layout);
void Bind() const;
void UnBind() const;
void Unbind() const;
};
6 changes: 3 additions & 3 deletions Blue/src/VertexBufferLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
template <>
void VertexBufferLayout::Push<float>(unsigned int count)
{
m_Elements.push_back({count, GL_FLOAT, GL_FALSE});
m_Elements.push_back({GL_FLOAT, count, GL_FALSE});
m_Stride += sizeof(GLfloat) * count;
}

template <>
void VertexBufferLayout::Push<unsigned int>(unsigned int count)
{
m_Elements.push_back({count, GL_UNSIGNED_INT, GL_FALSE});
m_Elements.push_back({GL_UNSIGNED_INT, count, GL_FALSE});
m_Stride += sizeof(GLuint) * count;
}

template <>
void VertexBufferLayout::Push<unsigned char>(unsigned int count)
{
m_Elements.push_back({count, GL_UNSIGNED_BYTE, GL_TRUE});
m_Elements.push_back({GL_UNSIGNED_BYTE, count, GL_TRUE});
m_Stride += sizeof(GLbyte) * count;
}
2 changes: 2 additions & 0 deletions Blue/src/stb_image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
Loading