Hi again!
Now I have changed my scenegraph according to your drawing, and put the RTT 
camera under a root node, but it still doesn't work.
 
This time I attach all the code in my application, hope anybody has time to 
have a look at it.
 
Åsa
 
// 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);

// Create a transform node for the sphere object.
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(worldParent);

// 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;

// Create main camera.
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 world parent'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); // View matrix is 
set manually.
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);
osg::Matrixd rotationToYUp;    // Conversion to OpenGL "Y up" standard.
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();
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");
}
while (!viewer.done())
{
    viewer.frame();
}



________________________________

Från: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] För Andreas Lindmark
Skickat: den 7 november 2008 10:33
Till: OpenSceneGraph Users
Ämne: Re: [osg-users] Render to texture problem


The camera has to be somewhere in the scenegraph under the root. 
Something like this:

         Root
     /            \
RTTCamera  Node1
     \            /
        World

Set the uniforms that you currently set at the Root in Node1 instead and they 
will not be used when rendering with the RTTCamera.

/Andreas


2008/11/7 Engvall Åsa <[EMAIL PROTECTED]>


        No, it isn't. Should it be?
        I tried to draw the scenegraph in my first message, but it's not easy 
just using text.
        If you want more code, please tell me.
        

        Åsa
        
        -----Ursprungligt meddelande-----
        Från: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] För J.P. Delport
        
        Skickat: den 7 november 2008 09:42
        
        Till: OpenSceneGraph Users
        Ämne: Re: [osg-users] Render to texture problem
        
        Hi,
        
        is your RTT camera the child of anything? I'm just guessing without 
having an example to run.
        
        jp
        
        Engvall Åsa wrote:
        > I thought that the texture must be in RGBA to be used in a shader. The
        > callback was just for debugging, so I didn't think so much about the
        > format. Now I have changed to RGBA format in the callback too. I have
        > also set the viewer to singlethreaded, using this command:
        > viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
        >
        > Unfortunately, the texture is still gray.
        >
        > Åsa
        >
        > -----Ursprungligt meddelande-----
        > Från: [EMAIL PROTECTED]
        > [mailto:[EMAIL PROTECTED] För J.P. Delport
        > Skickat: den 7 november 2008 08:22
        > Till: OpenSceneGraph Users
        > Ämne: Re: [osg-users] Render to texture problem
        >
        > Hi,
        >
        > any reason why you have RGBA texture, but RGB in callback?
        >
        > jp
        >
        > Engvall Åsa wrote:
        >> Hi!
        >>
        >> I want to render the scene to a texture and use the texture in a 
shader.
        >> The texture image is saved to file by a postdraw callback, so I can
        >> look at it. I'm doing something wrong because the texture becomes 
all gray.
        >>
        >> My scene graph looks like this:
        >>
        >> RTT camera      root
        >>        |                 |
        >>        \                /
        >>         world
        >>
        >> The main viewer looks at the root and the shader program is applied
        >> to the root stateset. Below is a code snippet.
        >>
        >> I would be very grateful if somebody could point out what I'm doing 
wrong.
        >> Thanks in advance,
        >>
        >> Åsa Engvall
        >>
        >>
        >> // 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,
        >> texture->osg::Texture2D::NEAREST);
        >> texture->setFilter(osg::Texture2D::MAG_FILTER,
        >> texture->osg::Texture2D::NEAREST);
        >> osg::Image* textureImage = new osg::Image;
        >> textureImage->allocateImage(traits->width, traits->height, 1,
        
        >> textureImage->GL_RGBA,
        
        >> GL_UNSIGNED_BYTE);
        >> texture->setImage(0, textureImage);
        >>
        >> // Add program and uniforms to the root's stateset.
        >> osg::StateSet* state = root->getOrCreateStateSet();
        >> state->addUniform(numberOfTimeStepsU);
        >> state->addUniform(timeConstantU);
        >> state->addUniform(frameTimeU);
        >> state->addUniform(sceneTextureU);
        >> state->addUniform(prevModelViewMatrixU);
        >> state->addUniform(prevModelViewProjectionMatrixU);
        >> state->setAttributeAndModes(motionBlurProgram,
        >> state->osg::StateAttribute::ON); setTextureAttributeAndModes(0,
        >> state->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->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR
        >> RTTcamera->) ; setRenderOrder(osg::Camera::PRE_RENDER);
        >> RTTcamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OB
        
        >> RTTcamera->J ECT); attach(osg::Camera::COLOR_BUFFER, texture);
        
        >> RTTcamera->addChild(world); attach(osg::Camera::COLOR_BUFFER,
        >> RTTcamera->textureImage); setPostDrawCallback(new
        >> RTTcamera->ScreenShotCallback(traits->width,
        >> traits->height, *textureImage, GL_RGB, GL_UNSIGNED_BYTE));
        >>
        >>
        >>
        >> ---------------------------------------------------------------------
        >> -
        >> --
        >>
        >> _______________________________________________
        >> osg-users mailing list
        >> osg-users@lists.openscenegraph.org
        >> 
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.
        >> org
        >
        > --
        > This message is subject to the CSIR's copyright terms and conditions, 
e-mail legal notice, and implemented Open Document Format (ODF) standard.
        > The full disclaimer details can be found at 
http://www.csir.co.za/disclaimer.html.
        >
        > This message has been scanned for viruses and dangerous content by 
MailScanner, and is believed to be clean.  MailScanner thanks Transtec 
Computers for their support.
        >
        > _______________________________________________
        > osg-users mailing list
        > osg-users@lists.openscenegraph.org
        > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.
        > org _______________________________________________
        > osg-users mailing list
        > osg-users@lists.openscenegraph.org
        > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.
        > org
        
        --
        This message is subject to the CSIR's copyright terms and conditions, 
e-mail legal notice, and implemented Open Document Format (ODF) standard.
        The full disclaimer details can be found at 
http://www.csir.co.za/disclaimer.html.
        
        This message has been scanned for viruses and dangerous content by 
MailScanner, and is believed to be clean.  MailScanner thanks Transtec 
Computers for their support.
        
        _______________________________________________
        osg-users mailing list
        osg-users@lists.openscenegraph.org
        
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
        _______________________________________________
        osg-users mailing list
        osg-users@lists.openscenegraph.org
        
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
        


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

Reply via email to