/* * Copyright (C) 2021 Christopher J. Howard * * This file is part of Antkeeper source code. * * Antkeeper source code is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Antkeeper source code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Antkeeper source code. If not, see . */ #include "gl/vertex-array.hpp" #include "gl/vertex-buffer.hpp" #include #include #include namespace gl { static constexpr GLenum vertex_attribute_type_lut[] = { GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT, GL_HALF_FLOAT, GL_FLOAT, GL_DOUBLE }; vertex_array::vertex_array(): gl_array_id(0) { glGenVertexArrays(1, &gl_array_id); } vertex_array::~vertex_array() { glDeleteVertexArrays(1, &gl_array_id); } void vertex_array::bind(attribute_location_type location, const vertex_attribute& attribute) { if (attribute.buffer == nullptr) { throw std::invalid_argument("Cannot bind vertex attribute that has a null vertex buffer."); } if (attribute.components < 1 || attribute.components > 4) { throw std::invalid_argument("Cannot bind vertex attribute that has an unsupported number of components (" + std::to_string(attribute.components) + ")"); } attributes[location] = attribute; GLenum gl_type = vertex_attribute_type_lut[static_cast(attribute.type)]; glBindVertexArray(gl_array_id); glBindBuffer(GL_ARRAY_BUFFER, attribute.buffer->gl_buffer_id); glVertexAttribPointer( static_cast(location), static_cast(attribute.components), gl_type, GL_FALSE, static_cast(attribute.stride), (const GLvoid*)attribute.offset); glEnableVertexAttribArray(static_cast(location)); } void vertex_array::unbind(attribute_location_type location) { if (auto it = attributes.find(location); it != attributes.end()) { glBindVertexArray(gl_array_id); glDisableVertexAttribArray(static_cast(location)); attributes.erase(it); } else { throw std::invalid_argument("Non-existent vertex attribute cannot be unbound."); } } const typename vertex_array::attribute_map_type& vertex_array::get_attributes() const { return attributes; } } // namespace gl