Inserted OpenGL Error checking

This commit is contained in:
2023-07-23 13:57:25 -07:00
parent 5b01eb904d
commit a94b16e23f
20 changed files with 305 additions and 132 deletions

View File

@ -23,9 +23,16 @@ void Mesh::createBuffers(
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];
@ -33,9 +40,13 @@ void Mesh::createBuffers(
// 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;
@ -44,7 +55,9 @@ void Mesh::createBuffers(
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
assertNoGLError();
glEnableVertexAttribArray(0);
assertNoGLError();
offset += sizePos;
glVertexAttribPointer(
@ -52,24 +65,36 @@ void Mesh::createBuffers(
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) {
@ -79,12 +104,14 @@ void Mesh::bufferPositions(int32_t pos, glm::vec3 *positions, int32_t len) {
assertTrue(len > 0, "Mesh::bufferPositions: Length must be greater than zero");
glBindBuffer(GL_ARRAY_BUFFER, this->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) {
@ -99,12 +126,14 @@ void Mesh::bufferCoordinates(int32_t pos, glm::vec2 *coordinates, int32_t len) {
);
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) {
@ -114,12 +143,14 @@ void Mesh::bufferIndices(int32_t pos, meshindice_t *indices, int32_t len) {
assertTrue(len > 0, "Mesh::bufferIndices: Length must be greater than zero");
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->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) {
@ -133,8 +164,12 @@ void Mesh::draw(enum MeshDrawMode drawMode, int32_t start, int32_t count) {
// 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;
@ -143,7 +178,9 @@ void Mesh::draw(enum MeshDrawMode drawMode, int32_t start, int32_t count) {
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
assertNoGLError();
glEnableVertexAttribArray(0);
assertNoGLError();
offset += sizeof(glm::vec3) * this->verticeCount;
glVertexAttribPointer(
@ -151,12 +188,15 @@ void Mesh::draw(enum MeshDrawMode drawMode, int32_t start, int32_t count) {
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() {

View File

@ -4,6 +4,7 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "assert/assertgl.hpp"
#include "display/mesh/_Mesh.hpp"
#include "dawnopengl.hpp"
#include "assert/assert.hpp"
@ -25,6 +26,8 @@ namespace Dawn {
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;