Hi,

Sorry to bother you guys... but I was wondering if you can help identify why my 
pass through vertex shader isn't working. In essence, if I enable the vertex 
shader, then the scene disappears (I only see that blue background color). If I 
disable it then the scene appears correctly. No errors are generated by OSG 
after linking the vertex shader. I know that my fragment shader is working 
because if I set the gl_FragColor to red then the scene is completely red.

Here are my pass-thru shaders:

[code]
VERTEX SHADER
=============
attribute vec4 osg_Vertex;
void main( void )
{
     gl_Position = osg_Vertex;
}

FRAGMENT SHADER
===============
uniform sampler2D tRttTexture;

void main( void )
{
     vec3 vColor = texture2D( tRttTexture, gl_TexCoord[0].st ).rgb;
     // Output color.
     gl_FragColor = vec4( vColor, 1.0 );
}
[/code]


The shader is applied on a fullscreen quad using typical Ortho2D projection. 
Here is the code that loads the shader which is applied to the post process 
geode containing a single geometry quad:

[code]
void loadShaders( )
{
    osg::StateSet* pStateSet = g_pPolyGeode->getOrCreateStateSet( );

    osg::ref_ptr<osg::Program> pProgram = new osg::Program;
    pProgram->setName( "PostProcessProgram" );

    osg::ref_ptr<osg::Shader> pVertShader = osg::Shader::readShaderFile( 
osg::Shader::VERTEX, osgDB::findDataFile( "shaders/osgprerender.vert" ) );
    if( pVertShader )
    {
        pVertShader->setName( "osgprerender.vert" );
        pProgram->addShader( pVertShader );
        pProgram->addBindAttribLocation( "osg_Vertex", 0 );
    }

    osg::ref_ptr<osg::Shader> pFragShader = osg::Shader::readShaderFile( 
osg::Shader::FRAGMENT, osgDB::findDataFile( "shaders/osgprerender.frag" ) );
    if( pFragShader )
    {
        pFragShader->setName( "osgprerender.frag" );
        pProgram->addShader( pFragShader );
    }

    // RTT texture.
    pStateSet->addUniform( new osg::Uniform( "tRttTexture", 0 ) );
    pStateSet->setAttributeAndModes( pProgram, osg::StateAttribute::ON );
}
[/code]


What am I doing wrong? I tried different variation of the shader but nothing 
works. Here are the variations that I tried:

[code]
VARIATION #1
===========
void main( void )
{
     gl_Position = gl_Vertex;
}

