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 { ...@@ -23,41 +23,61 @@ struct hit_record {
vec3 point_at_parameter(vec3 A, float t) vec3 point_at_parameter(vec3 A, float t)
{ return A + t * ray_d; } { 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); bool hit;
vec3 omc = ray_o - s.sphere_c; for (int i = 0; spherelist[i] = '\0'; i++)
float a = dot(ray_d, ray_d); {
float b = dot(ray_d, omc); vec3 ray_o = vec3(max_x, max_y, 0.0);
float c = dot(omc, omc) - s.sphere_r * s.sphere_r; vec3 omc = ray_o - s[i].sphere_c;
float discriminant = b * b - a*c; float a = dot(ray_d, ray_d);
float b = dot(ray_d, omc);
// hit one or both sides float c = dot(omc, omc) - s[i].sphere_r * s[i].sphere_r;
if (discriminant > 0.0){ float discriminant = b * b - a * c;
float temp = (-b - sqrt(discriminant)) / a;
if (temp < t_max && temp > t_min) { // hit one or both sides
rec.t = temp; if (discriminant > 0.0) {
rec.p = point_at_parameter(ray_o, rec.t); float temp = (-b - sqrt(discriminant)) / a;
rec.normal = (rec.p - s.sphere_c) / s.sphere_r; if (temp < t_max && temp > t_min) {
return true; rec.t = temp;
} rec.p = point_at_parameter(ray_o, rec.t);
temp = (-b + sqrt(discriminant)) / a; rec.normal = (rec.p - s[i].sphere_c) / s[i].sphere_r;
if (temp < t_max && temp > t_min) { hit = true;
rec.t = temp; }
rec.p = point_at_parameter(ray_o, rec.t); temp = (-b + sqrt(discriminant)) / a;
rec.normal = (rec.p - s.sphere_c) / s.sphere_r; if (temp < t_max && temp > t_min) {
return true; 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; 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); return 0.5 * vec4(rec.normal.x + 1, rec.normal.y + 1, rec.normal.z + 1, 2);
} }
else else
...@@ -104,10 +124,9 @@ void main() { ...@@ -104,10 +124,9 @@ void main() {
float x = (float(pixel_coords.x * 2 - dims.x) / dims.x); float x = (float(pixel_coords.x * 2 - dims.x) / dims.x);
float y = (float(pixel_coords.y * 2 - dims.y) / dims.y); 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[]);
vec4 pixel = color(x * max_x, y * max_y, spherelist[i]);
}
// output to a specific pixel in the image // output to a specific pixel in the image
imageStore(img_output, pixel_coords, pixel); 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