mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-22 23:19:19 +02:00
ubo (ugly for now)
This commit is contained in:
@@ -3,36 +3,6 @@
|
||||
|
||||
void gl::program_mvp::init()
|
||||
{
|
||||
mv_uniform = glGetUniformLocation(*this, "modelview");
|
||||
mvn_uniform = glGetUniformLocation(*this, "modelviewnormal");
|
||||
p_uniform = glGetUniformLocation(*this, "projection");
|
||||
}
|
||||
|
||||
void gl::program_mvp::set_mv(const glm::mat4 &m)
|
||||
{
|
||||
if (last_mv == m)
|
||||
return;
|
||||
last_mv = m;
|
||||
|
||||
if (mvn_uniform != -1)
|
||||
{
|
||||
glm::mat3 mvn = glm::mat3(glm::transpose(glm::inverse(m)));
|
||||
glUniformMatrix3fv(mvn_uniform, 1, GL_FALSE, glm::value_ptr(mvn));
|
||||
}
|
||||
glUniformMatrix4fv(mv_uniform, 1, GL_FALSE, glm::value_ptr(m));
|
||||
}
|
||||
|
||||
void gl::program_mvp::set_p(const glm::mat4 &m)
|
||||
{
|
||||
if (last_p == m)
|
||||
return;
|
||||
last_p = m;
|
||||
|
||||
glUniformMatrix4fv(p_uniform, 1, GL_FALSE, glm::value_ptr(m));
|
||||
}
|
||||
|
||||
void gl::program_mvp::copy_gl_mvp()
|
||||
{
|
||||
set_mv(OpenGLMatrices.data(GL_MODELVIEW));
|
||||
set_p(OpenGLMatrices.data(GL_PROJECTION));
|
||||
glUniformBlockBinding(*this, 0, glGetUniformBlockIndex(*this, "scene_ubo"));
|
||||
glUniformBlockBinding(*this, 1, glGetUniformBlockIndex(*this, "model_ubo"));
|
||||
}
|
||||
|
||||
@@ -6,20 +6,9 @@ namespace gl
|
||||
{
|
||||
class program_mvp : public program
|
||||
{
|
||||
GLuint mv_uniform;
|
||||
GLint mvn_uniform;
|
||||
GLuint p_uniform;
|
||||
|
||||
glm::mat4 last_mv;
|
||||
glm::mat4 last_p;
|
||||
|
||||
public:
|
||||
using program::program;
|
||||
|
||||
void set_mv(const glm::mat4 &);
|
||||
void set_p(const glm::mat4 &);
|
||||
void copy_gl_mvp();
|
||||
|
||||
virtual void init() override;
|
||||
};
|
||||
}
|
||||
|
||||
26
gl/ubo.cpp
Normal file
26
gl/ubo.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "stdafx.h"
|
||||
#include "ubo.h"
|
||||
|
||||
gl::ubo::ubo(int size, int index)
|
||||
{
|
||||
glGenBuffers(1, *this);
|
||||
bind();
|
||||
glBufferData(GL_UNIFORM_BUFFER, size, nullptr, GL_DYNAMIC_DRAW);
|
||||
glBindBufferBase(GL_UNIFORM_BUFFER, index, *this);
|
||||
}
|
||||
|
||||
gl::ubo::~ubo()
|
||||
{
|
||||
glDeleteBuffers(1, *this);
|
||||
}
|
||||
|
||||
void gl::ubo::bind(GLuint i)
|
||||
{
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, i);
|
||||
}
|
||||
|
||||
void gl::ubo::update(void *data, int offset, int size)
|
||||
{
|
||||
bind();
|
||||
glBufferSubData(GL_UNIFORM_BUFFER, offset, size, data);
|
||||
}
|
||||
48
gl/ubo.h
Normal file
48
gl/ubo.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "object.h"
|
||||
#include "bindable.h"
|
||||
|
||||
#define UBS_PAD(x) uint64_t : x * 4; uint64_t : x * 4;
|
||||
#define DEFINE_UBS(x, y) _Pragma("pack(push, 1)"); struct x y; _Pragma("pack(pop)")
|
||||
|
||||
namespace gl
|
||||
{
|
||||
static GLuint next_binding_point;
|
||||
|
||||
class ubo : public object, public bindable<ubo>
|
||||
{
|
||||
public:
|
||||
ubo(int size, int index);
|
||||
~ubo();
|
||||
|
||||
using bindable::bind;
|
||||
static void bind(GLuint i);
|
||||
|
||||
void update(void *data, int offset, int size);
|
||||
};
|
||||
|
||||
_Pragma("pack(push, 1)")
|
||||
|
||||
struct scene_ubs
|
||||
{
|
||||
glm::mat4 projection;
|
||||
|
||||
void set(const glm::mat4 &m)
|
||||
{
|
||||
projection = m;
|
||||
}
|
||||
};
|
||||
|
||||
struct model_ubs
|
||||
{
|
||||
glm::mat4 modelview;
|
||||
glm::mat4 modelviewnormal;
|
||||
|
||||
void set(const glm::mat4 &m)
|
||||
{
|
||||
modelview = m;
|
||||
modelviewnormal = glm::mat3(glm::transpose(glm::inverse(m)));
|
||||
}
|
||||
};
|
||||
|
||||
_Pragma("pack(pop)")
|
||||
}
|
||||
Reference in New Issue
Block a user