VARIATION #2
===========
void main( void )
{
     gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

VARIATION #3
===========
attribute vec4 osg_Vertex;
void main( void )
{
     gl_Position = gl_ModelViewProjectionMatrix * osg_Vertex;
}

VARIATION #4
===========
void main( void )
{
     gl_Position = ftransform();
}
[/code]

Any help would be really appreciated... The full source code is attached.

Thank you!

Cheers,
Guy

Guy Volckaert, ing.
Snr Software Engineer

Meggitt Training Systems (Quebec) Inc.
Systèmes d'entraînement Meggitt (Québec) Inc.
6140 Henri Bourassa West
Montreal, Quebec, H4R 3A6
Canada

Tel: 1 (514) 339 9938 Ext 617
Fax: 1 (514) 339 2641
Cell: 1 (514) 928-5641
email: guy.volcka...@meggitt.com<mailto:brian.bak...@meggitt.com>
url; www.meggitt.com<http://www.meggitt.com>
skype: guy.volckaert

Svp. Considérez l'environnement avant d'imprimer
Please consider the environment before printing this e-mail.


________________________________


This e-mail may contain proprietary information and/or copyright material. This 
e-mail is intended for the use of the addressee only. Any unauthorized use may 
be unlawful. If you receive this e-mail by mistake, please advise the sender 
immediately by using the reply facility in your e-mail software.

Information contained in and/or attached to this document may be subject to 
export control regulations of the European Community, USA, or other countries. 
Each recipient of this document is responsible to ensure that usage and/or 
transfer of any information contained in this document complies with all 
relevant export control regulations. If you are in any doubt about the export 
control restrictions that apply to this information, please contact the sender 
immediately.

Be aware that Meggitt may monitor incoming and outgoing e-mails to ensure 
compliance with the Meggitt IT Use policy.

/* OpenSceneGraph example, osgprerender.
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy
*  of this software and associated documentation files (the "Software"), to deal
*  in the Software without restriction, including without limitation the rights
*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the Software is
*  furnished to do so, subject to the following conditions:
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*  THE SOFTWARE.
*/

#include <osg/GLExtensions>
#include <osg/Node>
#include <osg/Geometry>
#include <osg/Notify>
#include <osg/PositionAttitudeTransform>
#include <osg/Texture2D>
#include <osg/TextureRectangle>
#include <osg/Stencil>
#include <osg/ColorMask>
#include <osg/Depth>
#include <osg/Billboard>
#include <osg/Material>
#include <osg/AnimationPath>
#include <osg/Math>

#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/StateSetManipulator>

#include <osgUtil/SmoothingVisitor>

#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgDB/FileUtils>

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>

#include <iostream>
#include <algorithm>

osg::ref_ptr<osg::Camera> g_pPreCamera = NULL;
osg::ref_ptr<osg::Camera> g_pPostCamera = NULL;
osg::ref_ptr<osg::Texture>  g_pRttTexture = NULL;
osg::ref_ptr<osg::Geode> g_pPolyGeode = NULL;
osg::ref_ptr<osg::Geometry> g_pPolyGeom = NULL;
bool g_bTexRect = false;
bool g_bForcePOT = false;

// Convert a number to the closest upper power-of-two value.
inline int ComputeUpperPowerOfTwo( int s )
{
    static double ks_dLog2 = log10( 2.0 );
    return ( 1 << ( int ) ceil( log10( ( double ) s ) / ks_dLog2 ) );
}

bool UpdateRtt( osg::Camera* pCam )
{
    bool bDirtyTexture = true;

    double win_width  = pCam->getGraphicsContext()->getTraits()->width;
    double win_height = pCam->getGraphicsContext()->getTraits()->height;

    double vp_x      = pCam->getViewport()->x();
    double vp_y      = pCam->getViewport()->y();
    double vp_width  = pCam->getViewport()->width();
    double vp_height = pCam->getViewport()->height();

    printf( "[resize] [win] w=%d h=%d\n", (int)win_width, (int)win_height );
    printf( "[resize] [vp]  x=%d y=%d w=%d h=%d\n", (int)vp_x, (int)vp_y, 
(int)vp_width, (int)vp_height);

    if( g_pRttTexture )
    {
        switch( g_pPreCamera->getRenderTargetImplementation( ) )
        {
        case osg::Camera::FRAME_BUFFER_OBJECT:
            {
                osg::Vec2d texcoord_min;
                osg::Vec2d texcoord_max;

                osg::Vec2d clip_min( osg::maximum( vp_x, 0.0 ), osg::maximum( 
vp_y, 0.0 ) );
                osg::Vec2d clip_max( osg::maximum( vp_x+vp_width, 0.0 ), 
osg::maximum( vp_y+vp_height, 0.0 ) );

                if( g_bTexRect )
                {
                    // Resize the render-to-texture according to the viewports 
size.
                    osg::TextureRectangle* pTexRect = 
dynamic_cast<osg::TextureRectangle*>( g_pRttTexture.get( ) );
                    pTexRect->setTextureSize( clip_max.x(), clip_max.y());
                    pTexRect->dirtyTextureObject( );

                    texcoord_min.set( clip_min.x(), clip_min.y() );
                    texcoord_max.set( clip_max.x(), clip_max.y() );
                }
                else
                {
                    osg::Texture2D* tex2d = dynamic_cast<osg::Texture2D*>( 
g_pRttTexture.get() );
                    if( g_bForcePOT )
                    {
                        // This will not work since OSG will reset the texture 
size to the window size.
                        int nPOTWidth = ComputeUpperPowerOfTwo( clip_max.x() );
                        int nPOTHeight = ComputeUpperPowerOfTwo( clip_max.y() );
                        if( nPOTWidth != tex2d->getTextureWidth( ) || 
nPOTHeight != tex2d->getTextureHeight( ) )
                        {
                            tex2d->setTextureSize( nPOTWidth, nPOTHeight );
                            tex2d->dirtyTextureObject( );
                        }
                        else
                        {
                            bDirtyTexture = false;
                        }
                    }
                    else
                    {
                        tex2d->setTextureSize( clip_max.x(), clip_max.x());
                        tex2d->dirtyTextureObject( );
                    }

                    texcoord_min.set( clip_min.x() / tex2d->getTextureWidth( ), 
clip_min.y() / tex2d->getTextureHeight( ) );
                    texcoord_max.set( clip_max.x() / tex2d->getTextureWidth( ), 
clip_max.y() / tex2d->getTextureHeight( ) );
                }

                // Adjust the quad's texture coordinates.
                osg::Vec2Array* pTexCoords = static_cast<osg::Vec2Array*>( 
g_pPolyGeom->getTexCoordArray( 0 ) );
                (*pTexCoords)[0].set( texcoord_min.x(), texcoord_min.y() );
                (*pTexCoords)[1].set( texcoord_max.x(), texcoord_min.y() );
                (*pTexCoords)[2].set( texcoord_max.x(), texcoord_max.y() );
                (*pTexCoords)[3].set( texcoord_min.x(), texcoord_max.y() );

                osg::Vec3Array* pVertices = static_cast<osg::Vec3Array*>( 
g_pPolyGeom->getVertexArray( ) );
                (*pVertices)[0].set( osg::Vec3( 
(clip_min.x()-vp_x)/(0.5*vp_width)-1, (clip_min.y()-vp_y)/(0.5*vp_height)-1, 
0.0 ) );
                (*pVertices)[1].set( osg::Vec3( 
(clip_max.x()-vp_x)/(0.5*vp_width)-1, (clip_min.y()-vp_y)/(0.5*vp_height)-1, 
0.0 ) );
                (*pVertices)[2].set( osg::Vec3( 
(clip_max.x()-vp_x)/(0.5*vp_width)-1, (clip_max.y()-vp_y)/(0.5*vp_height)-1, 
0.0 ) );
                (*pVertices)[3].set( osg::Vec3( 
(clip_min.x()-vp_x)/(0.5*vp_width)-1, (clip_max.y()-vp_y)/(0.5*vp_height)-1, 
0.0 ) );
                g_pPolyGeom->dirtyDisplayList();
            }
            break;
        case osg::Camera::FRAME_BUFFER:
            {
                osg::Vec2d texcoord_min;
                osg::Vec2d texcoord_max;

                osg::Vec2d clip_min( osg::maximum( vp_x, 0.0 ), osg::maximum( 
vp_y, 0.0 ) );
                osg::Vec2d clip_max( osg::maximum( vp_x+vp_width, 0.0 ), 
osg::maximum( vp_y+vp_height, 0.0 ) );

                if( g_bTexRect )
                {
                    // Resize the render-to-texture according to the viewports 
size.
                    osg::TextureRectangle* pTexRect = 
dynamic_cast<osg::TextureRectangle*>( g_pRttTexture.get( ) );
                    pTexRect->setTextureSize( clip_max.x(), clip_max.y());
                    pTexRect->dirtyTextureObject( );

                    texcoord_min.set( clip_min.x(), clip_min.y() );
                    texcoord_max.set( clip_max.x(), clip_max.y() );
                }
                else
                {
                    osg::Texture2D* tex2d = dynamic_cast<osg::Texture2D*>( 
g_pRttTexture.get() );

                    if( g_bForcePOT )
                    {
                        // This will not work since OSG will reset the texture 
size to the window size.
                        int nPOTWidth = ComputeUpperPowerOfTwo( clip_max.x() );
                        int nPOTHeight = ComputeUpperPowerOfTwo( clip_max.y() );
                        if( nPOTWidth != tex2d->getTextureWidth( ) || 
nPOTHeight != tex2d->getTextureHeight( ) )
                        {
                            tex2d->setTextureSize( nPOTWidth, nPOTHeight );
                            tex2d->dirtyTextureObject( );
                        }
                        else
                        {
                            bDirtyTexture = false;
                        }
                    }
                    else
                    {
                        tex2d->setTextureSize( clip_max.x(), clip_max.y());
                        tex2d->dirtyTextureObject( );
                    }

                    texcoord_min.set( clip_min.x() / tex2d->getTextureWidth( ), 
clip_min.y() / tex2d->getTextureHeight( ) );
                    texcoord_max.set( clip_max.x() / tex2d->getTextureWidth( ), 
clip_max.y() / tex2d->getTextureHeight( ) );
                }

                // Adjust the quad's texture coordinates.
                osg::Vec2Array* pTexCoords = static_cast<osg::Vec2Array*>( 
g_pPolyGeom->getTexCoordArray( 0 ) );
                (*pTexCoords)[0].set( texcoord_min.x(), texcoord_min.y() );
                (*pTexCoords)[1].set( texcoord_max.x(), texcoord_min.y() );
                (*pTexCoords)[2].set( texcoord_max.x(), texcoord_max.y() );
                (*pTexCoords)[3].set( texcoord_min.x(), texcoord_max.y() );

                osg::Vec3Array* pVertices = static_cast<osg::Vec3Array*>( 
g_pPolyGeom->getVertexArray( ) );
                (*pVertices)[0].set( osg::Vec3( 
(clip_min.x()-vp_x)/(0.5*vp_width)-1, (clip_min.y()-vp_y)/(0.5*vp_height)-1, 
0.0 ) );
                (*pVertices)[1].set( osg::Vec3( 
(clip_max.x()-vp_x)/(0.5*vp_width)-1, (clip_min.y()-vp_y)/(0.5*vp_height)-1, 
0.0 ) );
                (*pVertices)[2].set( osg::Vec3( 
(clip_max.x()-vp_x)/(0.5*vp_width)-1, (clip_max.y()-vp_y)/(0.5*vp_height)-1, 
0.0 ) );
                (*pVertices)[3].set( osg::Vec3( 
(clip_min.x()-vp_x)/(0.5*vp_width)-1, (clip_max.y()-vp_y)/(0.5*vp_height)-1, 
0.0 ) );
                g_pPolyGeom->dirtyDisplayList();
            }
            break;
        }
    }

    return bDirtyTexture;
}

struct CGeSgResizedCallback : public osg::GraphicsContext::ResizedCallback
{
public:
    CGeSgResizedCallback( osg::Camera* pCam ) { m_pCam = pCam; }
    virtual void resizedImplementation( osg::GraphicsContext* gc, int x, int y, 
int width, int height )
    {
        gc->resizedImplementation( x, y, width, height );

        // Only rebuild the cameras is the RTT texture has be resized.
        if( UpdateRtt( m_pCam ) )
        {
            if( g_pPreCamera && g_pPreCamera->getRenderTargetImplementation() 
!= osg::Camera::FRAME_BUFFER )
            {
                // Create a new camera.
                osg::ref_ptr<osg::Camera> pNewCam = new osg::Camera( 
*g_pPreCamera );

                // Destroy the old camera.
                osg::ref_ptr<osg::Group> pParent = g_pPreCamera->getParent( 0 
)->asGroup( );
                pParent->removeChild( g_pPreCamera );

                // Maintain the new camera.
                g_pPreCamera = pNewCam;
                pParent->addChild( g_pPreCamera );
            }
        }

        double fovy, aspectRatio, zNear, zFar;        
        m_pCam->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, 
zFar);

        double newAspectRatio = m_pCam->getViewport()->width( 
)/m_pCam->getViewport()->height();
        m_pCam->setProjectionMatrixAsPerspective( fovy, newAspectRatio, zNear, 
zFar);
    }
protected:
    osg::ref_ptr<osg::Camera> m_pCam;
};

