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;
};
111 changes: 111 additions & 0 deletions Blue/src/Shader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#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;
}
34 changes: 34 additions & 0 deletions Blue/src/Shader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#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 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);
};
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;
}
4 changes: 2 additions & 2 deletions src/IndexBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class IndexBuffer
glDeleteBuffers(1, &m_RendererID);
}

void Bind()
void Bind() const
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);
}
void Unbind()
void Unbind() const
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
Expand Down
27 changes: 26 additions & 1 deletion src/Renderer.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
#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;
};

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);
}

void Renderer::Clear() const
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
Loading