RayTracer 1.0
Ray tracing is a technique used to generate realistic digital images by simulating the inverse path of light. Our goal is to create a program able to generate an image from a file describing the scene.
 
Loading...
Searching...
No Matches
RayTracer::Sphere Class Reference

#include <Sphere.hpp>

Inheritance diagram for RayTracer::Sphere:
RayTracer::IPrimitive

Public Member Functions

 Sphere (const Math::Point3D &center, double radius, std::shared_ptr< IMaterial > material)
 
 ~Sphere () override=default
 
void translate (const Math::Vector3D &v) override
 
void rotate (const Math::Vector3D &axis, double angleDegrees) override
 
std::shared_ptr< IMaterialgetMaterial () const override
 
bool intersect (const Ray &ray, double &t, Math::Point3D &hitPoint, Math::Vector3D &normal) const override
 
- Public Member Functions inherited from RayTracer::IPrimitive
virtual ~IPrimitive ()=default
 
Color getBaseColor () const
 

Private Attributes

Math::Point3D _center
 
double _radius
 
std::shared_ptr< IMaterial_material
 

Detailed Description

Definition at line 13 of file Sphere.hpp.

Constructor & Destructor Documentation

◆ Sphere()

RayTracer::Sphere::Sphere ( const Math::Point3D & center,
double radius,
std::shared_ptr< IMaterial > material )

Definition at line 10 of file Sphere.cpp.

11 : _center(center), _radius(radius), _material(std::move(material)) {}
std::shared_ptr< IMaterial > _material
Definition Sphere.hpp:26
Math::Point3D _center
Definition Sphere.hpp:24

References _center, _material, and _radius.

◆ ~Sphere()

RayTracer::Sphere::~Sphere ( )
overridedefault

Member Function Documentation

◆ getMaterial()

std::shared_ptr< RayTracer::IMaterial > RayTracer::Sphere::getMaterial ( ) const
overridevirtual

Implements RayTracer::IPrimitive.

Definition at line 45 of file Sphere.cpp.

45 {
46 return _material;
47}

References _material.

◆ intersect()

bool RayTracer::Sphere::intersect ( const Ray & ray,
double & t,
Math::Point3D & hitPoint,
Math::Vector3D & normal ) const
overridevirtual

Implements RayTracer::IPrimitive.

Definition at line 14 of file Sphere.cpp.

14 {
15 Math::Vector3D oc = ray.origin - _center;
16 double a = ray.direction.dot(ray.direction);
17 double b = 2.0 * oc.dot(ray.direction);
18 double c = oc.dot(oc) - _radius * _radius;
19 double discriminant = b * b - 4 * a * c;
20
21 if (discriminant < 0)
22 return false;
23
24 double sqrtDiscriminant = std::sqrt(discriminant);
25 double t0 = (-b - sqrtDiscriminant) / (2.0 * a);
26 double t1 = (-b + sqrtDiscriminant) / (2.0 * a);
27 t = (t0 > 0) ? t0 : ((t1 > 0) ? t1 : -1);
28
29 if (t < 0)
30 return false;
31
32 hitPoint = ray.origin + ray.direction * t;
33 normal = (hitPoint - _center) / _radius;
34 return true;
35}
double dot(const Vector3D &o) const
Definition Math3D.cpp:20

References _center, _radius, RayTracer::Ray::direction, Math::Vector3D::dot(), and RayTracer::Ray::origin.

◆ rotate()

void RayTracer::Sphere::rotate ( const Math::Vector3D & axis,
double angleDegrees )
overridevirtual

Implements RayTracer::IPrimitive.

Definition at line 41 of file Sphere.cpp.

41 {
42 // une sphère ne change pas d’apparence après rotation
43}

◆ translate()

void RayTracer::Sphere::translate ( const Math::Vector3D & v)
overridevirtual

Implements RayTracer::IPrimitive.

Definition at line 37 of file Sphere.cpp.

37 {
38 _center = _center + v;
39}

References _center.

Member Data Documentation

◆ _center

Math::Point3D RayTracer::Sphere::_center
private

Definition at line 24 of file Sphere.hpp.

◆ _material

std::shared_ptr<IMaterial> RayTracer::Sphere::_material
private

Definition at line 26 of file Sphere.hpp.

◆ _radius

double RayTracer::Sphere::_radius
private

Definition at line 25 of file Sphere.hpp.


The documentation for this class was generated from the following files: