Move some heap code to using the stack.

This commit is contained in:
Fries 2024-04-28 14:30:18 -07:00
parent c591275bd1
commit 9801282409
2 changed files with 42 additions and 52 deletions

View file

@ -3,7 +3,6 @@
#include <iostream> #include <iostream>
#include <format> #include <format>
#include <memory>
#include "shader.hh" #include "shader.hh"
#include "utilities.hh" #include "utilities.hh"
@ -21,11 +20,7 @@ struct GlRenderObjects {
unsigned int vertexBufferObject; unsigned int vertexBufferObject;
unsigned int vertexArrayObject; unsigned int vertexArrayObject;
unsigned int elementBufferObject; unsigned int elementBufferObject;
GlRenderObjects(unsigned int vbo, unsigned int vao, unsigned int ebo) {
vertexBufferObject = vbo;
vertexArrayObject = vao;
elementBufferObject = ebo;
}
~GlRenderObjects() { ~GlRenderObjects() {
glDeleteBuffers(1, &vertexBufferObject); glDeleteBuffers(1, &vertexBufferObject);
glDeleteVertexArrays(1, &vertexArrayObject); glDeleteVertexArrays(1, &vertexArrayObject);
@ -33,7 +28,7 @@ struct GlRenderObjects {
} }
}; };
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[] = {
// positions // colors // texture coords // positions // colors // texture coords
@ -75,10 +70,10 @@ GlRenderObjects* triangle() {
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
return new GlRenderObjects(vertexBufferObject, vertexArrayObject, 0); return {vertexArrayObject, vertexArrayObject, 0};
} }
GlRenderObjects* rectangle() { GlRenderObjects rectangle() {
float vertices[] = { float vertices[] = {
// positions // colors // texture coords // positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
@ -129,12 +124,38 @@ GlRenderObjects* rectangle() {
glBindVertexArray(0); glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
return new GlRenderObjects(vertexBufferObject, vertexArrayObject, elementBufferObject); return {vertexBufferObject, vertexArrayObject, elementBufferObject};
}
void createTexture(uint* textureInt, const char* path, GLenum imageType, GLenum minFilter, GLenum magFilter) {
// Create a texture ID and assign it to the textureInt pointer.
glGenTextures(1, textureInt);
// Bind that texture to the GL_TEXTURE_2D state.
glBindTexture(GL_TEXTURE_2D, *textureInt);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
Image texture(utilities::getCurrentPath(path));
if (texture.data) {
// Read the texture data and assign it to the GL texture.
glTexImage2D(GL_TEXTURE_2D, 0, imageType, texture.width, texture.height, 0, imageType, GL_UNSIGNED_BYTE, texture.data);
// Generate a mipmap of the texture data.
glGenerateMipmap(GL_TEXTURE_2D);
} else {
std::cout << "Failed to load texture." << std::endl;
}
} }
int main() { int main() {
glfwInit(); glfwInit();
// These brackets are a RAII (Resource Acquisition Is Initialization) pattern,
// which means these brackets are its own "scope" which means stack allocated memory
// and heap allocated memory wrapped in a smart pointer like unique_ptr will be cleaned
// up after you go out of scope.
{ {
// Set OpenGL version to 3.3. // Set OpenGL version to 3.3.
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@ -166,48 +187,14 @@ int main() {
// Create a shader class. // Create a shader class.
Shader shader(std::format("{}/vertex.glsl", currentPath).c_str(), std::format("{}/fragment.glsl", currentPath).c_str()); Shader shader(std::format("{}/vertex.glsl", currentPath).c_str(), std::format("{}/fragment.glsl", currentPath).c_str());
std::unique_ptr<GlRenderObjects> triangleRenderObjs(triangle()); GlRenderObjects triangleRenderObjs = triangle();
std::unique_ptr<GlRenderObjects> rectangleRenderObjs(rectangle()); GlRenderObjects rectangleRenderObjs = rectangle();
unsigned int texture1, texture2; unsigned int texture1, texture2;
createTexture(&texture1, "container.jpg", GL_RGB, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
createTexture(&texture2, "awesomeface.png", GL_RGBA, GL_LINEAR, GL_LINEAR);
{
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Image texture(utilities::getCurrentPath("container.jpg"));
if (texture.data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture.width, texture.height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture.data);
glGenerateMipmap(GL_TEXTURE_2D);
} else {
std::cout << "Failed to load texture." << std::endl;
}
}
{
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Image texture(utilities::getCurrentPath("awesomeface.png"));
if (texture.data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.width, texture.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture.data);
glGenerateMipmap(GL_TEXTURE_2D);
} else {
std::cout << "Failed to load texture." << std::endl;
}
}
// activate the shader // activate the shader
shader.activate(); shader.activate();
@ -227,8 +214,11 @@ int main() {
// Clear the screen and use the color from the state. // Clear the screen and use the color from the state.
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
// assign texture1 to the GL_TEXTURE0 texture unit.
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1); glBindTexture(GL_TEXTURE_2D, texture1);
// assign texture1 to the GL_TEXTURE1 texture unit.
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2); glBindTexture(GL_TEXTURE_2D, texture2);
@ -236,12 +226,12 @@ int main() {
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 with the first index being 0 and the size of the vertex being 3 floats. // 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. // 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);

View file

@ -3,7 +3,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <unistd.h> #include <unistd.h>
#include <linux/limits.h> #include <limits.h>
#include <format> #include <format>
std::string utilities::getCurrentPath() { std::string utilities::getCurrentPath() {