christmas noise

This commit is contained in:
ktyl 2023-03-01 00:55:04 +00:00
parent 089458476f
commit 3a0ccfdbe8
2 changed files with 50 additions and 27 deletions

View File

@ -232,7 +232,10 @@ GLuint createWhiteNoiseTexture(int width, int height)
void generateBlueNoisePoints(vec2* points, int m, int n) void generateBlueNoisePoints(vec2* points, int m, int n)
{ {
vec2 firstSample = {randomFloat(), randomFloat()}; float start = now();
float elapsed;
int candidatesTested = 0;
struct Candidate struct Candidate
{ {
vec2 pos; vec2 pos;
@ -245,7 +248,6 @@ void generateBlueNoisePoints(vec2* points, int m, int n)
// generate a bunch of candidates // generate a bunch of candidates
int count = m * i; int count = m * i;
struct Candidate candidates[count]; struct Candidate candidates[count];
//int closest = -1;
for (int j = 0; j < count; j++) for (int j = 0; j < count; j++)
{ {
@ -264,9 +266,18 @@ void generateBlueNoisePoints(vec2* points, int m, int n)
if (distance < c->distance) if (distance < c->distance)
{ {
c->distance = 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 // 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]); 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) GLuint createBlueNoiseTexture(int width, int height)
@ -306,19 +324,17 @@ GLuint createBlueNoiseTexture(int width, int height)
float* data = (float*)malloc(length*sizeof(float)); float* data = (float*)malloc(length*sizeof(float));
// blue noise gen parameters
int m = 10; // roughly controls the evenness...?
int n = 100; // number of points to generate
// for each channel
for (int channel = 0; channel < channels; channel++)
{
// generate a bunch of points // generate a bunch of points
int m = 100;
int n = 100;
vec2 points[n]; vec2 points[n];
generateBlueNoisePoints(points, m, n); generateBlueNoisePoints(points, m, n);
// 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 (int i = 0; i < width*height; i++)
{ {
int pixelIdx = i; int pixelIdx = i;
@ -328,15 +344,21 @@ GLuint createBlueNoiseTexture(int width, int height)
float v = y / (float)height; float v = y / (float)height;
vec2 pixel = {u,v}; vec2 pixel = {u,v};
float value = 0.0f;
int componentIdx = i * channels + channel;
for (int j = 0; j < n; j++) for (int j = 0; j < n; j++)
{ {
float d = glm_vec2_distance(points[j], pixel); float d = glm_vec2_distance(points[j], pixel);
//value += 1.0f / (d*d);
float r = 0.01; float r = 0.01;
if (d < r) if (d < r)
{ {
data[i * channels + 2] = 1.0; data[componentIdx] = 1.0;
} }
} }
//data[componentIdx] = value / (float)n;
}
} }
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, data); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, data);

View File

@ -10,6 +10,7 @@
#include "io.h" #include "io.h"
#include "random.h" #include "random.h"
#include "clock.h"
struct Shaders struct Shaders
{ {