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(int light_index)
00044 {
00045 VL_DEBUG_SET_OBJECT_NAME()
00046 if (light_index<0 && light_index>7)
00047 Log::error( Say("Light index %n out of range. The light index must be between 0 and 7.\n") << light_index );
00048 VL_CHECK(light_index>=0 && light_index<8)
00049 mLightIndex = light_index;
00050 mAmbient = fvec4(0,0,0,1);
00051 mDiffuse = fvec4(1,1,1,1);
00052 mSpecular = fvec4(1,1,1,1);
00053 mPosition = fvec4(0,0,0,1);
00054 mSpotDirection = fvec3(0,0,-1);
00055 mSpotExponent = 0;
00056 mSpotCutoff = 180.0f;
00057 mConstantAttenuation = 1.0f;
00058 mLinearAttenuation = 0.0f;
00059 mQuadraticAttenuation = 0.0f;
00060 mFollowedTransform = NULL;
00061 }
00062
00063 void Light::setLightIndex(int light_index)
00064 {
00065 if (light_index<0 && light_index>7)
00066 Log::error( Say("Light index %n out of range. The light index must be between 0 and 7.\n") << light_index );
00067 VL_CHECK(light_index>=0 && light_index<8)
00068 mLightIndex = light_index;
00069 }
00070
00071 void Light::apply(const Camera* camera, OpenGLContext* ctx) const
00072 {
00073 if (camera)
00074 {
00075 glEnable (GL_LIGHT0 + lightIndex());
00076
00077 glMatrixMode(GL_MODELVIEW);
00078 glPushMatrix();
00079
00080
00081 if ( followedTransform() )
00082 camera->applyModelViewMatrix( followedTransform()->worldMatrix() );
00083 else
00084 {
00085
00086
00087 glLoadIdentity();
00088 }
00089
00090 glLightfv(GL_LIGHT0+lightIndex(), GL_AMBIENT, mAmbient.ptr());
00091 glLightfv(GL_LIGHT0+lightIndex(), GL_DIFFUSE, mDiffuse.ptr());
00092 glLightfv(GL_LIGHT0+lightIndex(), GL_SPECULAR, mSpecular.ptr());
00093 glLightfv(GL_LIGHT0+lightIndex(), GL_POSITION, mPosition.ptr());
00094
00095 glLightf(GL_LIGHT0+lightIndex(), GL_SPOT_CUTOFF, mSpotCutoff);
00096
00097
00098 if (mSpotCutoff != 180.0f)
00099 {
00100 VL_CHECK(mSpotCutoff>=0.0f && mSpotCutoff<=90.0f);
00101 glLightfv(GL_LIGHT0+lightIndex(), GL_SPOT_DIRECTION, mSpotDirection.ptr());
00102 glLightf(GL_LIGHT0+lightIndex(), GL_SPOT_EXPONENT, mSpotExponent);
00103 }
00104
00105
00106
00107 if (mSpotCutoff != 180.0f || mPosition.w() != 0)
00108 {
00109 glLightf(GL_LIGHT0+lightIndex(), GL_CONSTANT_ATTENUATION, mConstantAttenuation);
00110 glLightf(GL_LIGHT0+lightIndex(), GL_LINEAR_ATTENUATION, mLinearAttenuation);
00111 glLightf(GL_LIGHT0+lightIndex(), GL_QUADRATIC_ATTENUATION, mQuadraticAttenuation);
00112 }
00113
00114
00115 glPopMatrix();
00116 }
00117 else
00118 {
00119 glDisable(GL_LIGHT0 + lightIndex());
00120 }
00121 }
00122
00123 void Light::followTransform(Transform* transform)
00124 {
00125 mFollowedTransform = transform;
00126 }
00127
00128 Transform* Light::followedTransform()
00129 {
00130 return mFollowedTransform.get();
00131 }
00132
00133 const Transform* Light::followedTransform() const
00134 {
00135 return mFollowedTransform.get();
00136 }
00137