Commit d61e1b7f authored by Brian's avatar Brian

sphere.h placeholder

parent b653c2bb
......@@ -155,6 +155,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="glad.h" />
<ClInclude Include="sphere.h" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
......
......@@ -35,6 +35,9 @@
<ClInclude Include="glad.h">
<Filter>Headerdateien\include\glad</Filter>
</ClInclude>
<ClInclude Include="sphere.h">
<Filter>Headerdateien\include</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
......
#pragma once
#define SPHEREH
#include "hitable.h"
class sphere : public hitable {
public:
sphere() {}
sphere(vec3 cen, float r) : center(cen), radius(r) {};
virtual bool hit(const ray& r, float tmin, float tmax, hit_record& rec) const;
vec3 center;
float radius;
};
bool sphere::hit(const ray& r, float t_min, float t_max, hit_record& rec) const {
vec3 oc = r.origin() - center;
float a = dot(r.direction(), r.direction());
float b = dot(oc, r.direction());
float c = dot(oc, oc) - radius * radius;
float discriminant = b * b - a * c;
if (discriminant > 0) {
float temp = (-b - sqrt(b * b - a * c)) / a;
if (temp < t_max && temp > t_min) {
rec.t = temp
rec.p = r.point_at_parameter(rec.t);
rec.normal = (rec.p - center) / radius;
return true;
}
temp = (-b + sqrt(b*b - a*c)) / a;
if (temp < t_max && temp > t_min) {
rec.t = temp
rec.p = r.point_at_parameter(rec.t);
rec.normal = (rec.p - center) / radius;
return true;
}
}
return false;
}
\ No newline at end of file
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