INFO804 Introduction à l'informatique graphique
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
rt::Sphere Struct Reference

A sphere is a concrete GraphicalObject that represents a sphere in 3D space. More...

#include <Sphere.h>

Inheritance diagram for rt::Sphere:
rt::GraphicalObject

Public Member Functions

virtual ~Sphere ()
 Virtual destructor since object contains virtual methods.
 
 Sphere (Point3 xc, Real r, const Material &m)
 Creates a sphere of center xc and radius r.
 
Point3 localize (Real latitude, Real longitude) const
 
void init (Viewer &)
 
void draw (Viewer &viewer)
 
Vector3 getNormal (Point3 p)
 
Material getMaterial (Point3 p)
 
Real rayIntersection (const Ray &ray, Point3 &p)
 
- Public Member Functions inherited from rt::GraphicalObject
 GraphicalObject ()
 Default constructor. Nothing to do.
 
virtual ~GraphicalObject ()
 Virtual destructor since object contains virtual methods.
 

Public Attributes

Point3 center
 The center of the sphere.
 
Real radius
 The radius of the sphere.
 
Material material
 The material (global to the sphere).
 

Static Public Attributes

static const int NLAT = 16
 number of different latitudes for display
 
static const int NLON = 24
 number of different longitudes for display
 

Detailed Description

A sphere is a concrete GraphicalObject that represents a sphere in 3D space.

Definition at line 15 of file Sphere.h.

Constructor & Destructor Documentation

◆ ~Sphere()

virtual rt::Sphere::~Sphere ( )
inlinevirtual

Virtual destructor since object contains virtual methods.

Definition at line 21 of file Sphere.h.

21{}

◆ Sphere()

rt::Sphere::Sphere ( Point3  xc,
Real  r,
const Material m 
)
inline

Creates a sphere of center xc and radius r.

Definition at line 24 of file Sphere.h.

25 : GraphicalObject(), center( xc ), radius( r ), material( m )
26 {}
GraphicalObject()
Default constructor. Nothing to do.
Point3 center
The center of the sphere.
Definition Sphere.h:62
Real radius
The radius of the sphere.
Definition Sphere.h:64
Material material
The material (global to the sphere).
Definition Sphere.h:66

Member Function Documentation

◆ draw()

void rt::Sphere::draw ( Viewer viewer)
virtual

This method is called by Scene::draw() at each frame to redisplay objects in the OpenGL window.

Implements rt::GraphicalObject.

Definition at line 8 of file Sphere.cpp.

9{
10 Material m = material;
11 // Taking care of south pole
12 glBegin( GL_TRIANGLE_FAN );
13 glColor4fv( m.ambient );
14 glMaterialfv(GL_FRONT, GL_DIFFUSE, m.diffuse);
15 glMaterialfv(GL_FRONT, GL_SPECULAR, m.specular);
16 glMaterialf(GL_FRONT, GL_SHININESS, m.shinyness );
17 Point3 south_pole = localize( -90, 0 );
18 glNormal3fv( getNormal( south_pole ) );
19 glVertex3fv( south_pole );
20 for ( int x = 0; x <= NLON; ++x )
21 {
22 Point3 p = localize( -90 + 180/NLAT, x * 360 / NLON );
23 glNormal3fv( getNormal( p ) );
24 glVertex3fv( p );
25 }
26 glEnd();
27 // Taking care of in-between poles
28 for ( int y = 1; y < NLAT - 1; ++y )
29 {
30 glBegin( GL_QUAD_STRIP);
31 glColor4fv( m.ambient );
32 glMaterialfv(GL_FRONT, GL_DIFFUSE, m.diffuse);
33 glMaterialfv(GL_FRONT, GL_SPECULAR, m.specular);
34 glMaterialf(GL_FRONT, GL_SHININESS, m.shinyness );
35 for ( int x = 0; x <= NLON; ++x )
36 {
37 Point3 p = localize( -90 + y*180/NLAT, x * 360 / NLON );
38 Point3 q = localize( -90 + (y+1)*180/NLAT, x * 360 / NLON );
39 glNormal3fv( getNormal( p ) );
40 glVertex3fv( p );
41 glNormal3fv( getNormal( q ) );
42 glVertex3fv( q );
43 }
44 glEnd();
45 }
46 // Taking care of north pole
47 glBegin( GL_TRIANGLE_FAN );
48 glColor4fv( m.ambient );
49 glMaterialfv(GL_FRONT, GL_DIFFUSE, m.diffuse);
50 glMaterialfv(GL_FRONT, GL_SPECULAR, m.specular);
51 glMaterialf(GL_FRONT, GL_SHININESS, m.shinyness );
52 Point3 north_pole = localize( 90, 0 );
53 glNormal3fv( getNormal( north_pole ) );
54 glVertex3fv( north_pole );
55 for ( int x = NLON; x >= 0; --x )
56 {
57 Point3 p = localize( -90 + (NLAT-1)*180/NLAT, x * 360 / NLON );
58 glNormal3fv( getNormal( p ) );
59 glVertex3fv( p );
60 }
61 glEnd();
62}
PointVector< Real, 3 > Point3
The type for representing a 3d vector;.
static const int NLAT
number of different latitudes for display
Definition Sphere.h:17
Vector3 getNormal(Point3 p)
Definition Sphere.cpp:77
Point3 localize(Real latitude, Real longitude) const
Definition Sphere.cpp:65
static const int NLON
number of different longitudes for display
Definition Sphere.h:18

