Edit some comments and use destructors.

This commit is contained in:
Fries 2024-03-31 19:16:25 -07:00
parent 6d4b70aa1c
commit 7c694988d3
2 changed files with 22 additions and 14 deletions

View file

@ -1,5 +1,7 @@
{
"clangd.arguments": [
"-header-insertion=never"
]
],
"C_Cpp.default.compileCommands": "/home/user/Documents/Code/C/opengl-learning/builddir/compile_commands.json",
"C_Cpp.default.configurationProvider": "mesonbuild.mesonbuild"
}

View file

@ -34,14 +34,19 @@ struct GlRenderObjects {
unsigned int vertexBufferObject;
unsigned int vertexArrayObject;
unsigned int elementBufferObject;
void Destroy() {
GlRenderObjects(unsigned int vbo, unsigned int vao, unsigned int ebo) {
vertexBufferObject = vbo;
vertexArrayObject = vao;
elementBufferObject = ebo;
}
~GlRenderObjects() {
glDeleteBuffers(1, &vertexBufferObject);
glDeleteVertexArrays(1, &vertexArrayObject);
glDeleteBuffers(1, &elementBufferObject);
}
};
GlRenderObjects triangle() {
GlRenderObjects* triangle() {
// An array of verticies containing data for a triangle.
float vertices[] = {
-0.5f, -0.5f, 0.0f,
@ -63,7 +68,7 @@ GlRenderObjects triangle() {
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Tell OpenGL how to process the Vertex attributes so it can pass it to the Vertex shader.
// This will pass the vertex to the location "0". We tell OpenGL the size of the array is 3 elements.
// This will pass the vertex to the location "0". We tell OpenGL the size of each vertex is 3 floats.
// We tell OpenGL that we're using floating point types.
// We tell OpenGL we don't want our data to be normalized as its already normalized.
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
@ -72,10 +77,10 @@ GlRenderObjects triangle() {
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
return {vertexBufferObject, vertexArrayObject, 0};
return new GlRenderObjects(vertexBufferObject, vertexArrayObject, 0);
}
GlRenderObjects rectangle() {
GlRenderObjects* rectangle() {
float vertices[] = {
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
@ -116,7 +121,7 @@ GlRenderObjects rectangle() {
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
return {vertexBufferObject, vertexArrayObject, elementBufferObject};
return new GlRenderObjects(vertexBufferObject, vertexArrayObject, elementBufferObject);
}
int main() {
@ -196,8 +201,8 @@ int main() {
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
GlRenderObjects triangleRenderObjs = triangle();
GlRenderObjects rectangleRenderObjs = rectangle();
GlRenderObjects* triangleRenderObjs = triangle();
GlRenderObjects* rectangleRenderObjs = rectangle();
// Create a render loop, which keeps the program open until glfw tells the loop that the window should close.
while (!glfwWindowShouldClose(window)) {
@ -215,12 +220,13 @@ int main() {
glUseProgram(shaderProgram);
if (!rectangleToggled) {
// Bind the state to use the vertexArrayObject object so OpenGL knows what to do with the verticies.
glBindVertexArray(triangleRenderObjs.vertexArrayObject);
// Draw the verticies.
glBindVertexArray(triangleRenderObjs->vertexArrayObject);
// Draw the verticies with the first index being 0 and the size of the vertex being 3 floats.
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
} else {
glBindVertexArray(rectangleRenderObjs.vertexArrayObject);
glBindVertexArray(rectangleRenderObjs->vertexArrayObject);
// Draw 6 elements (indices) that are unsigned ints.
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
@ -232,8 +238,8 @@ int main() {
}
// Clean up objects from memory.
triangleRenderObjs.Destroy();
rectangleRenderObjs.Destroy();
delete triangleRenderObjs;
delete rectangleRenderObjs;
glDeleteProgram(shaderProgram);
glfwTerminate();