1 | |
---|
2 | #include <osg/Depth> |
---|
3 | #include <osg/Geode> |
---|
4 | #include <osg/Geometry> |
---|
5 | #include <osg/ShapeDrawable> |
---|
6 | #include <osg/Texture2D> |
---|
7 | #include <osg/Transform> |
---|
8 | #include <osg/PolygonOffset> |
---|
9 | #include <osgDB/Registry> |
---|
10 | #include <osgDB/ReadFile> |
---|
11 | #include <osgUtil/CullVisitor> |
---|
12 | |
---|
13 | |
---|
14 | class MoveHorizonWithEyePointTransform : public osg::Transform |
---|
15 | { |
---|
16 | |
---|
17 | public: |
---|
18 | |
---|
19 | // Get the transformation matrix which moves from local coords to world coords |
---|
20 | virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const |
---|
21 | { |
---|
22 | osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv); |
---|
23 | if (cv) |
---|
24 | { |
---|
25 | osg::Vec3 eyePointLocal = cv->getEyeLocal(); |
---|
26 | matrix.preMult(osg::Matrix::translate(eyePointLocal)); |
---|
27 | } |
---|
28 | return true; |
---|
29 | } |
---|
30 | |
---|
31 | // Get transformation matrix which moves from world coords to local coords |
---|
32 | virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const |
---|
33 | { |
---|
34 | osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv); |
---|
35 | if (cv) |
---|
36 | { |
---|
37 | osg::Vec3 eyePointLocal = cv->getEyeLocal(); |
---|
38 | matrix.postMult(osg::Matrix::translate(-eyePointLocal)); |
---|
39 | } |
---|
40 | return true; |
---|
41 | } |
---|
42 | }; |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | osg::Transform* createSky(float radius, char* filename) |
---|
47 | { |
---|
48 | osg::Geode* geode = new osg::Geode; |
---|
49 | osg::ShapeDrawable* geom = new osg::ShapeDrawable( new osg::Sphere(osg::Vec3(0,0,-radius/4.0),radius) ); |
---|
50 | |
---|
51 | // set up the texture state. |
---|
52 | osg::Texture2D* texture = new osg::Texture2D; |
---|
53 | texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state. |
---|
54 | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); |
---|
55 | texture->setImage(osgDB::readImageFile(filename)); |
---|
56 | |
---|
57 | osg::StateSet* stateset = new osg::StateSet; |
---|
58 | stateset->setTextureAttributeAndModes( 0, texture, osg::StateAttribute::ON ); |
---|
59 | stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); |
---|
60 | |
---|
61 | // to prevent being able to push the model back through the sky, we |
---|
62 | // do two things (i) ensure sky is drawn first, and (ii) skip writing |
---|
63 | // to the depth buffer |
---|
64 | stateset->setRenderBinDetails( -1, "RenderBin" ); |
---|
65 | osg::Depth* depth = new osg::Depth(); |
---|
66 | depth->setWriteMask( false ); |
---|
67 | stateset->setAttributeAndModes( depth, osg::StateAttribute::ON ); |
---|
68 | |
---|
69 | geom->setStateSet( stateset ); |
---|
70 | geom->setUseDisplayList( true ); |
---|
71 | geode->addDrawable(geom); |
---|
72 | |
---|
73 | osg::Transform* transform = new MoveHorizonWithEyePointTransform; |
---|
74 | transform->setCullingActive( false ); |
---|
75 | transform->addChild( geode ); |
---|
76 | |
---|
77 | return transform; |
---|
78 | } |
---|