Phongo Clap RT  1.0
Simple Raytracing Renderer
Public Member Functions | Private Attributes | List of all members
geo::Sphere Class Reference

Class that hold an implicit definition of a sphere through center and radius parameters. It also implements a method for finding intersections. More...

#include <Sphere.h>

Inheritance diagram for geo::Sphere:
Inheritance graph
Collaboration diagram for geo::Sphere:
Collaboration graph

Public Member Functions

 Sphere ()
 Sphere constructor, by default it will instanciate a 1 unit radius sphere in the origin (0,0,0). More...
 
 Sphere (ngl::Vec3 _center, float _radius, ngl::Colour _colour)
 Sphere ctor using a radius and a center. More...
 
 ~Sphere ()
 Destructor so that all memory is freed. More...
 
void setRadius (float _radius)
 Setter for the radius. More...
 
void setCenter (ngl::Vec3 _center)
 Setter for the center. More...
 
float getRadius () const
 Returns radius of a sphere. More...
 
ngl::Vec3 getCenter () const
 Returns center of a sphere. More...
 
virtual float getIntersection (geo::Ray &_ray)
 Calculates the intersection of the passed ray with the sphere according to the parametric form of a ray: R(t) = P + t * (Q - P) More...
 
virtual ngl::Vec3 getNormalAt (ngl::Vec3 _p)
 Calculates what the normal is at a given point of a sphere. It doesn't check whether it is on the surface or not because that point will be passed through an intersection which we are sure about. More...
 
virtual ngl::Colour getColour ()
 Gets colour of the sphere through its material object. More...
 
virtual ngl::Colour getColour (ngl::Vec3 &_isect)
 Gets colour of the sphere through its material object. More...
 
- Public Member Functions inherited from geo::Shape
 Shape ()
 Default constructor. More...
 
 ~Shape ()
 Frees any memory grabbed from the heap. More...
 
void hasRefraction (float _ior, float _transparency, float _diffuse_intensity)
 Sets parameters related to refraction, this will be given to the Material class. More...
 
void hasReflection (float _refl_intensity, float _diffuse_intensity)
 Sets reflection attributes. More...
 
MaterialgetMaterial ()
 Returns the material attached to a shape object. This is useful for querying or changing private interface, that's why it is not const. More...
 
char getType ()
 Derived classes will be Spheres or Planes, I will store the type of geometry in this attribute. More...
 

Private Attributes

ngl::Vec3 m_center
 Center of the sphere, in world space coordinates. More...
 
double m_radius
 Radius of the sphere. More...
 

Additional Inherited Members

- Protected Attributes inherited from geo::Shape
char m_type
 Type of geometric object it is. More...
 
ngl::Colour m_colour
 Colour of the shape. More...
 
Materialm_material
 Material attached to the Shape object. More...
 

Detailed Description

Class that hold an implicit definition of a sphere through center and radius parameters. It also implements a method for finding intersections.

Author
Ramon Blanquer
Todo:
Implement colour on the sphere.

Definition at line 23 of file Sphere.h.

Constructor & Destructor Documentation

geo::Sphere::Sphere ( )
inline

Sphere constructor, by default it will instanciate a 1 unit radius sphere in the origin (0,0,0).

Definition at line 29 of file Sphere.h.

References geo::Shape::m_colour, geo::Shape::m_material, and geo::Shape::m_type.

29  : m_center(ngl::Vec3(1,1,1)), m_radius(2.0f)
30  {
31  m_type = 's';
32  m_colour = ngl::Colour(1,1,0,1);
33  Material* mat = new Material(ngl::Colour(1,1,0,0));
34  m_material = mat;
35  }
Material * m_material
Material attached to the Shape object.
Definition: Shape.h:96
ngl::Colour m_colour
Colour of the shape.
Definition: Shape.h:92
ngl::Vec3 m_center
Center of the sphere, in world space coordinates.
Definition: Sphere.h:110
char m_type
Type of geometric object it is.
Definition: Shape.h:88
double m_radius
Radius of the sphere.
Definition: Sphere.h:114
geo::Sphere::Sphere ( ngl::Vec3  _center,
float  _radius,
ngl::Colour  _colour 
)
inline

Sphere ctor using a radius and a center.

Parameters
[in]_centerThe position in where the sphere is positioned.
[in]_radiusThe radius in scene units of the sphere object.
[in]_colourThe colour of the Sphere

Definition at line 42 of file Sphere.h.

References geo::Shape::m_colour, geo::Shape::m_material, and geo::Shape::m_type.

42  : m_center(_center), m_radius(_radius)
43  {
44  m_type = 's';
45  m_colour = _colour;
46  Material* mat = new Material(_colour);
47  m_material = mat;
48  }
Material * m_material
Material attached to the Shape object.
Definition: Shape.h:96
ngl::Colour m_colour
Colour of the shape.
Definition: Shape.h:92
ngl::Vec3 m_center
Center of the sphere, in world space coordinates.
Definition: Sphere.h:110
char m_type
Type of geometric object it is.
Definition: Shape.h:88
double m_radius
Radius of the sphere.
Definition: Sphere.h:114
geo::Sphere::~Sphere ( )
inline

Destructor so that all memory is freed.

Definition at line 52 of file Sphere.h.

52 {}

Member Function Documentation

ngl::Vec3 geo::Sphere::getCenter ( ) const
inline

Returns center of a sphere.

Returns
The center of a sphere.

Definition at line 76 of file Sphere.h.

References m_center.

76 {return m_center;}
ngl::Vec3 m_center
Center of the sphere, in world space coordinates.
Definition: Sphere.h:110

