Have 100 cubes flying around in a circle with refraction.

This commit is contained in:
Zed A. Shaw 2026-09-09 13:36:53 -04:00
parent 1107c86036
commit a199d41890
7 changed files with 29 additions and 15 deletions

View file

@ -58,6 +58,10 @@
"window": {
"directory": "assets",
"model_path": "window.glb"
},
"human": {
"directory": "../assets",
"model_path": "human.glb"
}
},
"things": [

View file

@ -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);
}

View file

@ -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;

View file

@ -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;

View file

@ -144,6 +144,7 @@ int main() {
glfwSetWindowUserPointer(window, &scene);
while(!glfwWindowShouldClose(window)) {
scene.time_warp();
update(window, scene);
render(window, scene);
}

View file

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

View file

@ -101,4 +101,5 @@ struct Scene {
void load_skybox_textures();
void render_skybox(const glm::mat4& projection);
void setup_instancing();
void time_warp();
};