INFO804 Introduction à l'informatique graphique
Loading...
Searching...
No Matches
PointLight.h
Go to the documentation of this file.
1/**
2@file PointLight.h
3@author JOL
4*/
5#pragma once
6#ifndef _POINT_LIGHT_H_
7#define _POINT_LIGHT_H_
8
9#include <QGLViewer/manipulatedFrame.h>
10#include "Light.h"
11
12/// Namespace RayTracer
13namespace rt {
14
15 /// This structure defines a point light, which may be at an
16 /// infinite distance. Such light does not suffer from any
17 /// attenuation. One can also draw it in order to be
18 /// displayed and manipulated.
19 struct PointLight : public Light {
20 /// Specifies which OpenGL light it is (necessary for draw())
21 GLenum number; // GL_LIGHT0, GL_LIGHT1, etc
22 /// The position of the light in homogeneous coordinates
24 /// The emission color of the light.
26 /// The material (global to the light).
28 /// Used to store a manipulator to move the light in space.
29 qglviewer::ManipulatedFrame* manipulator;
30
31 /// Constructor. \a light_number must be different for every light
32 /// (GL_LIGHT0, GL_LIGHT1, etc).
33 PointLight( GLenum light_number,
34 Point4 pos,
35 Color emission_color,
36 Color ambient_color = Color( 0.0, 0.0, 0.0 ),
37 Color diffuse_color = Color( 1.0, 1.0, 1.0 ),
38 Color specular_color = Color( 1.0, 1.0, 1.0 ) )
39 : number( light_number ), position( pos ), emission( emission_color ),
40 material( ambient_color, diffuse_color, specular_color ),
41 manipulator( 0 )
42 {}
43
44 /// Destructor.
46 {
47 if ( manipulator != 0 ) delete manipulator;
48 }
49
50 /// This method is called by Scene::init() at the beginning of the
51 /// display in the OpenGL window.
52 void init( Viewer& viewer )
53 {
54 glMatrixMode(GL_MODELVIEW);
55 glLoadIdentity();
56 glEnable( number );
57 glLightfv( number, GL_AMBIENT, material.ambient );
58 glLightfv( number, GL_DIFFUSE, material.diffuse );
59 glLightfv( number, GL_SPECULAR, material.specular );
60 std::cout << "Init light at " << position << std::endl;
61 if ( position[ 3 ] != 0.0 ) // the point light is not at infinity
62 {
63 std::cout << "Init manipulator for light at " << position << std::endl;
64 manipulator = new qglviewer::ManipulatedFrame;
65 viewer.setMouseTracking( true );
66 manipulator->setPosition( position[ 0 ] / position[ 3 ],
67 position[ 1 ] / position[ 3 ],
68 position[ 2 ] / position[ 3 ] );
69 }
70 }
71
72 /// This method is called by Scene::light() at each frame to
73 /// set the lights in the OpenGL window.
74 void light( Viewer& /* viewer */ )
75 {
76 Point4 pos = position;
77 if ( manipulator != 0 )
78 {
79 qglviewer::Vec pos2 = manipulator->position();
80 pos[0] = float(pos2.x);
81 pos[1] = float(pos2.y);
82 pos[2] = float(pos2.z);
83 pos[3] = 1.0f;
84 position = pos;
85 }
86 glLightfv( number, GL_POSITION, pos);
87 }
88
89 /// This method is called by Scene::draw() at each frame to
90 /// redisplay objects in the OpenGL window.
91 void draw( Viewer& viewer )
92 {
93 if ( manipulator != 0 && manipulator->grabsMouse() )
94 viewer.drawSomeLight( number, 1.2f );
95 else
96 viewer.drawSomeLight( number );
97 }
98
99 /// Given the point \a p, returns the normalized direction to this light.
100 Vector3 direction( const Vector3& p ) const
101 {
102 Vector3 pos( position.data() );
103 if ( position[ 3 ] == 0.0 ) return pos / pos.norm() ;
104 pos /= position[ 3 ];
105 pos -= p;
106 return pos / pos.norm();
107 }
108
109 /// @return the color of this light viewed from the given point \a p.
110 Color color( const Vector3& /* p */ ) const
111 {
112 return emission;
113 }
114
115 };
116
117
118
119} // namespace rt
120
121#endif // #define _POINT_LIGHT_H_
void drawSomeLight(GLenum light) const
To call the protected method drawLight.
Definition Viewer.h:33
Namespace RayTracer.
Definition Color.h:11
Lights are used to give lights in a scene.
Definition Light.h:17
Color diffuse
diffuse color
Definition Material.h:22
Color ambient
ambient color
Definition Material.h:20
Color specular
specular color
Definition Material.h:24
GLenum number
Specifies which OpenGL light it is (necessary for draw())
Definition PointLight.h:21
PointLight(GLenum light_number, Point4 pos, Color emission_color, Color ambient_color=Color(0.0, 0.0, 0.0), Color diffuse_color=Color(1.0, 1.0, 1.0), Color specular_color=Color(1.0, 1.0, 1.0))
Definition PointLight.h:33
qglviewer::ManipulatedFrame * manipulator
Used to store a manipulator to move the light in space.
Definition PointLight.h:29
void light(Viewer &)
Definition PointLight.h:74
~PointLight()
Destructor.
Definition PointLight.h:45
Point4 position
The position of the light in homogeneous coordinates.
Definition PointLight.h:23
Color emission
The emission color of the light.
Definition PointLight.h:25
Material material
The material (global to the light).
Definition PointLight.h:27
void init(Viewer &viewer)
Definition PointLight.h:52
void draw(Viewer &viewer)
Definition PointLight.h:91
Vector3 direction(const Vector3 &p) const
Given the point p, returns the normalized direction to this light.
Definition PointLight.h:100
Color color(const Vector3 &) const
Definition PointLight.h:110