From 3a0ccfdbe8539a63afadac18e03b09fea08e52ab Mon Sep 17 00:00:00 2001 From: ktyl Date: Wed, 1 Mar 2023 00:55:04 +0000 Subject: [PATCH] christmas noise --- src/gfx.c | 76 +++++++++++++++++++++++++++++++++++-------------------- src/gfx.h | 1 + 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/src/gfx.c b/src/gfx.c index 039cbdc..803ace3 100644 --- a/src/gfx.c +++ b/src/gfx.c @@ -232,7 +232,10 @@ GLuint createWhiteNoiseTexture(int width, int height) void generateBlueNoisePoints(vec2* points, int m, int n) { - vec2 firstSample = {randomFloat(), randomFloat()}; + float start = now(); + float elapsed; + int candidatesTested = 0; + struct Candidate { vec2 pos; @@ -245,7 +248,6 @@ void generateBlueNoisePoints(vec2* points, int m, int n) // generate a bunch of candidates int count = m * i; struct Candidate candidates[count]; - //int closest = -1; for (int j = 0; j < count; j++) { @@ -264,9 +266,18 @@ void generateBlueNoisePoints(vec2* points, int m, int n) if (distance < c->distance) { c->distance = distance; - //closest = k; } } + + candidatesTested += i; + + float killAfter = 10.0; + elapsed = now() - start; + if (elapsed > killAfter) + { + printf("noise generation took too long, exiting\n"); + exit(1); + } } // of our candidates, now determine which of them has the furthest @@ -284,6 +295,13 @@ void generateBlueNoisePoints(vec2* points, int m, int n) glm_vec2_copy(candidates[furthest].pos, points[i]); } + + elapsed = now() - start; + printf("generated %d blue noise (m=%d) points from %d candidates in %fs\n", + n, + m, + candidatesTested, + elapsed); } GLuint createBlueNoiseTexture(int width, int height) @@ -306,36 +324,40 @@ GLuint createBlueNoiseTexture(int width, int height) float* data = (float*)malloc(length*sizeof(float)); - // generate a bunch of points - int m = 100; - int n = 100; - vec2 points[n]; - generateBlueNoisePoints(points, m, n); + // blue noise gen parameters + int m = 10; // roughly controls the evenness...? + int n = 100; // number of points to generate - // use those points to generate a texture - - //for (int i = 0; i < length; i++) - //{ - // data[i] = randomFloat(); - //} - - for (int i = 0; i < width*height; i++) + // for each channel + for (int channel = 0; channel < channels; channel++) { - int pixelIdx = i; - int x = pixelIdx % width; - int y = pixelIdx / width; - float u = x / (float)width; - float v = y / (float)height; - vec2 pixel = {u,v}; + // generate a bunch of points + vec2 points[n]; + generateBlueNoisePoints(points, m, n); - for (int j = 0; j < n; j++) + for (int i = 0; i < width*height; i++) { - float d = glm_vec2_distance(points[j], pixel); - float r = 0.01; - if (d < r) + int pixelIdx = i; + int x = pixelIdx % width; + int y = pixelIdx / width; + float u = x / (float)width; + float v = y / (float)height; + vec2 pixel = {u,v}; + + float value = 0.0f; + int componentIdx = i * channels + channel; + for (int j = 0; j < n; j++) { - data[i * channels + 2] = 1.0; + float d = glm_vec2_distance(points[j], pixel); + //value += 1.0f / (d*d); + + float r = 0.01; + if (d < r) + { + data[componentIdx] = 1.0; + } } + //data[componentIdx] = value / (float)n; } } diff --git a/src/gfx.h b/src/gfx.h index 1c30422..b83c652 100644 --- a/src/gfx.h +++ b/src/gfx.h @@ -10,6 +10,7 @@ #include "io.h" #include "random.h" +#include "clock.h" struct Shaders {