I am writing a ray tracing program in C++ to get better at the language. However I am having some troubles implementing my ray-cube intersection functionality.
Here is my class
class Cube {
public:
Cube() {};
~Cube() {};
float Intersection(Ray ray) {
// Intersection Code Here
}
public:
glm::vec3 Position;
glm::vec3 Size;
int MaterialIndex = 0;
};
I can't find any good references anywhere that fit my need (using vec3s for size and position). I need to return a float that is the distance to the Cube or -1 if it does not hit.
For reference here is my Sphere class
class Sphere {
public:
Sphere() {};
~Sphere() {};
float Intersection(Ray ray) {
glm::vec3 origin = ray.Origin - Position;
float a = glm::dot(ray.Direction, ray.Direction);
float b = 2.0f * glm::dot(origin, ray.Direction);
float c = glm::dot(origin, origin) - Radius * Radius;
// Quadratic forumula discriminant:
// b^2 - 4ac
float discriminant = b * b - 4.0f * a * c;
// Quadratic formula:
// (-b +- sqrt(discriminant)) / 2a
// float t0 = (-b + glm::sqrt(discriminant)) / (2.0f * a); // Second hit distance (currently unused)
float closestT = (-b - glm::sqrt(discriminant)) / (2.0f * a);
return closestT;
};
public:
float Radius = 0.5f;
glm::vec3 Position;
int MaterialIndex = 0;
};
Can anyone help me?