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