add plane and some randomness

This commit is contained in:
ktyl 2021-08-08 16:47:25 +01:00
parent 3b7d413390
commit c848616928
9 changed files with 77 additions and 13 deletions

View File

@ -20,12 +20,12 @@ const int SPHERES = 250; // 253 is the maximum?? TODO: use uniform buffer object
layout (location = 12) uniform int _activeSpheres; layout (location = 12) uniform int _activeSpheres;
layout (location = 13) uniform Sphere _spheres[SPHERES]; layout (location = 13) uniform Sphere _spheres[SPHERES];
uniform float _seed; uniform vec4 _seed;
layout(local_size_x = 1, local_size_y = 1) in; // size of local work group - 1 pixel layout(local_size_x = 1, local_size_y = 1) in; // size of local work group - 1 pixel
layout(rgba32f, binding = 0) uniform image2D img_output; // rgba32f defines internal format, image2d for random write to output texture layout(rgba32f, binding = 0) uniform image2D img_output; // rgba32f defines internal format, image2d for random write to output texture
const float INF = 30.0; const float INF = 1000.0;
const float PI = 3.14159; const float PI = 3.14159;
struct Ray struct Ray
@ -86,6 +86,24 @@ void intersectSphere(Ray ray, inout RayHit bestHit, Sphere sphere)
} }
} }
void intersectPlane(Ray ray, inout RayHit bestHit, vec3 p, vec3 normal)
{
//normal = vec3(0.0,0.0,1.0);
float denom = dot(normal, ray.direction);
if (abs(denom) > 0.0001)
{
float t = dot(p-ray.origin, normal) / denom;
if (t >= 0 && t < bestHit.distance)
{
bestHit.distance = t;
bestHit.position = ray.origin + t*ray.direction;
bestHit.normal = normal;
bestHit.albedo = vec3(1.0,1.0,1.0);
}
}
}
Ray createCameraRay(vec2 uv) Ray createCameraRay(vec2 uv)
{ {
// transform -1..1 -> 0..1 // transform -1..1 -> 0..1
@ -110,16 +128,29 @@ RayHit trace(Ray ray)
// TODO: intersect something other than spheres // TODO: intersect something other than spheres
Sphere s;
s.cr = vec4(0.0,0.0,0.0,2.0);
s.albedo = vec3(1.0,0.0,0.0);
intersectPlane(ray, hit, vec3(0.0,-1.5,0.0),vec3(0.0,1.0,0.0));
for (int i = 0; i < _activeSpheres; i++) for (int i = 0; i < _activeSpheres; i++)
{ {
intersectSphere(ray, hit, _spheres[i]); intersectSphere(ray, hit, _spheres[i]);
} }
//intersectSphere(ray, hit, s);
return hit; return hit;
} }
float random(vec2 st) float random(vec2 st)
{ {
st += gl_GlobalInvocationID.xy;
st += _seed.xy;
st += _seed.zw;
normalize(st);
return fract(sin(dot(st.xy,vec2(12.9898,78.233)))*43758.5453123); return fract(sin(dot(st.xy,vec2(12.9898,78.233)))*43758.5453123);
} }
@ -144,10 +175,11 @@ vec3 sampleHemisphere(vec3 normal)
{ {
float cosTheta = random(normal.xy); float cosTheta = random(normal.xy);
float sinTheta = sqrt(max(0.0,1.0-cosTheta*cosTheta)); float sinTheta = sqrt(max(0.0,1.0-cosTheta*cosTheta));
float phi = 2.0*PI*random(vec2(cosTheta,sinTheta));
float phi = 2.0*PI*random(normal.yx);
vec3 tangentSpaceDir = vec3(cos(phi)*sinTheta, sin(phi)*sinTheta, cosTheta); vec3 tangentSpaceDir = vec3(cos(phi)*sinTheta, sin(phi)*sinTheta, cosTheta);
// TODO: this lookin sketch af rn // convert direction from tangent space to world space
mat3 ts = getTangentSpace(normal); mat3 ts = getTangentSpace(normal);
return ts* tangentSpaceDir; return ts* tangentSpaceDir;
} }
@ -186,8 +218,8 @@ void main()
uv.x = (float(pixel_coords.x * 2 - dims.x) / dims.x) * dims.x/dims.y; // account for aspect ratio uv.x = (float(pixel_coords.x * 2 - dims.x) / dims.x) * dims.x/dims.y; // account for aspect ratio
uv.y = (float(pixel_coords.y * 2 - dims.y) / dims.y); uv.y = (float(pixel_coords.y * 2 - dims.y) / dims.y);
int samples = 1; int samples = 2;
int bounces = 2; int bounces = 3;
for (int i = 0; i < samples; i++) for (int i = 0; i < samples; i++)
{ {
@ -200,10 +232,12 @@ void main()
RayHit hit = trace(ray); RayHit hit = trace(ray);
pixel.xyz += ray.energy * shade(ray, hit); pixel.xyz += ray.energy * shade(ray, hit);
if (length(ray.energy) < 0.001) break;
} }
} }
pixel /= samples; pixel.xyz /= samples;
// TODO: write depth to texture // TODO: write depth to texture
//float depth = hit.distance/INF; //float depth = hit.distance/INF;

View File

@ -4,6 +4,6 @@
#include <cglm/cam.h> #include <cglm/cam.h>
#include "time.h" #include "clock.h"
void updateCameraUniforms(GLuint shaderProgram, float aspect); void updateCameraUniforms(GLuint shaderProgram, float aspect);

View File

@ -1,4 +1,4 @@
#include "time.h" #include "clock.h"
float now() float now()
{ {

View File

@ -1,6 +1,7 @@
#include "main.h" #include "main.h"
#include "gfx.h" #include "gfx.h"
#include "time.h" #include "clock.h"
#include "random.h"
#include "sphere.h" #include "sphere.h"
#include "cam.h" #include "cam.h"
@ -59,6 +60,17 @@ int main()
void updateUniforms(GLuint shaderProgram) void updateUniforms(GLuint shaderProgram)
{ {
// update random values
vec4 seed =
{
randomFloat(),
randomFloat(),
randomFloat(),
randomFloat()
};
int loc = glGetUniformLocation(shaderProgram, "_seed");
glUniform4fv(loc, 1, seed);
float t = now(); float t = now();
float sin_t = sin(t); float sin_t = sin(t);
int tLocation = glGetUniformLocation(shaderProgram, "_t"); int tLocation = glGetUniformLocation(shaderProgram, "_t");

11
src/random.c Normal file
View File

@ -0,0 +1,11 @@
#include "random.h"
void randomInit()
{
srand(time(NULL));
}
float randomFloat()
{
return (float)rand()/(float)RAND_MAX;
}

7
src/random.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include <stdlib.h>
#include <time.h>
void randomInit();
float randomFloat();

View File

@ -26,7 +26,7 @@ void makeSpheres(struct Sphere *spheres, int count)
{ {
x = 2.0*CGLM_PI * (float)i/(float)count; x = 2.0*CGLM_PI * (float)i/(float)count;
sc[0] = sin(x)*d; sc[0] = sin(x)*d;
sc[1] = sin(x*3.0-2.5*sin(t)); sc[1] = sin(x*3.0-t);
sc[2] = cos(x)*d; sc[2] = cos(x)*d;
float ic = i/(float)count*CGLM_PI*2.0; float ic = i/(float)count*CGLM_PI*2.0;

View File

@ -7,7 +7,7 @@
#include <cglm/vec3.h> #include <cglm/vec3.h>
#include "time.h" #include "clock.h"
struct Sphere struct Sphere
{ {