void loadShaders( )
{
    osg::StateSet* pStateSet = g_pPolyGeode->getOrCreateStateSet( );

    osg::ref_ptr<osg::Program> pProgram = new osg::Program;
    pProgram->setName( "PostProcessProgram" ); 

    osg::ref_ptr<osg::Shader> pVertShader = osg::Shader::readShaderFile( 
osg::Shader::VERTEX, osgDB::findDataFile( "shaders/osgprerender.vert" ) );
    if( pVertShader )
    {
        pVertShader->setName( "osgprerender.vert" );
        pProgram->addShader( pVertShader );
//        pProgram->addBindAttribLocation( "osg_Vertex", 0 );
    }

    osg::ref_ptr<osg::Shader> pFragShader = osg::Shader::readShaderFile( 
osg::Shader::FRAGMENT, osgDB::findDataFile( "shaders/osgprerender.frag" ) );
    if( pFragShader )
    {
        pFragShader->setName( "osgprerender.frag" );
        pProgram->addShader( pFragShader );
    }

    // RTT texture.
    pStateSet->addUniform( new osg::Uniform( "tRttTexture", 0 ) );
    pStateSet->setAttributeAndModes( pProgram, osg::StateAttribute::ON );
}


osg::Node* createPreRenderSubGraph(osg::Camera* pMasterCam, osg::Node* subgraph,
                                   osg::Camera::RenderTargetImplementation 
renderImplementation,
                                   bool usePostCam )
{
    if (!subgraph) return 0;

    // create a group to contain the flag and the pre rendering camera.
    osg::Group* parent = new osg::Group;

    if( g_bTexRect )
    {
        // texture to render to and to use for rendering of flag.
        osg::TextureRectangle* textureRect = new osg::TextureRectangle;
        textureRect->setDataVariance( osg::Object::DYNAMIC );
        textureRect->setInternalFormat(GL_RGB);
        
textureRect->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
        
textureRect->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
        g_pRttTexture = textureRect;
    }
    else
    {
        osg::Texture2D* tex2d = new osg::Texture2D;
        tex2d->setDataVariance( osg::Object::DYNAMIC );
        tex2d->setInternalFormat( GL_RGB );
        tex2d->setNumMipmapLevels( 1 );
        tex2d->setFilter( osg::Texture::MIN_FILTER, osg::Texture::NEAREST );
        tex2d->setFilter( osg::Texture::MAG_FILTER, osg::Texture::NEAREST );
        tex2d->setWrap( osg::Texture::WRAP_S, osg::Texture::REPEAT );
        tex2d->setWrap( osg::Texture::WRAP_T, osg::Texture::REPEAT );
        g_pRttTexture = tex2d;
    }
   

    // then create the pre-render camera
    {    
        g_pPreCamera = new osg::Camera;

        // set viewport
        g_pPreCamera->setViewport( pMasterCam->getViewport( ));

        // set the camera to render before the main camera.
        g_pPreCamera->setDataVariance( osg::Object::DYNAMIC );
        g_pPreCamera->setRenderOrder(osg::Camera::PRE_RENDER);
        g_pPreCamera->setReferenceFrame( osg::Camera::RELATIVE_RF );
        g_pPreCamera->setAllowEventFocus( false );
        g_pPreCamera->setComputeNearFarMode( 
osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR );
        g_pPreCamera->setAllowEventFocus( false );
        g_pPreCamera->setCullingMode( osg::Camera::ENABLE_ALL_CULLING );
        g_pPreCamera->setClearMask( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | 
GL_STENCIL_BUFFER_BIT );
        g_pPreCamera->setClearStencil( 0 );

        // tell the camera to use OpenGL frame buffer object where supported.
        g_pPreCamera->setRenderTargetImplementation(renderImplementation);

        // attach the texture and use it as the color buffer.
        g_pPreCamera->attach( osg::Camera::BufferComponent( 
osg::Camera::COLOR_BUFFER ), g_pRttTexture );
        g_pPreCamera->attach( osg::Camera::PACKED_DEPTH_STENCIL_BUFFER, 
GL_DEPTH_STENCIL_EXT);

        // add subgraph to render
        g_pPreCamera->addChild(subgraph);
        parent->addChild(g_pPreCamera);
    }    

    // then create the post-render camera
    {  
        osg::Group* geoparent = NULL;

        if( usePostCam )
        {
            g_pPostCamera = new osg::Camera;

            // set viewport
            g_pPostCamera->setDataVariance( osg::Object::DYNAMIC );
            g_pPostCamera->setViewport(pMasterCam->getViewport( ));
            g_pPostCamera->setRenderOrder(osg::Camera::POST_RENDER );
            g_pPostCamera->setReferenceFrame( osg::Camera::ABSOLUTE_RF );
            g_pPostCamera->setProjectionMatrix( osg::Matrix::ortho2D( -1, 1, 
-1, 1 ) );
            g_pPostCamera->setViewMatrix( osg::Matrix::identity() ); 
            g_pPostCamera->setAllowEventFocus( false );
            g_pPostCamera->setCullingMode( osg::Camera::ENABLE_ALL_CULLING );
            parent->addChild(g_pPostCamera);

            geoparent = g_pPostCamera;
        }
        else
        {
            osg::Projection* projection = new osg::Projection;
            projection->setMatrix( osg::Matrixd::ortho2D( -1, 1, -1, 1 ) );
            parent->addChild( projection );

            osg::PositionAttitudeTransform* mat = new 
osg::PositionAttitudeTransform( );
            mat->setReferenceFrame( osg::PositionAttitudeTransform::ABSOLUTE_RF 
);
            projection->addChild( mat );

            geoparent = mat;
        }

        // first create the geometry of which to view.
        { 
            // create the to visualize.
            g_pPolyGeom = new osg::Geometry();
            g_pPolyGeom->setName( "PolyGeom" );
            g_pPolyGeom->setDataVariance( osg::Object::DYNAMIC );

            osg::Vec3Array* pVertices = new osg::Vec3Array(4);
            g_pPolyGeom->setVertexArray( pVertices );

            osg::Vec2Array* pTexCoords = new osg::Vec2Array(4);
            g_pPolyGeom->setTexCoordArray( 0, pTexCoords );

            osg::Vec4Array* pColours = new osg::Vec4Array;
            pColours->push_back( osg::Vec4( 1.0f, 1.0f, 1.0, 1.0f ) );
            g_pPolyGeom->setColorArray( pColours );
            g_pPolyGeom->setColorBinding( osg::Geometry::BIND_OVERALL );

            g_pPolyGeom->addPrimitiveSet( new osg::DrawArrays( 
osg::PrimitiveSet::QUADS, 0, pVertices->size( ) ) );

            osg::StateSet* pStateSet = g_pPolyGeom->getOrCreateStateSet( );
            pStateSet->setMode( GL_DEPTH_TEST, osg::StateAttribute::OFF );
            pStateSet->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
            pStateSet->setMode( GL_FOG, osg::StateAttribute::OFF );
            pStateSet->setMode( GL_CULL_FACE, osg::StateAttribute::OFF );
            pStateSet->setMode( GL_BLEND, osg::StateAttribute::OFF );
            pStateSet->setTextureAttributeAndModes( 0, g_pRttTexture, 
osg::StateAttribute::ON );

            g_pPolyGeode = new osg::Geode();
            g_pPolyGeode->setStateSet(pStateSet);
            g_pPolyGeode->addDrawable(g_pPolyGeom);
            
            geoparent->addChild(g_pPolyGeode);

            UpdateRtt( pMasterCam );

            loadShaders( );
        }
    }


    return parent;
}

