Recently, I tried to render the scene to a texture, and after some help I got 
something in the texture. 
Now, I've found that the texture doesn't show the scene but the previous frame. 
With shading and everything. How is that possible?

My scene graph looks like this:

        root
        /   \
RTTcamera worldParent
        \  /
        world

The shader is applied to the worldParent and the RTTcamera is pre render. 
I want the RTT camera to see the world "as it is", with no shading, and render 
the scene to a texture. This texture should then be used in the shader.

What am I doing wrong this time??? I would be thankful for any help.

Regards,
Åsa Engvall

-----------------------------Code-----------------------------------------------

int main()
{
        // Create group nodes.
        osg::Group* root = new osg::Group;
        osg::Group* worldParent = new osg::Group;
        osg::Group* world = new osg::Group;
        root->addChild(worldParent);
        worldParent->addChild(world);

        // Create a sphere object.
        osg::Sphere* sphere = new osg::Sphere(osg::Vec3(0, 0, 0), 10);          
        osg::ShapeDrawable* sphereDrawable = new osg::ShapeDrawable(sphere);
        sphereDrawable->setColor(osg::Vec4(0, 0, 0, 1));                        
                
        osg::Geode* object = new osg::Geode();
        object->addDrawable(sphereDrawable);

        // Declare transform nodes for dynamic objects.
        osg::PositionAttitudeTransform* objectTransform = new 
osg::PositionAttitudeTransform;
        objectTransform->addChild(object);              
        world->addChild(objectTransform);

        // Create a viewer that looks at the world.
        osgViewer::Viewer viewer;
        viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
        viewer.setSceneData(root);

        // Create a graphics window.
        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new 
osg::GraphicsContext::Traits;
        traits->x = 100;                                                
        traits->y = 100;                                                
        traits->width = 640;
        traits->height = 480;
        traits->windowDecoration = true;
        traits->doubleBuffer = true;
        traits->sharedContext = 0;

        // Attach a camera to the graphics window.
        osg::ref_ptr<osg::GraphicsContext> gc = 
osg::GraphicsContext::createGraphicsContext(traits.get());
        osg::ref_ptr<osg::Camera> camera = viewer.getCamera();
        camera->setGraphicsContext(gc.get());
        camera->setViewport(new osg::Viewport(0, 0, traits->width, 
traits->height));
        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
        camera->setDrawBuffer(buffer);
        camera->setReadBuffer(buffer);
        camera->setClearColor(osg::Vec4(1,1,1,1));
                
        // Camera callback for capturing the image.     
        osg::Image* image = new osg::Image;     
        camera->setPostDrawCallback(new ScreenShotCallback(traits->width, 
traits->height, *image, GL_RGBA, GL_UNSIGNED_BYTE));

      // Create shader objects.
        osg::Shader* motionBlurVertexShader = new 
osg::Shader(osg::Shader::VERTEX);
        osg::Shader* motionBlurFragmentShader = new 
osg::Shader(osg::Shader::FRAGMENT);
        motionBlurVertexShader->loadShaderSourceFromFile
                ("H:/My Documents/Visual Studio 
2008/Projects/osg_sfunction/osg_sfunction/motionblur.vert");
        motionBlurFragmentShader->loadShaderSourceFromFile
                ("H:/My Documents/Visual Studio 
2008/Projects/osg_sfunction/osg_sfunction/motionblur.frag");
        
        // Add shader objects to a program.
        osg::Program* motionBlurProgram = new osg::Program;
        motionBlurProgram->addShader(motionBlurVertexShader);
        motionBlurProgram->addShader(motionBlurFragmentShader);

        // Define uniforms that will be passed to the shader.
        osg::Uniform* numberOfTimeStepsU = new 
osg::Uniform("numberOfTimeSteps", int(10));
        osg::Uniform* timeConstantU = new osg::Uniform("timeConstant", 
float(0.01));
        osg::Uniform* frameTimeU = new osg::Uniform("frameTime", float(0.02));
        osg::Uniform* sceneTextureU = new osg::Uniform("sceneTexture", 0);
        osg::Uniform* prevModelViewMatrixU = new 
osg::Uniform(osg::Uniform::FLOAT_MAT4, "prevModelViewMatrix");
        osg::Uniform* prevModelViewProjectionMatrixU = new 
osg::Uniform(osg::Uniform::FLOAT_MAT4, "prevModelViewProjectionMatrix");

        // Create a scene texture.
        osg::Texture2D* texture = new osg::Texture2D;
        texture->setResizeNonPowerOfTwoHint(false);
        texture->setTextureSize(traits->width, traits->height);
        texture->setInternalFormat(GL_RGBA);
        texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST);
        texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST);
        osg::Image* textureImage = new osg::Image;
      textureImage->allocateImage(traits->width, traits->height, 1, GL_RGBA, 
GL_UNSIGNED_BYTE);
        //texture->setImage(0, textureImage);

        // Add program and uniforms to the main camera's stateset.
        osg::StateSet* state = worldParent->getOrCreateStateSet();
        state->addUniform(numberOfTimeStepsU);
        state->addUniform(timeConstantU);
        state->addUniform(frameTimeU);
        state->addUniform(sceneTextureU);
        state->addUniform(prevModelViewMatrixU);
        state->addUniform(prevModelViewProjectionMatrixU);
        state->setAttributeAndModes(motionBlurProgram, osg::StateAttribute::ON);
        state->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);

        // Create a camera that renders the scene to a texture.
        osg::Camera* RTTcamera = new osg::Camera;
        RTTcamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);              
        RTTcamera->setClearColor(osg::Vec4(1,1,1,1));
        RTTcamera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        RTTcamera->setViewport(0, 0, traits->width, traits->height);
        RTTcamera->setRenderOrder(osg::Camera::PRE_RENDER);
        
RTTcamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
        RTTcamera->attach(osg::Camera::COLOR_BUFFER, texture);
        root->addChild(RTTcamera);
        RTTcamera->addChild(world);
      RTTcamera->attach(osg::Camera::COLOR_BUFFER, textureImage);
      RTTcamera->setPostDrawCallback(new ScreenShotCallback(traits->width, 
traits->height, *textureImage, GL_RGBA, GL_UNSIGNED_BYTE));

        // Object position in world coordinates.
        objectTransform->setPosition(osg::Vec3(-25, 0, 0)); 

        // Camera position in world coordinates.
        osg::Matrixd cameraMatrix;
        osg::Matrixd cameraRotation;
        osg::Matrixd cameraTrans;

        cameraRotation.makeRotate(
                osg::DegreesToRadians(0.0), osg::Vec3(0,1,0),                   
// roll
                osg::DegreesToRadians(0.0), osg::Vec3(1,0,0),                   
// pitch
                osg::DegreesToRadians(0.0), osg::Vec3(0,0,1));              // 
yaw 

        cameraTrans.makeTranslate(0, -500, 0);
        cameraMatrix = cameraRotation*cameraTrans;

        // The view matrix is the inverse of the camera position matrix.
      osg::Matrixd viewMatrix = cameraMatrix.inverse(cameraMatrix);

        // Conversion to OpenGL "Y up" standard.
        osg::Matrixd rotationToYUp;
        rotationToYUp.makeRotate(-M_PI/2.0, osg::Vec3(1.0, 0.0, 0.0)); 
        viewMatrix = viewMatrix*rotationToYUp;  

        // Set view and projection matrices.
        camera->setViewMatrix(viewMatrix);
        camera->setProjectionMatrixAsPerspective(10., 4.0/3.0, 1., 1000.);
        RTTcamera->setViewMatrix(viewMatrix);
        RTTcamera->setProjectionMatrixAsPerspective(10., 4.0/3.0, 1., 1000.);

        // Create callbacks for updating uniform values.
        osg::NodePath np;
        np.push_back(objectTransform);
        prevModelViewMatrixU->setUpdateCallback
                (new UpdateUniformCallback(UpdateUniformCallback::MODEL_VIEW, 
np, camera.get()));
        prevModelViewProjectionMatrixU->setUpdateCallback
                (new 
UpdateUniformCallback(UpdateUniformCallback::MODEL_VIEW_PROJECTION, np, 
camera.get()));

        // Show first frame.
        if (!viewer.isRealized())
        {
                viewer.realize();
        }
        viewer.frame();                 // One extra frame to get things 
started.
        viewer.frame();

        if (image->data())
        {
                osgDB::writeImageFile(*image, "h:/temp/blurImage1.bmp");
        }
        if (textureImage->data())
        {
                osgDB::writeImageFile(*textureImage, 
"h:/temp/textureImage1.bmp");
        }

        // Move the sphere object.
        objectTransform->setPosition(osg::Vec3(25, 0, 0)); 
                
        // Show second frame.
        viewer.frame();
        if (image->data())
        {
                osgDB::writeImageFile(*image, "h:/temp/blurImage2.bmp");
        }
        if (textureImage->data())
        {
                osgDB::writeImageFile(*textureImage, 
"h:/temp/textureImage2.bmp");
        }
        
        viewer.frame();                 // Again one extra frame to see what 
happens with the texture.
        osgDB::writeImageFile(*textureImage, "h:/temp/textureImage3.bmp");

return 0;
}



_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to