Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Light.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include <vlGraphics/Light.hpp>
33 #include <vlGraphics/OpenGL.hpp>
34 #include <vlCore/Transform.hpp>
35 #include <vlGraphics/Camera.hpp>
36 #include <vlCore/Log.hpp>
37 #include <vlCore/Say.hpp>
38 
39 using namespace vl;
40 
41 //------------------------------------------------------------------------------
42 // Light
43 //------------------------------------------------------------------------------
45 {
46  VL_DEBUG_SET_OBJECT_NAME()
47  mAmbient = fvec4(0,0,0,1);
48  mDiffuse = fvec4(1,1,1,1);
49  mSpecular = fvec4(1,1,1,1);
50  mPosition = fvec4(0,0,0,1);
51  mSpotDirection = fvec3(0,0,-1);
52  mSpotExponent = 0;
53  mSpotCutoff = 180.0f;
54  mConstantAttenuation = 1.0f;
55  mLinearAttenuation = 0.0f;
56  mQuadraticAttenuation = 0.0f;
58  mEnabled = true;
59 }
60 //------------------------------------------------------------------------------
61 void Light::apply(int index, const Camera* camera, OpenGLContext*) const
62 {
63  VL_CHECK_OGL()
64 
65  if (enabled())
66  {
67  glEnable (GL_LIGHT0 + index); VL_CHECK_OGL()
68 
69  glMatrixMode(GL_MODELVIEW);
70  glPushMatrix();
71 
72  // follows the given node
73  if ( boundTransform() )
74  camera->applyModelViewMatrix( boundTransform()->worldMatrix() );
75  else
76  {
77  // follows the camera
78  /*glMatrixMode(GL_MODELVIEW);*/
79  glLoadIdentity();
80  }
81 
82  glLightfv(GL_LIGHT0+index, GL_AMBIENT, mAmbient.ptr());
83  glLightfv(GL_LIGHT0+index, GL_DIFFUSE, mDiffuse.ptr());
84  glLightfv(GL_LIGHT0+index, GL_SPECULAR, mSpecular.ptr());
85  glLightfv(GL_LIGHT0+index, GL_POSITION, mPosition.ptr());
86 
87  glLightf(GL_LIGHT0+index, GL_SPOT_CUTOFF, mSpotCutoff);
88 
89  // if its a spot light
90  if (mSpotCutoff != 180.0f)
91  {
92  VL_CHECK(mSpotCutoff>=0.0f && mSpotCutoff<=90.0f);
93  glLightfv(GL_LIGHT0+index, GL_SPOT_DIRECTION, mSpotDirection.ptr());
94  glLightf(GL_LIGHT0+index, GL_SPOT_EXPONENT, mSpotExponent);
95  }
96 
97  // if positional or spot light compute the attenuation factors, that is
98  // attenuation is useless of directional lights.
99  if (mSpotCutoff != 180.0f || mPosition.w() != 0)
100  {
101  glLightf(GL_LIGHT0+index, GL_CONSTANT_ATTENUATION, mConstantAttenuation);
102  glLightf(GL_LIGHT0+index, GL_LINEAR_ATTENUATION, mLinearAttenuation);
103  glLightf(GL_LIGHT0+index, GL_QUADRATIC_ATTENUATION, mQuadraticAttenuation);
104  }
105 
106  /*glMatrixMode(GL_MODELVIEW);*/
107  glPopMatrix();
108  }
109  else
110  {
111  glDisable(GL_LIGHT0 + index);
112  }
113 }
114 //------------------------------------------------------------------------------
116 {
117  mBoundTransform = transform;
118 }
119 //------------------------------------------------------------------------------
121 {
122  return mBoundTransform.get();
123 }
124 //------------------------------------------------------------------------------
126 {
127  return mBoundTransform.get();
128 }
129 //------------------------------------------------------------------------------
float mConstantAttenuation
Definition: Light.hpp:136
Vector3< float > fvec3
A 3 components vector with float precision.
Definition: Vector3.hpp:253
Implements a 4x4 matrix transform used to define the position and orientation of an Actor...
Definition: Transform.hpp:72
Vector4< float > fvec4
A 4 components vector with float precision.
Definition: Vector4.hpp:280
float mLinearAttenuation
Definition: Light.hpp:137
Represents an OpenGL context, possibly a widget or a pbuffer, which can also respond to keyboard...
virtual void apply(int index, const Camera *, OpenGLContext *ctx) const
The parameter cameara is NULL if we are disabling the state, non-NULL if we are enabling it...
Definition: Light.cpp:61
float mSpotCutoff
Definition: Light.hpp:135
Visualization Library main namespace.
fvec4 mSpecular
Definition: Light.hpp:131
float mSpotExponent
Definition: Light.hpp:134
fvec4 mPosition
Definition: Light.hpp:132
fvec4 mDiffuse
Definition: Light.hpp:130
fvec3 mSpotDirection
Definition: Light.hpp:133
bool enabled() const
Definition: Light.hpp:126
Transform * boundTransform()
Definition: Light.cpp:120
ref< Transform > mBoundTransform
Definition: Light.hpp:139
Light()
Definition: Light.cpp:44
#define NULL
Definition: OpenGLDefs.hpp:81
T_Scalar * ptr()
Definition: Vector3.hpp:87
#define VL_CHECK_OGL()
Definition: OpenGL.hpp:156
T_Scalar * ptr()
Definition: Vector4.hpp:99
void applyModelViewMatrix(const mat4 &model_matrix) const
Loads the GL_MODELVIEW matrix with the Camera&#39;s view matrix multiplied by the specified model matrix...
Definition: Camera.cpp:60
void bindTransform(Transform *transform)
If NULL follows the camera otherwise the given transformation node.
Definition: Light.cpp:115
fvec4 mAmbient
Definition: Light.hpp:129
bool mEnabled
Definition: Light.hpp:140
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:49
#define VL_CHECK(expr)
Definition: checks.hpp:73
float mQuadraticAttenuation
Definition: Light.hpp:138
const T_Scalar & w() const
Definition: Vector4.hpp:105