i post my source code for more clarity.. Maybe someone already try to have
the same effect.
DepthBuffer.cpp include a main method for test.
#include "DepthBuffer.h"
#include <osgFX/Registry>

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>

namespace
{
        osgFX::Registry::Proxy proxy(new osgFX::DepthBuffer);

        class DefaultTechnique: public osgFX::Technique 
        {
        public:
                DefaultTechnique(osg::Texture2D* tex_depth)
                        : osgFX::Technique(),
                        _tex_depth(tex_depth) {}

                META_Technique("GLSL Depth Buffer", "GLSL Depth Buffer");

                void getRequiredExtensions(std::vector<std::string>& 
extensions) const
                {
                        //extensions.push_back( "GL_ARB_shader_objects" );
                        //extensions.push_back( "GL_ARB_vertex_shader" );
                        //extensions.push_back( "GL_ARB_fragment_shader" );
                }

        protected:
                void define_passes()
                {
                        const unsigned int tex_depth_unit = 0;
                        
                        osg::ref_ptr<osg::StateSet> ss = new osg::StateSet;
                        ss->setTextureAttributeAndModes(tex_depth_unit, 
_tex_depth.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
                        /*
                        ss->setTextureMode(tex_depth_unit, GL_TEXTURE_GEN_S, 
osg::StateAttribute::ON);
                        ss->setTextureMode(tex_depth_unit, GL_TEXTURE_GEN_T, 
osg::StateAttribute::ON);
                        ss->setTextureMode(tex_depth_unit, GL_TEXTURE_GEN_R, 
osg::StateAttribute::ON);
                        ss->setTextureMode(tex_depth_unit, GL_TEXTURE_GEN_Q, 
osg::StateAttribute::ON);
                        */
                        addPass(ss.get());
                }
        private:
                osg::ref_ptr<osg::Texture2D> _tex_depth;

        };
        
}

///////////////////////////////////////////////////////////////////////////

osgFX::DepthBuffer::DepthBuffer()
:       Effect()
{
}

osgFX::DepthBuffer::DepthBuffer(const DepthBuffer& copy, const osg::CopyOp& 
copyop)
:   Effect(copy, copyop),
        _tex_depth(static_cast<osg::Texture2D* >(copyop(copy._tex_depth.get())))
{
}

void osgFX::DepthBuffer::setUpEffect(osg::Group* root, osg::Node* object)
{
        const unsigned int tex_depth_width = 1024;
        const unsigned int tex_depth_height = 1024;
        
        _tex_depth = new osg::Texture2D;
        _tex_depth->setTextureSize(tex_depth_width, tex_depth_height);
        _tex_depth->setInternalFormat(GL_DEPTH_COMPONENT);
        //_tex_depth->setWrap(osg::Texture2D::WRAP_R, 
osg::Texture2D::CLAMP_TO_BORDER);
        //_tex_depth->setWrap(osg::Texture2D::WRAP_S, 
osg::Texture2D::CLAMP_TO_BORDER);
        //_tex_depth->setWrap(osg::Texture2D::WRAP_T, 
osg::Texture2D::CLAMP_TO_BORDER);
        _tex_depth->setFilter(osg::Texture2D::MIN_FILTER, 
osg::Texture2D::LINEAR);
        _tex_depth->setFilter(osg::Texture2D::MAG_FILTER, 
osg::Texture2D::LINEAR);      

        osg::Camera* camera = new osg::Camera;
        camera->setName("DepthBufferCamera");
        camera->setClearColor( osg::Vec4(1.0f,1.0f,1.0f,1.0f) );
        camera->setClearMask( GL_DEPTH_BUFFER_BIT );    
        camera->setReferenceFrame(osg::Camera::RELATIVE_RF);
        camera->setComputeNearFarMode( osg::Camera::DO_NOT_COMPUTE_NEAR_FAR );
        camera->setViewport(0, 0, tex_depth_width, tex_depth_height);
        camera->setProjectionMatrix( osg::Matrix::identity() );
        camera->setViewMatrix( osg::Matrix::identity() );
        camera->setRenderOrder(osg::Camera::PRE_RENDER);
        camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
        camera->attach(osg::Camera::DEPTH_BUFFER, _tex_depth.get());
        camera->addChild( object );
        root->addChild( camera );

        dirtyTechniques();
}

bool osgFX::DepthBuffer::define_techniques()
{
        addTechnique( new DefaultTechnique(_tex_depth.get()) );
        return true;
}

int main(int argc, char *argv[])
{
        osgViewer::Viewer viewer;

        osg::ref_ptr<osg::Group> rootNode = new osg::Group;
        osg::ref_ptr<osg::Node> objectNode = osgDB::readNodeFile("cow.osg");

        
        osg::ref_ptr<osgFX::DepthBuffer> effect = new osgFX::DepthBuffer;
        effect->setUpEffect(rootNode.get(), objectNode.get());
        effect->addChild( objectNode.get() );
        rootNode->addChild( effect.get() );

        viewer.setSceneData( rootNode.get() );

        return viewer.run();
}
#ifndef _DEPTHBUFFER_HPP_
#define _DEPTHBUFFER_HPP_

#include <osgFX/Export>
#include <osgFX/Effect>
#include <osg/Texture2D>

namespace osgFX
{
        class DepthBuffer : public osgFX::Effect
        {
        public:
                DepthBuffer();
                DepthBuffer(const DepthBuffer& copy, const osg::CopyOp& copyop 
= osg::CopyOp::SHALLOW_COPY);

                // effect class informations
                META_Effect(
                        osgFX, 
                        DepthBuffer, 
                        "Depth Buffer", 
                        "",
                        "");

                void setUpEffect(osg::Group* root, osg::Node* object);

        protected:
                virtual ~DepthBuffer() {}
                DepthBuffer& operator=(const DepthBuffer&) { return *this; }

                bool define_techniques();

        private:
                osg::ref_ptr<osg::Texture2D> _tex_depth;
                
        };
}
#endif
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to