finished state for now

This commit is contained in:
Hopeless Tyromancy 2026-04-15 21:17:44 -04:00
parent 68ef854065
commit 793987409f
5 changed files with 109 additions and 5 deletions

View File

@ -21,7 +21,11 @@ const float EPS_DIST = 1e-6;
float sdf(vec3 point){ float sdf(vec3 point){
float time = time_padded.x; float time = time_padded.x;
return abs(length(point) - (point.x * point.y - point.z)); float rad = 20 * abs(sin(time));
vec3 pos = rad * vec3(
sin(time/20.0 + 3.4), cos(time/5.3), sin(time/2.3) * cos(time)
);
return length(point-pos) - log(rad * time);
} }
vec3 getnormal(vec3 p) { vec3 getnormal(vec3 p) {
@ -71,7 +75,7 @@ float t(float x, float y) { return length(texture(screen, vec2(x, y)).xyz); }
float linearize_depth(vec2 uv) { float linearize_depth(vec2 uv) {
float d = texture(depth_tex, uv).r; float d = texture(depth_tex, uv).r;
if (d >= 1.0) return 1000.0; // nothing written if (d >= 1.0) return 1000.0;
vec4 ndc = vec4((uv * 2.0 - 1.0), d * 2.0 - 1.0, 1.0); vec4 ndc = vec4((uv * 2.0 - 1.0), d * 2.0 - 1.0, 1.0);
vec4 view_pos = inv_projection * ndc; vec4 view_pos = inv_projection * ndc;
view_pos /= view_pos.w; view_pos /= view_pos.w;

View File

@ -44,6 +44,6 @@ void main() {
const mat3 sobely = mat3(-1.0, 0.0, 1.0, -2.0, 0.0, 2.0, -1.0, 0.0, 1.0); const mat3 sobely = mat3(-1.0, 0.0, 1.0, -2.0, 0.0, 2.0, -1.0, 0.0, 1.0);
out_color = texture(screen, uv) + ((t(uv.x, uv.y) > 0.01)? out_color = texture(screen, uv) + 0.01 * ((t(uv.x, uv.y) > 0.01)?
vec4(abs(f(sobelx) + f(sobely)), 0.0, 0.0, 1.0) : vec4(0.0)); vec4(abs(f(sobelx) + f(sobely)), 0.0, 0.0, 1.0) : vec4(0.0));
} }

View File

@ -19,7 +19,7 @@ void Camera::update() {
vec3 right = normalize(cross(d, up)); vec3 right = normalize(cross(d, up));
target = pos + d; target = pos + d;
float shift_speed = shifted? 100.0 : 1.0; float shift_speed = shifted? 10.0 : 1.0;
float speed = move_speed * shift_speed; float speed = move_speed * shift_speed;
if (move_forward) { pos += d * speed; target += d * speed; } if (move_forward) { pos += d * speed; target += d * speed; }

View File

@ -198,7 +198,104 @@ void Renderer::pass_post(SDL_GPUCommandBuffer* cmd, SDL_GPUTexture* swapchain, c
SDL_EndGPURenderPass(pass); SDL_EndGPURenderPass(pass);
} }
static int neighbors(const std::array<uint8_t, 256*256>& grid, int x, int y) {
int count = 0;
for (int dy = -1; dy <= 1; dy++)
for (int dx = -1; dx <= 1; dx++)
if (dx || dy)
count += grid[((y + dy + 256) & 255) * 256 + ((x + dx + 256) & 255)];
return count;
}
std::array<std::array<uint8_t, 256*256>, 2> graph;
static void gol(std::array<std::array<uint8_t, 256*256>, 2>& graph, size_t frame) {
size_t src = (frame >> 8) & 1;
size_t dst = src ^ 1;
for (int y = 0; y < 256; y++) {
for (int x = 0; x < 256; x++) {
int n = neighbors(graph[src], x, y);
bool alive = graph[src][y * 256 + x];
graph[dst][y * 256 + x] = alive ? (n == 2 || n == 3) : (n == 3);
}
}
}
static vec4 int_to_color(int i) {
uint32_t x = static_cast<uint32_t>(i);
x ^= x >> 17;
x *= 0xed5ad4bb;
x ^= x >> 11;
x *= 0xac4c1b51;
x ^= x >> 15;
x *= 0x31848bab;
x ^= x >> 14;
float r = ((x >> 0) & 0xFF) / 255.0f;
float g = ((x >> 8) & 0xFF) / 255.0f;
float b = ((x >> 16) & 0xFF) / 255.0f;
return vec4(r, g, b, 1.0f);
}
void Renderer::add_gol_layer() {
size_t layerno = frame >> 8;
if (frame == 0) {
for (size_t i = 0; i < 256 * 256; i++) {
graph[0][i] = rand() & 1;
}
}
gol(graph, frame);
size_t src = ((frame >> 8) & 1) ^ 1;
float height = layerno * 10.0f;
for (int y = 0; y < 256; y++) {
for (int x = 0; x < 256; x++) {
if (graph[src][y * 256 + x]) {
float s = 0.01f;
vec3 p = vec3(x + s * sinf((float)frame), height, y + cosf((float)frame)) * s * 3.0f;
vec3 v0 = p + vec3(-s, -s, 0);
vec3 v1 = p + vec3( s, -s, 0);
vec3 v2 = p + vec3( s, s, 0);
vec3 v3 = p + vec3(-s, s, 0);
const vec4 color = int_to_color(layerno);
const vec3 normal = vec3(0.0, 1.0, 0.0);
vertices.push_back(Vertex {v0, color, normal});
vertices.push_back(Vertex {v1, color, normal});
vertices.push_back(Vertex {v2, color, normal});
vertices.push_back(Vertex {v2, color, normal});
vertices.push_back(Vertex {v3, color, normal});
vertices.push_back(Vertex {v0, color, normal});
}
}
}
if (vert_buff) SDL_ReleaseGPUBuffer(dev, vert_buff);
SDL_GPUBufferCreateInfo info {
.usage = SDL_GPU_BUFFERUSAGE_VERTEX,
.size = static_cast<uint32_t>(vertices.size() * sizeof(Vertex)),
};
vert_buff = SDL_CreateGPUBuffer(dev, &info);
upload_buffer(dev, vert_buff, vertices.data(), info.size);
}
void Renderer::draw(const CameraUBO& ubo) { void Renderer::draw(const CameraUBO& ubo) {
if(frame % 256 == 0 && (frame >> 8) < 20) {
add_gol_layer();
}
SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(dev); SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(dev);
SDL_GPUTexture* swapchain; SDL_GPUTexture* swapchain;
@ -218,4 +315,5 @@ void Renderer::draw(const CameraUBO& ubo) {
pass_post(cmd, swapchain, ubo); pass_post(cmd, swapchain, ubo);
SDL_SubmitGPUCommandBuffer(cmd); SDL_SubmitGPUCommandBuffer(cmd);
frame++;
} }

View File

@ -19,6 +19,7 @@ struct Renderer {
SDL_GPUSampler* render_sampler = nullptr; SDL_GPUSampler* render_sampler = nullptr;
SDL_GPUSampler* depth_sampler = nullptr; SDL_GPUSampler* depth_sampler = nullptr;
uint32_t render_w = 0, render_h = 0; uint32_t render_w = 0, render_h = 0;
uint64_t frame = 0;
std::vector<Vertex> vertices; std::vector<Vertex> vertices;
@ -29,6 +30,7 @@ struct Renderer {
private: private:
bool load_obj(); bool load_obj();
void add_gol_layer();
void resize_render_texture(uint32_t w, uint32_t h); void resize_render_texture(uint32_t w, uint32_t h);
void pass_scene(SDL_GPUCommandBuffer* cmd, const CameraUBO& ubo); void pass_scene(SDL_GPUCommandBuffer* cmd, const CameraUBO& ubo);
void pass_blit(SDL_GPUCommandBuffer* cmd, const CameraUBO& ubo); void pass_blit(SDL_GPUCommandBuffer* cmd, const CameraUBO& ubo);