Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <vlGraphics/Light.hpp>
00033 #include <vlCore/Transform.hpp>
00034 #include <vlGraphics/Camera.hpp>
00035 #include <vlCore/Log.hpp>
00036 #include <vlCore/Say.hpp>
00037
00038 using namespace vl;
00039
00040
00041
00042
00043 Light::Light()
00044 {
00045 VL_DEBUG_SET_OBJECT_NAME()
00046 mAmbient = fvec4(0,0,0,1);
00047 mDiffuse = fvec4(1,1,1,1);
00048 mSpecular = fvec4(1,1,1,1);
00049 mPosition = fvec4(0,0,0,1);
00050 mSpotDirection = fvec3(0,0,-1);
00051 mSpotExponent = 0;
00052 mSpotCutoff = 180.0f;
00053 mConstantAttenuation = 1.0f;
00054 mLinearAttenuation = 0.0f;
00055 mQuadraticAttenuation = 0.0f;
00056 mBoundTransform = NULL;
00057 mEnabled = true;
00058 }
00059
00060 void Light::apply(int index, const Camera* camera, OpenGLContext*) const
00061 {
00062 VL_CHECK_OGL()
00063
00064 if (enabled())
00065 {
00066 glEnable (GL_LIGHT0 + index); VL_CHECK_OGL()
00067
00068 glMatrixMode(GL_MODELVIEW);
00069 glPushMatrix();
00070
00071
00072 if ( boundTransform() )
00073 camera->applyModelViewMatrix( boundTransform()->worldMatrix() );
00074 else
00075 {
00076
00077
00078 glLoadIdentity();
00079 }
00080
00081 glLightfv(GL_LIGHT0+index, GL_AMBIENT, mAmbient.ptr());
00082 glLightfv(GL_LIGHT0+index, GL_DIFFUSE, mDiffuse.ptr());
00083 glLightfv(GL_LIGHT0+index, GL_SPECULAR, mSpecular.ptr());
00084 glLightfv(GL_LIGHT0+index, GL_POSITION, mPosition.ptr());
00085
00086 glLightf(GL_LIGHT0+index, GL_SPOT_CUTOFF, mSpotCutoff);
00087
00088
00089 if (mSpotCutoff != 180.0f)
00090 {
00091 VL_CHECK(mSpotCutoff>=0.0f && mSpotCutoff<=90.0f);
00092 glLightfv(GL_LIGHT0+index, GL_SPOT_DIRECTION, mSpotDirection.ptr());
00093 glLightf(GL_LIGHT0+index, GL_SPOT_EXPONENT, mSpotExponent);
00094 }
00095
00096
00097
00098 if (mSpotCutoff != 180.0f || mPosition.w() != 0)
00099 {
00100 glLightf(GL_LIGHT0+index, GL_CONSTANT_ATTENUATION, mConstantAttenuation);
00101 glLightf(GL_LIGHT0+index, GL_LINEAR_ATTENUATION, mLinearAttenuation);
00102 glLightf(GL_LIGHT0+index, GL_QUADRATIC_ATTENUATION, mQuadraticAttenuation);
00103 }
00104
00105
00106 glPopMatrix();
00107 }
00108 else
00109 {
00110 glDisable(GL_LIGHT0 + index);
00111 }
00112 }
00113
00114 void Light::bindTransform(Transform* transform)
00115 {
00116 mBoundTransform = transform;
00117 }
00118
00119 Transform* Light::boundTransform()
00120 {
00121 return mBoundTransform.get();
00122 }
00123
00124 const Transform* Light::boundTransform() const
00125 {
00126 return mBoundTransform.get();
00127 }
00128