feat: uniform buffer configure shader
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "engine/renderer/shader.h"
|
||||
#include "engine/export.h"
|
||||
|
||||
namespace Core {
|
||||
@ -16,6 +17,7 @@ namespace OpenGL {
|
||||
public:
|
||||
Buffer(BufferTarget target, BufferUsage usage);
|
||||
|
||||
protected:
|
||||
void Data(void* data, size_t size);
|
||||
void SubData(void *data, size_t size, size_t offset);
|
||||
|
||||
@ -33,8 +35,17 @@ namespace OpenGL {
|
||||
class ENGINE_API UniformBuffer : public Buffer {
|
||||
public:
|
||||
UniformBuffer(size_t size, unsigned int index);
|
||||
public:
|
||||
|
||||
|
||||
void ConfigureShader(Shader& shader, const char* uniformName);
|
||||
|
||||
template<typename T, typename S = size_t>
|
||||
void UpdateUniform(void* data, S offset) {
|
||||
SubData(data, sizeof(T), offset);
|
||||
}
|
||||
private:
|
||||
unsigned int m_uniformBinding;
|
||||
private:
|
||||
static unsigned int s_bufferNextId;
|
||||
};
|
||||
} // namespace OpenGL
|
||||
|
||||
|
||||
@ -15,11 +15,11 @@ public:
|
||||
Shader();
|
||||
~Shader();
|
||||
|
||||
unsigned int m_id;
|
||||
|
||||
void init(const std::string &vertexCode, const std::string &fragmentCode);
|
||||
void use();
|
||||
|
||||
const unsigned int GetID() const { return m_id; }
|
||||
|
||||
void setBool(const std::string &name, bool value) const;
|
||||
void setInt(const std::string &name, int value) const;
|
||||
void setFloat(const std::string &name, float value) const;
|
||||
@ -32,8 +32,8 @@ public:
|
||||
void setMat2(const std::string &name, const glm::mat2 &mat) const;
|
||||
void setMat3(const std::string &name, const glm::mat3 &mat) const;
|
||||
void setMat4(const std::string &name, const glm::mat4 &mat) const;
|
||||
|
||||
private:
|
||||
unsigned int m_id;
|
||||
unsigned int m_vertexId;
|
||||
unsigned int m_fragmentId;
|
||||
|
||||
|
||||
@ -45,12 +45,19 @@ namespace OpenGL {
|
||||
Unbind();
|
||||
}
|
||||
|
||||
unsigned int UniformBuffer::s_bufferNextId = 1;
|
||||
|
||||
UniformBuffer::UniformBuffer(size_t size, unsigned int index)
|
||||
: Buffer(GL_UNIFORM_BUFFER, GL_STATIC_DRAW)
|
||||
: Buffer(GL_UNIFORM_BUFFER, GL_STATIC_DRAW), m_uniformBinding(s_bufferNextId++)
|
||||
{
|
||||
Data(nullptr, size);
|
||||
|
||||
BindBuffer(index);
|
||||
BindBuffer(m_uniformBinding);
|
||||
}
|
||||
|
||||
void UniformBuffer::ConfigureShader(Shader& shader, const char* uniformName) {
|
||||
auto uniformIndex = glGetUniformBlockIndex(shader.GetID(), uniformName);
|
||||
glUniformBlockBinding(shader.GetID(), uniformIndex, m_uniformBinding);
|
||||
}
|
||||
|
||||
} // namespace OpenGL
|
||||
|
||||
@ -36,8 +36,9 @@ Renderer::Renderer(std::shared_ptr<Scene> scene)
|
||||
FileManager::read("./engine/src/shaders/depth.fs")
|
||||
);
|
||||
|
||||
glUniformBlockBinding(m_shader.m_id, glGetUniformBlockIndex(m_shader.m_id, "Matrices"), 1);
|
||||
// glUniformBlockBinding(m_depthShader.m_id, glGetUniformBlockIndex(m_depthShader.m_id, "Matrices"), 1);
|
||||
// glUniformBlockBinding(m_shader.m_id, glGetUniformBlockIndex(m_shader.m_id, "Matrices"), 1);
|
||||
|
||||
m_uniform_matrices.ConfigureShader(m_shader, "Matrices");
|
||||
|
||||
m_proj = glm::perspective(
|
||||
static_cast<float>(M_PI_2),
|
||||
@ -46,7 +47,7 @@ Renderer::Renderer(std::shared_ptr<Scene> scene)
|
||||
100.0f
|
||||
);
|
||||
|
||||
m_uniform_matrices.SubData(glm::value_ptr(m_proj), sizeof(glm::mat4), 0);
|
||||
m_uniform_matrices.UpdateUniform<glm::mat4>(glm::value_ptr(m_proj), 0);
|
||||
|
||||
m_model = glm::mat4(1.f);
|
||||
|
||||
@ -65,7 +66,7 @@ void Renderer::OnWindowResized(int w, int h) {
|
||||
0.01f,
|
||||
100.0f
|
||||
);
|
||||
m_uniform_matrices.SubData(glm::value_ptr(m_proj), sizeof(glm::mat4), 0);
|
||||
m_uniform_matrices.UpdateUniform<glm::mat4>(glm::value_ptr(m_proj), 0);
|
||||
}
|
||||
|
||||
void Renderer::ApplyLights(Shader &shader) {
|
||||
@ -128,7 +129,7 @@ void Renderer::UpdateView() {
|
||||
glm::vec3(0.f, 1.f, 0.f)
|
||||
);
|
||||
|
||||
m_uniform_matrices.SubData(glm::value_ptr(m_view), sizeof(glm::mat4), sizeof(glm::mat4));
|
||||
m_uniform_matrices.UpdateUniform<glm::mat4>(glm::value_ptr(m_view), sizeof(glm::mat4));
|
||||
|
||||
m_shader.setVec3("viewPos", camTransform.position);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user