Edit some comments and use destructors.
This commit is contained in:
parent
6d4b70aa1c
commit
7c694988d3
2 changed files with 22 additions and 14 deletions
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
|
@ -1,5 +1,7 @@
|
||||||
{
|
{
|
||||||
"clangd.arguments": [
|
"clangd.arguments": [
|
||||||
"-header-insertion=never"
|
"-header-insertion=never"
|
||||||
]
|
],
|
||||||
|
"C_Cpp.default.compileCommands": "/home/user/Documents/Code/C/opengl-learning/builddir/compile_commands.json",
|
||||||
|
"C_Cpp.default.configurationProvider": "mesonbuild.mesonbuild"
|
||||||
}
|
}
|
||||||
|
|
32
src/main.cc
32
src/main.cc
|
@ -34,14 +34,19 @@ struct GlRenderObjects {
|
||||||
unsigned int vertexBufferObject;
|
unsigned int vertexBufferObject;
|
||||||
unsigned int vertexArrayObject;
|
unsigned int vertexArrayObject;
|
||||||
unsigned int elementBufferObject;
|
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);
|
glDeleteBuffers(1, &vertexBufferObject);
|
||||||
glDeleteVertexArrays(1, &vertexArrayObject);
|
glDeleteVertexArrays(1, &vertexArrayObject);
|
||||||
glDeleteBuffers(1, &elementBufferObject);
|
glDeleteBuffers(1, &elementBufferObject);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
GlRenderObjects triangle() {
|
GlRenderObjects* triangle() {
|
||||||
// An array of verticies containing data for a triangle.
|
// An array of verticies containing data for a triangle.
|
||||||
float vertices[] = {
|
float vertices[] = {
|
||||||
-0.5f, -0.5f, 0.0f,
|
-0.5f, -0.5f, 0.0f,
|
||||||
|
@ -63,7 +68,7 @@ GlRenderObjects triangle() {
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
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.
|
// 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 that we're using floating point types.
|
||||||
// We tell OpenGL we don't want our data to be normalized as its already normalized.
|
// 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);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||||
|
@ -72,10 +77,10 @@ GlRenderObjects triangle() {
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
|
|
||||||
return {vertexBufferObject, vertexArrayObject, 0};
|
return new GlRenderObjects(vertexBufferObject, vertexArrayObject, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
GlRenderObjects rectangle() {
|
GlRenderObjects* rectangle() {
|
||||||
float vertices[] = {
|
float vertices[] = {
|
||||||
0.5f, 0.5f, 0.0f,
|
0.5f, 0.5f, 0.0f,
|
||||||
0.5f, -0.5f, 0.0f,
|
0.5f, -0.5f, 0.0f,
|
||||||
|
@ -116,7 +121,7 @@ GlRenderObjects rectangle() {
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||||
|
|
||||||
return {vertexBufferObject, vertexArrayObject, elementBufferObject};
|
return new GlRenderObjects(vertexBufferObject, vertexArrayObject, elementBufferObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
@ -196,8 +201,8 @@ int main() {
|
||||||
glDeleteShader(vertexShader);
|
glDeleteShader(vertexShader);
|
||||||
glDeleteShader(fragmentShader);
|
glDeleteShader(fragmentShader);
|
||||||
|
|
||||||
GlRenderObjects triangleRenderObjs = triangle();
|
GlRenderObjects* triangleRenderObjs = triangle();
|
||||||
GlRenderObjects rectangleRenderObjs = rectangle();
|
GlRenderObjects* rectangleRenderObjs = rectangle();
|
||||||
|
|
||||||
// Create a render loop, which keeps the program open until glfw tells the loop that the window should close.
|
// Create a render loop, which keeps the program open until glfw tells the loop that the window should close.
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
|
@ -215,12 +220,13 @@ int main() {
|
||||||
glUseProgram(shaderProgram);
|
glUseProgram(shaderProgram);
|
||||||
if (!rectangleToggled) {
|
if (!rectangleToggled) {
|
||||||
// Bind the state to use the vertexArrayObject object so OpenGL knows what to do with the verticies.
|
// Bind the state to use the vertexArrayObject object so OpenGL knows what to do with the verticies.
|
||||||
glBindVertexArray(triangleRenderObjs.vertexArrayObject);
|
glBindVertexArray(triangleRenderObjs->vertexArrayObject);
|
||||||
// Draw the verticies.
|
// Draw the verticies with the first index being 0 and the size of the vertex being 3 floats.
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
} else {
|
} else {
|
||||||
glBindVertexArray(rectangleRenderObjs.vertexArrayObject);
|
glBindVertexArray(rectangleRenderObjs->vertexArrayObject);
|
||||||
|
// Draw 6 elements (indices) that are unsigned ints.
|
||||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
@ -232,8 +238,8 @@ int main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up objects from memory.
|
// Clean up objects from memory.
|
||||||
triangleRenderObjs.Destroy();
|
delete triangleRenderObjs;
|
||||||
rectangleRenderObjs.Destroy();
|
delete rectangleRenderObjs;
|
||||||
glDeleteProgram(shaderProgram);
|
glDeleteProgram(shaderProgram);
|
||||||
|
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
|
Loading…
Reference in a new issue