Compare commits

..

7 Commits
wip ... main

Author SHA1 Message Date
ktyl ce6376df81 remove unused array 2023-03-01 00:55:31 +00:00
ktyl 16c3e46930 reduce image size 2023-03-01 00:55:21 +00:00
Cat Flynn 82fe146549 update window dimensions 2023-02-26 03:30:07 +00:00
Cat Flynn 6ca53a5329 apply gamma correction 2023-02-26 03:30:07 +00:00
Cat Flynn 892bec867c tune denoise 2023-02-26 03:30:03 +00:00
Cat Flynn 10c1daf676 adjust fog depth 2023-02-26 03:27:15 +00:00
Cat Flynn 55d9620983 add upper spheres 2023-02-26 03:26:42 +00:00
10 changed files with 53 additions and 117 deletions

View File

@ -1,10 +1,9 @@
const float INF = 20.0;
const float INF = 45.0;
const float PI = 3.14159;
const float E = 2.71828;
const int BOUNCES = 4;
const int BOUNCES = 5;
// materials
const int MAT_SKY = -1;
const int MAT_LAMBERT = 0;
const int MAT_CHROME = 1;
const int MAT_GLOW = 2;

View File

@ -12,8 +12,5 @@ float getLogarithmicDepth(float distance)
float z = distance;
// logarithmic depth
float d = log(z*pow(E,n)/f)/n;
//d = d < 0 ? 1 : d;
return d;
//return max(0,d);
return max(0,log(z*pow(E,n)/f)/n);
}

View File

@ -21,29 +21,6 @@ void intersectSphere(Ray ray, inout RayHit bestHit, Sphere sphere)
}
}
void intersectInsideOutSphere(Ray ray, inout RayHit bestHit, Sphere sphere)
{
vec3 c = sphere.cr.xyz;
float r = sphere.cr.w;
vec3 d = ray.origin-c;
float p1 = -dot(ray.direction,d);
float p2sqr = p1*p1-dot(d,d)+r*r;
if (p2sqr < 0) return;
float p2 = sqrt(p2sqr);
float t = p1-p2 > 0 ? p1-p2 : p1+p2;
if (t > 0 && t < bestHit.distance)
{
bestHit.distance = t;
bestHit.position = ray.origin + t * ray.direction;
bestHit.normal = -normalize(bestHit.position-c);
bestHit.albedo = sphere.albedo;
bestHit.material = sphere.material;
}
}
void intersectPlane(Ray ray, inout RayHit bestHit, vec3 p, vec3 normal)
{
float denom = dot(normal, ray.direction);
@ -56,7 +33,7 @@ void intersectPlane(Ray ray, inout RayHit bestHit, vec3 p, vec3 normal)
bestHit.distance = t;
bestHit.position = ray.origin + t*ray.direction;
bestHit.normal = normal;
bestHit.albedo = vec3(1.0,.8,.7);
bestHit.albedo = vec3(1.0,.4,.4);
bestHit.material = MAT_LAMBERT;
}
}

View File

@ -35,30 +35,18 @@ vec3 sampleHemisphere(vec3 normal)
vec3 scatterMetal(inout Ray ray, RayHit hit)
{
//float d = length(ray.origin - hit.position);
ray.origin = hit.position + hit.normal*0.001;
ray.direction = reflect(ray.direction,hit.normal);
ray.energy *= 0.95;
ray.energy *= 0.5;
return vec3(0.0);
}
vec3 scatterLambert(inout Ray ray, RayHit hit)
{
//float d = length(ray.origin - hit.position);
ray.origin = hit.position + hit.normal*0.001;
ray.direction = sampleHemisphere(hit.normal);
ray.energy = hit.albedo * sdot(hit.normal, ray.direction);
//ray.distance += d;
ray.energy *= hit.albedo * sdot(hit.normal, ray.direction);
return vec3(0.0);
}
vec3 scatterGlow(inout Ray ray, RayHit hit)
{
ray.origin = hit.position + hit.normal*0.001;
//ray.direction = reflect(ray.direction,hit.normal);
ray.energy = hit.albedo * 2.0;
return vec3(hit.albedo);
}

