christmas noise
This commit is contained in:
parent
089458476f
commit
3a0ccfdbe8
76
src/gfx.c
76
src/gfx.c
|
@ -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,36 +324,40 @@ GLuint createBlueNoiseTexture(int width, int height)
|
||||||
|
|
||||||
float* data = (float*)malloc(length*sizeof(float));
|
float* data = (float*)malloc(length*sizeof(float));
|
||||||
|
|
||||||
// generate a bunch of points
|
// blue noise gen parameters
|
||||||
int m = 100;
|
int m = 10; // roughly controls the evenness...?
|
||||||
int n = 100;
|
int n = 100; // number of points to generate
|
||||||
vec2 points[n];
|
|
||||||
generateBlueNoisePoints(points, m, n);
|
|
||||||
|
|
||||||
// use those points to generate a texture
|
// for each channel
|
||||||
|
for (int channel = 0; channel < channels; channel++)
|
||||||
//for (int i = 0; i < length; i++)
|
|
||||||
//{
|
|
||||||
// data[i] = randomFloat();
|
|
||||||
//}
|
|
||||||
|
|
||||||
for (int i = 0; i < width*height; i++)
|
|
||||||
{
|
{
|
||||||
int pixelIdx = i;
|
// generate a bunch of points
|
||||||
int x = pixelIdx % width;
|
vec2 points[n];
|
||||||
int y = pixelIdx / width;
|
generateBlueNoisePoints(points, m, n);
|
||||||
float u = x / (float)width;
|
|
||||||
float v = y / (float)height;
|
|
||||||
vec2 pixel = {u,v};
|
|
||||||
|
|
||||||
for (int j = 0; j < n; j++)
|
for (int i = 0; i < width*height; i++)
|
||||||
{
|
{
|
||||||
float d = glm_vec2_distance(points[j], pixel);
|
int pixelIdx = i;
|
||||||
float r = 0.01;
|
int x = pixelIdx % width;
|
||||||
if (d < r)
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue