Hi community,
can some of you find a minute or two to just run the attached source (it is
based on the #define NUM_LIGHTS) on the hardware you have and see if it
runs, and if so just let me know what is the OS and the hardware?
Thanks a lot for the help!
Nick
http://www.linkedin.com/in/tnick
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commericial and non commericial
applications,
* as long as this copyright notice is maintained.
*
* This application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>
#include <osg/CoordinateSystemNode>
#include <osg/Switch>
#include <osg/LightSource>
#include <osgText/Text>
#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgSim/OverlayNode>
#include <osg/Geode>
#include <osgText/Text3D>
#include <osg/MatrixTransform>
#include <osg/ShapeDrawable>
#include <osg/TexGen>
#include <osg/TexGenNode>
#include <osg/CullFace>
#include <osg/PolygonOffset>
#include <iostream>
#include <sstream>
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/LightSource>
#include <osg/LightModel>
#include <osg/Program>
#include <osg/ImageStream>
#include <osg/TextureRectangle>
#include <osg/TexMat>
#include <osg/BlendFunc>
#include <osg/AlphaFunc>
#include <osg/Material>
#include <osgUtil/SmoothingVisitor>
#include <osg/ShapeDrawable>
#include <osg/PositionAttitudeTransform>
#include <osg/TextureRectangle>
#define NUM_LIGHTS 7
#if 1
osg::Matrixd createTransformMatrix(double x, double y, double z, double h,
double p, double r)
{
osg::Matrixd mxR;
mxR.makeRotate(osg::DegreesToRadians(r),osg::Vec3(0,1,0));
osg::Matrixd mxP;
mxP.makeRotate(osg::DegreesToRadians(p),osg::Vec3(1,0,0));
osg::Matrixd mxH;
mxH.makeRotate(osg::DegreesToRadians(h),osg::Vec3(0,0,1));
osg::Matrixd mxT;
mxT.makeTranslate(osg::Vec3(x,y,z));
return (mxR*mxP*mxH*mxT);
}
class DrawableDrawWithDepthShadowComparisonOffCallback:
public osg::Drawable::DrawCallback
{
public:
//
DrawableDrawWithDepthShadowComparisonOffCallback
( osg::Texture2D * texture, unsigned stage = 0 )
: _texture( texture ), _stage( stage )
{
}
virtual void drawImplementation
( osg::RenderInfo & ri,const osg::Drawable* drawable ) const
{
if( _texture.valid() ) {
// make sure proper texture is currently applied
ri.getState()->applyTextureAttribute( _stage, _texture.get() );
// Turn off depth comparison mode
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB,
GL_NONE );
}
drawable->drawImplementation(ri);
if( _texture.valid() ) {
// Turn it back on
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB,
GL_COMPARE_R_TO_TEXTURE_ARB );
}
}
osg::ref_ptr< osg::Texture2D > _texture;
unsigned _stage;
};
class UpdateLMVPM : public osg::Uniform::Callback
{
public:
UpdateLMVPM(const std::vector<osg::Camera*>& cameras)
: mCameras(cameras)
{
}
virtual void operator () (osg::Uniform* u, osg::NodeVisitor*)
{
for (unsigned int i=0; i<mCameras.size(); ++i)
{
osg::Camera* camera = mCameras.at(i);
osg::Matrixf lmvpm =
camera->getViewMatrix() * camera->getProjectionMatrix() *
osg::Matrix::translate( 1,1,1 ) * osg::Matrix::scale( 0.5, 0.5,
0.5 );
u->setElement(i,lmvpm);
}
}
protected:
std::vector<osg::Camera*> mCameras;
};
class UpdateLMVPMS : public osg::Uniform::Callback
{
public:
UpdateLMVPMS(osg::Camera* camera)
: mCamera(camera)
{
}
virtual void operator () (osg::Uniform* u, osg::NodeVisitor*)
{
osg::Camera* camera = mCamera;
osg::Matrixf lmvpm =
camera->getViewMatrix() * camera->getProjectionMatrix() *
osg::Matrix::translate( 1,1,1 ) * osg::Matrix::scale( 0.5, 0.5,
0.5 );
u->set(lmvpm);
}
protected:
osg::Camera* mCamera;
};
std::vector< osg::ref_ptr<osg::MatrixTransform> > LightLobes;
osg::Node* createScene(osg::ArgumentParser& arguments, osgViewer::Viewer*
viewer = 0)
{
osg::Group* group = new osg::Group;
osg::Group* scene = new osg::Group;
scene->addChild(group);
scene->getOrCreateStateSet()->setAttribute( new osg::CullFace,
osg::StateAttribute::ON );
scene->getOrCreateStateSet()->setAttribute( new osg::PolygonOffset( 1.1, 4
),osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
scene->getOrCreateStateSet()->setMode(
GL_POLYGON_OFFSET_FILL,osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE
);
osg::MatrixTransform* mx = 0;
osg::MatrixTransform* mx2 = 0;
osg::Geometry* scenegeometry = 0;
osg::Geode* geode = new osg::Geode;
group->addChild(geode);
// "TERRAIN"
{
mx2 = new osg::MatrixTransform;
mx2->setMatrix(osg::Matrix::scale(osg::Vec3(35,35,35)));
group->addChild(mx2);
osg::Geode* geode = new osg::Geode;
mx2->addChild(geode);
scenegeometry = new osg::Geometry;
geode->addDrawable(scenegeometry);
int numStepsX = 200;
int numStepsY = 200;
osg::Vec3Array* vxs = new osg::Vec3Array;
osg::Vec3Array* nms = new osg::Vec3Array;
osg::Vec2Array* uvs = new osg::Vec2Array;
for (unsigned int i=0; i<numStepsX; ++i)
for (unsigned int j=0; j<numStepsY; ++j)
{
double dx = 1.0/numStepsX;
double dy = 1.0/numStepsY;
vxs->push_back(
osg::Vec3(-0.5+i*dx,-0.5+j*dy,0) );
vxs->push_back(
osg::Vec3(-0.5+(i+1)*dx,-0.5+j*dy,0) );
vxs->push_back(
osg::Vec3(-0.5+(i+1)*dx,-0.5+(j+1)*dy,0) );
vxs->push_back(
osg::Vec3(-0.5+i*dx,-0.5+(j+1)*dy,0) );
nms->push_back( osg::Vec3(0,0,1) );
nms->push_back( osg::Vec3(0,0,1) );
nms->push_back( osg::Vec3(0,0,1) );
nms->push_back( osg::Vec3(0,0,1) );
uvs->push_back( osg::Vec2(dx*i,j*dy) );
uvs->push_back( osg::Vec2(dx*(i+1),j*dy) );
uvs->push_back( osg::Vec2(dx*(i+1),(j+1)*dy) );
uvs->push_back( osg::Vec2(dx*i,(j+1)*dy) );
}
scenegeometry->setVertexArray( vxs );
scenegeometry->setNormalArray( nms );
scenegeometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
scenegeometry->setTexCoordArray(0,uvs);
for (unsigned int i=0; i<10; ++i)
{
double pos = -0.5+(0.1*i);
double w = 0.05;
vxs->push_back( osg::Vec3(pos,0.0,0) );
vxs->push_back( osg::Vec3(pos+w,0.0,0) );
vxs->push_back( osg::Vec3(pos+w,0.0,0.1) );
vxs->push_back( osg::Vec3(pos,0.0,0.1) );
uvs->push_back( osg::Vec2(0,0) );
uvs->push_back( osg::Vec2(1,0) );
uvs->push_back( osg::Vec2(1,1) );
uvs->push_back( osg::Vec2(0,1) );
nms->push_back( osg::Vec3(0,-1,0) );
nms->push_back( osg::Vec3(0,-1,0) );
nms->push_back( osg::Vec3(0,-1,0) );
nms->push_back( osg::Vec3(0,-1,0) );
}
osg::Image* image = osgDB::readImageFile("Images/lz.rgb");
if (image)
{
osg::Texture2D* texture = new osg::Texture2D;
texture->setImage(image);
scenegeometry->getOrCreateStateSet()->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
}
scenegeometry->addPrimitiveSet( new
osg::DrawArrays(osg::PrimitiveSet::QUADS,0,vxs->size()) );
osg::Material* mt = new osg::Material;
mt->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(0.3,0.3,0.3,1));
mt->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(0.9,0.5,0.5,1));
mt->setSpecular(osg::Material::FRONT_AND_BACK,osg::Vec4(0.6,0.6,0.6,1));
mt->setShininess(osg::Material::FRONT_AND_BACK,60);
scenegeometry->getOrCreateStateSet()->setAttributeAndModes(mt,osg::StateAttribute::ON);
}
// "LIGHT SOURCE"
{
std::vector<osg::Vec4> colors;
colors.push_back(osg::Vec4(10,0,0,1));
colors.push_back(osg::Vec4(0,10,0,1));
colors.push_back(osg::Vec4(0,0,10,1));
colors.push_back(osg::Vec4(10,0,10,1));
colors.push_back(osg::Vec4(10,10,0,1));
colors.push_back(osg::Vec4(0,10,10,1));
colors.push_back(osg::Vec4(5,10,5,1));
colors.push_back(osg::Vec4(10,0,0,1));
colors.push_back(osg::Vec4(0,10,0,1));
colors.push_back(osg::Vec4(0,0,10,1));
colors.push_back(osg::Vec4(10,0,10,1));
colors.push_back(osg::Vec4(10,10,0,1));
colors.push_back(osg::Vec4(0,10,10,1));
colors.push_back(osg::Vec4(5,10,5,1));
colors.push_back(osg::Vec4(10,0,0,1));
colors.push_back(osg::Vec4(0,10,0,1));
colors.push_back(osg::Vec4(0,0,10,1));
colors.push_back(osg::Vec4(10,0,10,1));
colors.push_back(osg::Vec4(10,10,0,1));
colors.push_back(osg::Vec4(0,10,10,1));
colors.push_back(osg::Vec4(5,10,5,1));
for (unsigned int i=1; i<=NUM_LIGHTS; ++i)
{
osg::LightSource* light = new osg::LightSource;
light->getLight()->setLightNum(i);
light->getLight()->setAmbient(osg::Vec4(0,0,0,1));
light->getLight()->setDiffuse(colors.at(i-1));
light->getLight()->setSpecular(osg::Vec4(1,1,1,1));
light->getLight()->setConstantAttenuation(0.01);
light->getLight()->setSpotCutoff(20);
light->setStateSetModes(*group->getOrCreateStateSet(),osg::StateAttribute::ON);
light->getLight()->setDirection(osg::Vec3(0,1,0));
light->getLight()->setPosition(osg::Vec4(0,0,0,1));
mx = new osg::MatrixTransform;
mx->setMatrix(createTransformMatrix(15-i*1.5,-2,1.5,0,-45,0));
mx->addChild(light);
LightLobes.push_back( mx );
osg::Geode* geode = new osg::Geode;
mx->addChild(geode);
float radius = 0.2f;
osg::TessellationHints* hints = new osg::TessellationHints;
hints->setDetailRatio(0.5f);
geode->addDrawable(new osg::ShapeDrawable(new
osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),radius),hints));
group->addChild(mx);
}
}
// SCENE LIGHTING
{
std::ostringstream ossVertSource;
ossVertSource << "uniform mat4 osg_ViewMatrixInverse;
\n"; //1
ossVertSource << "vec3 lightDirs[" << NUM_LIGHTS+1 << "];
\n"; //3
ossVertSource << "vec3 normal, eyeVec;
\n"; //3
ossVertSource << "varying vec4 projShadow[" << NUM_LIGHTS << "];
\n"; //4
ossVertSource << "varying vec4 lightColor[" <<
NUM_LIGHTS+1 << "]; \n";
//5
ossVertSource << "uniform mat4 LightModelViewProjectionMatrix[" <<
NUM_LIGHTS << "]; \n"; //6
ossVertSource << "const float cos_outer_cone_angle =
0.8; // 36 degrees \n"; //13
ossVertSource << "vec4 computeColorForLightSource(int
i) \n";
//14
ossVertSource << "{
\n"; //15
#if 1
ossVertSource << " vec4 sceneColor =
gl_FrontLightModelProduct.sceneColor;
\n";
ossVertSource << " vec4 ambient =
vec4(0.3,0.3,0.3,1.0);
\n";
ossVertSource << " vec4 final_color =
\n"; //16
ossVertSource << " (sceneColor * ambient) + \n"; //17
ossVertSource << " (gl_LightSource[i].ambient * ambient);
\n"; //18
ossVertSource << "
\n"; //19
ossVertSource << " float distSqr =
dot(lightDirs[i],lightDirs[i]); \n"; //20
ossVertSource << " float invRadius =
gl_LightSource[i].constantAttenuation; \n"; //21
ossVertSource << " float att = clamp(1.0-invRadius *
sqrt(distSqr), 0.0, 1.0); \n"; //22
ossVertSource << " vec3 L = lightDirs[i] * inversesqrt(distSqr);
\n"; //23
ossVertSource << " vec3 D =
normalize(gl_LightSource[i].spotDirection); \n"; //24
ossVertSource << "
\n"; //25
ossVertSource << " float cos_cur_angle = dot(-L, D);
\n"; //26
ossVertSource << "
\n"; //27
ossVertSource << " float cos_inner_cone_angle =
gl_LightSource[i].spotCosCutoff; \n"; //28
ossVertSource << "
\n"; //29
ossVertSource << " float cos_inner_minus_outer_angle =
\n"; //30
ossVertSource << " cos_inner_cone_angle -
cos_outer_cone_angle; \n"; //31
ossVertSource << "
\n"; //32
ossVertSource << " float spot = 0.0;
\n"; //33
ossVertSource << " spot = clamp((cos_cur_angle -
cos_outer_cone_angle) / \n"; //34
ossVertSource << " cos_inner_minus_outer_angle, 0.0,
1.0); \n"; //35
ossVertSource << "
\n"; //36
ossVertSource << " vec3 N = normalize(normal);
\n"; //37
ossVertSource << "
\n"; //38
ossVertSource << " float lambertTerm = max( dot(N,L), 0.0);
\n"; //39
ossVertSource << " if(lambertTerm > 0.0)
\n"; //40
ossVertSource << " {
\n"; //41
ossVertSource << " vec4 diffuse =
vec4(0.9,0.5,0.5,1.0); \n";
ossVertSource << " final_color += gl_LightSource[i].diffuse
* \n"; //42
ossVertSource << " diffuse *
\n"; //43
ossVertSource << " lambertTerm * spot * att;
\n"; //44
ossVertSource << "
\n"; //45
ossVertSource << " vec3 E = normalize(eyeVec);
\n"; //46
ossVertSource << " vec3 R = reflect(-L, N);
\n"; //47
ossVertSource << "
\n"; //48
ossVertSource << " float specular = pow( max(dot(R, E),
0.0), \n"; //49
ossVertSource << " 60.0 );
\n"; //50
ossVertSource << "
\n"; //51
ossVertSource << " vec4 vspecular = vec4(
0.6,0.6,0.6,1.0 ); \n"; //51
ossVertSource << " final_color += gl_LightSource[i].specular
* \n"; //52
ossVertSource << " vspecular *
\n"; //53
ossVertSource << " specular * spot * att;
\n"; //54
ossVertSource << " }
\n"; //55
ossVertSource << " return final_color;
\n"; //56
#else
ossVertSource << " return vec4(0.0,0.0,0.0,1.0);";
#endif
ossVertSource << "}
\n"; //57
ossVertSource << "void main()
\n"; //58
ossVertSource << "{
\n"; //59
ossVertSource << " gl_TexCoord[0] = gl_MultiTexCoord0;
\n"; //60
ossVertSource << " normal = gl_NormalMatrix * gl_Normal;
\n"; //61
ossVertSource << " vec3 vVertex = vec3(gl_ModelViewMatrix
* gl_Vertex); \n"; //62
ossVertSource << " eyeVec = -vVertex;
\n"; //65
ossVertSource << " // We need vertex in world spaces
\n"; //66
ossVertSource << " // ie something like: glVertex *
gl_ModelMatrix \n"; //67
ossVertSource << " // but there is no such thing like
gl_ModelMatrix in GL \n"; //68
ossVertSource << " // but we could get it from view space
by multiplication by \n"; //69
ossVertSource << " // inverted viewMatrix
\n"; //70
ossVertSource << " // gl_Vertex * gl_ModelViewMatrix *
osg_ViewMatrixInverse \n"; //71
ossVertSource << " vec4 worldSpaceVertex =
osg_ViewMatrixInverse*vec4( vVertex, 1.0 ); \n"; //72
ossVertSource << " gl_Position = ftransform();
\n";
//80
ossVertSource << " for (int i=0; i<" <<
NUM_LIGHTS << "; ++i) \n";
//63
ossVertSource << " {
\n"; //
ossVertSource << " projShadow[i] =
LightModelViewProjectionMatrix[i] \n"; //81
ossVertSource << "
* worldSpaceVertex;
\n"; //82
ossVertSource << " lightDirs[i] =
vec3(gl_LightSource[i].position.xyz - vVertex); \n"; //64
ossVertSource << " lightColor[i] =
computeColorForLightSource(i); \n"; //82
ossVertSource << " }
\n"; //
ossVertSource << " lightDirs[" <<
NUM_LIGHTS << "] =
\n"; // \n:
ossVertSource << "
vec3(gl_LightSource["<<NUM_LIGHTS<<"].position.xyz - vVertex); \n"; //64
ossVertSource << "
lightColor["<<NUM_LIGHTS<<"] = computeColorForLightSource("<<NUM_LIGHTS<<");
\n"; //82
ossVertSource << "}
\n"; //83
std::ostringstream ossFragSource;
ossFragSource << "uniform sampler2D color_texture;
\n";
ossFragSource << "uniform sampler2DShadow shadow_texture[7];
\n";
ossFragSource << "varying vec4 projShadow[" << NUM_LIGHTS << "];
\n";
ossFragSource << "varying vec4 lightColor[" <<
NUM_LIGHTS+1 << "]; \n";
ossFragSource << "const int NumEnabledLights = " <<
NUM_LIGHTS << "; \n";
ossFragSource << "bool notShadowed(int i)
\n";
ossFragSource << "{
\n";
ossFragSource << " return shadow2DProj(
shadow_texture[i-1], projShadow[i-1]).r != 0.0; \n";
ossFragSource << "}
\n";
ossFragSource << "void main (void)
\n";
ossFragSource << "{
\n";
ossFragSource << " vec4 combined_color =
vec4(0.0,0.0,0.0,1.0); \n";
ossFragSource << " for ( int i=1; i<1+" <<
NUM_LIGHTS <<"; ++i) \n";
ossFragSource << " {
\n";
ossFragSource << " if (notShadowed(i))
\n";
ossFragSource << " combined_color
+= lightColor[i]; \n";
ossFragSource << " }
\n";
ossFragSource << " combined_color += lightColor[0];
\n";
ossFragSource << " gl_FragColor = texture2D(color_texture,
gl_TexCoord[0].st) * \n";
ossFragSource << " combined_color;
\n";
ossFragSource << "}
\n";
osg::Program* program = new osg::Program;
program->setName( "microshader" );
program->addShader( new osg::Shader( osg::Shader::VERTEX,
ossVertSource.str() ) );
program->addShader( new osg::Shader( osg::Shader::FRAGMENT,
ossFragSource.str() ) );
group->getOrCreateStateSet()->setAttributeAndModes( program,
osg::StateAttribute::ON );
group->getOrCreateStateSet()->addUniform( new
osg::Uniform("color_texture",0) );
#if 1
osg::Uniform* sampler2DShadows = new
osg::Uniform(osg::Uniform::SAMPLER_2D_SHADOW, "shadow_texture",
LightLobes.size());
group->getOrCreateStateSet()->addUniform( sampler2DShadows );
for (unsigned int i=0; i<LightLobes.size(); ++i)
{
sampler2DShadows->setElement(i, (int)i+1);
}
#else
for (unsigned int i=0; i<LightLobes.size(); ++i)
{
std::ostringstream oss;
oss << "shadow_texture" << i+1;
osg::Uniform* u;
group->getOrCreateStateSet()->addUniform( u = new
osg::Uniform(osg::Uniform::SAMPLER_2D_SHADOW,oss.str()) );
u->set((int)(i+1));
}
#endif
}
// HUD
{
osg::Camera* camera = new osg::Camera;
scene->addChild(camera);
double width = 1280;
double height = 1024;
camera->setProjectionMatrix(osg::Matrix::ortho2D(0,width,0,height));
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setViewMatrix(osg::Matrix::identity());
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
camera->setRenderOrder(osg::Camera::POST_RENDER);
camera->setAllowEventFocus(false);
osg::Geode* geode = new osg::Geode;
camera->addChild(geode);
camera->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE|osg::StateAttribute::PROTECTED);
osg::Shader* shader = new osg::Shader( osg::Shader::FRAGMENT,
#if 0
"uniform sampler2D texture; \n"
" \n"
"void main(void) \n"
"{ \n"
" float value = texture2D(texture, gl_TexCoord[0].st ).r; \n"
" gl_FragColor = vec4( value, value, value, 0.8 ); \n"
"} \n"
#else
"uniform sampler2D texture;
\n"
"
\n"
"void main(void)
\n"
"{
\n"
" float f = texture2D( texture, gl_TexCoord[0].st ).r;
\n"
"
\n"
"
\n"
" f = 256.0 * f;
\n"
" float fC = floor( f ) / 256.0;
\n"
"
\n"
" f = 256.0 * fract( f );
\n"
" float fS = floor( f ) / 256.0;
\n"
"
\n"
" f = 256.0 * fract( f );
\n"
" float fH = floor( f ) / 256.0;
\n"
"
\n"
" fS *= 0.5;
\n"
" fH = ( fH * 0.34 + 0.66 ) * ( 1.0 - fS );
\n"
"
\n"
" vec3 rgb = vec3( ( fC > 0.5 ? ( 1.0 - fC ) : fC ),
\n"
" abs( fC - 0.333333 ),
\n"
" abs( fC - 0.666667 ) );
\n"
"
\n"
" rgb = min( vec3( 1.0, 1.0, 1.0 ), 3.0 * rgb );
\n"
"
\n"
" float fMax = max( max( rgb.r, rgb.g ), rgb.b );
\n"
" fMax = 1.0 / fMax;
\n"
"
\n"
" vec3 color = fMax * rgb;
\n"
"
\n"
" gl_FragColor = vec4( fS + fH * color, 1 ) * gl_Color;
\n"
"}
\n"
#endif
);
std::vector< osg::Camera* > cameras;
for (unsigned int i=0; i < LightLobes.size(); ++i)
// LIGHT CAMERA
{
double offset = 50;
double wf = 128;
double hf = 128;
unsigned int j;
unsigned int k;
j = i%8;
k = i/8;
osg::Geometry *geometry =
osg::createTexturedQuadGeometry(osg::Vec3(offset+j*wf+j*10,offset+k*hf+k*10,-0.1),osg::Vec3(wf,0,0),osg::Vec3(0,hf,0));
geode->addDrawable(geometry);
osg::Program* program = new osg::Program;
program->addShader( shader );
geometry->getOrCreateStateSet()->setAttribute( program );
geometry->getOrCreateStateSet()->addUniform( new osg::Uniform(
"texture" , i ) );
osg::Texture2D* dbgDepthTexture = new osg::Texture2D;
dbgDepthTexture->setTextureSize(512, 512);
dbgDepthTexture->setInternalFormat(GL_DEPTH_COMPONENT);
dbgDepthTexture->setShadowTextureMode(osg::Texture2D::LUMINANCE);
dbgDepthTexture->setShadowComparison(true);
dbgDepthTexture->setShadowCompareFunc(osg::Texture::LEQUAL);
dbgDepthTexture->setWrap(osg::Texture::WRAP_S,
osg::Texture::CLAMP_TO_EDGE);
dbgDepthTexture->setWrap(osg::Texture::WRAP_T,
osg::Texture::CLAMP_TO_EDGE);
dbgDepthTexture->setWrap(osg::Texture::WRAP_R,
osg::Texture::CLAMP_TO_EDGE);
dbgDepthTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
dbgDepthTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
geometry->getOrCreateStateSet()->setTextureAttributeAndModes(0,dbgDepthTexture,osg::StateAttribute::ON);
scenegeometry->getOrCreateStateSet()->setTextureAttributeAndModes(i+1,dbgDepthTexture,osg::StateAttribute::ON);
geometry->setDrawCallback( new
DrawableDrawWithDepthShadowComparisonOffCallback(dbgDepthTexture) );
osg::Camera* camera = new osg::Camera;
camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
camera->setProjectionMatrixAsPerspective(70, 1, 1.001, 100 );
camera->setCullingMode(osg::CullSettings::NO_CULLING);
camera->setViewport(0,0,512,512);
camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
camera->setClearColor(osg::Vec4(1.f,1.f,1.f,1.0f));
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
camera->addChild(mx2);
camera->setRenderOrder(osg::Camera::PRE_RENDER);
camera->attach(osg::Camera::DEPTH_BUFFER,dbgDepthTexture);
camera->setCullingActive(true);
LightLobes.at(i)->setUserData(camera);
osg::MatrixTransform* mx =
dynamic_cast<osg::MatrixTransform*>(LightLobes.at(i).get());
if (mx)
{
osg::Matrixd mxd = mx->getMatrix();
osg::Quat q = mxd.getRotate();
osg::Vec3 t = mxd.getTrans();
osg::Quat fq;
fq.makeRotate(osg::Vec3(0,0,-1),osg::Vec3(0,1,0));
osg::Matrixd mR;
mR.makeRotate(q);
osg::Matrixd mT;
mT.makeTranslate(t);
osg::Matrixd mF;
mF.makeRotate(fq);
osg::Matrixd m;
m = mF * mR * mT;
camera->setViewMatrix(osg::Matrixd::inverse(m));
}
cameras.push_back(camera);
scene->addChild(camera);
}
osg::Uniform* u = new
osg::Uniform(osg::Uniform::FLOAT_MAT4,"LightModelViewProjectionMatrix",LightLobes.size());
u->setUpdateCallback( new UpdateLMVPM(cameras) );
group->getOrCreateStateSet()->addUniform( u );
}
return scene;
}
#endif
void setupViewer(osgViewer::Viewer* viewer, osg::ArgumentParser& arguments)
{
// set up the camera manipulators.
{
osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator =
new osgGA::KeySwitchMatrixManipulator;
keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new
osgGA::TrackballManipulator() );
keyswitchManipulator->addMatrixManipulator( '2', "Flight", new
osgGA::FlightManipulator() );
keyswitchManipulator->addMatrixManipulator( '3', "Drive", new
osgGA::DriveManipulator() );
keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new
osgGA::TerrainManipulator() );
viewer->setCameraManipulator( keyswitchManipulator.get() );
}
// add the state manipulator
viewer->addEventHandler( new
osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()) );
// add the thread model handler
viewer->addEventHandler(new osgViewer::ThreadingHandler);
// add the window size toggle handler
viewer->addEventHandler(new osgViewer::WindowSizeHandler);
// add the stats handler
viewer->addEventHandler(new osgViewer::StatsHandler);
// add the help handler
viewer->addEventHandler(new
osgViewer::HelpHandler(arguments.getApplicationUsage()));
// add the record camera path handler
viewer->addEventHandler(new osgViewer::RecordCameraPathHandler);
// add the LOD Scale handler
viewer->addEventHandler(new osgViewer::LODScaleHandler);
// add the screen capture handler
viewer->addEventHandler(new osgViewer::ScreenCaptureHandler);
}
int main(int argc, char** argv)
{
// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc,argv);
arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+"
is the standard OpenSceneGraph example which loads and visualises 3d models.");
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+"
[options] filename ...");
arguments.getApplicationUsage()->addCommandLineOption("--image
<filename>","Load an image and render it on a quad");
arguments.getApplicationUsage()->addCommandLineOption("--dem
<filename>","Load an image/DEM and render it on a HeightField");
arguments.getApplicationUsage()->addCommandLineOption("--login <url>
<username> <password>","Provide authentication information for http file
access.");
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer(arguments);
viewer->getLight()->setAmbient(osg::Vec4(0.2,0.2,0.2,1));
viewer->getLight()->setDiffuse(osg::Vec4(0.7,0.5,0.5,1));
unsigned int helpType = 0;
if ((helpType = arguments.readHelpType()))
{
arguments.getApplicationUsage()->write(std::cout, helpType);
return 1;
}
// report any errors if they have occurred when parsing the program
arguments.
if (arguments.errors())
{
arguments.writeErrorMessages(std::cout);
return 1;
}
#if 0
if (arguments.argc()<=1)
{
arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
return 1;
}
#endif
std::string url, username, password;
while(arguments.read("--login",url, username, password))
{
if (!osgDB::Registry::instance()->getAuthenticationMap())
{
osgDB::Registry::instance()->setAuthenticationMap(new
osgDB::AuthenticationMap);
osgDB::Registry::instance()->getAuthenticationMap()->addAuthenticationDetails(
url,
new osgDB::AuthenticationDetails(username, password)
);
}
}
setupViewer(viewer.get(),arguments);
// load the data
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
if (!loadedModel)
{
loadedModel = createScene(arguments,viewer.get());
//arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
//std::cout << arguments.getApplicationName() <<": No data loaded" <<
std::endl;
//return 1;
}
// any option left unread are converted into errors to write out later.
arguments.reportRemainingOptionsAsUnrecognized();
// report any errors if they have occurred when parsing the program
arguments.
if (arguments.errors())
{
arguments.writeErrorMessages(std::cout);
return 1;
}
// optimize the scene graph, remove redundant nodes and state etc.
#if 0
osgUtil::Optimizer optimizer;
optimizer.optimize(loadedModel.get());
osgUtil::SmoothingVisitor sv;
loadedModel->accept(sv);
#endif
viewer->setSceneData( loadedModel.get() );
viewer->realize();
std::vector< double > waits;
for (unsigned int i=0; i<LightLobes.size(); ++i)
{
waits.push_back(i*4);
}
std::vector< double > vs;
for (unsigned int i=0; i<LightLobes.size(); ++i)
{
vs.push_back(0.03);
}
osg::Timer_t startTick = osg::Timer::instance()->tick();
while (!viewer->done())
{
viewer->frame();
static bool query = true;
if (query)
{
int intval;
#define GL_MAX_TEXTURE_IMAGE_UNITS_ARB 0x8872
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, &intval);
query = false;
std::cout << "MAX TEXTURE UNITS" << intval << std::endl;
}
for (unsigned int i=0; i<LightLobes.size(); ++i)
{
osg::Timer_t now = osg::Timer::instance()->tick();
double& w = waits[i];
if (w != 0.0)
{
if (osg::Timer::instance()->delta_s(startTick,now) < w)
continue;
}
w = 0.0;
osg::MatrixTransform* mx = LightLobes.at(i).get();
osg::Quat q = mx->getMatrix().getRotate();
osg::Vec3 t = mx->getMatrix().getTrans();
double& v = vs[i];
t.y() += v;
if (t.y()>15 || t.y()<-15)
v *= -1.0;
mx->setMatrix(osg::Matrix::rotate(q)*osg::Matrix::translate(t));
osg::Camera* camera = dynamic_cast<osg::Camera*>(mx->getUserData());
if (camera)
{
osg::Matrixd mxd = mx->getMatrix();
osg::Quat q = mxd.getRotate();
osg::Vec3 t = mxd.getTrans();
osg::Quat fq;
fq.makeRotate(osg::Vec3(0,0,-1),osg::Vec3(0,1,0));
osg::Matrixd mR;
mR.makeRotate(q);
osg::Matrixd mT;
mT.makeTranslate(t);
osg::Matrixd mF;
mF.makeRotate(fq);
osg::Matrixd m;
m = mF * mR * mT;
camera->setViewMatrix(osg::Matrixd::inverse(m));
}
}
}
viewer = 0;
return 0;
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org