Visualization Library 2.0.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 <vlCore/Transform.hpp>
34 #include <vlGraphics/Camera.hpp>
35 #include <vlCore/Log.hpp>
36 #include <vlCore/Say.hpp>
37 
38 using namespace vl;
39 
40 //------------------------------------------------------------------------------
41 // Light
42 //------------------------------------------------------------------------------
44 {
45  VL_DEBUG_SET_OBJECT_NAME()
46  mAmbient = fvec4(0,0,0,1);
47  mDiffuse = fvec4(1,1,1,1);
48  mSpecular = fvec4(1,1,1,1);
49  mPosition = fvec4(0,0,0,1);
50  mSpotDirection = fvec3(0,0,-1);
51  mSpotExponent = 0;
52  mSpotCutoff = 180.0f;
53  mConstantAttenuation = 1.0f;
54  mLinearAttenuation = 0.0f;
55  mQuadraticAttenuation = 0.0f;
57  mEnabled = true;
58 }
59 //------------------------------------------------------------------------------
60 void Light::apply(int index, const Camera* camera, OpenGLContext*) const
61 {
62  VL_CHECK_OGL()
63 
64  if (enabled())
65  {
66  glEnable (GL_LIGHT0 + index); VL_CHECK_OGL()
67 
68  glMatrixMode(GL_MODELVIEW);
69  glPushMatrix();
70 
71  // follows the given node
72  if ( boundTransform() )
73  camera->applyModelViewMatrix( boundTransform()->worldMatrix() );
74  else
75  {
76  // follows the camera
77  /*glMatrixMode(GL_MODELVIEW);*/
78  glLoadIdentity();
79  }
80 
81  glLightfv(GL_LIGHT0+index, GL_AMBIENT, mAmbient.ptr());
82  glLightfv(GL_LIGHT0+index, GL_DIFFUSE, mDiffuse.ptr());
83  glLightfv(GL_LIGHT0+index, GL_SPECULAR, mSpecular.ptr());
84  glLightfv(GL_LIGHT0+index, GL_POSITION, mPosition.ptr());
85 
86  glLightf(GL_LIGHT0+index, GL_SPOT_CUTOFF, mSpotCutoff);
87 
88  // if its a spot light
89  if (mSpotCutoff != 180.0f)
90  {
91  VL_CHECK(mSpotCutoff>=0.0f && mSpotCutoff<=90.0f);
92  glLightfv(GL_LIGHT0+index, GL_SPOT_DIRECTION, mSpotDirection.ptr());
93  glLightf(GL_LIGHT0+index, GL_SPOT_EXPONENT, mSpotExponent);
94  }
95 
96  // if positional or spot light compute the attenuation factors, that is
97  // attenuation is useless of directional lights.
98  if (mSpotCutoff != 180.0f || mPosition.w() != 0)
99  {
100  glLightf(GL_LIGHT0+index, GL_CONSTANT_ATTENUATION, mConstantAttenuation);
101  glLightf(GL_LIGHT0+index, GL_LINEAR_ATTENUATION, mLinearAttenuation);
102  glLightf(GL_LIGHT0+index, GL_QUADRATIC_ATTENUATION, mQuadraticAttenuation);
103  }
104 
105  /*glMatrixMode(GL_MODELVIEW);*/
106  glPopMatrix();
107  }
108  else
109  {
110  glDisable(GL_LIGHT0 + index);
111  }
112 }
113 //------------------------------------------------------------------------------
115 {
116  mBoundTransform = transform;
117 }
118 //------------------------------------------------------------------------------
120 {
121  return mBoundTransform.get();
122 }
123 //------------------------------------------------------------------------------
125 {
126  return mBoundTransform.get();
127 }
128 //------------------------------------------------------------------------------
float mConstantAttenuation
Definition: Light.hpp:136
Vector3< float > fvec3
A 3 components vector with float precision.
Definition: Vector3.hpp:252
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:279
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:60
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:119
ref< Transform > mBoundTransform
Definition: Light.hpp:139
Light()
Definition: Light.cpp:43
#define NULL
Definition: OpenGLDefs.hpp:81
T_Scalar * ptr()
Definition: Vector3.hpp:86
#define VL_CHECK_OGL()
Definition: OpenGL.hpp:156
T_Scalar * ptr()
Definition: Vector4.hpp:98
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:114
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:50
#define VL_CHECK(expr)
Definition: checks.hpp:73
float mQuadraticAttenuation
Definition: Light.hpp:138
const T_Scalar & w() const
Definition: Vector4.hpp:104