From 9e5ecc3acea611ffe0a009dc7f965bb7b7235422 Mon Sep 17 00:00:00 2001 From: frank <420@shampoo.ooo> Date: Tue, 26 Oct 2021 12:48:02 -0400 Subject: [PATCH] bind and enable/disable attributes --- src/Attributes.cpp | 30 ++++++++++++++++++++++++++++++ src/Attributes.hpp | 7 +++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Attributes.cpp b/src/Attributes.cpp index 7901184..ac73b54 100644 --- a/src/Attributes.cpp +++ b/src/Attributes.cpp @@ -49,6 +49,18 @@ std::size_t sb::Attributes::count() const }, vertices); } +/* Enable the attributes in the VAO. */ +void sb::Attributes::enable() const +{ + glEnableVertexAttribArray(*this); +} + +/* Disable the attributes in the VAO. */ +void sb::Attributes::disable() const +{ + glDisableVertexAttribArray(*this); +} + /* Returns the size in bytes of the object's underlying vector of vertices. This can be passed to OpenGL * along with the memory pointer when copying vertices to the GPU. */ std::size_t sb::Attributes::size() const @@ -66,6 +78,22 @@ std::size_t sb::Attributes::size() const }, vertices); } +/* Set the index to the location of attributes with given name in a shader program. The program + * must already be linked to get the location. */ +void sb::Attributes::bind(GLuint program, const std::string& name) +{ + index(glGetAttribLocation(program, name.c_str())); +} + +/* Set the index and bind it to the given name in a shader program. This should be called before + * the shader program is linked, otherwise the name will already have been assigned a location + * index by GL. */ +void sb::Attributes::bind(std::uint32_t index, GLuint program, const std::string& name) +{ + glBindAttribLocation(program, index, name.c_str()); + this->index(index); +} + /* Return the GLenum that corresponds to the type of scalar being held in the vertices. This * will return either GL_FLOAT, GL_INT, GL_UNSIGNED_INT, GL_UNSIGNED_BYTE, GL_BOOL, or * GL_INVALID_ENUM. A return value of GL_INVALID_ENUM indicates an error meaning there are no @@ -224,11 +252,13 @@ void sb::Attributes::extend(const Attributes& other, std::size_t count) * prevent it being looked up with arguments other than attributes. */ std::ostream& sb::operator<<(std::ostream& out, const Attributes& attributes) { + out << ", std::monostate>) { out << vector; } }, attributes.vertices); + out << ">"; return out; } diff --git a/src/Attributes.hpp b/src/Attributes.hpp index 67c9812..453be76 100644 --- a/src/Attributes.hpp +++ b/src/Attributes.hpp @@ -120,8 +120,8 @@ namespace sb * Each variant alternative is a vector of vertex type, so only homogenous vectors can be used. Index 0 is the std::monostate * alternative, which is used here for default initialization to indicate Attributes are in an empty state where no variant * alternative is selected. 1D vertices are specified by vectors of scalars, rather than the 1D glm vertex types. 1D vertices - * also include an unsigned byte type. The std::vector alternative is not included because its specialization doesn't - * include a data member function. */ + * also include an unsigned byte type. The std::vector alternative is not included because it is a special case of + * std::vector that doesn't have a data member function. */ using Vertices = std::variant< std::monostate, std::vector, std::vector, std::vector, std::vector, std::vector, std::vector, std::vector, std::vector, @@ -213,7 +213,10 @@ namespace sb void extend(const Attributes&, std::size_t = 1); void index(std::uint32_t); GLuint index() const; + void bind(GLuint, const std::string&); + void bind(std::uint32_t, GLuint, const std::string&); void enable() const; + void disable() const; std::size_t size() const; std::size_t count() const; GLenum type() const;