This is my modified test. You might have to fiddle with it a bit to get it to work. (best post it to the list so others can get it, again.)

Let me know if there are problems.

/Marcus

José Pedro Dias wrote:
Could you be so kind to send my a OSGFBOViewport example?
I have successfully compiled your classes and created an instance but I'm not quite sure how to set it up to capture the viewport to a texture2d without showing it up. I believe there is/was an example named testFboVp.cpp,
it is mentioned in opensg-users but I couldn't get it.
Thanks in advance!

On 3/12/07, *Yvonne Jung* < [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:

    Hi Andreas and Marcus,

    I like the idea of checking the FBOViewport in into the real cvs,
    but when
    doing so please merge with my original version, which is attached,
    and which I
    already have tried to merge with your improvements some times ago.
    ... because otherwise I will get problems having two of them in
    the same
    namespace!

    Bye
    Yvonne

    P.S.: Thanks for the MField ShaderParam support :-)


    > Andreas Zieringer wrote:
    >> Hi Marcus,
    >>
    >>> You could use a FrameBufferObject-viewport (FBOViewport for
    short). It's
    >>> not a part of 1.8 (it really should be.. hm) but it's been
    posted on
    >>> this list a few times, and I have it available (with some slight
    >>> improvements). Anyway, it would allow you to add a viewport
    that does
    >>> not render to the framebuffer, but still uses the same
    window/gl-context
    >>> as everything else. Ideal for any render-to-texture work.
    >>>
    >> hmm there is no real reason for not adding it to 1.8, if you
    send me the
    >> code I will check it into the cvs. What improvements did you add?
    >>
    > Support for explicit depth texture. I needed that for my depth
    peeling
    > algorithm. I'll e-mail you the code.
    >
    > /Marcus
    >
    >
    -------------------------------------------------------------------------
    > Take Surveys. Earn Cash. Influence the Future of IT
    > Join SourceForge.net's Techsay panel and you'll get the chance
    to share your
    > opinions on IT & business topics through brief surveys-and earn cash
    >
    http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
    <http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV>
    > _______________________________________________
    > Opensg-users mailing list
    > [email protected]
    <mailto:[email protected]>
    > https://lists.sourceforge.net/lists/listinfo/opensg-users
    >

    -------------------------------------------------------------------------
    Take Surveys. Earn Cash. Influence the Future of IT
    Join SourceForge.net 's Techsay panel and you'll get the chance to
    share your
    opinions on IT & business topics through brief surveys-and earn cash
    http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
    <http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV>
    _______________________________________________
    Opensg-users mailing list
    [email protected]
    <mailto:[email protected]>
    https://lists.sourceforge.net/lists/listinfo/opensg-users





