source: Swollen/swollen/createSky.cpp @ 43

Last change on this file since 43 was 43, checked in by darran, 20 years ago
  • sky sphere remains at fixed distance from camera
  • back to zero vertical water surface offset
File size: 2.1 KB
RevLine 
[6]1
2#include <osg/Geode>
3#include <osg/Geometry>
4#include <osg/ShapeDrawable>
5#include <osg/Texture2D>
[43]6#include <osg/Transform>
[6]7#include <osg/PolygonOffset>
8#include <osgDB/Registry>
9#include <osgDB/ReadFile>
[43]10#include <osgUtil/CullVisitor>
[6]11
12
[43]13class MoveHorizonWithEyePointTransform : public osg::Transform
[6]14{
[43]15
16public:
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
45osg::Transform* createSky(float radius, char* filename)
46{
[6]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 );
[43]60
[6]61    geode->addDrawable(geom);
[43]62
63    osg::Transform* transform = new MoveHorizonWithEyePointTransform;
64    transform->setCullingActive(false);
65    transform->addChild(geode);
66
67    return transform;
[6]68}
Note: See TracBrowser for help on using the repository browser.