예제 #1
0
int main(int argc, char **argv)
{
    osgInit(argc,argv);

    // GLUT init
    glutInit(&argc, argv);
    
    bool stereobuffer = false;
    bool amberblue = false;
    if(argc >= 2 && !strcmp(argv[1],"-s"))
    {
        stereobuffer = true;
        --argc, ++argv;
    }
    if(argc >= 2 && !strcmp(argv[1],"-a"))
    {
        amberblue = true;
        --argc, ++argv;
    }
    
    
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE |
                        (stereobuffer?GLUT_STEREO:0) );
    
    glutCreateWindow("OpenSG");
    
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    PassiveWindowPtr pwin=PassiveWindow::create();
    pwin->init();

    // create the scene
    NodePtr scene;
    
    if(argc > 1)
    {
        scene = SceneFileHandler::the().read(argv[1]);
    }
    else
    {
        scene = makeBox(2,2,2, 1,1,1);
    }

    // create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;

    // create the window and initial camera/viewport
    mgr->setWindow(pwin );
    // tell the manager what to manage
    mgr->setRoot  (scene);
    
    // now create the second vp for stereo
    ViewportPtr vp = pwin->getPort(0);
    
    PerspectiveCameraPtr cam = PerspectiveCameraPtr::dcast(vp->getCamera());
    beginEditCP(cam);
    cam->setFov(deg2rad(90));
    cam->setAspect(1);
    endEditCP  (cam);

    Navigator *nav = mgr->getNavigator();
    nav->setAt(Pnt3f(0,0,0));
    nav->setDistance(1.5);
    
    mgr->showAll();
    
    // create the decorators and the second viewport
    ViewportPtr vpleft,vpright;
       
    decoleft = ShearedStereoCameraDecorator::create();
    decoright = ShearedStereoCameraDecorator::create();
    
    beginEditCP(decoleft);
    decoleft->setEyeSeparation(ed);
    decoleft->setZeroParallaxDistance(zpp);
    decoleft->setLeftEye(true);  
    decoleft->setDecoratee(cam);  
    endEditCP  (decoleft);
    
    beginEditCP(decoright);
    decoright->setEyeSeparation(ed);
    decoright->setZeroParallaxDistance(zpp);
    decoright->setLeftEye(false);  
    decoright->setDecoratee(cam);  
    endEditCP  (decoright);

    if(amberblue)
    {
        ColorBufferViewportPtr svpleft = ColorBufferViewport::create();
        ColorBufferViewportPtr svpright = ColorBufferViewport::create();
 
        beginEditCP(svpleft);
        svpleft->setLeft(0);
        svpleft->setRight(1);
        svpleft->setBottom(0);
        svpleft->setTop(1);
        svpleft->setCamera(decoleft);
        svpleft->setBackground(vp->getBackground());
        svpleft->setRoot(vp->getRoot());
        svpleft->setRed(GL_TRUE);
        svpleft->setGreen(GL_TRUE);
        svpleft->setBlue(GL_FALSE);
        svpleft->setAlpha(GL_TRUE);
        endEditCP  (svpleft);
 
        beginEditCP(svpright);
        svpright->setLeft(0);
        svpright->setRight(1);
        svpright->setBottom(0);
        svpright->setTop(1);
        svpright->setCamera(decoright);
        svpright->setBackground(vp->getBackground());
        svpright->setRoot(vp->getRoot());
        svpright->setRed(GL_FALSE);
        svpright->setGreen(GL_FALSE);
        svpright->setBlue(GL_TRUE);
        svpright->setAlpha(GL_FALSE);
        endEditCP  (svpright);
        
        vpleft = svpleft;
        vpright = svpright;
    }
    else if(stereobuffer)
    {
        StereoBufferViewportPtr svpleft = StereoBufferViewport::create();
        StereoBufferViewportPtr svpright = StereoBufferViewport::create();
 
        beginEditCP(svpleft);
        svpleft->setLeft(0);
        svpleft->setRight(1);
        svpleft->setBottom(0);
        svpleft->setTop(1);
        svpleft->setCamera(decoleft);
        svpleft->setBackground(vp->getBackground());
        svpleft->setRoot(vp->getRoot());
        svpleft->setLeftBuffer(true);
        svpleft->setRightBuffer(false);
        endEditCP  (svpleft);
 
        beginEditCP(svpright);
        svpright->setLeft(0);
        svpright->setRight(1);
        svpright->setBottom(0);
        svpright->setTop(1);
        svpright->setCamera(decoright);
        svpright->setBackground(vp->getBackground());
        svpright->setRoot(vp->getRoot());
        svpright->setLeftBuffer(false);
        svpright->setRightBuffer(true);
        endEditCP  (svpright);
        
        vpleft = svpleft;
        vpright = svpright;
    }
    else
    {
        vpleft = vp;
        vpright = Viewport::create();

        beginEditCP(vpleft);
        vpleft->setLeft(0);
        vpleft->setRight(1);
        vpleft->setBottom(0);
        vpleft->setTop(0.5);
        endEditCP  (vpleft);

        beginEditCP(vpright);
        vpright->setLeft(0);
        vpright->setRight(1);
        vpright->setBottom(0.5);
        vpright->setTop(1);
        vpright->setBackground(vp->getBackground());
        vpright->setRoot(vp->getRoot());
        endEditCP  (vpright);
    }
    
    beginEditCP(vpleft);
    vpleft->setCamera(decoleft);
    endEditCP  (vpleft);
    
    beginEditCP(vpright);
    vpright->setCamera(decoright);
    endEditCP  (vpright);
 
    beginEditCP(pwin); 
    pwin->subPort(vp);  
    pwin->addPort(vpleft);  
    pwin->addPort(vpright);  
    endEditCP  (pwin); 

    // make it notice the changes
    mgr->setWindow(pwin );
    
    // show the whole scene
    mgr->showAll();
    
    // OpenGL info
    GLint val;
    
