#include <cmath>
#include <iostream>
// Assume these are defined elsewhere in the rendering system
struct float3 {
float x, y, z;
float3(float a = 0, float b = 0, float c = 0) : x(a), y(b), z(c) {}
float3 operator*(float s) const { return float3(x * s, y * s, z * s); }
float3 operator+(const float3& other) const { return float3(x + other.x, y + other.y, z + other.z); }
float3 operator-(const float3& other) const { return float3(x - other.x, y - other.y, z - other.z); }
float3& operator*=(float s) { x *= s; y *= s; z *= s; return *this; }
float3& operator+=(const float3& other) { x += other.x; y += other.y; z += other.z; return *this; }
float length() const { return std::sqrt(x * x + y * y + z * z); }
float3 normalize() const {
float len = length();
if (len > 1e-8) return *this / len;
return *this;
}
float3 operator/(float s) const { return float3(x / s, y / s, z / s); }
float dot(const float3& other) const { return x * other.x + y * other.y + z * other.z; }
};
struct Ray {
float3 origin;
float3 direction;
};
struct HitInfo {
float3 point; // Point of intersection
float3 normal; // Normal at the intersection point
float t; // Distance from the ray origin to the intersection point
int object_id; // ID of the object that was hit
float ior; // Index of refraction of the material (e.g., 1.5 for glass)
};
struct Scene {
// Scene data (not used in this function, but assumed to be available)
};
// Generic shade function (assumed to be implemented elsewhere)
float3 shade(const HitInfo& hit, const float3& viewDir, int level);
// Function to reflect a vector over a normal
float3 reflect(const float3& incident, const float3& normal) {
return incident - 2 * normal * (incident.dot(normal));
}
// Function to refract a vector using Snell's law
bool refract(const float3& incident, const float3& normal, float n1, float n2, float3& refracted) {
float cosTheta = -incident.dot(normal);
float r = n1 / n2;
float k = 1 - r * r * (1 - cosTheta * cosTheta);
if (k < 0) return false; // Total internal reflection
refracted = (incident * r) + (normal * (r * cosTheta - std::sqrt(k)));
return true;
}
// Schlick's approximation for the Fresnel term
float schlickFresnel(float cosTheta, float n1, float n2) {
float R0 = std::pow((n1 - n2) / (n1 + n2), 2);
return R0 + (1 - R0) * std::pow(1 - cosTheta, 5);
}
// Main glass shading function
float3 shadeGlass(Scene& scene, const HitInfo& hit, const float3& viewDir, const int level) {
if (level <= 0) {
// Avoid infinite recursion by capping the recursion depth
return float3(0, 0, 0);
}
float3 normal = hit.normal;
float3 incident = -viewDir; // Incident direction is opposite to the view direction
float n1, n2;
// Determine which side of the surface the viewer is on
float cosTheta = incident.dot(normal);
if (cosTheta > 0) {
// Ray is coming from outside (e.g., air to glass)
n1 = 1.0; // Index of refraction of air
n2 = hit.ior; // Index of refraction of glass
} else {
// Ray is coming from inside the glass (e.g., glass to air)
n1 = hit.ior; // Index of refraction of glass
n2 = 1.0; // Index of refraction of air
normal = -normal; // Flip the normal
cosTheta = -cosTheta; // Make cosTheta positive for the calculation
}
// Compute the reflection direction
float3 reflected = reflect(incident, normal).normalize();
// Compute the refraction direction
float3 refracted;
bool canRefract = refract(incident, normal, n1, n2, refracted);
// Compute the Fresnel term to determine the reflection vs. refraction
float fresnel = schlickFresnel(cosTheta, n1, n2);
// Recursively shade the reflected ray
float3 reflectionColor;
if (fresnel > 0) {
// Create a new ray for reflection
Ray reflectionRay;
reflectionRay.origin = hit.point;
reflectionRay.direction = reflected;
HitInfo reflectionHit = hit; // Use the same hit info, but update the normal if needed
reflectionHit.normal = normal; // Ensure the normal is correct for the reflection
reflectionColor = shade(reflectionHit, -reflected, level - 1);
} else {
reflectionColor = float3(0, 0, 0);
}
// Recursively shade the refracted ray if refraction is possible
float3 refractionColor = float3(0, 0, 0);
if (canRefract) {
// Create a new ray for refraction
Ray refractionRay;
refractionRay.origin = hit.point;
refractionRay.direction = refracted;
HitInfo refractionHit = hit; // Use the same hit info
refractionHit.normal = normal; // Ensure the normal is correct for the refraction
refractionColor = shade(refractionHit, -refracted, level - 1);
}
// Combine the reflection and refraction colors
float3 glassColor = reflectionColor * fresnel + refractionColor * (1 - fresnel);
return glassColor;
}