Commit 23593e9e authored by Brian's avatar Brian

adding struct for sphere and try loop

Warning:
-loop is still not working, color() will just output last sphere instead of the closest hit
parent f161dc09
#version 430
#define MAXBOUNDS 1000
//size of local work group
layout(local_size_x = 1, local_size_y = 1) in;
//internal image format
layout(rgba32f, binding = 0) uniform image2D img_output;
vec3 ray_d = vec3(0.0, 0.0, -1.0); // ortho
vec3 sphere_c = vec3(0.0, 0.0, -10.0);
float sphere_r = 1.0;
struct sphere {
float sphere_r;
vec3 sphere_c;
vec4 color;
};
struct hit_record {
float t;
vec3 p;
vec3 normal;
};
vec3 point_at_parameter(vec3 A, float t)
{ return A + t * ray_d; }
float hit_sphere(float max_x, float max_y)
bool hit_sphere(float max_x, float max_y, struct sphere s, hit_record& rec, float t_max, float t_min)
{
vec3 ray_o = vec3(max_x, max_y, 0.0);
vec3 omc = ray_o - sphere_c;
vec3 omc = ray_o - s.sphere_c;
float a = dot(ray_d, ray_d);
float b = 2 * dot(ray_d, omc);
float c = dot(omc, omc) - sphere_r * sphere_r;
float discriminant = b * b - 4*a*c;
float b = dot(ray_d, omc);
float c = dot(omc, omc) - s.sphere_r * s.sphere_r;
float discriminant = b * b - a*c;
// hit one or both sides
if (discriminant < 0.0)
{
return -1.0;
}
else {
return (-b - sqrt(discriminant)) / (2 * a);
if (discriminant > 0.0){
float temp = (-b - sqrt(discriminant)) / a;
if (temp < t_max && temp > t_min) {
rec.t = temp;
rec.p = point_at_parameter(ray_o, rec.t);
rec.normal = (rec.p - s.sphere_c) / s.sphere_r;
return true;
}
temp = (-b + sqrt(discriminant)) / a;
if (temp < t_max && temp > t_min) {
rec.t = temp;
rec.p = point_at_parameter(ray_o, rec.t);
rec.normal = (rec.p - s.sphere_c) / s.sphere_r;
return true;
}
}
return false;
}
vec4 color(float max_x, float max_y)
vec4 color(float max_x, float max_y, sphere s)
{
float t = hit_sphere(max_x, max_y);
if (t > 0)) {
vec3 N = normalize(point_at_parameter(vec3(max_x, max_y, 0.0), t)-vec3(0,0,-1));
return 0.5*vec4(N.x + 1, N.y +1, N.z + 1, 2);
hit_record rec;
if (hit_sphere(max_x, max_y, s, rec, MAXBOUNDS, 0.0)) {
return 0.5 * vec4(rec.normal.x + 1, rec.normal.y + 1, rec.normal.z + 1, 2);
}
else
{ // base pixel colour for image
vec4 pixel(0.0, 0.0, 0.0, 1.0);
{ // base pixel color for image
vec4 pixel = vec4(0.0, 0.0, 0.0, 1.0);
/*
//possible background transition
vec4 unit_direction = normalize(ray_d);
float t = 0.5 * (unit_directioon.y + 1.0);
vec4 pixel = (1.0 - t) * vec4(1.0, 1.0, 1.0, 1.0) + t * vec4(0.5, 0.7, 1.0, 1.0);
*/
return pixel;
}
}
}
void main() {
//vec4 pixel = vec4(0.0, 0.0, 0.0, 1.0);
// get index in global work group i.e x,y position
ivec2 pixel_coords = ivec2(gl_GlobalInvocationID.xy);
struct sphere s1;
struct sphere s2;
sphere spherelist[2];
s1.sphere_r = 1;
s1.sphere_c = (0.0, 0.0, -10);
s1.color = (0.5, 0.4, 0.0, 0.5);
s2.sphere_r = 3;
s2.sphere_c = (3.0, 2.0, -20);;
s2.color = (0, 0.4, 0.9, 0.5);;
spherelist[] += s1;
spherelist[] += s2;
//
// Raytracing stuff
//
......@@ -65,8 +103,11 @@ void main() {
ivec2 dims = imageSize(img_output); // fetch image dimensions
float x = (float(pixel_coords.x * 2 - dims.x) / dims.x);
float y = (float(pixel_coords.y * 2 - dims.y) / dims.y);
pixel = color(x* max_x, y* max_y, pixel);
for (int i = 0; spherelist[] = "\0", i++)
{
vec4 pixel = color(x * max_x, y * max_y, spherelist[i]);
}
// output to a specific pixel in the image
imageStore(img_output, pixel_coords, pixel);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment