Messing around with when to do glGen, solving a memory leak because of that, and rewriting Scene::spawn to use the instancing stuff.

This commit is contained in:
Zed A. Shaw 2026-09-10 15:22:21 -04:00
parent a199d41890
commit 6ffc6cb4ca
9 changed files with 23 additions and 31 deletions

View file

@ -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) {