Skip to content

Commit

Permalink
everything is chrome in the future
Browse files Browse the repository at this point in the history
  • Loading branch information
ktyldev committed Jan 30, 2022
1 parent 0cb47db commit a79dfe0
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 31 deletions.
7 changes: 6 additions & 1 deletion shader/include/constants.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
const float INF = 50.0;
const float PI = 3.14159;
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;
3 changes: 2 additions & 1 deletion shader/include/intersect.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ void intersectSphere(Ray ray, inout RayHit bestHit, Sphere sphere)
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)
{
//normal = vec3(0.0,0.0,1.0);
float denom = dot(normal, ray.direction);

if (abs(denom) > 0.0001)
Expand All @@ -34,6 +34,7 @@ void intersectPlane(Ray ray, inout RayHit bestHit, vec3 p, vec3 normal)
bestHit.position = ray.origin + t*ray.direction;
bestHit.normal = normal;
bestHit.albedo = vec3(1.0,1.0,1.0);
bestHit.material = MAT_LAMBERT;
}
}
}
11 changes: 10 additions & 1 deletion shader/include/lighting.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
ray.origin = hit.position + hit.normal*0.001;
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);
}
1 change: 1 addition & 0 deletions shader/include/ray.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct RayHit
float distance;
vec3 normal;
vec3 albedo;
int material;
};
RayHit createRayHit()
{
Expand Down
4 changes: 3 additions & 1 deletion shader/include/scene.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ RayHit trace(Ray ray)
RayHit hit = createRayHit();

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.material = MAT_CHROME;
intersectSphere(ray, hit, s);

intersectPlane(ray, hit, vec3(0.0,-1.5,0.0),vec3(0.0,1.0,0.0));

Expand Down
5 changes: 3 additions & 2 deletions shader/include/sphere.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ struct Sphere
// (c.x,c.y,c.z,r)
vec4 cr;
vec3 albedo;
int material;
};

// 253 is the maximum?? TODO: use uniform buffer objects
const int SPHERES = 250;
// TODO: use uniform buffer objects
const int SPHERES = 167;
uniform int _activeSpheres;
layout (location = 1) uniform Sphere _spheres[SPHERES];
18 changes: 10 additions & 8 deletions shader/root/rt.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ vec3 shade(inout Ray ray, RayHit hit)
{
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
Expand Down Expand Up @@ -78,15 +87,8 @@ void main()
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(firstHit.albedo, pixel.xyz, depth);

//pixel.a = 1.0;

//pixel.xyz = texture(_g0, uv).xyz;

// output to a specific pixel in the image
imageStore(img_output, ivec2(gl_GlobalInvocationID.xy), pixel);
}
32 changes: 18 additions & 14 deletions src/sphere.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ void makeSpheres(struct Sphere *spheres, int count)
glm_vec3_scale(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;

Expand All @@ -53,20 +54,22 @@ struct Sphere makeSphere(vec3 center, float radius, vec3 albedo)

glm_vec3_copy(albedo, s.albedo);

s.material = material;

return s;
}

void updateSphereUniform(GLuint shaderProgram, struct Sphere sphere)
{
int scrloc, saloc;
scrloc = glGetUniformLocation(shaderProgram, "_sphere.cr");
saloc = glGetUniformLocation(shaderProgram, "_sphere.albedo");

glUniform4fv(scrloc, 1, sphere.cr);
glUniform3fv(saloc, 1, sphere.albedo);

updateSphereUniforms(shaderProgram, &sphere, 0);
}
//void updateSphereUniform(GLuint shaderProgram, struct Sphere sphere)
//{
// int scrloc, saloc;
// scrloc = glGetUniformLocation(shaderProgram, "_sphere.cr");
// saloc = glGetUniformLocation(shaderProgram, "_sphere.albedo");
//
// glUniform4fv(scrloc, 1, sphere.cr);
// glUniform3fv(saloc, 1, sphere.albedo);
//
// updateSphereUniforms(shaderProgram, &sphere, 0);
//}

void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int count)
{
Expand All @@ -75,7 +78,7 @@ void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int coun
glUniform1i(loc, count);

// each sphere takes up two uniform locations
const int stride = 2;
const int stride = 3;

// first location in the array
loc = glGetUniformLocation(shaderProgram, "_spheres[0].cr");
Expand All @@ -85,5 +88,6 @@ void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int coun
struct Sphere s = spheres[i];
glUniform4fv(loc+i*stride, 1, s.cr);
glUniform3fv(loc+i*stride+1, 1, s.albedo);
glUniform1i( loc+i*stride+2, s.material);
}
}
5 changes: 3 additions & 2 deletions src/sphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ struct Sphere
{
vec4 cr;
vec3 albedo;
int material;
};

void makeSpheres(struct Sphere *spheres, int count);
struct Sphere makeSphere(vec3 center, float radius, vec3 albedo);
void updateSphereUniform(GLuint shaderProgram, struct Sphere sphere);
struct Sphere makeSphere(vec3 center, float radius, vec3 albedo, int material);
//void updateSphereUniform(GLuint shaderProgram, struct Sphere sphere);
void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int count);
3 changes: 2 additions & 1 deletion todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* [x] perspective
* [x] camera lookat
* [x] do a bounce
* [ ] do a 'flect
* [x] do a 'flect
* [ ] properly sample depth for reflective surfaces
* [ ] do a 'fract
* [ ] depth of field
* [ ] emission
Expand Down

0 comments on commit a79dfe0

Please sign in to comment.