Found it! You have to bind the VAO before attaching it to the VBO. I told you it was one line of code.

This commit is contained in:
Zed A. Shaw 2026-09-09 13:06:29 -04:00
parent 753df69b4c
commit 1107c86036
2 changed files with 14 additions and 15 deletions

View file

@ -21,6 +21,7 @@ void main()
// THIS DOES NOT, see Scene::setup_instancing and Mesh::draw // THIS DOES NOT, see Scene::setup_instancing and Mesh::draw
vec3 in_offset = aOffset; vec3 in_offset = aOffset;
// in_offset.x = gl_InstanceID;
vec4 offset = vec4(aPos + in_offset, 1.0); vec4 offset = vec4(aPos + in_offset, 1.0);
gl_Position = projection * view * model * offset; gl_Position = projection * view * model * offset;

View file

@ -2,6 +2,7 @@
#include "dbc.hpp" #include "dbc.hpp"
#include <math.h> #include <math.h>
#include "skybox.hpp" #include "skybox.hpp"
#include <print>
void Scene::update() { void Scene::update() {
float currentFrame = glfwGetTime(); float currentFrame = glfwGetTime();
@ -84,16 +85,10 @@ void Scene::spawn(const std::string& name, components::Position& position, compo
} }
void Scene::setup_instancing() { void Scene::setup_instancing() {
int index = 0; for(size_t i = 0; i < INSTANCE_COUNT; i++) {
float offset = 0.01f; translations[i].x = (float)i;
int z = -20; translations[i].z = (float)i;
int x = -20; translations[i].y = 0.0f;
for(auto& t : translations) {
t.x = (float)x / 2.0f + offset;
t.z = (float)z / 2.0f + offset;
z += 4;
x += 4;
} }
glGenBuffers(1, &instanceVBO); glGenBuffers(1, &instanceVBO);
@ -101,11 +96,14 @@ void Scene::setup_instancing() {
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * INSTANCE_COUNT, &translations[0], GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * INSTANCE_COUNT, &translations[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(3); for(auto& mesh : things[0].model.meshes) {
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO); glBindVertexArray(mesh.VAO);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void*)0); glEnableVertexAttribArray(3);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
glVertexAttribDivisor(3, 1); glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glVertexAttribDivisor(3, 1);
}
// this works // this works
shader.use(); shader.use();