From 6ffc6cb4cabc69b59ff6ce1dbc56f99537dab1cc Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 10 Sep 2026 15:22:21 -0400 Subject: [PATCH] Messing around with when to do glGen, solving a memory leak because of that, and rewriting Scene::spawn to use the instancing stuff. --- 27-instancing/src/config.hpp | 2 +- 27-instancing/src/main.cpp | 4 ++-- 27-instancing/src/mesh.cpp | 4 ++-- 27-instancing/src/mesh.hpp | 2 +- 27-instancing/src/model.cpp | 4 ++-- 27-instancing/src/model.hpp | 2 +- 27-instancing/src/scene.cpp | 22 +++++----------------- 27-instancing/src/scene.hpp | 8 ++++++-- 27-instancing/tests/model_tests.cpp | 6 +++--- 9 files changed, 23 insertions(+), 31 deletions(-) diff --git a/27-instancing/src/config.hpp b/27-instancing/src/config.hpp index 27c5490..ab9e01e 100644 --- a/27-instancing/src/config.hpp +++ b/27-instancing/src/config.hpp @@ -1,6 +1,6 @@ #pragma once -#define INSTANCE_COUNT 100 +#define INSTANCE_COUNT 5 const unsigned int SCR_WIDTH = 1792; const unsigned int SCR_HEIGHT = 1008; diff --git a/27-instancing/src/main.cpp b/27-instancing/src/main.cpp index 6adcf47..d3ad4ad 100644 --- a/27-instancing/src/main.cpp +++ b/27-instancing/src/main.cpp @@ -76,8 +76,8 @@ void process_input(GLFWwindow *window, Scene& scene) { } if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) { - components::Rotation rotation{90.0f, {1.0f, 0.0f, 0.0f}}; - scene.spawn("grass", scene.camera.position, rotation); + components::Rotation rotation{0.0f, {1.0f, 1.0f, 1.0f}}; + scene.spawn("marble_cube", scene.camera.position, rotation); } if(glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) { diff --git a/27-instancing/src/mesh.cpp b/27-instancing/src/mesh.cpp index 3caaa60..fabce88 100644 --- a/27-instancing/src/mesh.cpp +++ b/27-instancing/src/mesh.cpp @@ -2,13 +2,13 @@ #include "dbc.hpp" #include "config.hpp" -void Mesh::draw(Shader &shader, bool with_textures) { +void Mesh::draw(Shader &shader, bool with_textures, unsigned int count) { if(with_textures) { apply_textures(shader); } glBindVertexArray(VAO); - glDrawElementsInstanced(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0, INSTANCE_COUNT); + glDrawElementsInstanced(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0, count); glBindVertexArray(0); glActiveTexture(GL_TEXTURE0); } diff --git a/27-instancing/src/mesh.hpp b/27-instancing/src/mesh.hpp index d33095e..6392f37 100644 --- a/27-instancing/src/mesh.hpp +++ b/27-instancing/src/mesh.hpp @@ -66,7 +66,7 @@ struct Mesh { setup_mesh(); } - void draw(Shader &shader, bool with_textures); + void draw(Shader &shader, bool with_textures, unsigned int count); void setup_mesh(); void apply_textures(const Shader& shader); }; diff --git a/27-instancing/src/model.cpp b/27-instancing/src/model.cpp index 8c296bb..f58e994 100644 --- a/27-instancing/src/model.cpp +++ b/27-instancing/src/model.cpp @@ -83,9 +83,9 @@ unsigned int texture_from_file(const std::string& directory, const std::string& return textureID; } -void Model::draw(Shader &shader, bool with_textures) { +void Model::draw(Shader &shader, bool with_textures, unsigned int count) { for(auto& mesh : meshes) { - mesh.draw(shader, with_textures); + mesh.draw(shader, with_textures, count); } } diff --git a/27-instancing/src/model.hpp b/27-instancing/src/model.hpp index c81bbcd..372d764 100644 --- a/27-instancing/src/model.hpp +++ b/27-instancing/src/model.hpp @@ -32,7 +32,7 @@ struct Model { load_model(); } - void draw(Shader &shader, bool with_textures=true); + void draw(Shader &shader, bool with_textures, unsigned int count); void load_model(); void process_node(const aiScene *scene, aiNode *node); diff --git a/27-instancing/src/scene.cpp b/27-instancing/src/scene.cpp index c2d164d..580513c 100644 --- a/27-instancing/src/scene.cpp +++ b/27-instancing/src/scene.cpp @@ -33,7 +33,7 @@ void Scene::draw_thing(Shader& with_shader, Thing& thing) with_shader.setMat4("model", model); // !reflect_on turns it on? yeah weird - thing.model.draw(with_shader, !reflect_on); + thing.model.draw(with_shader, !reflect_on, translations.size()); } void Scene::render(GLFWwindow* window) { @@ -72,13 +72,7 @@ void Scene::cleanup() { void Scene::spawn(const std::string& name, components::Position& position, components::Rotation& rotation) { if(models.contains(name)) { - things.emplace_back( - name, - models.at(name), - materials.at("default"), - position, - rotation, - 1.0f); + translations.push_back(position); } else { dbc::log($F("No model named: {}", name)); } @@ -86,7 +80,7 @@ void Scene::spawn(const std::string& name, components::Position& position, compo void Scene::time_warp() { float time = glfwGetTime(); - for(size_t i = 0; i < INSTANCE_COUNT; i++) { + for(size_t i = 0; i < translations.size(); i++) { auto& t = translations[i]; float factor = (float)i / 3.0f; t.y = cos((time + i)) * factor; @@ -94,9 +88,8 @@ void Scene::time_warp() { t.x = sin((time + i)) * factor; } - glGenBuffers(1, &instanceVBO); glBindBuffer(GL_ARRAY_BUFFER, instanceVBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * INSTANCE_COUNT, &translations[0], GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * translations.size(), translations.data(), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); for(auto& mesh : things[0].model.meshes) { @@ -111,17 +104,13 @@ void Scene::time_warp() { void Scene::setup_instancing() { for(size_t i = 0; i < INSTANCE_COUNT; i++) { - translations[i].x = (float)i; - translations[i].z = (float)i; - translations[i].y = (float)i; + translations.emplace_back((float)i, (float)i, (float)i); } time_warp(); } void Scene::init_skybox() { - glGenVertexArrays(1, &skyboxVAO); - glGenBuffers(1, &skyboxVBO); glBindVertexArray(skyboxVAO); glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO); glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW); @@ -138,7 +127,6 @@ void Scene::load_skybox_textures() { int nrChannels = 0; size_t face_i = 0; - glGenTextures(1, &skybox_texture_id); glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture_id); for(const auto& face : skybox_faces) { diff --git a/27-instancing/src/scene.hpp b/27-instancing/src/scene.hpp index c6ab8ee..24fccf7 100644 --- a/27-instancing/src/scene.hpp +++ b/27-instancing/src/scene.hpp @@ -34,7 +34,6 @@ struct Scene { Screen screen; FrameBuffer framebuffer; - std::unordered_map materials; Camera camera; components::Lighting light; @@ -47,7 +46,7 @@ struct Scene { unsigned int skybox_texture_id = 0; bool reflect_on = false; - glm::vec3 translations[INSTANCE_COUNT]; + std::vector translations; unsigned int instanceVBO = 0; Scene(components::Scene& config): @@ -80,6 +79,11 @@ struct Scene { thing.scale); } + glGenVertexArrays(1, &skyboxVAO); + glGenBuffers(1, &skyboxVBO); + glGenTextures(1, &skybox_texture_id); + glGenBuffers(1, &instanceVBO); + shader.use(); shader.setInt("texture1", 0); shader.apply_lighting(light); diff --git a/27-instancing/tests/model_tests.cpp b/27-instancing/tests/model_tests.cpp index cecaca8..335e265 100644 --- a/27-instancing/tests/model_tests.cpp +++ b/27-instancing/tests/model_tests.cpp @@ -14,13 +14,13 @@ namespace model_tests { Shader shader({"shaders/14-vert.glsl", "shaders/14-frag.glsl", ""}); Model popcorn{"assets", "popcorn_model_a.glb"}; - popcorn.draw(shader); + popcorn.draw(shader, true, 1); Model crate_glb{"assets", "crate.glb"}; - crate_glb.draw(shader); + crate_glb.draw(shader, true, 1); Model crate_obj{"assets", "crate.obj"}; - crate_obj.draw(shader); + crate_obj.draw(shader, true, 1); } void test_failures() {