1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
#include <iostream> #include <cmath> using namespace std; // Structure for a 3D vector struct Vec3 { float x, y, z; // Constructor to initialize vector Vec3(float x = 0, float y = 0, float z = 0) : x(x), y(y), z(z) {} // Vector addition Vec3 operator+(const Vec3& v) const { return Vec3(x + v.x, y + v.y, z + v.z); } // Vector subtraction Vec3 operator-(const Vec3& v) const { return Vec3(x - v.x, y - v.y, z - v.z); } // Scalar multiplication Vec3 operator*(float t) const { return Vec3(x * t, y * t, z * t); } // Dot product float dot(const Vec3& v) const { return x * v.x + y * v.y + z * v.z; } // Length of the vector (magnitude) float length() const { return sqrt(x * x + y * y + z * z); } // Normalize the vector Vec3 normalize() const { float len = length(); return Vec3(x / len, y / len, z / len); } }; // Structure for a ray struct Ray { Vec3 origin; Vec3 direction; // Constructor to initialize ray Ray(const Vec3& origin, const Vec3& direction) : origin(origin), direction(direction.normalize()) {} // Get point at distance t along the ray Vec3 pointAtParameter(float t) const { return origin + direction * t; } }; // Structure for a sphere struct Sphere { Vec3 center; float radius; // Constructor to initialize sphere Sphere(const Vec3& center, float radius) : center(center), radius(radius) {} // Check if a ray intersects with the sphere bool intersect(const Ray& ray, float& t) const { Vec3 oc = ray.origin - center; float a = ray.direction.dot(ray.direction); float b = 2.0f * oc.dot(ray.direction); float c = oc.dot(oc) - radius * radius; float discriminant = b * b - 4 * a * c; if (discriminant < 0) { return false; } else { t = (-b - sqrt(discriminant)) / (2.0f * a); return true; } } }; int main() { // Define a sphere at position (0, 0, -5) with radius 1 Sphere sphere(Vec3(0, 0, -5), 1); // Define a ray originating at the camera position (0, 0, 0) and pointing in the z direction Ray ray(Vec3(0, 0, 0), Vec3(0, 0, -1)); float t; if (sphere.intersect(ray, t)) { Vec3 hitPoint = ray.pointAtParameter(t); cout << "Ray intersects the sphere at point (" << hitPoint.x << ", " << hitPoint.y << ", " << hitPoint.z << ")" << endl; } else { cout << "Ray does not intersect the sphere." << endl; } return 0; } |
Explanation
- Vec3 Structure:
Vec3
is a structure representing a 3D vector withx
,y
, andz
components. It includes various vector operations such as addition, subtraction, scalar multiplication, dot product, length, and normalization.
- Ray Structure:
- The
Ray
structure represents a ray with an origin and a direction. The direction is normalized to ensure it’s a unit vector. ThepointAtParameter()
t
.
- The
- Sphere Structure:
- The
Sphere
structure defines a sphere with a center and a radius. Theintersect()
function checks if a given ray intersects with the sphere. It uses the quadratic formula to solve for the intersection points. If the discriminant is positive, there is an intersection, and the function returnstrue
t
.
- The
- Main Function:
- In the
main()
function, a sphere is defined at position(0, 0, -5)
with a radius of1
. A ray is defined starting at(0, 0, 0)
(camera position) and pointing in the negative z-direction. The program checks if the ray intersects with the sphere and prints the intersection point if it exists.
- In the