--
José Pedro Dias, n.48296, LEIC Tagus Park
[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
------------------------------------------------------------------------

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
------------------------------------------------------------------------

_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users

#include "stdafx.h"

#define USE_FBO 1

#include <OpenSG/OSGGLUT.h>
#include <OpenSG/OSGConfig.h>

#include <OpenSG/OSGSimpleGeometry.h>
#include <OpenSG/OSGGLUTWindow.h>
#include <OpenSG/OSGBaseFunctions.h>

#include <OpenSG/OSGSpotLight.h>
#include <OpenSG/OSGDirectionalLight.h>
#include <OpenSG/OSGPointLight.h>

#include <OpenSG/OSGViewport.h>
#include <OpenSG/OSGPerspectiveCamera.h>
#include <OpenSG/OSGTextureBackground.h>
#include <OpenSG/OSGGradientBackground.h>
#include <OpenSG/OSGSolidBackground.h>
#include <OpenSG/OSGDrawAction.h>
#include <OpenSG/OSGRenderAction.h>
#include <OpenSG/OSGImage.h>
#include <OpenSG/OSGSimpleMaterial.h>
#include <OpenSG/OSGTextureChunk.h>
#include <OpenSG/OSGPolygonChunk.h>

#include <OpenSG/OSGTransform.h>
#include <OpenSG/OSGGroup.h>
#include <OpenSG/OSGTrackball.h>

#include <OpenSG/OSGMatrix.h>
#include <OpenSG/OSGQuaternion.h>
#include <OpenSG/OSGVector.h>

#include <OpenSG/OSGSHLChunk.h>3
#include <OpenSG/OSGSimpleSceneManager.h>
#include <OpenSG/OSGSceneFileHandler.h>

#include <styx/opensg/OSGFBOViewport.h>

#include <boost/test/auto_unit_test.hpp>

// vertex shader program for bump mapping in surface local coordinates
static std::string _vp_program = "void main(void)\n""{\n"
"    gl_Position = ftransform();\n"
"    gl_TexCoord[0]  = gl_TextureMatrix[0] * gl_MultiTexCoord0;\n"
"}\n";

// fragment shader program for bump mapping in surface local coordinates
static std::string _fp_program = "uniform sampler2DRect tex0;\n"
"void main (void)\n""{\n"
"    gl_FragColor = texture2DRect(tex0, gl_TexCoord[0].st);\n""}\n";

OSG_USING_NAMESPACE SimpleSceneManager*  mgr;
GLUTWindowPtr gwin;
ViewportPtr vp;

FBOViewportPtr fbo_vp;
UInt16 imageWinWidth = 512;
UInt16 imageWinHeight = 512;

NodePtr rootNode;
SpotLightPtr spot1_core;
SpotLightPtr spot2_core;
DirectionalLightPtr raumlicht_core;
PointLightPtr _point1_core;
PointLightPtr _point2_core;
TransformPtr _box_trans;
TransformPtr _cylinder1_trans;
TransformPtr _cylinder2_trans;

#ifndef GL_TEXTURE_RECTANGLE_ARB
#define GL_TEXTURE_RECTANGLE_ARB    0x84F5
#endif
NodePtr box_trans_node;
PerspectiveCameraPtr cam;

// forward declaration so we can have the interesting stuff upfront
int setupGLUT(int* argc, char* argv[]);

// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char ** argv)
{
    // OSG init
    osgInit(argc, argv);

    // GLUT init
    int winid = setupGLUT(&argc, argv);
    gwin = GLUTWindow::create();

    // create root
    rootNode = makeCoredNode<Group> ();

    NodePtr scene = makeCoredNode<Group> ();

    // create lights
    TransformPtr point1_trans;
    NodePtr point1 = makeCoredNode<PointLight> (&_point1_core);
    NodePtr point1_beacon = makeCoredNode<Transform> (&point1_trans);
    beginEditCP(point1_trans);
    point1_trans->getMatrix().setTranslate(0.0, 0.0, 25.0);
    endEditCP(point1_trans);

    beginEditCP(_point1_core);
    _point1_core->setAmbient(0.15, 0.15, 0.15, 1);
    _point1_core->setDiffuse(0.4, 0.4, 0.4, 1);
    _point1_core->setSpecular(0.0, 0.0, 0.0, 1);
    _point1_core->setBeacon(point1_beacon);
    _point1_core->setOn(true);
    endEditCP(_point1_core);

    TransformPtr point2_trans;
    NodePtr point2 = makeCoredNode<PointLight> (&_point2_core);
    NodePtr point2_beacon = makeCoredNode<Transform> (&point2_trans);
    beginEditCP(point2_trans);
    point2_trans->getMatrix().setTranslate(5.0, 5.0, 20.0);
    endEditCP(point2_trans);

    beginEditCP(_point2_core);
    _point2_core->setAmbient(0.15, 0.15, 0.15, 1);
    _point2_core->setDiffuse(0.4, 0.4, 0.4, 1);
    _point2_core->setSpecular(0.0, 0.0, 0.0, 1);
    _point2_core->setBeacon(point2_beacon);
    _point2_core->setOn(true);
    endEditCP(_point2_core);

    beginEditCP(point1);
    point1->addChild(point2);
    endEditCP(point1);

    beginEditCP(point2);
    point2->addChild(scene);
    endEditCP(point2);

    // create scene
    // bottom
    NodePtr plane = makePlane(25.0, 25.0, 128, 128);

    ImagePtr plane_img = Image::create();

    beginEditCP(plane_img);
    plane_img->set(Image::OSG_RGBA_PF, imageWinWidth, imageWinHeight);
    endEditCP(plane_img);

    TextureChunkPtr plane_tex = TextureChunk::create();
    beginEditCP(plane_tex);
    plane_tex->setImage(plane_img);
    plane_tex->setMinFilter(GL_NEAREST);
    plane_tex->setMagFilter(GL_NEAREST);
    plane_tex->setTarget(GL_TEXTURE_2D); // RECTANGLE_ARB);
    endEditCP(plane_tex);

    SHLChunkPtr shl = SHLChunk::create();
    beginEditCP(shl);
    shl->setVertexProgram(_vp_program);
    shl->setFragmentProgram(_fp_program);
    shl->setUniformParameter("tex0", 0);
    endEditCP(shl);

    SimpleMaterialPtr plane_mat = SimpleMaterial::create();
    beginEditCP(plane_mat);
    plane_mat->setAmbient(Color3f(0.3, 0.3, 0.3));
    plane_mat->setDiffuse(Color3f(1.0, 1.0, 1.0));
    plane_mat->addChunk(plane_tex);

    //plane_mat->addChunk(shl);
    endEditCP(plane_mat);

    GeometryPtr plane_geo = GeometryPtr::dcast(plane->getCore());
    beginEditCP(plane_geo);
    plane_geo->setMaterial(plane_mat);
    beginEditCP(plane_geo);

    // box
    box_trans_node = makeCoredNode<Transform> (&_box_trans);
    beginEditCP(_box_trans);
    _box_trans->getMatrix().setTranslate(0.0, 0.0, 12.0);
    endEditCP(_box_trans);

    NodePtr box = makeBox(4.0, 4.0, 0.8, 10, 10, 10);
    beginEditCP(box_trans_node);
    box_trans_node->addChild(box);
    endEditCP(box_trans_node);

    PolygonChunkPtr pchunk = osg::PolygonChunk::create();
    beginEditCP(pchunk);
    pchunk->setCullFace(GL_BACK);
    endEditCP(pchunk);

    SimpleMaterialPtr box_mat = SimpleMaterial::create();
    beginEditCP(box_mat);
    box_mat->setAmbient(Color3f(0.0, 0.0, 0.0));
    box_mat->setDiffuse(Color3f(0.0, 0.0, 1.0));
    box_mat->addChunk(pchunk);
    endEditCP(box_mat);

    GeometryPtr box_geo = GeometryPtr::dcast(box->getCore());
    beginEditCP(box_geo);
    box_geo->setMaterial(box_mat);
    beginEditCP(box_geo);

    // cylinder1
    NodePtr cylinder1_trans_node = makeCoredNode<Transform>
        (&_cylinder1_trans);
    beginEditCP(_cylinder1_trans);
    _cylinder1_trans->getMatrix().setTranslate(0.0, 0.0, 5.0);
    endEditCP(_cylinder1_trans);

    NodePtr cylinder1 = OSG::makeCylinder(10.0, 0.4, 32, true, true, true);
    beginEditCP(cylinder1_trans_node);
    cylinder1_trans_node->addChild(cylinder1);
    endEditCP(cylinder1_trans_node);

    SimpleMaterialPtr cylinder1_mat = SimpleMaterial::create();
    beginEditCP(cylinder1_mat);
    cylinder1_mat->setAmbient(Color3f(0.0, 0.0, 0.0));
    cylinder1_mat->setDiffuse(Color3f(1.0, 0.0, 0.0));
    cylinder1_mat->addChunk(pchunk);
    endEditCP(cylinder1_mat);

    GeometryPtr cylinder1_geo = GeometryPtr::dcast(cylinder1->getCore());
    beginEditCP(cylinder1_geo);
    cylinder1_geo->setMaterial(cylinder1_mat);
    beginEditCP(cylinder1_geo);

    // cylinder2
    NodePtr cylinder2_trans_node = makeCoredNode<Transform>
        (&_cylinder2_trans);
    beginEditCP(_cylinder2_trans);
    _cylinder2_trans->getMatrix().setTranslate(0.0, 0.0, 8.0);
    endEditCP(_cylinder2_trans);

    NodePtr cylinder2 = OSG::makeCylinder(10.0, 0.4, 32, true, true, true);
    beginEditCP(cylinder2_trans_node);
    cylinder2_trans_node->addChild(cylinder2);
    endEditCP(cylinder2_trans_node);

    SimpleMaterialPtr cylinder2_mat = SimpleMaterial::create();
    beginEditCP(cylinder2_mat);
    cylinder2_mat->setAmbient(Color3f(0.0, 0.0, 0.0));
    cylinder2_mat->setDiffuse(Color3f(0.0, 1.0, 0.0));
    cylinder2_mat->addChunk(pchunk);
    endEditCP(cylinder2_mat);

    GeometryPtr cylinder2_geo = GeometryPtr::dcast(cylinder2->getCore());
    beginEditCP(cylinder2_geo);
    cylinder2_geo->setMaterial(cylinder2_mat);
    beginEditCP(cylinder2_geo);

    // scene
    beginEditCP(scene);
    scene->addChild(plane);
    scene->addChild(box_trans_node);
    scene->addChild(cylinder1_trans_node);
    scene->addChild(cylinder2_trans_node);
    endEditCP(scene);

    vp = Viewport::create();

    GradientBackgroundPtr gbg = GradientBackground::create();
    SolidBackgroundPtr sbg = SolidBackground::create();

    ImagePtr img1 = ImageFileHandler::the().read("froekk.jpg");
    if (!img1) {
        std::cout << "meep!\n";
    }

    TextureChunkPtr tcPtr = TextureChunk::create();
    beginEditCP(tcPtr);
    tcPtr->setImage(img1);
    endEditCP(tcPtr);

    TextureBackgroundPtr bkg = TextureBackground::create();
    beginEditCP(bkg);
    bkg->setTexture(tcPtr);
    endEditCP(bkg);

    beginEditCP(sbg);
    sbg->setColor(Color3f(0.2, 0.4, 0.6));
    endEditCP(sbg);

    beginEditCP(gbg);
    gbg->addLine(Color3f(0.7, 0.7, 0.8), 0);
    gbg->addLine(Color3f(0.1, 0.1, 0.3), 1);
    endEditCP(gbg);

    beginEditCP(rootNode);
    rootNode->addChild(point1);
    rootNode->addChild(point1_beacon);
    rootNode->addChild(point2_beacon);
    endEditCP(rootNode);

    fbo_vp = FBOViewport::create();
    if (!fbo_vp) {
        std::clog << "error creating FBOViewport.\n";
        exit(1);
    }

    // the Camera for the map
    cam = PerspectiveCamera::create();
    beginEditCP(cam);
    cam->setFov(osgdegree2rad(90));
    cam->setAspect(1);
    cam->setNear(0.01);
    cam->setFar(1000);
    cam->setBeacon(box_trans_node);
    endEditCP(cam);

    beginEditCP(fbo_vp);
    fbo_vp->setBackground(bkg);
    fbo_vp->setRoot(rootNode);
    fbo_vp->setCamera(cam);
    fbo_vp->setSize(0, 0, imageWinWidth-1, imageWinHeight-1);
    fbo_vp->setStorageWidth(imageWinWidth);
    fbo_vp->setStorageHeight(imageWinHeight);
    fbo_vp->getTextures().push_back(plane_tex);
    fbo_vp->getExcludeNodes().push_back(plane);
#ifdef USE_FBO
    fbo_vp->setFboOn(true);
#else
    fbo_vp->setFboOn(false);
#endif
    endEditCP(fbo_vp);

    // normal viewport
    beginEditCP(vp);
    vp->setBackground(gbg);
    vp->setRoot(rootNode);
    vp->setSize(0, 0, 1, 1);
    endEditCP(vp);

    beginEditCP(gwin);  //Window
    gwin->setId(winid);
    #ifndef USE_FBO
    gwin->addPort(fbo_vp);
    #endif
    gwin->addPort(vp);
    #ifdef USE_FBO
    gwin->addPort(fbo_vp);
    #endif
    gwin->init();
    endEditCP(gwin);

    Vec3f min, max;
    rootNode->updateVolume();
    rootNode->getVolume().getBounds(min, max);

    // create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;

    mgr->setWindow(gwin);
    mgr->setRoot(rootNode);

    //Viewport
    beginEditCP(vp);
    vp->setCamera(mgr->getCamera());
    endEditCP(vp);

    mgr->turnHeadlightOff();

    mgr->showAll();

    // GLUT main loop
    glutMainLoop();

    return 0;
}

//
// GLUT callback functions
//
//do Animation-Calculations here
void Animate()
{
    static Real64 t0 = OSG::getSystemTime();

    Real64 t = OSG::getSystemTime() - t0;

    Real32 rotb = t * 10.0;
    if (rotb > 360.0) {
        rotb -= 360.0;
    }

    Real32 rotc1 = t * 20.0;
    if (rotc1 > 360.0) {
        rotc1 -= 360.0;
    }

    Real32 rotc2 = t * 40.0;
    if (rotc2 > 360.0) {
        rotc2 -= 360.0;
    }

    Quaternion q;
    beginEditCP(_box_trans);
    q.setValueAsAxisDeg(0.2, 0, 1, rotb);
    _box_trans->getMatrix().setRotate(q);
    endEditCP(_box_trans);

    beginEditCP(_cylinder1_trans);
    q.setValueAsAxisDeg(1, 0, 1, rotc1);
    _cylinder1_trans->getMatrix().setRotate(q);
    endEditCP(_cylinder1_trans);

    beginEditCP(_cylinder2_trans);
    q.setValueAsAxisDeg(0, 1, 1, rotc2);
    _cylinder2_trans->getMatrix().setRotate(q);
    endEditCP(_cylinder2_trans);

    beginEditCP(box_trans_node);
    endEditCP(box_trans_node);
    beginEditCP(cam);
    endEditCP(cam);

    mgr->redraw();
}

// redraw the window
void display(void)
{
    mgr->redraw();
}

// react to size changes
void reshape(int w, int h)
{
    mgr->resize(w, h);
    glutPostRedisplay();
}

// react to mouse button presses
void mouse(int button, int state, int x, int y)
{
    if (state) {
        mgr->mouseButtonRelease(button, x, y);
    } else {
        mgr->mouseButtonPress(button, x, y);
    }

    glutPostRedisplay();
}

// react to mouse motions with pressed buttons
void motion(int x, int y)
{
    mgr->mouseMove(x, y);
    glutPostRedisplay();
}

// react to keys
void keyboard(unsigned char k, int x, int y)
{
    switch (k) {
    case 27:
        {
            OSG::osgExit();
            exit(0);
        }

        break;
    case '1':
        {
            beginEditCP(fbo_vp);
            fbo_vp->setCamera(mgr->getCamera());
            endEditCP(fbo_vp);
            break;
        }

    case '2':
        {
            beginEditCP(fbo_vp);
            fbo_vp->setCamera(cam);
            endEditCP(fbo_vp);
            break;
        }

    case '3':
        {
            beginEditCP(fbo_vp);
                fbo_vp->setFboOn(!fbo_vp->getFboOn());
            endEditCP(fbo_vp);
        }

    case 'x':
        {
            SceneFileHandler::the().write(rootNode, "test.osb.gz", true);
        }
    }

    glutPostRedisplay();
}

// setup the GLUT library which handles the windows for us
int setupGLUT(int* argc, char* argv[])
{
    glutInit(argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);

    //Set WindowSize here
    glutInitWindowSize(512, 512);
    glutInitWindowPosition(0, 0);

    int winid = glutCreateWindow("Framebuffer-Objects Mirror-Scene");

    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutIdleFunc(Animate);

    return winid;
}
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to