View File

@ -11,19 +11,35 @@ RayHit trace(inout Ray ray)
intersectSphere(ray, hit, _spheres[i]);
}
Sphere s;
s.cr = vec4(0.0,0.0,0.0,INF*2.0);
s.material = MAT_GLOW;
s.albedo = vec3(1.0,1.0,1.0);
intersectInsideOutSphere(ray, hit, s);
int sphereCount = 10;
for (int i = 0; i < sphereCount; i++)
{
Sphere s;
float a = i/float(sphereCount)*2.0*PI;
float d = 17.0 + cos((1.3+a)*3.0) * 3.0;
float r = 4.0 + sin(a*3.0)*2.0;
s.cr = vec4(sin(a)*d,2.0*r+cos(a*5.0),cos(a)*d, r);
s.albedo = vec3(.2);
s.material = i % 3 == 0 ? MAT_CHROME : MAT_LAMBERT;
//ray.distance += hit.material == MAT_CHROME
// ? hit.distance * length(ray.energy) * float(hit.distance < INF)
// : hit.distance * float(hit.distance < INF);
//ray.distance += hit.distance * float(hit.distance < INF);
ray.distance += hit.distance;
//ray.distance = clamp(ray.distance, 0, INF);
//ray.distance += hit.distance * length(ray.energy);
intersectSphere(ray, hit, s);
}
sphereCount = 3;
for (int i = 0; i < sphereCount; i++)
{
Sphere s;
float a = i/float(sphereCount)*2.0*PI;
float d = 5.0 + cos((5.34+a)*5.0) * 3.0;
float r = 3.0 + sin(a*2.0)*1.5;
s.cr = vec4(sin(a)*d,4.0*r+cos(a*5.0),cos(a)*d, r);
s.albedo = vec3(.2);
s.material = i % 3 == 0 ? MAT_CHROME : MAT_LAMBERT;
intersectSphere(ray, hit, s);
}
ray.distance += hit.distance * float(hit.distance < INF);
return hit;
}

View File

