make vao mode selectable at runtime (enabled by default, disabled by multiviewports)

This commit is contained in:
milek7
2019-08-08 15:48:55 +02:00
parent 37fea6db7a
commit 613bdb91d9
4 changed files with 52 additions and 41 deletions

View File

@@ -1,48 +1,48 @@
#include "stdafx.h"
#include "vao.h"
/*
bool gl::vao::use_vao = true;
gl::vao::vao()
{
if (!use_vao)
return;
glGenVertexArrays(1, *this);
}
gl::vao::~vao()
{
if (!use_vao)
return;
glDeleteVertexArrays(1, *this);
}
void gl::vao::setup_attrib(gl::buffer &buffer, int attrib, int size, int type, int stride, int offset)
{
bind();
buffer.bind(gl::buffer::ARRAY_BUFFER);
glVertexAttribPointer(attrib, size, type, GL_FALSE, stride, reinterpret_cast<void*>(offset));
glEnableVertexAttribArray(attrib);
}
void gl::vao::setup_ebo(buffer &ebo)
{
bind();
ebo.bind(gl::buffer::ELEMENT_ARRAY_BUFFER);
}
void gl::vao::bind(GLuint i)
{
glBindVertexArray(i);
}
*/
void gl::vao::setup_attrib(gl::buffer &buffer, int attrib, int size, int type, int stride, int offset)
{
params.push_back({buffer, attrib, size, type, stride, offset});
active = nullptr;
if (use_vao) {
bind();
buffer.bind(buffer::ARRAY_BUFFER);
glVertexAttribPointer(attrib, size, type, GL_FALSE, stride, reinterpret_cast<void*>(offset));
glEnableVertexAttribArray(attrib);
}
else {
params.push_back({buffer, attrib, size, type, stride, offset});
active = nullptr;
}
}
void gl::vao::setup_ebo(buffer &e)
{
ebo = &e;
active = nullptr;
if (use_vao) {
bind();
e.bind(buffer::ELEMENT_ARRAY_BUFFER);
}
else {
ebo = &e;
active = nullptr;
}
}
void gl::vao::bind()
@@ -51,19 +51,24 @@ void gl::vao::bind()
return;
active = this;
for (attrib_params &param : params) {
param.buffer.bind(gl::buffer::ARRAY_BUFFER);
glVertexAttribPointer(param.attrib, param.size, param.type, GL_FALSE, param.stride, reinterpret_cast<void*>(param.offset));
glEnableVertexAttribArray(param.attrib);
if (use_vao) {
glBindVertexArray(*this);
}
else {
for (attrib_params &param : params) {
param.buffer.bind(gl::buffer::ARRAY_BUFFER);
glVertexAttribPointer(param.attrib, param.size, param.type, GL_FALSE, param.stride, reinterpret_cast<void*>(param.offset));
glEnableVertexAttribArray(param.attrib);
}
for (size_t i = params.size(); i < 4; i++)
glDisableVertexAttribArray(i);
for (size_t i = params.size(); i < 4; i++)
glDisableVertexAttribArray(i);
if (ebo)
ebo->bind(gl::buffer::ELEMENT_ARRAY_BUFFER);
else
gl::buffer::unbind(gl::buffer::ELEMENT_ARRAY_BUFFER);
if (ebo)
ebo->bind(gl::buffer::ELEMENT_ARRAY_BUFFER);
else
gl::buffer::unbind(gl::buffer::ELEMENT_ARRAY_BUFFER);
}
}
void gl::vao::unbind()

View File

@@ -6,7 +6,7 @@
namespace gl
{
class vao : public bindable<vao>
class vao : object, public bindable<vao>
{
struct attrib_params {
// TBD: should be shared_ptr? (when buffer is destroyed by owner VAO could still potentially exist)
@@ -23,15 +23,15 @@ namespace gl
std::vector<attrib_params> params;
public:
//vao();
//~vao();
vao();
~vao();
void setup_attrib(buffer &buffer, int attrib, int size, int type, int stride, int offset);
void setup_ebo(buffer &ebo);
//using bindable::bind;
void bind();
static void unbind();
//static void bind(GLuint i);
static bool use_vao;
};
}