◆ getMaterial()

rt::Material rt::Sphere::getMaterial ( Point3  p)
virtual
Returns
the material associated to this part of the object

Implements rt::GraphicalObject.

Definition at line 86 of file Sphere.cpp.

87{
88 return material; // the material is constant along the sphere.
89}

◆ getNormal()

rt::Vector3 rt::Sphere::getNormal ( Point3  p)
virtual
Returns
the normal vector at point p on the sphere (p should be on or close to the sphere).

Implements rt::GraphicalObject.

Definition at line 77 of file Sphere.cpp.

78{
79 Vector3 u = p - center;
80 Real l2 = u.dot( u );
81 if ( l2 != 0.0 ) u /= sqrt( l2 );
82 return u;
83}
float Real
the type for representing a real number.
PointVector< Real, 3 > Vector3
The type for representing a 3d point.
T dot(const Self &other) const
dot product (produit scalaire).

◆ init()

void rt::Sphere::init ( Viewer )
inlinevirtual

This method is called by Scene::init() at the beginning of the display in the OpenGL window. May be useful for some precomputations.

Implements rt::GraphicalObject.

Definition at line 38 of file Sphere.h.

38{}

◆ localize()

rt::Point3 rt::Sphere::localize ( Real  latitude,
Real  longitude 
) const

Given latitude and longitude in degrees, returns the point on the sphere at these coordinates.

Definition at line 65 of file Sphere.cpp.

66{
67 static const Real conv_deg_rad = 2.0 * M_PI / 360.0;
68 latitude *= conv_deg_rad;
69 longitude *= conv_deg_rad;
70 return center
71 + radius * Point3( cos( longitude ) * cos( latitude ),
72 sin( longitude ) * cos( latitude ),
73 sin( latitude ) );
74}

◆ rayIntersection()

rt::Real rt::Sphere::rayIntersection ( const Ray ray,
Point3 p 
)
virtual
Parameters
[in]raythe incoming ray
[out]returnsthe point of intersection with the object (if any), or the closest point to it.
Returns
either a real < 0.0 if there is an intersection, or a kind of distance to the closest point of intersection.

Implements rt::GraphicalObject.

Definition at line 92 of file Sphere.cpp.

93{
94 // TO DO
95 return 1.0f;
96}

Member Data Documentation

◆ center

Point3 rt::Sphere::center

The center of the sphere.

Definition at line 62 of file Sphere.h.

◆ material

Material rt::Sphere::material

The material (global to the sphere).

Definition at line 66 of file Sphere.h.

◆ NLAT

const int rt::Sphere::NLAT = 16
static

number of different latitudes for display

Definition at line 17 of file Sphere.h.

◆ NLON

const int rt::Sphere::NLON = 24
static

number of different longitudes for display

Definition at line 18 of file Sphere.h.

◆ radius

Real rt::Sphere::radius

The radius of the sphere.

Definition at line 64 of file Sphere.h.


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