#define cv(v)\
    glGetIntegerv((v),&val);\
    printf("%s: %d\n", #v, val)
    
    cv(GL_RED_BITS);
    cv(GL_GREEN_BITS);
    cv(GL_BLUE_BITS);
    cv(GL_DEPTH_BITS);
    
    glEnable(GL_DEPTH_TEST);
    
    // GLUT main loop
    glutMainLoop();

    return 0;
}
void MultiDisplayWindow::serverRender(WindowPtr         serverWindow,
                                      UInt32            id,
                                      DrawActionBase *action )
{
    TileCameraDecoratorPtr deco;
    ViewportPtr serverPort;
    ViewportPtr clientPort;
    StereoBufferViewportPtr clientStereoPort;
    UInt32 sv,cv;
    Int32 l,r,t,b;
    Int32 cleft,cright,ctop,cbottom;

    // sync, otherwise viewports will be out of date

    if(!getHServers())
    {
        setHServers(getServers().size());
    }
    if(!getVServers())
    {
        setVServers(1);
    }

    UInt32 row   =id/getHServers();
    UInt32 column=id%getHServers();

    // calculate width and height from local width and height
    UInt32 width  = serverWindow->getWidth() ;
    UInt32 height = serverWindow->getHeight();

    if(getWidth()==0)
    {
        setWidth( width*getHServers() );
    }
    if(getHeight()==0)
    {
        setHeight( height*getVServers() );
    }

    Int32 left   = column * width  - column * getXOverlap();
    Int32 bottom = row    * height - row    * getYOverlap();
    Int32 right  = left   + width  - 1;
    Int32 top    = bottom + height - 1;
    Real64 scaleCWidth  =
        ((width - getXOverlap()) * (getHServers() - 1) + width) /
        (float)getWidth();
    Real64 scaleCHeight =
        ((height - getYOverlap())* (getVServers() - 1) + height)/
        (float)getHeight();

    bool   isVirtualPort = false;

    // duplicate viewports
    for(cv = 0, sv = 0; cv < getPort().size(); ++cv)
    {
        clientPort = getPort()[cv];

#if 0
        isVirtualPort = clientPort->getType().isDerivedFrom(FBOViewport::getClassType());

        if(isVirtualPort)
        {
            // TODO -- seems wrong to render this on all servers, though rendering
            // then transmitting the texture doesn't seem like a good idea either.
            if(serverWindow->getPort().size() <= sv)
            {
                serverPort = ViewportPtr::dcast(clientPort->shallowCopy());
                beginEditCP(serverWindow);
                serverWindow->addPort(serverPort);
                endEditCP(serverWindow);
            }
            else
            {
                serverPort = serverWindow->getPort()[sv];
                if(serverWindow->getPort()[sv]->getType() !=
                        clientPort->getType())
                {
                    // there is a viewport with the wrong type
                    subRefCP(serverWindow->getPort()[sv]);
                    serverPort = ViewportPtr::dcast(clientPort->shallowCopy());
                    beginEditCP(serverWindow);
                    {
                        serverWindow->getPort()[sv] = serverPort;
                    }
                    endEditCP(serverWindow);
                }
            }
            // update changed viewport fields
            updateViewport(serverPort,clientPort);
        }
        else
#endif
        {
            clientStereoPort =
                dynamic_cast<StereoBufferViewportPtr>(clientPort);

            cleft   = (Int32)(clientPort->getPixelLeft()      * scaleCWidth)   ;
            cbottom = (Int32)(clientPort->getPixelBottom()    * scaleCHeight)  ;
            cright  = (Int32)((clientPort->getPixelRight()+1) * scaleCWidth) -1;
            ctop    = (Int32)((clientPort->getPixelTop()+1)   * scaleCHeight)-1;

            if(cright  < left   ||
                    cleft   > right  ||
                    ctop    < bottom ||
                    cbottom > top      )
            {
                // invisible on this server screen
                continue;
            }

            // calculate overlapping viewport
            l = osgMax(cleft  ,left  ) - left;
            b = osgMax(cbottom,bottom) - bottom;
            r = osgMin(cright ,right ) - left;
            t = osgMin(ctop   ,top   ) - bottom;

            if(serverWindow->getPort().size() <= sv)
            {
                serverPort =
                    dynamic_cast<ViewportPtr>(clientPort->shallowCopy());

                deco = TileCameraDecorator::create();

                serverWindow->addPort(serverPort);

                serverPort->setCamera(deco);
            }
            else
            {
                serverPort = serverWindow->getPort()[sv];

                deco = dynamic_cast<TileCameraDecoratorPtr>(
                           serverPort->getCamera());

                if(serverWindow->getPort()[sv]->getType() !=
                        clientPort->getType())
                {
                    // there is a viewport with the wrong type
                    serverPort =
                        dynamic_cast<ViewportPtr>(clientPort->shallowCopy());

                    serverWindow->replacePort(sv,
                                              serverPort);//[sv] = serverPort;
                    serverPort->setCamera(deco);
                }
                else
                {
                    deco = dynamic_cast<TileCameraDecoratorPtr>(
                               serverPort->getCamera());
                }
            }

            // update changed viewport fields
            updateViewport(serverPort,clientPort);

            // set viewport size
            serverPort->setSize(Real32(l),Real32(b),Real32(r),Real32(t));

            // use pixel even if pixel = 1
            if(serverPort->getLeft() == 1.0)
                serverPort->setLeft(1.0001);

            if(serverPort->getRight() == 1.0)
                serverPort->setRight(1.0001);

            if(serverPort->getTop() == 1.0)
                serverPort->setTop(1.0001);

            if(serverPort->getBottom() == 1.0)
                serverPort->setBottom(1.0001);

            // calculate tile parameters
            deco->setFullWidth ( cright-cleft );
            deco->setFullHeight( ctop-cbottom );
            deco->setSize( ( l+left-cleft     ) / (float)( cright-cleft ),
                           ( b+bottom-cbottom ) / (float)( ctop-cbottom ),
                           ( r+left-cleft     ) / (float)( cright-cleft ),
                           ( t+bottom-cbottom ) / (float)( ctop-cbottom ) );
            deco->setDecoratee( clientPort->getCamera() );
        }
        sv++;
    }

    // remove unused ports
    while(serverWindow->getPort().size()>sv)
    {
        serverWindow->subPort(sv);
    }

    Inherited::serverRender(serverWindow,id,action);
}
예제 #3
0
// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv)
{
    // OSG init
    glutInit(&argc, argv);
    osgInit(argc, argv);

    std::string fontfile("testfont.ttf");
    std::string testtext("Test Text");
    UInt32 drawmode;
    
    if(argc > 1)
        testtext = argv[1];
    if(argc > 2)
        fontfile = argv[2];
    if(argc < 4 || sscanf(argv[3], "%d", &drawmode) != 1 )
        drawmode = FTGLFont::Outline;
        
    // GLUT 
    int winid = setupGLUT(&argc, argv);
    GLUTWindowPtr gwin= GLUTWindow::create();
    gwin->setId(winid);
    gwin->init();
   
    
    // Create the Cubes node

    NodePtr scene = makeCoredNode<Group>();

    beginEditCP(scene);
    
    scene->addChild( makeBox(200,200,200, 1,1,1) );
//    scene->addChild( SceneFileHandler::the().read("tie.wrl") );
    endEditCP(scene);


    // Create the text
    
    FTGLFontPtr font = FTGLFont::create();
    
    beginEditCP(font);
    font->setName(fontfile);
    font->setDrawType(drawmode);
    font->setSize(40);
    font->setRes(72);
    font->setDepth(20);
    endEditCP(font);

    FTGLTextPtr text;
    NodePtr tnode = makeCoredNode<FTGLText>(&text);
    
    beginEditCP(text);
    text->setFont(font);
    text->setText(testtext);
    text->setPosition(Pnt3f(0,300,0));
    text->setMaterial(getDefaultMaterial());      
    endEditCP(text);
    

    beginEditCP(scene);
    scene->addChild(tnode);
    endEditCP(scene);
    
    
    // create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;

    // tell the manager what to manage
    mgr->setWindow(gwin );
    mgr->setRoot  (scene);

    // show the whole scene
    mgr->showAll();
        
    // copy to second window 
    int winid2 = setupGLUT(&argc, argv);
    gwin2= GLUTWindow::create();
    gwin2->setId(winid2);
    gwin2->init();
    
    ViewportPtr ovp = gwin->getPort()[0];
    
    ViewportPtr vp = Viewport::create();
    
    beginEditCP(vp);
    vp->setLeft(0);
    vp->setRight(400);
    vp->setBottom(0);
    vp->setTop(400);
    vp->setCamera(ovp->getCamera());
    vp->setRoot(ovp->getRoot());
    vp->setBackground(ovp->getBackground());
    vp->setParent(gwin2);
    endEditCP(vp);
    
    beginEditCP(gwin2);
    gwin2->getPort().push_back(vp);
    endEditCP(gwin2);
    
    // GLUT main loop
    glutMainLoop();

    return 0;
}
예제 #4
0
    virtual void redraw( void ) {
        static bool first=true;
        static RenderNode rn;

        int i;
        double tbalance;
        GeoLoadManager::ResultT region;
        if (_internalRoot == NullFC)
        {
            initialize();
            showAll();
        }
        _cart->getSFMatrix()->setValue(_navigator.getMatrix());    
        updateHighlight();
        _win->activate();
        _win->frameInit();
        if(first)
        {
            loadManager=new GeoLoadManager(useFaceDistribution);
            first=false;
            rn.determinePerformance(_win);
        }
        if(_win->getPort().size() < (serverCount+1))
        {
            ViewportPtr vp,ovp=_win->getPort(0);
            addRefCP(ovp);
            addRefCP(ovp);
            TileCameraDecoratorPtr deco;
            for(int i=_win->getPort().size()-1;i<serverCount;i++)
            {
                cout << "Add new" << endl;
                loadManager->addRenderNode(rn,i);
                if(simulateRendering)
                {
                    beginEditCP(_win);
                    deco=TileCameraDecorator::create();
                    beginEditCP(deco);
                    deco->setFullWidth ( _win->getWidth() );
                    deco->setFullHeight( _win->getHeight() );
                    deco->setDecoratee( ovp->getCamera() );
                    vp=Viewport::create();
                    beginEditCP(vp);
                    vp->setRoot      ( ovp->getRoot()       );

                    /*
                    SkyBackgroundPtr sky = SkyBackground::create();
                    beginEditCP(sky);
                    sky->setSphereRes(16);
                    
                    sky->getMFSkyColor()->addValue(Color3f(0, 0, .2));
                    sky->getMFSkyAngle()->addValue(Pi / 2);
                    sky->getMFSkyColor()->addValue(Color3f(.6, .6, 1)); 
                    
                    sky->getMFGroundColor()->addValue(Color3f(0, .3, 1));
                    sky->getMFGroundAngle()->addValue(Pi / 2);
                    sky->getMFGroundColor()->addValue(Color3f(1, .3, 0));

                    endEditCP(sky);
                    vp->setBackground( sky );
                    */

                    vp->setBackground( ovp->getBackground() );
                    vp->getMFForegrounds()->setValues( ovp->getForegrounds() );
                    vp->setCamera(deco);
                    _win->addPort(vp);
                    endEditCP(_win);
                    endEditCP(vp);
                    endEditCP(deco);
                }
            }
        }
        tbalance = -getSystemTime();
        loadManager->update(_win->getPort()[0]->getRoot());
        loadManager->balance(_win->getPort()[0],false,region);
        tbalance += getSystemTime();
        if(simulateRendering)
        {
            ViewportPtr vp;
            TileCameraDecoratorPtr deco;
            for(i=0;i<region.size();i+=4)
            {
#if 1
                cout << "Region: " << i << " ";
                cout << region[i+0] << " ";
                cout << region[i+1] << " ";
                cout << region[i+2] << " ";
                cout << region[i+3] << endl;
                if(region[i+0] >= region[i+2]) {
                    cout << "!!!" << endl;
                    region[i+2]++;
                }
                if(region[i+1] >= region[i+3]) {
                    cout << "!!!" << endl;
                    region[i+3]++;
                }
#endif
                vp=_win->getPort()[i/4+1];
                deco=TileCameraDecoratorPtr::dcast(vp->getCamera());
                beginEditCP(deco);
                beginEditCP(vp);
                vp->setSize(region[i+0],
                            region[i+1],
                            region[i+2],
                            region[i+3]);
                deco->setSize(region[i+0]/(float)_win->getWidth(),
                              region[i+1]/(float)_win->getHeight(),
                              region[i+2]/(float)_win->getWidth(),
                              region[i+3]/(float)_win->getHeight());
                endEditCP(deco);
                endEditCP(vp);
            }
        }
        Time t,tmin,tmax;
        for(i=0;i<_win->getPort().size();++i)
        {
            t=-getSystemTime();
            _action->setWindow( _win.getCPtr() );
            _win->getPort(i)->render( _action );
            glFlush();
            t+=getSystemTime();
            if(i==0)
                continue;
            if(i==1)
            {
                tmin=tmax=t;
            }
            else
            {
                if(t<tmin) tmin=t;
                if(t>tmax) tmax=t;
            }
        }
        if(!cache)
            printf("speed %5d %10.6f %10.6f %10.6f\n",_win->getPort().size()-1,
                   tmin,
                   tmax,
                   tbalance);
        glPushAttrib(GL_ALL_ATTRIB_BITS);
        glDisable(GL_SCISSOR_TEST);
        glViewport(0,0,
                   _win->getWidth(),
                   _win->getHeight());
        if(viewVolume)
            loadManager->drawVolumes(_win);
        glPushMatrix();
        glLoadIdentity();
        glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glLoadIdentity();
        gluOrtho2D(0,_win->getWidth(),
                   0,_win->getHeight());
        glDisable(GL_DEPTH_TEST);
        glEnable(GL_COLOR_MATERIAL);
        for(i=0;i<region.size();i+=4)
        {
#if 0
            cout << "Region: ";
            cout << region[i+0] << " ";
            cout << region[i+1] << " ";
            cout << region[i+2] << " ";
            cout << region[i+3] << endl;
#endif
            glBegin(GL_LINE_LOOP);
            glColor3f(1, 1, 0);
            glVertex3f(region[i+0],region[i+1],0);
            glVertex3f(region[i+2],region[i+1],0);
            glVertex3f(region[i+2],region[i+3],0);
            glVertex3f(region[i+0],region[i+3],0);
            glEnd();
        }
        glDisable(GL_COLOR_MATERIAL);
        glEnable(GL_DEPTH_TEST);
        glPopMatrix();
        glMatrixMode(GL_MODELVIEW);
        glPopMatrix();
        glPopAttrib();

        if(doSave)
        {
            Int32 w,h;
            w=_win->getPort(0)->getPixelWidth();
            h=_win->getPort(0)->getPixelHeight();
            Image image(Image::OSG_RGB_PF,
                        w,h,1,
                        1,1,0.0,
                        NULL,true);
            ImageFileType *imgTransType=ImageFileHandler::the().getFileType("JPEG");
            char filename[256];
            if(imgTransType==NULL)
            {
                cerr << "Unknown image trans type" << endl;
                return;
            }
            sprintf(filename,"%s_%d.jpg",dumpImage,dumpImageNr++);
            // read buffer data into image
            glPixelStorei(GL_PACK_ALIGNMENT,1);
            glReadPixels(0,0,w,h,
                         GL_RGB,GL_UNSIGNED_BYTE,
                         image.getData());
            imgTransType->write(image,filename);
        }
        _win->swap();
        _win->frameExit();
    }