Updated compute raytrace example. Pass scene primitives via SSBOs, shader tweaks, etc.
This commit is contained in:
parent
5862dc0479
commit
eaf76fd6e7
6 changed files with 310 additions and 133 deletions
|
|
@ -10,16 +10,16 @@ layout (binding = 0, rgba8) uniform writeonly image2D resultImage;
|
|||
|
||||
#define EPSILON 0.0001
|
||||
#define MAXLEN 1000.0
|
||||
#define PLANEID 1
|
||||
#define SPHERECOUNT 3
|
||||
#define SHADOW 0.5
|
||||
#define RAYBOUNCES 1
|
||||
#define RAYBOUNCES 2
|
||||
#define REFLECTIONSTRENGTH 0.4
|
||||
#define REFLECTIONFALLOFF 0.5
|
||||
|
||||
struct Camera {
|
||||
vec3 pos;
|
||||
vec3 lookat;
|
||||
float fov;
|
||||
struct Camera
|
||||
{
|
||||
vec3 pos;
|
||||
vec3 lookat;
|
||||
float fov;
|
||||
};
|
||||
|
||||
layout (binding = 1) uniform UBO
|
||||
|
|
@ -31,50 +31,60 @@ layout (binding = 1) uniform UBO
|
|||
mat4 rotMat;
|
||||
} ubo;
|
||||
|
||||
struct Sphere
|
||||
{
|
||||
vec3 pos;
|
||||
float radius;
|
||||
vec3 diffuse;
|
||||
float specular;
|
||||
int id;
|
||||
};
|
||||
|
||||
struct Plane
|
||||
{
|
||||
vec3 normal;
|
||||
float distance;
|
||||
vec3 diffuse;
|
||||
float specular;
|
||||
int id;
|
||||
};
|
||||
|
||||
layout (std140, binding = 2) buffer Spheres
|
||||
{
|
||||
Sphere spheres[ ];
|
||||
};
|
||||
|
||||
layout (std140, binding = 3) buffer Planes
|
||||
{
|
||||
Plane planes[ ];
|
||||
};
|
||||
|
||||
void reflectRay(inout vec3 rayD, in vec3 mormal)
|
||||
{
|
||||
rayD = rayD + 2.0 * -dot(mormal, rayD) * mormal;
|
||||
rayD = rayD + 2.0 * -dot(mormal, rayD) * mormal;
|
||||
}
|
||||
|
||||
// Lighting calculations
|
||||
// Lighting =========================================================
|
||||
|
||||
float lightDiffuse(vec3 normal, vec3 lightDir)
|
||||
{
|
||||
return clamp(dot(normal, lightDir), 0.0, 1.0);
|
||||
return clamp(dot(normal, lightDir), 0.25, 1.0);
|
||||
}
|
||||
|
||||
float lightSpecular(vec3 normal, vec3 lightDir)
|
||||
float lightSpecular(vec3 normal, vec3 lightDir, float specularFactor)
|
||||
{
|
||||
vec3 viewVec = normalize(ubo.camera.pos);
|
||||
vec3 halfVec = normalize(lightDir + viewVec);
|
||||
return pow(clamp(dot(normal, halfVec), 0.0, 1.0), 32.0);
|
||||
vec3 viewVec = normalize(ubo.camera.pos);
|
||||
vec3 halfVec = normalize(lightDir + viewVec);
|
||||
return pow(clamp(dot(normal, halfVec), 0.0, 1.0), specularFactor);
|
||||
}
|
||||
|
||||
// Primitives
|
||||
|
||||
// Basic material description
|
||||
struct Material
|
||||
{
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
};
|
||||
|
||||
// Sphere
|
||||
struct Sphere
|
||||
{
|
||||
int id;
|
||||
vec3 pos;
|
||||
float r;
|
||||
Material material;
|
||||
} sphere;
|
||||
|
||||
Sphere spheres[SPHERECOUNT];
|
||||
// Sphere ===========================================================
|
||||
|
||||
float sphereIntersect(in vec3 rayO, in vec3 rayD, in Sphere sphere)
|
||||
{
|
||||
vec3 oc = rayO - sphere.pos;
|
||||
float b = 2.0 * dot(oc, rayD);
|
||||
float c = dot(oc, oc) - sphere.r*sphere.r;
|
||||
float c = dot(oc, oc) - sphere.radius*sphere.radius;
|
||||
float h = b*b - 4.0*c;
|
||||
if (h < 0.0)
|
||||
{
|
||||
|
|
@ -86,51 +96,58 @@ float sphereIntersect(in vec3 rayO, in vec3 rayD, in Sphere sphere)
|
|||
|
||||
vec3 sphereNormal(in vec3 pos, in Sphere sphere)
|
||||
{
|
||||
return (pos - sphere.pos) / sphere.r;
|
||||
return (pos - sphere.pos) / sphere.radius;
|
||||
}
|
||||
|
||||
// Plane
|
||||
// Plane ===========================================================
|
||||
|
||||
float planeIntersect(vec3 rayO, vec3 rayD)
|
||||
float planeIntersect(vec3 rayO, vec3 rayD, Plane plane)
|
||||
{
|
||||
return -rayO.y/rayD.y;
|
||||
float d = dot(rayD, plane.normal);
|
||||
|
||||
if (d == 0.0)
|
||||
return 0.0;
|
||||
|
||||
float t = -(plane.distance + dot(rayO, plane.normal)) / d;
|
||||
|
||||
if (t < 0.0)
|
||||
return 0.0;
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
vec3 planeNormal(in vec3 pos)
|
||||
{
|
||||
return vec3(0.0, 1.0, 0.0);
|
||||
}
|
||||
|
||||
int intersect(in vec3 rayO, in vec3 rayD, out float resT)
|
||||
int intersect(in vec3 rayO, in vec3 rayD, inout float resT)
|
||||
{
|
||||
int id = -1;
|
||||
resT = MAXLEN;
|
||||
|
||||
for (int i = 0; i < SPHERECOUNT; i++)
|
||||
for (int i = 0; i < spheres.length(); i++)
|
||||
{
|
||||
float tSphere = sphereIntersect(rayO, rayD, spheres[i]);
|
||||
if (tSphere > EPSILON)
|
||||
if ((tSphere > EPSILON) && (tSphere < resT))
|
||||
{
|
||||
id = spheres[i].id;
|
||||
resT = tSphere;
|
||||
return id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
float tplane = planeIntersect(rayO, rayD);
|
||||
if ((tplane > EPSILON) && (tplane < resT))
|
||||
|
||||
for (int i = 0; i < planes.length(); i++)
|
||||
{
|
||||
id = PLANEID;
|
||||
resT = tplane;
|
||||
}
|
||||
float tplane = planeIntersect(rayO, rayD, planes[i]);
|
||||
if ((tplane > EPSILON) && (tplane < resT))
|
||||
{
|
||||
id = planes[i].id;
|
||||
resT = tplane;
|
||||
}
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
float calcShadow(in vec3 rayO, in vec3 rayD, in int id)
|
||||
{
|
||||
for (int i = 0; i < SPHERECOUNT; i++)
|
||||
//todo: avoid backprojection
|
||||
for (int i = 0; i < spheres.length(); i++)
|
||||
{
|
||||
float tSphere = sphereIntersect(rayO, rayD, spheres[i]);
|
||||
if (tSphere > EPSILON)
|
||||
|
|
@ -143,13 +160,13 @@ float calcShadow(in vec3 rayO, in vec3 rayD, in int id)
|
|||
|
||||
vec3 fog(in float t, in vec3 color)
|
||||
{
|
||||
return mix(color, ubo.fogColor.rgb, clamp(sqrt(t*t)/20.0, 0.0, 1.0));
|
||||
return mix(color, ubo.fogColor.rgb, clamp(sqrt(t*t)/20.0, 0.0, 1.0));
|
||||
}
|
||||
|
||||
vec3 renderScene(inout vec3 rayO, inout vec3 rayD, inout int id)
|
||||
{
|
||||
vec3 color = vec3(0.0);
|
||||
float t = 0.0;
|
||||
float t = MAXLEN;
|
||||
|
||||
// Get intersected object ID
|
||||
int objectID = intersect(rayO, rayD, t);
|
||||
|
|
@ -162,64 +179,53 @@ vec3 renderScene(inout vec3 rayO, inout vec3 rayD, inout int id)
|
|||
vec3 pos = rayO + t * rayD;
|
||||
vec3 lightVec = normalize(ubo.lightPos - pos);
|
||||
vec3 normal;
|
||||
|
||||
if (objectID == PLANEID)
|
||||
|
||||
// Planes
|
||||
|
||||
// Spheres
|
||||
|
||||
for (int i = 0; i < planes.length(); i++)
|
||||
{
|
||||
normal = planeNormal(pos);
|
||||
float diffuse = clamp(dot(normal, lightVec), 0.0, 1.0);
|
||||
color = vec3(1.0, 1.0, 1.0) * diffuse;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < SPHERECOUNT; i++)
|
||||
if (objectID == planes[i].id)
|
||||
{
|
||||
if (objectID == spheres[i].id)
|
||||
{
|
||||
normal = sphereNormal(pos, spheres[i]);
|
||||
float diffuse = lightDiffuse(normal, lightVec);
|
||||
float specular = lightSpecular(normal, lightVec);
|
||||
color = diffuse * spheres[i].material.diffuse + specular * spheres[i].material.specular;
|
||||
}
|
||||
normal = planes[i].normal;
|
||||
float diffuse = lightDiffuse(normal, lightVec);
|
||||
float specular = lightSpecular(normal, lightVec, planes[i].specular);
|
||||
color = diffuse * planes[i].diffuse + specular;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < spheres.length(); i++)
|
||||
{
|
||||
if (objectID == spheres[i].id)
|
||||
{
|
||||
normal = sphereNormal(pos, spheres[i]);
|
||||
float diffuse = lightDiffuse(normal, lightVec);
|
||||
float specular = lightSpecular(normal, lightVec, spheres[i].specular);
|
||||
color = diffuse * spheres[i].diffuse.rgb + specular;
|
||||
}
|
||||
}
|
||||
|
||||
if (id == -1)
|
||||
return color;
|
||||
|
||||
id = objectID;
|
||||
|
||||
// Shadows
|
||||
color *= calcShadow(pos, lightVec, objectID);
|
||||
color *= calcShadow(pos, lightVec, objectID);
|
||||
|
||||
// Fog
|
||||
color = fog(t, color);
|
||||
|
||||
// Reflect ray for next render pass
|
||||
reflectRay(rayD, normal);
|
||||
rayO = pos;
|
||||
rayO = pos;
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
// Scene setup
|
||||
// todo : from ubo
|
||||
spheres[0].id = 2;
|
||||
spheres[0].pos = vec3(-2.25, 1.0, 0.0);
|
||||
spheres[0].r = 1.0;
|
||||
spheres[0].material.diffuse = vec3(1.0, 0.0, 0.0);
|
||||
spheres[0].material.specular = vec3(2.0);
|
||||
|
||||
spheres[1].id = 3;
|
||||
spheres[1].pos = vec3(0.0, 2.5, 0.0);
|
||||
spheres[1].r = 1.0;
|
||||
spheres[1].material.diffuse = vec3(0.0, 0.0, 1.0);
|
||||
spheres[1].material.specular = vec3(2.0);
|
||||
|
||||
spheres[2].id = 4;
|
||||
spheres[2].pos = vec3(2.25, 1.0, 0.0);
|
||||
spheres[2].r = 1.0;
|
||||
spheres[2].material.diffuse = vec3(0.0, 1.0, 0.0);
|
||||
spheres[2].material.specular = vec3(2.0);
|
||||
|
||||
ivec2 dim = imageSize(resultImage);
|
||||
vec2 uv = vec2(gl_GlobalInvocationID.xy) / dim;
|
||||
|
||||
|
|
@ -230,14 +236,16 @@ void main()
|
|||
int id = 0;
|
||||
vec3 finalColor = renderScene(rayO, rayD, id);
|
||||
|
||||
bool reflections = true;
|
||||
const bool reflections = true;
|
||||
// Reflection
|
||||
if (reflections)
|
||||
{
|
||||
float reflectionStrength = REFLECTIONSTRENGTH;
|
||||
for (int i = 0; i < RAYBOUNCES; i++)
|
||||
{
|
||||
vec3 reflectionColor = renderScene(rayO, rayD, id);
|
||||
finalColor = (1.0 - REFLECTIONSTRENGTH) * finalColor + REFLECTIONSTRENGTH * mix(reflectionColor, finalColor, 1.0 - REFLECTIONSTRENGTH);
|
||||
finalColor = (1.0 - reflectionStrength) * finalColor + reflectionStrength * mix(reflectionColor, finalColor, 1.0 - reflectionStrength);
|
||||
reflectionStrength *= REFLECTIONFALLOFF;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue