christmas noise
This commit is contained in:
parent
089458476f
commit
3a0ccfdbe8
48
src/gfx.c
48
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,19 +324,17 @@ GLuint createBlueNoiseTexture(int width, int height)
|
|||
|
||||
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
|
||||
int m = 100;
|
||||
int n = 100;
|
||||
vec2 points[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++)
|
||||
{
|
||||
int pixelIdx = i;
|
||||
|
@ -328,15 +344,21 @@ GLuint createBlueNoiseTexture(int width, int height)
|
|||
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++)
|
||||
{
|
||||
float d = glm_vec2_distance(points[j], pixel);
|
||||
//value += 1.0f / (d*d);
|
||||
|
||||
float r = 0.01;
|
||||
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);
|
||||
|
|
Loading…
Reference in New Issue