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 }
00058
00059 void Light::apply(int index, const Camera* camera, OpenGLContext*) const
00060 {
00061 VL_CHECK_OGL()
00062
00063 if (camera)
00064 {
00065 glEnable (GL_LIGHT0 + index); VL_CHECK_OGL()
00066
00067 glMatrixMode(GL_MODELVIEW);
00068 glPushMatrix();
00069
00070
00071 if ( boundTransform() )
00072 camera->applyModelViewMatrix( boundTransform()->worldMatrix() );
00073 else
00074 {
00075
00076
00077 glLoadIdentity();
00078 }
00079
00080 glLightfv(GL_LIGHT0+index, GL_AMBIENT, mAmbient.ptr());
00081 glLightfv(GL_LIGHT0+index, GL_DIFFUSE, mDiffuse.ptr());
00082 glLightfv(GL_LIGHT0+index, GL_SPECULAR, mSpecular.ptr());
00083 glLightfv(GL_LIGHT0+index, GL_POSITION, mPosition.ptr());
00084
00085 glLightf(GL_LIGHT0+index, GL_SPOT_CUTOFF, mSpotCutoff);
00086
00087
00088 if (mSpotCutoff != 180.0f)
00089 {
00090 VL_CHECK(mSpotCutoff>=0.0f && mSpotCutoff<=90.0f);
00091 glLightfv(GL_LIGHT0+index, GL_SPOT_DIRECTION, mSpotDirection.ptr());
00092 glLightf(GL_LIGHT0+index, GL_SPOT_EXPONENT, mSpotExponent);
00093 }
00094
00095
00096
00097 if (mSpotCutoff != 180.0f || mPosition.w() != 0)
00098 {
00099 glLightf(GL_LIGHT0+index, GL_CONSTANT_ATTENUATION, mConstantAttenuation);
00100 glLightf(GL_LIGHT0+index, GL_LINEAR_ATTENUATION, mLinearAttenuation);
00101 glLightf(GL_LIGHT0+index, GL_QUADRATIC_ATTENUATION, mQuadraticAttenuation);
00102 }
00103
00104
00105 glPopMatrix();
00106 }
00107 else
00108 {
00109 glDisable(GL_LIGHT0 + index);
00110 }
00111 }
00112
00113 void Light::bindTransform(Transform* transform)
00114 {
00115 mBoundTransform = transform;
00116 }
00117
00118 Transform* Light::boundTransform()
00119 {
00120 return mBoundTransform.get();
00121 }
00122
00123 const Transform* Light::boundTransform() const
00124 {
00125 return mBoundTransform.get();
00126 }
00127