int main( int argc, char **argv )
{
    // use an ArgumentParser object to manage the program arguments.
    osg::ArgumentParser arguments(&argc,argv);

    // set up the usage document, in case we need to print out how to use this 
program.
    
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+"
 is the example which demonstrates pre rendering of scene to a texture, and 
then apply this texture to geometry.");
    
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+"
 [options] filename ...");
    arguments.getApplicationUsage()->addCommandLineOption("-h or 
--help","Display this information");
    arguments.getApplicationUsage()->addCommandLineOption("--fbo","Use Frame 
Buffer Object for render to texture, where supported.");
    arguments.getApplicationUsage()->addCommandLineOption("--fb","Use 
FrameBuffer for render to texture.");
    arguments.getApplicationUsage()->addCommandLineOption("--pbuffer","Use 
Pixel Buffer for render to texture, where supported.");
    arguments.getApplicationUsage()->addCommandLineOption("--window","Use a 
separate Window for render to texture.");
    arguments.getApplicationUsage()->addCommandLineOption("--vp_x","Set the x 
coordinate of the viewport.");
    arguments.getApplicationUsage()->addCommandLineOption("--vp_y","Set the y 
coordinate of the viewport.");
    arguments.getApplicationUsage()->addCommandLineOption("--vp_width","Set the 
width of the viewport.");
    arguments.getApplicationUsage()->addCommandLineOption("--vp_height","Set 
the height of the viewport.");
    arguments.getApplicationUsage()->addCommandLineOption("--tex_width","Set 
the width of the render to texture.");
    arguments.getApplicationUsage()->addCommandLineOption("--tex_height","Set 
the height of the render to texture.");
    arguments.getApplicationUsage()->addCommandLineOption("--image","Render to 
an image, then apply a post draw callback to it, and use this image to update a 
texture.");
    arguments.getApplicationUsage()->addCommandLineOption("--texrect","Use 
osg::TextureRectangle for doing the render to texture to.");
    arguments.getApplicationUsage()->addCommandLineOption("--pot","Forces the 
use of power-of-two textures.");
   
    // construct the viewer.
    osgViewer::Viewer viewer(arguments);

    // add stats
    viewer.addEventHandler(new osgViewer::StatsHandler());

    // add the record camera path handler
    viewer.addEventHandler(new osgViewer::RecordCameraPathHandler());

    // add the threading handler
    viewer.addEventHandler(new osgViewer::ThreadingHandler());

    // add the windeow size handler
    viewer.addEventHandler(new osgViewer::WindowSizeHandler());

    // add the state set handler
    viewer.addEventHandler(new 
osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));

    // if user request help write it out to cout.
    if (arguments.read("-h") || arguments.read("--help"))
    {
        arguments.getApplicationUsage()->write(std::cout);
        return 1;
    }



    int tex_width  = 1024;
    int tex_height = 768;
    int vp_x       = 0;
    int vp_y       = 0;
    int vp_width   = 1024;
    int vp_height  = 768;
    bool usePostCam = false;
    
    while (arguments.read("--tex_width", tex_width)) {}
    while (arguments.read("--tex_height", tex_height)) {}
    while (arguments.read("--vp_x", vp_x)) {}
    while (arguments.read("--vp_y", vp_y)) {}
    while (arguments.read("--vp_width", vp_width)) {}
    while (arguments.read("--vp_height", vp_height)) {}

    osg::Camera::RenderTargetImplementation renderImplementation = 
osg::Camera::FRAME_BUFFER_OBJECT;
    
    while (arguments.read("--fbo")) { renderImplementation = 
osg::Camera::FRAME_BUFFER_OBJECT; }
    while (arguments.read("--pbuffer")) { renderImplementation = 
osg::Camera::PIXEL_BUFFER; }
    while (arguments.read("--pbuffer-rtt")) { renderImplementation = 
osg::Camera::PIXEL_BUFFER_RTT; }
    while (arguments.read("--fb")) { renderImplementation = 
osg::Camera::FRAME_BUFFER; }
    while (arguments.read("--window")) { renderImplementation = 
osg::Camera::SEPERATE_WINDOW; }
    while (arguments.read("--postcam")) { usePostCam = true; }
    while (arguments.read("--texrect")) { g_bTexRect = true; }
    while (arguments.read("--pot")) 
    { 
        g_bForcePOT = true;
        osg::setGLExtensionDisableString( osg::getGLExtensionDisableString() + 
";GL_ARB_texture_non_power_of_two;" );
    }

#if 1
        osg::GraphicsContext::Traits* pTraits = new 
osg::GraphicsContext::Traits;
        pTraits->windowName       = "osgprerender2";
        pTraits->x                = 20; // GetSystemMetrics( SM_CYSIZE ) + 
GetSystemMetrics( SM_CXFRAME );
    pTraits->y                = 20; // GetSystemMetrics( SM_CYFRAME );
        pTraits->width            = tex_width;
        pTraits->height           = tex_height;
        pTraits->windowDecoration = true;
        pTraits->doubleBuffer     = true;
        pTraits->supportsResize   = true;
        pTraits->useCursor        = true;
    pTraits->stencil          = 8;
    pTraits->alpha            = 8;
        pTraits->sharedContext    = NULL;
//  pTraits->pbuffer          = false;
//      pTraits->target                   = 0;
//      pTraits->format                   = 0;
//      pTraits->level                    = 0;
//      pTraits->face                     = 0;
        pTraits->setInheritedWindowPixelFormat = true;

    osg::ref_ptr<osg::GraphicsContext> gc = 
osg::GraphicsContext::createGraphicsContext(pTraits);
    viewer.getCamera( )->setGraphicsContext(gc.get());
    viewer.getCamera( )->setViewport(new osg::Viewport(vp_x, vp_y, vp_width, 
vp_height));
    viewer.getCamera( )->setProjectionResizePolicy( osg::Camera::FIXED );
    // Register the resized callback.
    gc->setResizedCallback( new CGeSgResizedCallback( viewer.getCamera( ) ) );

    double fovy, aspectRatio, zNear, zFar;        
    viewer.getCamera( )->getProjectionMatrixAsPerspective(fovy, aspectRatio, 
zNear, zFar);

    double newAspectRatio = double(vp_width) / vp_height;
    double aspectRatioChange = newAspectRatio / aspectRatio;
    if (aspectRatioChange != 1.0)
    {
        viewer.getCamera( )->getProjectionMatrix() *= 
osg::Matrix::scale(1.0/aspectRatioChange,1.0,1.0);
    }

    GLenum buffer = pTraits->doubleBuffer ? GL_BACK : GL_FRONT;

    viewer.getCamera( )->setDrawBuffer(buffer);
    viewer.getCamera( )->setReadBuffer(buffer);
#else
    viewer.setUpViewInWindow( 0, 0, tex_width, tex_height );
#endif

    // set up the background color and clear mask.
    viewer.getCamera( )->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
    viewer.getCamera( )->setClearMask( GL_COLOR_BUFFER_BIT | 
GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
    viewer.getCamera( )->setViewMatrix( osg::Matrixd( ) );
    
    // load the nodes from the commandline arguments.
    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
    
    // if not loaded assume no arguments passed in, try use default mode 
instead.
    if (!loadedModel) loadedModel = osgDB::readNodeFile("cessna.osg");
    
    if (!loadedModel)
    {
        printf("Could not load model <cessna.osg>\n");
        return 1;
    }
    
    // create a transform to spin the model.
    osg::PositionAttitudeTransform* loadedModelTransform = new 
osg::PositionAttitudeTransform;
    loadedModelTransform->addChild(loadedModel);
    loadedModelTransform->setPosition( osg::Vec3( 0.0, 0.0, -50.0f ) );

    osg::Group* rootNode = new osg::Group();
    rootNode->addChild(createPreRenderSubGraph(viewer.getCamera( ), 
loadedModelTransform, renderImplementation, usePostCam ));
    //rootNode->addChild( loadedModelTransform );

    osgDB::writeNodeFile(*rootNode, "test.osg");

    // add model to the viewer.
    viewer.setSceneData( rootNode );

    viewer.setThreadingModel( osgViewer::ViewerBase::SingleThreaded );
    int run_ret = viewer.run();
    printf("run returned:%d\n", run_ret );
    return run_ret;
}
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to