[osg-users] 2 spotlights with the spotlight example ?
josselin.petit at lcpc.fr
josselin.petit at lcpc.fr
Wed Jul 30 23:48:14 PDT 2008
Hello,
I'm new in OSG.
I would like to have 2 spotlights in the spotlight example. I created
a second spotlight, a second texgen and a texgennode associated, I
enabled my second light, assigned texturesAttibutesAndModes, but that
don't work (with just one light that works fine).
Any Idea about this problem? (you can find the code below)
Thanks in advance,
Josselin.
#include <osg/Notify>
#include <osg/MatrixTransform>
#include <osg/ShapeDrawable>
#include <osg/PositionAttitudeTransform>
#include <osg/Geometry>
#include <osg/Texture2D>
#include <osg/Geode>
#include <osg/LightSource>
#include <osg/TexGenNode>
#include <osgUtil/Optimizer>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <iostream>
// for the grid data..
#include "terrain_coords.h"
osg::Image* createSpotLightImage(const osg::Vec4& centerColour, const
osg::Vec4& backgroudColour, unsigned int size, float power)
{
osg::Image* image = new osg::Image;
image->allocateImage(size,size,1,GL_RGBA,GL_UNSIGNED_BYTE);
float mid = (float(size)-1)*0.5f;
float div = 2.0f/float(size);
for(unsigned int r=0;r<size;++r)
{
unsigned char* ptr = image->data(0,r,0);
for(unsigned int c=0;c<size;++c)
{
float dx = (float(c) - mid)*div;
float dy = (float(r) - mid)*div;
float r = powf(1.0f-sqrtf(dx*dx+dy*dy),power);
if (r<0.0f) r=0.0f;
osg::Vec4 color = centerColour*r+backgroudColour*(1.0f-r);
*ptr++ = (unsigned char)((color[0])*255.0f);
*ptr++ = (unsigned char)((color[1])*255.0f);
*ptr++ = (unsigned char)((color[2])*255.0f);
*ptr++ = (unsigned char)((color[3])*255.0f);
//std::cout << ((color[0])*255.0f) << ", " << ((color[1])*255.0f)
<< ", " << ((color[2])*255.0f) << "." << std::endl;
}
}
return image;
}
osg::StateSet* create2SpotLightDecoratorState(unsigned int lightNum1,
unsigned int textureUnit1, unsigned int lightNum2, unsigned int
textureUnit2)
{
osg::StateSet* stateset = new osg::StateSet;
stateset->setMode(GL_LIGHT0+lightNum1, osg::StateAttribute::ON);
stateset->setMode(GL_LIGHT0+lightNum2, osg::StateAttribute::ON);
osg::Vec4 centerColour(1.0f,1.0f,1.0f,1.0f);
osg::Vec4 ambientColour(0.05f,0.05f,0.05f,1.0f);
// set up spot light texture
osg::Image* spotLightImage = createSpotLightImage(centerColour,
ambientColour, 64, 1.0);
osg::Texture2D* texture1 = new osg::Texture2D();
texture1->setImage(spotLightImage); // blanc au milieu, plus
sombre sur les cotés
texture1->setBorderColor(osg::Vec4(ambientColour));
texture1->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_BORDER);
texture1->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_BORDER);
texture1->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_BORDER);
stateset->setTextureAttributeAndModes(textureUnit1, texture1,
osg::StateAttribute::ON);
//stateset->setTextureAttributeAndModes(textureUnit1, texture1,
osg::StateAttribute::PROTECTED);
osg::Texture2D* texture2 = new osg::Texture2D();
texture2->setImage(spotLightImage);
texture2->setBorderColor(osg::Vec4(ambientColour));
texture2->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_BORDER);
texture2->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_BORDER);
texture2->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_BORDER);
stateset->setTextureAttributeAndModes(textureUnit2, texture2,
osg::StateAttribute::ON);
//stateset->setTextureAttributeAndModes(textureUnit2, texture2,
osg::StateAttribute::PROTECTED);
// set up tex gens
stateset->setTextureMode(textureUnit1, GL_TEXTURE_GEN_S,
osg::StateAttribute::ON);
stateset->setTextureMode(textureUnit1, GL_TEXTURE_GEN_T,
osg::StateAttribute::ON);
stateset->setTextureMode(textureUnit1, GL_TEXTURE_GEN_R,
osg::StateAttribute::ON);
stateset->setTextureMode(textureUnit1, GL_TEXTURE_GEN_Q,
osg::StateAttribute::ON);
stateset->setTextureMode(textureUnit2, GL_TEXTURE_GEN_S,
osg::StateAttribute::ON);
stateset->setTextureMode(textureUnit2, GL_TEXTURE_GEN_T,
osg::StateAttribute::ON);
stateset->setTextureMode(textureUnit2, GL_TEXTURE_GEN_R,
osg::StateAttribute::ON);
stateset->setTextureMode(textureUnit2, GL_TEXTURE_GEN_Q,
osg::StateAttribute::ON);
return stateset;
}
osg::Node* create2SpotLightNode(const osg::Vec3& direction, float
angle, unsigned int lightNum1, unsigned int textureUnit1, unsigned int
lightNum2, unsigned int textureUnit2)
{
osg::Group* group = new osg::Group;
osg::Vec3 position1 = osg::Vec3(0.f, 0.f, 0.f);
osg::Vec3 position2 = osg::Vec3(40.f, 40.f, 10.f);
// create first light source.
osg::LightSource* lightsource1 = new osg::LightSource;
//lightsource1->setLocalStateSetModes(osg::StateAttribute::ON);
//lightsource1->setStateSetModes(*lightsource1->getStateSet(),
osg::StateAttribute::ON);
osg::Light* light1 = lightsource1->getLight();
light1->setLightNum(lightNum1);
light1->setPosition(osg::Vec4(position1,1.0f));
light1->setDirection(direction);
light1->setAmbient(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
light1->setDiffuse(osg::Vec4(1.0f,.0f,.0f,1.0f));
light1->setSpecular(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
light1->setSpotCutoff(angle/2);
//light1->setSpotExponent(0.5f);
group->addChild(lightsource1);
// create second light source.
osg::LightSource* lightsource2 = new osg::LightSource;
//lightsource2->setLocalStateSetModes(osg::StateAttribute::ON);
//lightsource2->setStateSetModes(*lightsource2->getStateSet(),
osg::StateAttribute::ON);
osg::Light* light2 = lightsource2->getLight();
light2->setLightNum(lightNum2);
light2->setPosition(osg::Vec4(position2,1.0f));
light2->setDirection(direction);
light2->setAmbient(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
light2->setDiffuse(osg::Vec4(.0f,1.0f,.0f,1.0f));
light2->setSpecular(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
light2->setSpotCutoff(angle/2);
//light2->setSpotExponent(0.5f);
group->addChild(lightsource2);
// create tex gen.
osg::Vec3 up(0.0f,0.0f,1.0f);
up = (direction ^ up) ^ direction;
up.normalize();
osg::TexGenNode* texgenNode1 = new osg::TexGenNode;
texgenNode1->setTextureUnit(textureUnit1);
osg::TexGen* texgen1 = texgenNode1->getTexGen();
texgen1->setMode(osg::TexGen::EYE_LINEAR);
texgen1->setPlanesFromMatrix(osg::Matrixd::lookAt(position1,
position1+direction, up)*
osg::Matrixd::perspective(angle,1.0,0.1,100));
osg::TexGenNode* texgenNode2 = new osg::TexGenNode;
texgenNode2->setTextureUnit(textureUnit2);
osg::TexGen* texgen2 = texgenNode2->getTexGen();
texgen2->setMode(osg::TexGen::EYE_LINEAR);
texgen2->setPlanesFromMatrix(osg::Matrixd::lookAt(position2,
position2+direction, up)*
osg::Matrixd::perspective(angle,1.0,0.1,100));
group->addChild(texgenNode1);
group->addChild(texgenNode2);
return group;
}
osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float
radius,double looptime)
{
// set up the animation path
osg::AnimationPath* animationPath = new osg::AnimationPath;
animationPath->setLoopMode(osg::AnimationPath::LOOP);
int numSamples = 40;
float yaw = 0.0f;
float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
float roll = osg::inDegrees(30.0f);
double time=0.0f;
double time_delta = looptime/(double)numSamples;
for(int i=0;i<numSamples;++i)
{
osg::Vec3
position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
osg::Quat
rotation(osg::Quat(roll,osg::Vec3(0.0,1.0,0.0))*osg::Quat(-(yaw+osg::inDegrees(90.0f)),osg::Vec3(0.0,0.0,1.0)));
animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
yaw += yaw_delta;
time += time_delta;
}
return animationPath;
}
osg::Node* createBase(const osg::Vec3& center,float radius)
{
osg::Geode* geode = new osg::Geode;
// set up the texture of the base.
osg::StateSet* stateset = new osg::StateSet();
osg::Image* image =
osgDB::readImageFile("../../../OpenSceneGraph-Data/Images/lz.rgb");
if (image)
{
osg::Texture2D* texture1 = new osg::Texture2D;
texture1->setImage(image);
stateset->setTextureAttributeAndModes(0,texture1,osg::StateAttribute::ON);
}
geode->setStateSet( stateset );
osg::HeightField* grid = new osg::HeightField;
grid->allocate(38,39);
grid->setOrigin(center+osg::Vec3(-radius,-radius,0.0f));
grid->setXInterval(radius*2.0f/(float)(38-1));
grid->setYInterval(radius*2.0f/(float)(39-1));
float minHeight = FLT_MAX;
float maxHeight = -FLT_MAX;
unsigned int r;
for(r=0;r<39;++r)
{
for(unsigned int c=0;c<38;++c)
{
float h = vertex[r+c*39][2];
if (h>maxHeight) maxHeight=h;
if (h<minHeight) minHeight=h;
}
}
float hieghtScale = radius*0.5f/(maxHeight-minHeight);
float hieghtOffset = -(minHeight+maxHeight)*0.5f;
for(r=0;r<39;++r)
{
for(unsigned int c=0;c<38;++c)
{
float h = vertex[r+c*39][2];
grid->setHeight(c,r,(h+hieghtOffset)*hieghtScale);
}
}
geode->addDrawable(new osg::ShapeDrawable(grid));
osg::Group* group = new osg::Group;
group->addChild(geode);
return group;
}
osg::Node* createMovingModel(const osg::Vec3& center, float radius,
unsigned int lightNum1, unsigned int textureUnit1, unsigned int
lightNum2, unsigned int textureUnit2)
{
float animationLength = 10.0f;
osg::AnimationPath* animationPath =
createAnimationPath(center,radius,animationLength);
osg::Group* model = new osg::Group;
osg::Node* cessna =
osgDB::readNodeFile("../../../OpenSceneGraph-Data/cessna.osg");
if (cessna)
{
const osg::BoundingSphere& bs = cessna->getBound();
float size = radius/bs.radius()*0.3f;
osg::MatrixTransform* positioned = new osg::MatrixTransform;
positioned->setDataVariance(osg::Object::STATIC);
positioned->setMatrix(osg::Matrix::translate(-bs.center())*
osg::Matrix::scale(size,size,size)*
osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,2.0f));
positioned->addChild(cessna);
osg::MatrixTransform* xform = new osg::MatrixTransform;
xform->setUpdateCallback(new
osg::AnimationPathCallback(animationPath,0.0f,2.0));
xform->addChild(positioned);
xform->addChild(create2SpotLightNode(osg::Vec3(0.0f,1.0f,-1.0f),
60.0f, lightNum1, textureUnit1, lightNum2, textureUnit2));
model->addChild(xform);
}
return model;
}
osg::Node* createModel()
{
unsigned int lightNum1 = 0; // ok
unsigned int lightNum2 = 1;
unsigned int textureUnit1 = 1;
unsigned int textureUnit2 = 2;
osg::Vec3 center(0.0f,0.0f,0.0f);
float radius = 100.0f;
osg::Vec3 lightPosition(center+osg::Vec3(0.0f,0.0f,radius));
// the shadower model
osg::Node* shadower = createMovingModel(center,radius*0.5f,
lightNum1, textureUnit1, lightNum2, textureUnit2);
// the shadowed model
osg::Node* shadowed =
createBase(center-osg::Vec3(0.0f,0.0f,radius*0.1),radius);
// combine the models together to create one which has the
shadower and the shadowed with the required callback.
osg::Group* root = new osg::Group;
// StateSet for lights and textures
root->setStateSet(create2SpotLightDecoratorState(lightNum1,textureUnit1,
lightNum2,textureUnit2));
root->addChild(shadower);
root->addChild(shadowed);
return root;
}
int main(int, char **)
{
// construct the viewer.
osgViewer::Viewer viewer;
viewer.getCamera()->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
// add the spoit light model to the viewer
viewer.setSceneData( createModel() );
// run the viewer main frame loop.
return viewer.run();
}
----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
More information about the osg-users
mailing list