diff --git a/27-instancing/shaders/27-framebuffers.vert.glsl b/27-instancing/shaders/27-framebuffers.vert.glsl index 68a8bff..c495100 100644 --- a/27-instancing/shaders/27-framebuffers.vert.glsl +++ b/27-instancing/shaders/27-framebuffers.vert.glsl @@ -21,6 +21,7 @@ void main() // THIS DOES NOT, see Scene::setup_instancing and Mesh::draw vec3 in_offset = aOffset; + // in_offset.x = gl_InstanceID; vec4 offset = vec4(aPos + in_offset, 1.0); gl_Position = projection * view * model * offset; diff --git a/27-instancing/src/scene.cpp b/27-instancing/src/scene.cpp index 857c176..0bb5c70 100644 --- a/27-instancing/src/scene.cpp +++ b/27-instancing/src/scene.cpp @@ -2,6 +2,7 @@ #include "dbc.hpp" #include #include "skybox.hpp" +#include void Scene::update() { float currentFrame = glfwGetTime(); @@ -84,16 +85,10 @@ void Scene::spawn(const std::string& name, components::Position& position, compo } void Scene::setup_instancing() { - int index = 0; - float offset = 0.01f; - int z = -20; - int x = -20; - - for(auto& t : translations) { - t.x = (float)x / 2.0f + offset; - t.z = (float)z / 2.0f + offset; - z += 4; - x += 4; + for(size_t i = 0; i < INSTANCE_COUNT; i++) { + translations[i].x = (float)i; + translations[i].z = (float)i; + translations[i].y = 0.0f; } 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); glBindBuffer(GL_ARRAY_BUFFER, 0); - glEnableVertexAttribArray(3); - glBindBuffer(GL_ARRAY_BUFFER, instanceVBO); - glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void*)0); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glVertexAttribDivisor(3, 1); + for(auto& mesh : things[0].model.meshes) { + glBindVertexArray(mesh.VAO); + glEnableVertexAttribArray(3); + glBindBuffer(GL_ARRAY_BUFFER, instanceVBO); + glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void*)0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glVertexAttribDivisor(3, 1); + } // this works shader.use();