Phongo Clap RT  1.0
Simple Raytracing Renderer
Sphere.h
Go to the documentation of this file.
1 #ifndef _SPHERE_H__
2 #define _SPHERE_H__
3 
7 
8 #include <ngl/Vec3.h>
9 #include <ngl/Colour.h>
10 #include "Shape.h"
11 #include "Ray.h"
12 #include "Material.h"
13 
14 namespace geo
15 {
16 //----------------------------------------------------------------------------------------------------------------------
22 //----------------------------------------------------------------------------------------------------------------------
23 class Sphere : public Shape
24 {
25 public:
26  //------------------------------------------------------------------------------------------------------------------
28  // -----------------------------------------------------------------------------------------------------------------
29  Sphere() : 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  }
36  //------------------------------------------------------------------------------------------------------------------
41  //------------------------------------------------------------------------------------------------------------------
42  Sphere(ngl::Vec3 _center, float _radius, ngl::Colour _colour) : 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  }
49  //------------------------------------------------------------------------------------------------------------------
51  //------------------------------------------------------------------------------------------------------------------
52  ~Sphere() {}
53  //------------------------------------------------------------------------------------------------------------------
54  // setters
55  //------------------------------------------------------------------------------------------------------------------
58  //------------------------------------------------------------------------------------------------------------------
59  void setRadius(float _radius) {m_radius = _radius;}
60  //------------------------------------------------------------------------------------------------------------------
63  //------------------------------------------------------------------------------------------------------------------
64  void setCenter(ngl::Vec3 _center) {m_center = _center;}
65  //------------------------------------------------------------------------------------------------------------------
66  // getters
67  //------------------------------------------------------------------------------------------------------------------
70  //------------------------------------------------------------------------------------------------------------------
71  float getRadius() const {return m_radius;}
72  //------------------------------------------------------------------------------------------------------------------
75  //------------------------------------------------------------------------------------------------------------------
76  ngl::Vec3 getCenter() const {return m_center;}
77  //------------------------------------------------------------------------------------------------------------------
78  // other methods
79  //------------------------------------------------------------------------------------------------------------------
84  //------------------------------------------------------------------------------------------------------------------
85  virtual float getIntersection(geo::Ray& _ray);
86  //------------------------------------------------------------------------------------------------------------------
91  //------------------------------------------------------------------------------------------------------------------
92  virtual ngl::Vec3 getNormalAt(ngl::Vec3 _p);
93  //------------------------------------------------------------------------------------------------------------------
97  //------------------------------------------------------------------------------------------------------------------
98  virtual ngl::Colour getColour() {return m_material->m_colour1;}
99  //------------------------------------------------------------------------------------------------------------------
103  //------------------------------------------------------------------------------------------------------------------
104  virtual ngl::Colour getColour(ngl::Vec3 &_isect) {return m_material->m_colour1;}
105 
106 private:
107  //------------------------------------------------------------------------------------------------------------------
109  //------------------------------------------------------------------------------------------------------------------
110  ngl::Vec3 m_center;
111  //------------------------------------------------------------------------------------------------------------------
113  //------------------------------------------------------------------------------------------------------------------
114  double m_radius;
115 };
116 }
117 
118 #endif // Sphere
Material * m_material
Material attached to the Shape object.
Definition: Shape.h:96
void setCenter(ngl::Vec3 _center)
Setter for the center.
Definition: Sphere.h:64
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 sur...
Definition: Sphere.cpp:61
virtual ngl::Colour getColour()
Gets colour of the sphere through its material object.
Definition: Sphere.h:98
Class that hold an implicit definition of a sphere through center and radius parameters. It also implements a method for finding intersections.
Definition: Sphere.h:23
This will be used to return the colour when queried from the Shape derived classes.
virtual float getIntersection(geo::Ray &_ray)
Calculates the intersection of the passed ray with the sphere according to the parametric form of a r...
Definition: Sphere.cpp:12
void setRadius(float _radius)
Setter for the radius.
Definition: Sphere.h:59
virtual ngl::Colour getColour(ngl::Vec3 &_isect)
Gets colour of the sphere through its material object.
Definition: Sphere.h:104
float getRadius() const
Returns radius of a sphere.
Definition: Sphere.h:71
Definition: Ray.h:17
ngl::Vec3 getCenter() const
Returns center of a sphere.
Definition: Sphere.h:76
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
ngl::Colour m_colour1
Colour of the object.
Definition: Material.h:121
~Sphere()
Destructor so that all memory is freed.
Definition: Sphere.h:52
Sphere()
Sphere constructor, by default it will instanciate a 1 unit radius sphere in the origin (0...
Definition: Sphere.h:29
Semi abstract class with virtual methods that holds all the calls for getting intersections, getting normals, also for specifying the properties of the material that will be hold by the Material member of this class.
Definition: Shape.h:21
char m_type
Type of geometric object it is.
Definition: Shape.h:88
Sphere(ngl::Vec3 _center, float _radius, ngl::Colour _colour)
Sphere ctor using a radius and a center.
Definition: Sphere.h:42
This class handles the implementation of ray: an object with an origin and a direction.
Definition: Plane.cpp:9
double m_radius
Radius of the sphere.
Definition: Sphere.h:114