// =====================================================================================================================
// glw, an open-source library that wraps OpenGL structures into classes.
// Copyright (C) 2024 Medusa Slockbower
//
// This program 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.
//
// This program 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 this program. If not, see .
// =====================================================================================================================
#ifndef GLW_VERTEX_DESCRIPTOR_H
#define GLW_VERTEX_DESCRIPTOR_H
#include "common.h"
#include
namespace ocu = open_cpp_utils;
namespace glw
{
// =====================================================================================================================
// Definitions
// =====================================================================================================================
class vertex_descriptor
{
// Functions ===========================================================================================================
private:
struct attribute
{
size_t size;
enum_t type;
bool normalized;
index_t offset;
};
// Functions ===========================================================================================================
public:
vertex_descriptor();
vertex_descriptor(const vertex_descriptor&);
vertex_descriptor(vertex_descriptor&&) = default;
~vertex_descriptor();
void add_attribute(enum_t type, size_t size, bool normalized);
void bind();
// Variables ===========================================================================================================
private:
handle_t handle_;
ocu::dynarray attributes_;
};
inline vertex_descriptor::vertex_descriptor()
: handle_(NULL)
{
glCreateVertexArrays(1, &handle_);
}
inline vertex_descriptor::vertex_descriptor(const vertex_descriptor& other)
: handle_(NULL)
{
glCreateVertexArrays(1, &handle_);
attributes_.reserve(other.attributes_.size());
for(const attribute& attrib : other.attributes_)
{
add_attribute(attrib.type, attrib.size, attrib.normalized);
}
}
inline vertex_descriptor::~vertex_descriptor()
{
glDeleteVertexArrays(1, &handle_);
}
inline void vertex_descriptor::add_attribute(enum_t type, size_t size, bool normalized)
{
index_t offset = 0;
if(attributes_.empty() == false)
{
const attribute& prev = attributes_.back();
offset = prev.offset + prev.size * size_of(prev.type);
}
glEnableVertexArrayAttrib(handle_, attributes_.size());
glVertexArrayAttribFormat(handle_, attributes_.size(), size, type, normalized, offset);
attributes_.push_back(attribute(size, type, normalized, offset));
}
inline void vertex_descriptor::bind()
{
glBindVertexArray(handle_);
}
}
#endif //VERTEX_DESCRIPTOR_H