This tutorial demonstrates how to use the following light types: directional lights, point lights, spot lights, how to animate the position and direction of a light and how to attach a light to a camera.
class App_Lights: public BaseDemo
{
public:
virtual void initEvent()
{
mCameraLightType = Disable;
sceneManager()->tree()->addActor(ground.
get(), mLightFx.get());
for(int i=0; i<9; ++i)
{
sceneManager()->tree()->addActor(pillar.
get(), mLightFx.get());
}
for(int i=0; i<5; ++i)
{
sceneManager()->tree()->addActor(pillar.
get(), mLightFx.get());
}
mLightFx->shader()->setRenderState( mLight0.get(), 0 );
mLightFx->shader()->setRenderState( mLight1.get(), 1 );
mLightFx->shader()->setRenderState( mLight2.get(), 2 );
mLightFx->shader()->setRenderState( mLight3.get(), 3 );
mLight1->setDiffuse(vl::green);
mLight2->setDiffuse(vl::blue);
mLight3->setDiffuse(vl::white);
mLight4->setDiffuse(vl::white);
mLight0->setConstantAttenuation(1.0f);
mLight0->setLinearAttenuation(0.0f);
mLight0->setQuadraticAttenuation(0.0f);
mLight1->setConstantAttenuation(0.0f);
mLight1->setLinearAttenuation(1.0f);
mLight1->setQuadraticAttenuation(0.0f);
mLight2->setConstantAttenuation(0.0f);
mLight2->setLinearAttenuation(0.0f);
mLight2->setQuadraticAttenuation(1.0f);
mLight3->setSpotCutoff(45.0f);
mLight3->setSpotExponent(5.0f);
mLight3->setConstantAttenuation(1.0f);
mLight3->setLinearAttenuation(0.0f);
mLight3->setQuadraticAttenuation(0.0f);
rendering()->
as<
vl::Rendering>()->transform()->addChild(mLight0_Transform.get());
rendering()->
as<
vl::Rendering>()->transform()->addChild(mLight1_Transform.get());
rendering()->
as<
vl::Rendering>()->transform()->addChild(mLight2_Transform.get());
rendering()->
as<
vl::Rendering>()->transform()->addChild(mLight3_Transform.get());
mLight0->bindTransform(mLight0_Transform.get());
mLight1->bindTransform(mLight1_Transform.get());
mLight2->bindTransform(mLight2_Transform.get());
mLight3->bindTransform(mLight3_Transform.get());
mLight4->bindTransform(
NULL);
sceneManager()->tree()->addActor(bulb.
get(), bulb_fx.
get(), mLight0_Transform.get());
sceneManager()->tree()->addActor(bulb.
get(), bulb_fx.
get(), mLight1_Transform.get());
sceneManager()->tree()->addActor(bulb.
get(), bulb_fx.
get(), mLight2_Transform.get());
sceneManager()->tree()->addActor(bulb.
get(), bulb_fx.
get(), mLight3_Transform.get());
mText->translate(0,-10,0);
sceneManager()->tree()->addActor(mText.get(), text_fx.
get());
updateText();
}
void updateText()
{
switch(mCameraLightType)
{
case Disable: mText->setText("Camera light: Disable"); break;
case DirectionalLight: mText->setText("Camera light: Directional Light"); break;
case PositionalLight: mText->setText("Camera light: Positional Light"); break;
case SpotLight: mText->setText("Camera light: Spot Light"); break;
}
}
void keyPressEvent(
unsigned short ch,
vl::EKey key)
{
BaseDemo::keyPressEvent(ch, key);
{
mCameraLightType = (ECameraLightType)((mCameraLightType+1)%4);
switch(mCameraLightType)
{
case Disable:
mLightFx->shader()->eraseRenderState(mLight4.get(), 4);
break;
case DirectionalLight:
mLightFx->shader()->setRenderState(mLight4.get(), 4);
mLight4->setSpotCutoff(180.0f);
break;
case PositionalLight:
mLightFx->shader()->setRenderState(mLight4.get(), 4);
mLight4->setConstantAttenuation(1.0f);
mLight4->setLinearAttenuation(0.0f);
mLight4->setQuadraticAttenuation(0.0f);
mLight4->setSpotCutoff(180.0f);
break;
case SpotLight:
mLightFx->shader()->setRenderState(mLight4.get(), 4);
mLight4->setConstantAttenuation(1.0f);
mLight4->setLinearAttenuation(0.0f);
mLight4->setQuadraticAttenuation(0.0f);
mLight4->setSpotCutoff(25.0f);
mLight4->setSpotExponent(40.0f);
mLight4->setSpotDirection(
vl::fvec3(0,0,-1));
break;
}
}
updateText();
}
virtual void updateScene()
{
vl::real phase = 120.0f;
mLight0_Transform->setLocalMatrix(m);
mLight1_Transform->setLocalMatrix(m);
mLight2_Transform->setLocalMatrix(m);
mLight3_Transform->setLocalMatrix(m);
}
protected:
enum ECameraLightType { Disable, DirectionalLight, PositionalLight, SpotLight } mCameraLightType;
};