From a199d4189045f96ebdd9b8d5f8cb67b25f960a72 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 9 Sep 2026 13:36:53 -0400 Subject: [PATCH] Have 100 cubes flying around in a circle with refraction. --- 27-instancing/config.json | 4 ++++ .../shaders/27-framebuffers.frag.glsl | 10 +++++++-- .../shaders/27-framebuffers.vert.glsl | 5 ----- 27-instancing/src/config.hpp | 2 +- 27-instancing/src/main.cpp | 1 + 27-instancing/src/scene.cpp | 21 ++++++++++++------- 27-instancing/src/scene.hpp | 1 + 7 files changed, 29 insertions(+), 15 deletions(-) diff --git a/27-instancing/config.json b/27-instancing/config.json index 0f7bfeb..367ff25 100644 --- a/27-instancing/config.json +++ b/27-instancing/config.json @@ -58,6 +58,10 @@ "window": { "directory": "assets", "model_path": "window.glb" + }, + "human": { + "directory": "../assets", + "model_path": "human.glb" } }, "things": [ diff --git a/27-instancing/shaders/27-framebuffers.frag.glsl b/27-instancing/shaders/27-framebuffers.frag.glsl index 4b161b8..44b8a3d 100644 --- a/27-instancing/shaders/27-framebuffers.frag.glsl +++ b/27-instancing/shaders/27-framebuffers.frag.glsl @@ -1,11 +1,17 @@ #version 330 core out vec4 FragColor; +in vec3 Normal; +in vec3 FragPos; in vec2 TexCoords; -uniform sampler2D texture1; +uniform vec3 cameraPos; +uniform samplerCube skybox; void main() { - FragColor = texture(texture1, TexCoords); + float ratio = 1.0/1.52; + vec3 I = normalize(FragPos - cameraPos); + vec3 R = refract(I, normalize(Normal), ratio); + FragColor = vec4(texture(skybox, R).rgb, 1.0); } diff --git a/27-instancing/shaders/27-framebuffers.vert.glsl b/27-instancing/shaders/27-framebuffers.vert.glsl index c495100..f12b886 100644 --- a/27-instancing/shaders/27-framebuffers.vert.glsl +++ b/27-instancing/shaders/27-framebuffers.vert.glsl @@ -16,12 +16,7 @@ uniform vec3 offsets[10]; void main() { - // THIS WORKS - // vec3 in_offset = offsets[gl_InstanceID]; - - // 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/config.hpp b/27-instancing/src/config.hpp index ee4a201..27c5490 100644 --- a/27-instancing/src/config.hpp +++ b/27-instancing/src/config.hpp @@ -1,6 +1,6 @@ #pragma once -#define INSTANCE_COUNT 10 +#define INSTANCE_COUNT 100 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 1ee4fdd..6adcf47 100644 --- a/27-instancing/src/main.cpp +++ b/27-instancing/src/main.cpp @@ -144,6 +144,7 @@ int main() { glfwSetWindowUserPointer(window, &scene); while(!glfwWindowShouldClose(window)) { + scene.time_warp(); update(window, scene); render(window, scene); } diff --git a/27-instancing/src/scene.cpp b/27-instancing/src/scene.cpp index 0bb5c70..c2d164d 100644 --- a/27-instancing/src/scene.cpp +++ b/27-instancing/src/scene.cpp @@ -84,11 +84,14 @@ void Scene::spawn(const std::string& name, components::Position& position, compo } } -void Scene::setup_instancing() { +void Scene::time_warp() { + float time = glfwGetTime(); for(size_t i = 0; i < INSTANCE_COUNT; i++) { - translations[i].x = (float)i; - translations[i].z = (float)i; - translations[i].y = 0.0f; + auto& t = translations[i]; + float factor = (float)i / 3.0f; + t.y = cos((time + i)) * factor; + t.z = sin((time + i)) * factor; + t.x = sin((time + i)) * factor; } glGenBuffers(1, &instanceVBO); @@ -104,12 +107,16 @@ void Scene::setup_instancing() { glBindBuffer(GL_ARRAY_BUFFER, 0); glVertexAttribDivisor(3, 1); } +} - // this works - shader.use(); +void Scene::setup_instancing() { for(size_t i = 0; i < INSTANCE_COUNT; i++) { - shader.setVec3(std::format("offsets[{}]", i), translations[i]); + translations[i].x = (float)i; + translations[i].z = (float)i; + translations[i].y = (float)i; } + + time_warp(); } void Scene::init_skybox() { diff --git a/27-instancing/src/scene.hpp b/27-instancing/src/scene.hpp index 6183569..c6ab8ee 100644 --- a/27-instancing/src/scene.hpp +++ b/27-instancing/src/scene.hpp @@ -101,4 +101,5 @@ struct Scene { void load_skybox_textures(); void render_skybox(const glm::mat4& projection); void setup_instancing(); + void time_warp(); };