@ -49,9 +49,9 @@ vec4 denoise(sampler2D tex, vec2 uv, float sigma, float kSigma, float threshold)
void main()
{
float sigma = 2.2;
float kSigma = 10.0;
float threshold = 0.2;
float sigma = 2.5;
float kSigma = 7.0;
float threshold = 0.3;
FragColor = denoise(ourTexture, TexCoord, sigma, kSigma, threshold);
//FragColor = texture(ourTexture, TexCoord);

View File

@ -9,8 +9,7 @@ layout(rgba32f, binding = 3) readonly uniform image2D _g1;
// final output
layout(rgba32f, binding = 0) uniform image2D img_output; // rgba32f defines internal format, image2d for random write to output texture
//uniform vec3 _skyColor = vec3(0.75,0.9,1.0);
uniform vec3 _skyColor = vec3(1.0,1.0,1.0);
uniform vec3 _skyColor = vec3(0.68,0.85,0.9);
// TODO: some of these depend on each other!! need be be in this order for now c:
#include func.glsl
@ -40,23 +39,16 @@ vec3 shade(inout Ray ray, RayHit hit)
case MAT_CHROME:
return scatterMetal(ray, hit);
break;
case MAT_GLOW:
return scatterGlow(ray, hit);
break;
}
}
// sky color
return _skyColor;
//ray.distance = INF;
//return _skyColor * (1-getLinearDepth(ray.distance));
//return vec3(1.0,0.0,0.0);
}
void main()
{
// base pixel colour for the image
//vec4 pixel = vec4(0.0, 0.0, 0.0, 1.0);
vec4 pixel = vec4(0.0, 0.0, 0.0, 1.0);
ivec2 pixelCoords = ivec2(gl_GlobalInvocationID.xy);
@ -82,14 +74,12 @@ void main()
firstHit.normal = normal;
firstHit.albedo = albedo;
int bounces = BOUNCES;
//pixel.xyz = mix(pixel.xyz, _skyColor, sky);
int sky = depth >= INF ? 1 : 0;
int bounces = (1-sky) * BOUNCES;
pixel.xyz = mix(pixel.xyz, _skyColor, sky);
// sample
int samples = 2;
//int reflections = 0;
//depth = 0;
float tracedDepth = 0;
for (int i = 0; i < samples; i++)
{
float sampleDepth = 0;
@ -99,44 +89,24 @@ void main()
{
RayHit hit = trace(ray);
//ray.distance = hit.material == MAT_CHROME
// ? ray.distance * hit.material
// : 0;
//int reflection = hit.material == MAT_CHROME ? 1 : 0;
//reflections |= (reflection << j);
sampleDepth = getLogarithmicDepth(ray.distance);
depth = getLogarithmicDepth(ray.distance);
pixel.xyz += ray.energy * shade(ray, hit);
if (length(ray.energy) < 0.001) break;
}
//depth = max(getLogarithmicDepth(ray.distance
tracedDepth += sampleDepth / float(samples);
depth += sampleDepth / float(samples);
}
// apply gamma correction
float gamma = 2.0;
float scale = 1.0 / gamma;
pixel.xyz = sqrt(pixel.xyz * scale);
pixel.xyz /= 2.0;
//
// apply fog
//float fogDepth = (clamp(tracedDepth - depth, 0, 1) - 1.0) * 2.0;
//fogDepth *= depth * depth;
//fogDepth -= depth;
//fogDepth = depth * tracedDepth;
vec3 fogColor = _skyColor;
pixel.xyz = mix(pixel.xyz, fogColor, depth);
//pixel = clamp(pixel, 0, 1);
// include the first sample we took
samples++;
// gamma correction
float scale = 1.0 / samples;
pixel.xyz = sqrt(scale * pixel.xyz);
//tracedDepth = max(depth,
//tracedDepth -= 0.2;
//tracedDepth *= (5.0/4.0);
vec3 depthDebugColor = mix(vec3(depth), vec3(fogDepth), step(-uv.x, 0.0));
pixel.xyz = mix(pixel.xyz, depthDebugColor, 1);
pixel.xyz = mix(pixel.xyz, vec3(1.0), depth);
// output to a specific pixel in the image
imageStore(img_output, ivec2(gl_GlobalInvocationID.xy), pixel);

View File

@ -44,6 +44,6 @@ void main()
imageStore(g0_output, pixelCoords, pixel);
pixel.xyz = hit.albedo;
pixel.w = hit.material;
pixel.w = 0;
imageStore(g1_output, pixelCoords, pixel);
}

View File

@ -135,7 +135,7 @@ void updateUniforms(GLuint shaderProgram, float t)
updateCameraUniforms(shaderProgram, aspect, t);
// make and update spheres
const int sphereCount = 41;
const int sphereCount = 25;
struct Sphere spheres[sphereCount];
makeSpheres(spheres, sphereCount, t);
updateSphereUniforms(shaderProgram, spheres, sphereCount);

View File

@ -2,17 +2,6 @@
void makeSpheres(struct Sphere *spheres, int count, float t)
{
vec3 albedos[] =
{
{0.0,0.0,1.0},
{0.0,1.0,0.0},
{0.0,1.0,1.0},
{1.0,0.0,0.0},
{1.0,0.0,1.0},
{1.0,1.0,0.0},
{1.0,1.0,1.0}
};
vec3 sc = {0.0,0.0,1.0};
int sphereIdx = 0;
@ -30,13 +19,13 @@ void makeSpheres(struct Sphere *spheres, int count, float t)
int rainbowSpheres = count - middleSpheres;
// distance from center
float d = 6.0;
radius = 0.5;
radius = 0.7;
float x;
for (int i = 0; i < rainbowSpheres; i++)
{
x = 2.0*CGLM_PI * (float)i/(float)rainbowSpheres;
sc[0] = sin(x)*d;
sc[1] = sin(x*3.0-5.0*sin(t));
sc[1] = radius*sin(x*3.0-5.0*sin(t));
sc[2] = cos(x)*d;
float ic = i/(float)rainbowSpheres*CGLM_PI*2.0;