Commit 926f3dff authored by Brian's avatar Brian

shere loop and diffuse material update

-unfortunately we cannot use classes like in the paper [https://gitlab.hrz.tu-chemnitz.de/bp59gudo--tu-freiberg.de/mm_raytracer/blob/sphere/paper/Ray_Tracing_in_a_Weekend.pdf] described, so the loop is still not as it should be.
parent 23593e9e
......@@ -23,41 +23,61 @@ struct hit_record {
vec3 point_at_parameter(vec3 A, float t)
{ return A + t * ray_d; }
bool hit_sphere(float max_x, float max_y, struct sphere s, hit_record& rec, float t_max, float t_min)
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 - s.sphere_c;
float a = dot(ray_d, ray_d);
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){
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;
bool hit;
for (int i = 0; spherelist[i] = '\0'; i++)
{
vec3 ray_o = vec3(max_x, max_y, 0.0);
vec3 omc = ray_o - s[i].sphere_c;
float a = dot(ray_d, ray_d);
float b = dot(ray_d, omc);
float c = dot(omc, omc) - s[i].sphere_r * s[i].sphere_r;
float discriminant = b * b - a * c;
// hit one or both sides
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[i].sphere_c) / s[i].sphere_r;
hit = 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[i].sphere_c) / s[i].sphere_r;
hit = true;
}
}
hit = false;
}
return false;
return hit;
}
vec3 random_in_unit_sphere() {
vec3 p;
do {
p = 2.0 * vec3(drand48(), drand48(), drand()) - vec3(1, 1, 1);
} while (p.squared_length() >= 1.0);
return p;
}
vec4 color(float max_x, float max_y, sphere s)
vec4 color(float max_x, float max_y, sphere s[])
{
hit_record rec;
if (hit_sphere(max_x, max_y, s, rec, MAXBOUNDS, 0.0)) {
if (hit_sphere(max_x, max_y, s[], rec, MAXBOUNDS, 0.0)) {
//vec3 target = rec.p + rec.normal + random_in_unit_sphere();
//todo diffuse output
return 0.5 * vec4(rec.normal.x + 1, rec.normal.y + 1, rec.normal.z + 1, 2);
}
else
......@@ -104,10 +124,9 @@ void main() {
float x = (float(pixel_coords.x * 2 - dims.x) / dims.x);
float y = (float(pixel_coords.y * 2 - dims.y) / dims.y);
for (int i = 0; spherelist[] = "\0", i++)
{
vec4 pixel = color(x * max_x, y * max_y, spherelist[i]);
}
vec4 pixel = color(x * max_x, y * max_y, spherelist[]);
// 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