Base refactor

This commit is contained in:
2023-11-14 09:16:48 -06:00
parent 214082d00f
commit 1817dcaf3a
410 changed files with 749 additions and 20823 deletions

View File

@ -1,10 +0,0 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
Mesh.cpp
)

View File

@ -1,204 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Mesh.hpp"
using namespace Dawn;
void Mesh::createBuffers(
int32_t verticeCount,
int32_t indiceCount
) {
if(verticeCount <= 0) throw "Vertice count must be greater than zero.";
if(indiceCount <= 0) throw "Indice count must be greater than zero.";
this->disposeBuffers();
this->verticeCount = verticeCount;
this->indiceCount = indiceCount;
auto sizePos = sizeof(glm::vec3) * verticeCount;
auto sizeInds = sizeof(meshindice_t) * indiceCount;
auto sizeCoords = sizeof(glm::vec2) * verticeCount;
// Generate vertex array, I don't think I need to do this tbh.
glGenVertexArrays(1, &this->vertexArray);
assertNoGLError();
glBindVertexArray(this->vertexArray);
assertNoGLError();
// Create some buffers, one for the vertex data, one for the indices
GLuint buffer[2];
glGenBuffers(2, buffer);
assertNoGLError();
this->vertexBuffer = buffer[0];
if(this->vertexBuffer < 0) throw "Failed to create vertex buffer";
this->indexBuffer = buffer[1];
if(this->indexBuffer < 0) throw "Failed to create index buffer";
// Buffer an empty set of data then buffer each component
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
assertNoGLError();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
assertNoGLError();
glBufferData(GL_ARRAY_BUFFER, sizePos+sizeCoords, 0, GL_DYNAMIC_DRAW);
assertNoGLError();
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeInds, 0, GL_DYNAMIC_DRAW);
assertNoGLError();
// Setup the attrib pointers
size_t offset = 0;
glVertexAttribPointer(
0, sizeof(glm::vec3) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
assertNoGLError();
glEnableVertexAttribArray(0);
assertNoGLError();
offset += sizePos;
glVertexAttribPointer(
1, sizeof(glm::vec2) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
assertNoGLError();
glEnableVertexAttribArray(1);
assertNoGLError();
}
void Mesh::disposeBuffers() {
glBindBuffer(GL_ARRAY_BUFFER, 0);
assertNoGLError();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
assertNoGLError();
if(this->vertexBuffer != -1) {
glDeleteBuffers(1, &this->vertexBuffer);
assertNoGLError();
this->vertexBuffer = -1;
this->verticeCount = -1;
}
if(this->indexBuffer != -1) {
glDeleteBuffers(1, &this->indexBuffer);
assertNoGLError();
this->indexBuffer = -1;
this->indiceCount = -1;
}
if(this->vertexArray) {
glDeleteVertexArrays(1, &this->vertexArray);
assertNoGLError();
this->vertexArray = -1;
}
}
void Mesh::bufferPositions(int32_t pos, glm::vec3 *positions, int32_t len) {
assertNotNull(positions, "Positions cannot be null");
assertTrue(pos >= 0 && pos < verticeCount, "Position must be within range");
assertTrue(pos+len <= verticeCount, "Position + Length must be within range");
assertTrue(len > 0, "Length must be greater than zero");
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
assertNoGLError();
glBufferSubData(
GL_ARRAY_BUFFER,
sizeof(glm::vec3) * pos,
sizeof(glm::vec3) * len,
(void*)positions
);
assertNoGLError();
}
void Mesh::bufferCoordinates(int32_t pos, glm::vec2 *coordinates, int32_t len) {
assertNotNull(coordinates, "Coordinates cannot be null");
assertTrue(pos >= 0 && pos < verticeCount, "Position must be within range");
assertTrue(pos+len <= verticeCount, "Position + Length must be within range");
assertTrue(len > 0, "Length must be greater than zero");
auto offsetCoordinates = (
(sizeof(glm::vec3) * this->verticeCount) +
(sizeof(glm::vec2) * pos)
);
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
assertNoGLError();
glBufferSubData(
GL_ARRAY_BUFFER,
offsetCoordinates,
sizeof(glm::vec2) * len,
(void*)coordinates
);
assertNoGLError();
}
void Mesh::bufferIndices(int32_t pos, meshindice_t *indices, int32_t len) {
assertNotNull(indices, "Indices cannot be null");
assertTrue(pos >= 0 && pos < indiceCount, "Position must be within range");
assertTrue(pos+len <= indiceCount, "Position + Length must be within range");
assertTrue(len > 0, "Length must be greater than zero");
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
assertNoGLError();
glBufferSubData(
GL_ELEMENT_ARRAY_BUFFER,
sizeof(meshindice_t) * pos,
sizeof(meshindice_t) * len,
(void*)indices
);
assertNoGLError();
}
void Mesh::draw(enum MeshDrawMode drawMode, int32_t start, int32_t count) {
if(
count == 0 ||
this->vertexBuffer == -1 ||
this->indexBuffer == -1
) return;
if(count == -1) count = this->indiceCount;
// Re-Bind the buffers
glBindVertexArray(this->vertexArray);
assertNoGLError();
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
assertNoGLError();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
assertNoGLError();
// Re-Calculate the attrib pointers.
size_t offset = 0;
glVertexAttribPointer(
0, sizeof(glm::vec3) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
assertNoGLError();
glEnableVertexAttribArray(0);
assertNoGLError();
offset += sizeof(glm::vec3) * this->verticeCount;
glVertexAttribPointer(
1, sizeof(glm::vec2) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
assertNoGLError();
glEnableVertexAttribArray(1);
assertNoGLError();
// Render the elements.
glDrawElements(
drawMode, count, GL_UNSIGNED_INT, (void *)(sizeof(meshindice_t) * start)
);
assertNoGLError();
}
Mesh::~Mesh() {
this->disposeBuffers();
}

View File

@ -1,95 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "assert/assertgl.hpp"
#include "dawnopengl.hpp"
#include "assert/assert.hpp"
/** Indice that references a specific vertice */
typedef int32_t meshindice_t;
namespace Dawn {
enum MeshDrawMode {
MESH_DRAW_MODE_TRIANGLES = GL_TRIANGLES,
MESH_DRAW_MODE_LINES = GL_LINES
};
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;
/** Pointer to the vertex buffer on the GPU */
GLuint vertexArray = -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.
*
* @param pos Position, within the buffer, to write to.
* @param vertices Array of positions to write.
* @param len How many positions are in the array.
*/
void bufferPositions(int32_t pos, glm::vec3 *positions, int32_t len);
/**
* Write vertice coordinates to the mesh.
*
* @param pos Position, within the buffer, to write to.
* @param coordinates Array of coordinates to write.
* @param len How many coordinates are in the array.
*/
void bufferCoordinates(int32_t pos, glm::vec2 *coordinates, int32_t len);
/**
* Write indices to the mesh.
*
* @param pos Position, within the buffer, to write to.
* @param indices Array of indices to write.
* @param len How many indices are in the array.
*/
void bufferIndices(int32_t pos, meshindice_t *indices, int32_t len);
/**
* 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();
};
}