BLT-With-Graphics-Template/include/blt/gfx/model.h

121 lines
3.6 KiB
C
Raw Normal View History

2023-12-27 01:10:53 -05:00
/*
* <Short Description>
* Copyright (C) 2023 Brett Terpstra
*
* 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 <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_WITH_GRAPHICS_MODEL_H
#define BLT_WITH_GRAPHICS_MODEL_H
#include <blt/math/vectors.h>
#include <vector>
#include <blt/gfx/gl_includes.h>
2023-12-27 22:04:33 -05:00
#include <variant>
2023-12-28 00:10:02 -05:00
#include <array>
2023-12-27 01:10:53 -05:00
namespace blt::gfx
{
class model_data
{
public:
private:
std::vector<blt::vec3f> vertices;
std::vector<blt::vec2f> uvs;
std::vector<blt::vec3f> normals;
};
2023-12-28 03:49:00 -05:00
struct vbo_t
{
GLuint bufferID_ = 0;
GLsizeiptr size_ = 0;
void create();
2023-12-28 03:49:00 -05:00
void bind(GLint buffer_type = GL_ARRAY_BUFFER) const;
void allocate(GLsizeiptr size, GLint buffer_type = GL_ARRAY_BUFFER, GLint memory_type = GL_STATIC_DRAW, void* data = nullptr);
static void sub_update(GLsizeiptr offset, GLsizeiptr size, void* data, GLint buffer_type = GL_ARRAY_BUFFER);
void update(GLsizeiptr size, void* data, GLint buffer_type = GL_ARRAY_BUFFER, GLint memory_type = GL_STATIC_DRAW);
void destroy();
2023-12-28 03:49:00 -05:00
};
2023-12-28 00:10:02 -05:00
/**
* Since most VAOs will not use more than 8 VBOs it makes no sense to heap allocate memory to store them
* This class is used to make that easier to handle
*/
2023-12-27 22:04:33 -05:00
class static_dynamic_array
{
private:
static constexpr size_t DATA_SIZE = 8;
2023-12-28 03:49:00 -05:00
typedef std::array<vbo_t, DATA_SIZE> array_t;
std::variant<array_t, vbo_t*> data_;
2023-12-27 22:04:33 -05:00
size_t size_ = DATA_SIZE;
2023-12-28 03:49:00 -05:00
size_t max = 0;
2023-12-28 00:10:02 -05:00
void swap();
2023-12-27 22:04:33 -05:00
public:
2023-12-28 00:10:02 -05:00
static_dynamic_array();
2023-12-27 22:04:33 -05:00
static_dynamic_array(const static_dynamic_array& copy) = delete;
2023-12-28 03:49:00 -05:00
static_dynamic_array(static_dynamic_array&& move) noexcept = default;
2023-12-27 22:04:33 -05:00
static_dynamic_array& operator=(const static_dynamic_array& copy) = delete;
2023-12-28 03:49:00 -05:00
static_dynamic_array& operator=(static_dynamic_array&& move) noexcept = default;
2023-12-28 00:10:02 -05:00
2023-12-28 03:49:00 -05:00
vbo_t& operator[](size_t index)
2023-12-28 00:10:02 -05:00
{
if (index >= size_)
swap();
2023-12-28 03:49:00 -05:00
max = std::max(index, max);
if (std::holds_alternative<vbo_t*>(data_))
return std::get<vbo_t*>(data_)[index];
2023-12-28 00:10:02 -05:00
else
return std::get<array_t>(data_)[index];
}
2023-12-27 22:04:33 -05:00
2023-12-28 03:49:00 -05:00
[[nodiscard]] size_t used() const noexcept {
return max;
}
2023-12-27 22:04:33 -05:00
~static_dynamic_array()
{
2023-12-28 03:49:00 -05:00
if (std::holds_alternative<vbo_t*>(data_))
delete[] std::get<vbo_t*>(data_);
2023-12-27 22:04:33 -05:00
}
};
/**
* basic VAO class.
*/
class basic_vertex_array
{
private:
2023-12-28 03:49:00 -05:00
2023-12-28 00:10:02 -05:00
2023-12-27 22:04:33 -05:00
GLuint vaoID;
2023-12-28 03:49:00 -05:00
static_dynamic_array VBOs;
2023-12-27 22:04:33 -05:00
public:
basic_vertex_array();
};
2023-12-27 01:10:53 -05:00
}
#endif //BLT_WITH_GRAPHICS_MODEL_H