everything is chrome in the future

This commit is contained in:
ktyl 2022-01-30 22:01:02 +00:00
parent 0cb47db342
commit a79dfe064c
10 changed files with 58 additions and 31 deletions

View File

@ -1,4 +1,9 @@
const float INF = 50.0; const float INF = 50.0;
const float PI = 3.14159; const float PI = 3.14159;
const float E = 2.71828; const float E = 2.71828;
const int BOUNCES = 3; const int BOUNCES = 4;
// materials
const int MAT_SKY = -1;
const int MAT_LAMBERT = 0;
const int MAT_CHROME = 1;

View File

@ -17,12 +17,12 @@ void intersectSphere(Ray ray, inout RayHit bestHit, Sphere sphere)
bestHit.position = ray.origin + t * ray.direction; bestHit.position = ray.origin + t * ray.direction;
bestHit.normal = normalize(bestHit.position-c); bestHit.normal = normalize(bestHit.position-c);
bestHit.albedo = sphere.albedo; bestHit.albedo = sphere.albedo;
bestHit.material = sphere.material;
} }
} }
void intersectPlane(Ray ray, inout RayHit bestHit, vec3 p, vec3 normal) 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); float denom = dot(normal, ray.direction);
if (abs(denom) > 0.0001) if (abs(denom) > 0.0001)
@ -34,6 +34,7 @@ void intersectPlane(Ray ray, inout RayHit bestHit, vec3 p, vec3 normal)
bestHit.position = ray.origin + t*ray.direction; bestHit.position = ray.origin + t*ray.direction;
bestHit.normal = normal; bestHit.normal = normal;
bestHit.albedo = vec3(1.0,1.0,1.0); bestHit.albedo = vec3(1.0,1.0,1.0);
bestHit.material = MAT_LAMBERT;
} }
} }
} }

View File

@ -33,11 +33,20 @@ vec3 sampleHemisphere(vec3 normal)
} }
vec3 scatterMetal(inout Ray ray, RayHit hit)
{
ray.origin = hit.position + hit.normal*0.001;
ray.direction = reflect(ray.direction,hit.normal);
ray.energy *= 0.5;
return vec3(0.0);
}
vec3 scatterLambert(inout Ray ray, RayHit hit) vec3 scatterLambert(inout Ray ray, RayHit hit)
{ {
ray.origin = hit.position + hit.normal*0.001; ray.origin = hit.position + hit.normal*0.001;
ray.direction = sampleHemisphere(hit.normal); ray.direction = sampleHemisphere(hit.normal);
ray.energy *= 2.0 * hit.albedo * sdot(hit.normal, ray.direction); ray.energy *= hit.albedo * sdot(hit.normal, ray.direction);
return vec3(0.0); return vec3(0.0);
} }

View File

@ -21,6 +21,7 @@ struct RayHit
float distance; float distance;
vec3 normal; vec3 normal;
vec3 albedo; vec3 albedo;
int material;
}; };
RayHit createRayHit() RayHit createRayHit()
{ {

View File

@ -3,8 +3,10 @@ RayHit trace(Ray ray)
RayHit hit = createRayHit(); RayHit hit = createRayHit();
Sphere s; Sphere s;
s.cr = vec4(0.0,0.0,0.0,2.0); s.cr = vec4(0.0,0.0,0.0,5.0);
s.albedo = vec3(1.0,0.0,0.0); s.albedo = vec3(1.0,0.0,0.0);
s.material = MAT_CHROME;
intersectSphere(ray, hit, s);
intersectPlane(ray, hit, vec3(0.0,-1.5,0.0),vec3(0.0,1.0,0.0)); intersectPlane(ray, hit, vec3(0.0,-1.5,0.0),vec3(0.0,1.0,0.0));

View File

@ -3,9 +3,10 @@ struct Sphere
// (c.x,c.y,c.z,r) // (c.x,c.y,c.z,r)
vec4 cr; vec4 cr;
vec3 albedo; vec3 albedo;
int material;
}; };
// 253 is the maximum?? TODO: use uniform buffer objects // TODO: use uniform buffer objects
const int SPHERES = 250; const int SPHERES = 167;
uniform int _activeSpheres; uniform int _activeSpheres;
layout (location = 1) uniform Sphere _spheres[SPHERES]; layout (location = 1) uniform Sphere _spheres[SPHERES];

View File

