Starting basic physics, nothing fancy

This commit is contained in:
2023-02-20 00:29:28 -08:00
parent 4bfec16ba7
commit 9606b4dc9b
32 changed files with 779 additions and 286 deletions

View File

@ -1,118 +1,153 @@
// 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;
// Create some buffers, one for the vertex data, one for the indices
GLuint buffer[2];
glGenBuffers(2, buffer);
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);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizePos+sizeCoords, 0, GL_DYNAMIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeInds, 0, GL_DYNAMIC_DRAW);
// Setup the attrib pointers
size_t offset = 0;
glVertexAttribPointer(
0, sizeof(glm::vec3) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
glEnableVertexAttribArray(0);
offset += sizePos;
glVertexAttribPointer(
1, sizeof(glm::vec2) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
glEnableVertexAttribArray(1);
}
void Mesh::disposeBuffers() {
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
if(this->vertexBuffer != -1) {
glDeleteBuffers(1, &this->vertexBuffer);
this->vertexBuffer = -1;
this->verticeCount = -1;
}
if(this->indexBuffer != -1) {
glDeleteBuffers(1, &this->indexBuffer);
this->indexBuffer = -1;
this->indiceCount = -1;
}
}
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
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
// Re-Calculate the attrib pointers.
size_t offset = 0;
glVertexAttribPointer(
0, sizeof(glm::vec3) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
glEnableVertexAttribArray(0);
offset += sizeof(glm::vec3) * this->verticeCount;
glVertexAttribPointer(
1, sizeof(glm::vec2) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
glEnableVertexAttribArray(1);
// Render the elements.
glDrawElements(
drawMode, count, GL_UNSIGNED_INT, (void *)(sizeof(meshindice_t) * start)
);
}
Mesh::~Mesh() {
this->disposeBuffers();
// 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;
// Create some buffers, one for the vertex data, one for the indices
GLuint buffer[2];
glGenBuffers(2, buffer);
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);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizePos+sizeCoords, 0, GL_DYNAMIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeInds, 0, GL_DYNAMIC_DRAW);
// Setup the attrib pointers
size_t offset = 0;
glVertexAttribPointer(
0, sizeof(glm::vec3) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
glEnableVertexAttribArray(0);
offset += sizePos;
glVertexAttribPointer(
1, sizeof(glm::vec2) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
glEnableVertexAttribArray(1);
}
void Mesh::disposeBuffers() {
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
if(this->vertexBuffer != -1) {
glDeleteBuffers(1, &this->vertexBuffer);
this->vertexBuffer = -1;
this->verticeCount = -1;
}
if(this->indexBuffer != -1) {
glDeleteBuffers(1, &this->indexBuffer);
this->indexBuffer = -1;
this->indiceCount = -1;
}
}
void Mesh::bufferPositions(int32_t pos, glm::vec3 *positions, int32_t len) {
assertNotNull(positions);
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
glBufferSubData(
GL_ARRAY_BUFFER,
sizeof(glm::vec3) * pos,
sizeof(glm::vec3) * len,
(void*)positions
);
}
void Mesh::bufferCoordinates(int32_t pos, glm::vec2 *coordinates, int32_t len) {
assertNotNull(coordinates);
auto offsetCoordinates = (
(sizeof(glm::vec3) * this->verticeCount) +
(sizeof(glm::vec2) * pos)
);
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
glBufferSubData(
GL_ARRAY_BUFFER,
offsetCoordinates,
sizeof(glm::vec2) * len,
(void*)coordinates
);
}
void Mesh::bufferIndices(int32_t pos, meshindice_t *indices, int32_t len) {
assertNotNull(indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
glBufferSubData(
GL_ELEMENT_ARRAY_BUFFER,
sizeof(meshindice_t) * pos,
sizeof(meshindice_t) * len,
(void*)indices
);
}
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
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
// Re-Calculate the attrib pointers.
size_t offset = 0;
glVertexAttribPointer(
0, sizeof(glm::vec3) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
glEnableVertexAttribArray(0);
offset += sizeof(glm::vec3) * this->verticeCount;
glVertexAttribPointer(
1, sizeof(glm::vec2) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
glEnableVertexAttribArray(1);
// Render the elements.
glDrawElements(
drawMode, count, GL_UNSIGNED_INT, (void *)(sizeof(meshindice_t) * start)
);
}
Mesh::~Mesh() {
this->disposeBuffers();
}

View File

@ -51,70 +51,29 @@ namespace Dawn {
/**
* 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 pos Position, within the buffer, to write to.
* @param vertices Array of positions to write.
* @param len How many positions are in the array.
*/
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()
);
}
void bufferPositions(int32_t pos, glm::vec3 *positions, int32_t len);
/**
* 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 pos Position, within the buffer, to write to.
* @param coordinates Array of coordinates to write.
* @param len How many coordinates are in the array.
*/
template<size_t N>
void bufferCoordinates(
int32_t position,
std::array<glm::vec2, N> coordinates
) {
auto offsetCoordinates = (
(sizeof(glm::vec3) * this->verticeCount) +
(sizeof(glm::vec2) * position)
);
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
glBufferSubData(
GL_ARRAY_BUFFER,
offsetCoordinates,
sizeof(coordinates),
(void *)coordinates.data()
);
}
void bufferCoordinates(int32_t pos, glm::vec2 *coordinates, int32_t len);
/**
* 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 pos Position, within the buffer, to write to.
* @param indices Array of indices to write.
* @param len How many indices are in the array.
*/
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()
);
}
void bufferIndices(int32_t pos, meshindice_t *indices, int32_t len);
/**
* Draw a primitive. Primitives are drawn by their indices.
@ -123,11 +82,7 @@ namespace Dawn {
* @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
);
void draw(enum MeshDrawMode drawMode, int32_t start, int32_t count);
/**
* Cleanup a previously initiated mesh.