2022-10-18 22:02:03 -07:00

131 lines
3.6 KiB
C++

// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/mesh/_Mesh.hpp"
#include "dawnopengl.hpp"
/** Indice that references a specific vertice */
typedef int32_t meshindice_t;
namespace Dawn {
enum MeshDrawMode {
MESH_DRAW_MODE_TRIANGLES = GL_TRIANGLES
};
class Mesh {
private:
protected:
/** Pointer to the vertex buffer on the GPU */
GLuint vertexBuffer = -1;
/** Pointer to the index buffer on the GPU */
GLuint indexBuffer = -1;
/** How many vertices are in the mesh */
int32_t verticeCount = -1;
/** How many indices are in the mesh */
int32_t indiceCount = -1;
public:
/**
* Create a new set of buffers for the mesh to use.
*
* @param verticeCount How many Vertices will this buffer support.
* @param indiceCount How many Indices will this buffer support.
*/
void createBuffers(
int32_t verticeCount,
int32_t indiceCount
);
/**
* Cleanup the buffers on a given mesh. This is useful if you intend to
* expand the count of vertices your mesh supports.
*/
void disposeBuffers();
/**
* Write vertice positions to the mesh.
*
* @tparam N Size of the array, in terms of number of elements.
* @param position Position, within the buffer, to write to.
* @param vertices Array of positions to write.
*/
template<size_t N>
void bufferPositions(
int32_t position,
std::array<glm::vec3, N> positions
) {
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
glBufferSubData(
GL_ARRAY_BUFFER,
sizeof(glm::vec3) * position,
sizeof(positions),
(void *)positions.data()
);
}
/**
* Write vertice coordinates to the mesh.
*
* @tparam N Size of the array, in terms of number of elements.
* @param position Position, within the buffer, to write to.
* @param coordinates Array of coordinates to write.
*/
template<size_t N>
void bufferCoordinates(
int32_t position,
std::array<glm::vec2, N> coordinates
) {
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
glBufferSubData(
GL_ARRAY_BUFFER,
sizeof(glm::vec2) * position,
sizeof(coordinates),
(void *)coordinates.data()
);
}
/**
* Write indices to the mesh.
*
* @tparam N Size of the array, in terms of number of elements.
* @param position Position, within the buffer, to write to.
* @param indices Array of indices to write.
*/
template<size_t N>
void bufferIndices(
int32_t position,
std::array<meshindice_t, N> indices
) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
glBufferSubData(
GL_ELEMENT_ARRAY_BUFFER,
sizeof(meshindice_t) * position,
sizeof(indices),
(void *)indices.data()
);
}
/**
* Draw a primitive. Primitives are drawn by their indices.
*
* @param drawMode Which drawing mode to use to draw the primitive.
* @param start Start indice (index) to draw.
* @param count Count of indices to draw. Use -1 to draw all.
*/
void draw(
enum MeshDrawMode drawMode,
int32_t start,
int32_t count
);
/**
* Cleanup a previously initiated mesh.
*/
~Mesh();
};
}