mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-22 23:19:19 +02:00
vao
This commit is contained in:
27
gl/bindable.h
Normal file
27
gl/bindable.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
namespace gl
|
||||
{
|
||||
template <typename T>
|
||||
class bindable
|
||||
{
|
||||
private:
|
||||
static bindable<T>* active;
|
||||
|
||||
public:
|
||||
void bind()
|
||||
{
|
||||
if (active == this)
|
||||
return;
|
||||
active = this;
|
||||
T::bind(*static_cast<T*>(active));
|
||||
}
|
||||
void unbind()
|
||||
{
|
||||
active = nullptr;
|
||||
T::bind(0);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T> bindable<T>* bindable<T>::active;
|
||||
}
|
||||
@@ -99,7 +99,7 @@ gl::program::~program()
|
||||
glDeleteProgram(*this);
|
||||
}
|
||||
|
||||
void gl::program::bind()
|
||||
void gl::program::bind(GLuint i)
|
||||
{
|
||||
glUseProgram(*this);
|
||||
glUseProgram(i);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <functional>
|
||||
|
||||
#include "object.h"
|
||||
#include "bindable.h"
|
||||
|
||||
namespace gl
|
||||
{
|
||||
@@ -15,14 +16,15 @@ namespace gl
|
||||
~shader();
|
||||
};
|
||||
|
||||
class program : public object
|
||||
class program : public object, public bindable<program>
|
||||
{
|
||||
public:
|
||||
program();
|
||||
program(std::vector<std::reference_wrapper<const gl::shader>>);
|
||||
~program();
|
||||
|
||||
void bind();
|
||||
using bindable::bind;
|
||||
static void bind(GLuint i);
|
||||
|
||||
void attach(const shader &);
|
||||
void link();
|
||||
|
||||
@@ -10,6 +10,10 @@ void gl::program_mvp::init()
|
||||
|
||||
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)));
|
||||
@@ -20,6 +24,10 @@ void gl::program_mvp::set_mv(const glm::mat4 &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));
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,9 @@ namespace gl
|
||||
GLint mvn_uniform;
|
||||
GLuint p_uniform;
|
||||
|
||||
glm::mat4 last_mv;
|
||||
glm::mat4 last_p;
|
||||
|
||||
public:
|
||||
using program::program;
|
||||
|
||||
|
||||
24
gl/vao.cpp
Normal file
24
gl/vao.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "stdafx.h"
|
||||
#include "vao.h"
|
||||
|
||||
gl::vao::vao()
|
||||
{
|
||||
glGenVertexArrays(1, *this);
|
||||
}
|
||||
|
||||
gl::vao::~vao()
|
||||
{
|
||||
glDeleteVertexArrays(1, *this);
|
||||
}
|
||||
|
||||
void gl::vao::setup_attrib(int attrib, int size, int type, int stride, int offset)
|
||||
{
|
||||
bind();
|
||||
glVertexAttribPointer(attrib, size, type, GL_FALSE, stride, reinterpret_cast<void*>(offset));
|
||||
glEnableVertexAttribArray(attrib);
|
||||
}
|
||||
|
||||
void gl::vao::bind(GLuint i)
|
||||
{
|
||||
glBindVertexArray(i);
|
||||
}
|
||||
19
gl/vao.h
Normal file
19
gl/vao.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "object.h"
|
||||
#include "bindable.h"
|
||||
|
||||
namespace gl
|
||||
{
|
||||
class vao : public object, public bindable<vao>
|
||||
{
|
||||
public:
|
||||
vao();
|
||||
~vao();
|
||||
|
||||
void setup_attrib(int attrib, int size, int type, int stride, int offset);
|
||||
|
||||
using bindable::bind;
|
||||
static void bind(GLuint i);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user