Here is the caller graph for this function:

virtual ngl::Colour geo::Sphere::getColour ( )
inlinevirtual

Gets colour of the sphere through its material object.

Parameters
[in]_pThe position in the surface of the spehere where we calculate the normal.
Returns
The colour of the sphere

Implements geo::Shape.

Definition at line 98 of file Sphere.h.

References Material::m_colour1, and geo::Shape::m_material.

98 {return m_material->m_colour1;}
Material * m_material
Material attached to the Shape object.
Definition: Shape.h:96
ngl::Colour m_colour1
Colour of the object.
Definition: Material.h:121
virtual ngl::Colour geo::Sphere::getColour ( ngl::Vec3 &  _isect)
inlinevirtual

Gets colour of the sphere through its material object.

Parameters
[in]_pThe position in the surface of the spehere where we calculate the normal.
Returns
The colour of the sphere

Implements geo::Shape.

Definition at line 104 of file Sphere.h.

References Material::m_colour1, and geo::Shape::m_material.

104 {return m_material->m_colour1;}
Material * m_material
Material attached to the Shape object.
Definition: Shape.h:96
ngl::Colour m_colour1
Colour of the object.
Definition: Material.h:121
float geo::Sphere::getIntersection ( geo::Ray _ray)
virtual

Calculates the intersection of the passed ray with the sphere according to the parametric form of a ray: R(t) = P + t * (Q - P)

Parameters
[in]_ray
Returns
The distance from the origin of the ray where it intersects the sphere.

Implements geo::Shape.

Definition at line 12 of file Sphere.cpp.

References getCenter(), geo::Ray::getDirection(), geo::Ray::getOrigin(), and getRadius().

13  {
14  float ray_origin_x = ray.getOrigin().m_x;
15  float ray_origin_y = ray.getOrigin().m_y;
16  float ray_origin_z = ray.getOrigin().m_z;
17 
18  float ray_direction_x = ray.getDirection().m_x;
19  float ray_direction_y = ray.getDirection().m_y;
20  float ray_direction_z = ray.getDirection().m_z;
21 
22  float sphere_center_x = this->getCenter().m_x;
23  float sphere_center_y = this->getCenter().m_y;
24  float sphere_center_z = this->getCenter().m_z;
25 
26  //float a = 1;
27  float b = (2*(ray_origin_x - sphere_center_x)*ray_direction_x)
28  + (2*(ray_origin_y - sphere_center_y)*ray_direction_y)
29  + (2*(ray_origin_z - sphere_center_z)*ray_direction_z);
30  float c = pow(ray_origin_x - sphere_center_x, 2)
31  + pow(ray_origin_y - sphere_center_y, 2)
32  + pow(ray_origin_z - sphere_center_z, 2)
33  - (this->getRadius()*this->getRadius());
34  float discriminant = b*b - 4*c;
35 
36  if(discriminant > 0)
37  {
38  //ray intersects sphere
39 
40  // first root
41  float root_1 = ((-1*b - sqrt(discriminant))/2) - 0.000001;
42 
43  if (root_1 > 0)
44  {
45  return root_1;
46  }
47 
48  else
49  {
50  // second root is the smallest positive root
51  float root_2 = ((sqrt(discriminant)-b)/2) - 0.000001;
52  return root_2;
53  }
54  }
55  else
56  {
57  return -1;
58  }
59  }
float getRadius() const
Returns radius of a sphere.
Definition: Sphere.h:71
ngl::Vec3 getCenter() const
Returns center of a sphere.
Definition: Sphere.h:76

Here is the call graph for this function:

ngl::Vec3 geo::Sphere::getNormalAt ( ngl::Vec3  _p)
virtual

Calculates what the normal is at a given point of a sphere. It doesn't check whether it is on the surface or not because that point will be passed through an intersection which we are sure about.

Parameters
[in]_pThe position in the surface of the spehere where we calculate the normal.
Returns
The normal of the sphere at a given point.

Implements geo::Shape.

Definition at line 61 of file Sphere.cpp.

References m_center.

62  {
63  ngl::Vec3 normal;
64  normal = _p - m_center;
65  normal.normalize();
66  return normal;
67  }
ngl::Vec3 m_center
Center of the sphere, in world space coordinates.
Definition: Sphere.h:110
float geo::Sphere::getRadius ( ) const
inline

Returns radius of a sphere.

Returns
The radius of a sphere.

Definition at line 71 of file Sphere.h.

References m_radius.

71 {return m_radius;}
double m_radius
Radius of the sphere.
Definition: Sphere.h:114

Here is the caller graph for this function:

void geo::Sphere::setCenter ( ngl::Vec3  _center)
inline

Setter for the center.

Parameters
[in]_centerCenter of the sphere.

Definition at line 64 of file Sphere.h.

References m_center.

64 {m_center = _center;}
ngl::Vec3 m_center
Center of the sphere, in world space coordinates.
Definition: Sphere.h:110
void geo::Sphere::setRadius ( float  _radius)
inline

Setter for the radius.

Parameters
[in]_centerThe radius of the sphere.

Definition at line 59 of file Sphere.h.

References m_radius.

59 {m_radius = _radius;}
double m_radius
Radius of the sphere.
Definition: Sphere.h:114

Member Data Documentation

ngl::Vec3 geo::Sphere::m_center
private

Center of the sphere, in world space coordinates.

Definition at line 110 of file Sphere.h.

double geo::Sphere::m_radius
private

Radius of the sphere.

Definition at line 114 of file Sphere.h.


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