INFO804 Introduction à l'informatique graphique
Loading...
Searching...
No Matches
Sphere.cpp
Go to the documentation of this file.
1/**
2@file Sphere.cpp
3*/
4#include <cmath>
5#include "Sphere.h"
6
7void
8rt::Sphere::draw( Viewer& /* viewer */ )
9{
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}
63
65rt::Sphere::localize( Real latitude, Real longitude ) const
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}
75
78{
79 Vector3 u = p - center;
80 Real l2 = u.dot( u );
81 if ( l2 != 0.0 ) u /= sqrt( l2 );
82 return u;
83}
84
87{
88 return material; // the material is constant along the sphere.
89}
90
93{
94 // TO DO
95 return 1.0f;
96}
float Real
the type for representing a real number.
PointVector< Real, 3 > Point3
The type for representing a 3d vector;.
Real shinyness
shinyness (50: very high, 1: low )
Definition Material.h:26
Color diffuse
diffuse color
Definition Material.h:22
Color ambient
ambient color
Definition Material.h:20
Color specular
specular color
Definition Material.h:24
T dot(const Self &other) const
dot product (produit scalaire).
Definition Ray.h:18
static const int NLAT
number of different latitudes for display
Definition Sphere.h:17
Vector3 getNormal(Point3 p)
Definition Sphere.cpp:77
void draw(Viewer &viewer)
Definition Sphere.cpp:8
Point3 localize(Real latitude, Real longitude) const
Definition Sphere.cpp:65
Real rayIntersection(const Ray &ray, Point3 &p)
Definition Sphere.cpp:92
Material material
The material (global to the sphere).
Definition Sphere.h:66
Material getMaterial(Point3 p)
Definition Sphere.cpp:86
static const int NLON
number of different longitudes for display
Definition Sphere.h:18