@ -29,7 +29,16 @@ vec3 shade(inout Ray ray, RayHit hit)
{ {
if (hit.distance < INF) if (hit.distance < INF)
{ {
return scatterLambert(ray, hit); switch (hit.material)
{
case MAT_SKY: break;
case MAT_LAMBERT:
scatterLambert(ray, hit);
break;
case MAT_CHROME:
return scatterMetal(ray, hit);
break;
}
} }
// sky color // sky color
@ -78,15 +87,8 @@ void main()
if (length(ray.energy) < 0.001) break; if (length(ray.energy) < 0.001) break;
} }
//pixel.xyz = mix(pixel.xyz, normal, 1.0-depth);
pixel.xyz = mix(pixel.xyz, vec3(1.0), depth); pixel.xyz = mix(pixel.xyz, vec3(1.0), depth);
//pixel.xyz = mix(firstHit.albedo, pixel.xyz, depth);
//pixel.a = 1.0;
//pixel.xyz = texture(_g0, uv).xyz;
// output to a specific pixel in the image // output to a specific pixel in the image
imageStore(img_output, ivec2(gl_GlobalInvocationID.xy), pixel); imageStore(img_output, ivec2(gl_GlobalInvocationID.xy), pixel);
} }

View File

@ -38,11 +38,12 @@ void makeSpheres(struct Sphere *spheres, int count)
glm_vec3_scale(col, 0.5, col); glm_vec3_scale(col, 0.5, col);
glm_vec3_adds(col, 0.5, col); glm_vec3_adds(col, 0.5, col);
spheres[i] = makeSphere(sc,radius,col); int material = i % 2 == 0;
spheres[i] = makeSphere(sc,radius,col,material);
} }
} }
struct Sphere makeSphere(vec3 center, float radius, vec3 albedo) struct Sphere makeSphere(vec3 center, float radius, vec3 albedo, int material)
{ {
struct Sphere s; struct Sphere s;
@ -53,20 +54,22 @@ struct Sphere makeSphere(vec3 center, float radius, vec3 albedo)
glm_vec3_copy(albedo, s.albedo); glm_vec3_copy(albedo, s.albedo);
s.material = material;
return s; return s;
} }
void updateSphereUniform(GLuint shaderProgram, struct Sphere sphere) //void updateSphereUniform(GLuint shaderProgram, struct Sphere sphere)
{ //{
int scrloc, saloc; // int scrloc, saloc;
scrloc = glGetUniformLocation(shaderProgram, "_sphere.cr"); // scrloc = glGetUniformLocation(shaderProgram, "_sphere.cr");
saloc = glGetUniformLocation(shaderProgram, "_sphere.albedo"); // saloc = glGetUniformLocation(shaderProgram, "_sphere.albedo");
//
glUniform4fv(scrloc, 1, sphere.cr); // glUniform4fv(scrloc, 1, sphere.cr);
glUniform3fv(saloc, 1, sphere.albedo); // glUniform3fv(saloc, 1, sphere.albedo);
//
updateSphereUniforms(shaderProgram, &sphere, 0); // updateSphereUniforms(shaderProgram, &sphere, 0);
} //}
void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int count) void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int count)
{ {
@ -75,7 +78,7 @@ void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int coun
glUniform1i(loc, count); glUniform1i(loc, count);
// each sphere takes up two uniform locations // each sphere takes up two uniform locations
const int stride = 2; const int stride = 3;
// first location in the array // first location in the array
loc = glGetUniformLocation(shaderProgram, "_spheres[0].cr"); loc = glGetUniformLocation(shaderProgram, "_spheres[0].cr");
@ -85,5 +88,6 @@ void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int coun
struct Sphere s = spheres[i]; struct Sphere s = spheres[i];
glUniform4fv(loc+i*stride, 1, s.cr); glUniform4fv(loc+i*stride, 1, s.cr);
glUniform3fv(loc+i*stride+1, 1, s.albedo); glUniform3fv(loc+i*stride+1, 1, s.albedo);
glUniform1i( loc+i*stride+2, s.material);
} }
} }

View File

@ -13,9 +13,10 @@ struct Sphere
{ {
vec4 cr; vec4 cr;
vec3 albedo; vec3 albedo;
int material;
}; };
void makeSpheres(struct Sphere *spheres, int count); void makeSpheres(struct Sphere *spheres, int count);
struct Sphere makeSphere(vec3 center, float radius, vec3 albedo); struct Sphere makeSphere(vec3 center, float radius, vec3 albedo, int material);
void updateSphereUniform(GLuint shaderProgram, struct Sphere sphere); //void updateSphereUniform(GLuint shaderProgram, struct Sphere sphere);
void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int count); void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int count);

View File

@ -11,7 +11,8 @@
* [x] perspective * [x] perspective
* [x] camera lookat * [x] camera lookat
* [x] do a bounce * [x] do a bounce
* [ ] do a 'flect * [x] do a 'flect
* [ ] properly sample depth for reflective surfaces
* [ ] do a 'fract * [ ] do a 'fract
* [ ] depth of field * [ ] depth of field
* [ ] emission * [ ] emission