bind and enable/disable attributes

This commit is contained in:
frank 2021-10-26 12:48:02 -04:00
parent 8972b0c868
commit 9e5ecc3ace
2 changed files with 35 additions and 2 deletions

View File

@ -49,6 +49,18 @@ std::size_t sb::Attributes::count() const
}, vertices); }, 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 /* 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. */ * along with the memory pointer when copying vertices to the GPU. */
std::size_t sb::Attributes::size() const std::size_t sb::Attributes::size() const
@ -66,6 +78,22 @@ std::size_t sb::Attributes::size() const
}, vertices); }, 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 /* 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 * 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 * 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. */ * prevent it being looked up with arguments other than attributes. */
std::ostream& sb::operator<<(std::ostream& out, const Attributes& attributes) std::ostream& sb::operator<<(std::ostream& out, const Attributes& attributes)
{ {
out << "<Attributes (#" << attributes.index() << ", " << attributes.size() << " bytes, ";
std::visit([&] (const auto& vector) { std::visit([&] (const auto& vector) {
if constexpr (!std::is_same_v<std::decay_t<decltype(vector)>, std::monostate>) if constexpr (!std::is_same_v<std::decay_t<decltype(vector)>, std::monostate>)
{ {
out << vector; out << vector;
} }
}, attributes.vertices); }, attributes.vertices);
out << ">";
return out; return out;
} }

View File

@ -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 * 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, 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 * 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<bool> alternative is not included because its specialization doesn't * also include an unsigned byte type. The std::vector<bool> alternative is not included because it is a special case of
* include a data member function. */ * std::vector that doesn't have a data member function. */
using Vertices = std::variant< using Vertices = std::variant<
std::monostate, std::vector<std::uint8_t>, std::vector<std::uint32_t>, std::vector<std::int32_t>, std::vector<float>, std::monostate, std::vector<std::uint8_t>, std::vector<std::uint32_t>, std::vector<std::int32_t>, std::vector<float>,
std::vector<glm::bvec2>, std::vector<glm::uvec2>, std::vector<glm::ivec2>, std::vector<glm::vec2>, std::vector<glm::bvec2>, std::vector<glm::uvec2>, std::vector<glm::ivec2>, std::vector<glm::vec2>,
@ -213,7 +213,10 @@ namespace sb
void extend(const Attributes&, std::size_t = 1); void extend(const Attributes&, std::size_t = 1);
void index(std::uint32_t); void index(std::uint32_t);
GLuint index() const; GLuint index() const;
void bind(GLuint, const std::string&);
void bind(std::uint32_t, GLuint, const std::string&);
void enable() const; void enable() const;
void disable() const;
std::size_t size() const; std::size_t size() const;
std::size_t count() const; std::size_t count() const;
GLenum type() const; GLenum type() const;