Esempio n. 1
0
int main(int argc, char **argv)
{
    osgInit(argc,argv);

    // GLUT init
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    
    int id=glutCreateWindow("OpenSG");
    
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    GLUTWindowPtr gwin=GLUTWindow::create();
    gwin->setId(id);
    gwin->init();

    // create the scene
//    NodePtr scene = makeTorus(.5, 2, 16, 16);
    NodePtr scene = makeBox(1,1,1, 1,1,1);

    // create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;

    // tell the manager what to manage
    mgr->setWindow(gwin );
    mgr->setRoot  (scene);
    
    // Create the textured material
    UChar8 imgdata[] =
      {  255,0,0,128,  0,255,0,128,  0,0,255,255,  255,255,255,255 };
   
    ImagePtr img = Image::create();
    beginEditCP(img);
    if(argc > 1)
        img->read(argv[1]);
    else
        img->set( Image::OSG_RGBA_PF, 2, 2, 1, 1, 1, 0, imgdata );
    endEditCP(img);
    addRefCP(img);
   
    SimpleTexturedMaterialPtr mat = SimpleTexturedMaterial::create();
    beginEditCP(mat);
    mat->setLit(false);
    mat->setDiffuse(Color3f(0,1,0));
    mat->setImage(img);
    mat->setMinFilter(GL_NEAREST);
    mat->setMagFilter(GL_NEAREST);
    mat->setEnvMode(GL_REPLACE);
    endEditCP(mat);
    
    // Add the polygon foregrounds
    
    Pnt2f pos[][4] = { 
        { Vec2f(.4,.4), Vec2f(.6,.4), Vec2f(.6,.6), Vec2f(.4,.6) },
        { Vec2f(0,0), Vec2f(1,0), Vec2f(1,.1), Vec2f(0,.1) },
        { Vec2f(1,-100), Vec2f(-2,-100), Vec2f(-2,-2), Vec2f(1,-2) },
        { Vec2f(-1000,-1000), Vec2f(0,0), Vec2f(0,0), Vec2f(0,0) }
    };
    bool normx[] = { true, true, false };
    bool normy[] = { true, true, false };
    
    for(UInt16 ind = 0; pos[ind][0][0] != -1000; ++ind)
    {   
        PolygonForegroundPtr pg = PolygonForeground::create();

        beginEditCP(pg);
        pg->setMaterial(mat);
        pg->editMFTexCoords()->push_back(Vec3f( 0.,0.,0.));
        pg->editMFPositions()->push_back(pos[ind][0]);
        pg->editMFTexCoords()->push_back(Vec3f( 1.,0.,0.));
        pg->editMFPositions()->push_back(pos[ind][1]);
        pg->editMFTexCoords()->push_back(Vec3f( 1.,1.,0.));
        pg->editMFPositions()->push_back(pos[ind][2]);
        pg->editMFTexCoords()->push_back(Vec3f( 0.,1.,0.));
        pg->editMFPositions()->push_back(pos[ind][3]);
 
        pg->setNormalizedX(normx[ind]);
        pg->setNormalizedY(normy[ind]);
        endEditCP(pg);

        // take the viewport
        ViewportPtr vp = gwin->getPort(0);

        beginEditCP(vp);
        vp->editMFForegrounds()->push_back(pg);    
        endEditCP  (vp);
    }
     
    // Create the edgeblend foreground
    const int blendpixel = 50;
    
    UChar8 ebimgdata[] =
      {  0,0,0,0,  0,0,0,255 };
   
    ImagePtr ebimg = Image::create();
    beginEditCP(ebimg);
    ebimg->set( Image::OSG_RGBA_PF, 2, 1, 1, 1, 1, 0, ebimgdata );
    endEditCP(ebimg);
    addRefCP(ebimg);
   
    TextureChunkPtr tc = TextureChunk::create();
    beginEditCP(tc);
    tc->setImage(ebimg);
    tc->setMinFilter(GL_NEAREST);
    tc->setMagFilter(GL_LINEAR);
    tc->setEnvMode(GL_REPLACE);
    tc->setWrapS(GL_CLAMP);
    endEditCP(tc);
    
    BlendChunkPtr bl = BlendChunk::create();
    beginEditCP(bl);
    bl->setSrcFactor(GL_SRC_ALPHA);
    bl->setDestFactor(GL_ONE_MINUS_SRC_ALPHA);
    endEditCP(bl);
    
    ChunkMaterialPtr ebmat = ChunkMaterial::create();
    beginEditCP(ebmat);
    ebmat->addChunk(tc);
    ebmat->addChunk(bl);
    endEditCP(ebmat);

    PolygonForegroundPtr ebpg = PolygonForeground::create();

    beginEditCP(ebpg);
    ebpg->setMaterial(ebmat);
    ebpg->editMFTexCoords()->push_back(Vec3f( 0.25,0.,0.));
    ebpg->editMFTexCoords()->push_back(Vec3f( 0.75,0.,0.));
    ebpg->editMFTexCoords()->push_back(Vec3f( 0.75,1.,0.));
    ebpg->editMFTexCoords()->push_back(Vec3f( 0.25,1.,0.));

    ebpg->editMFPositions()->push_back(Pnt2f(-blendpixel-1, 0));
    ebpg->editMFPositions()->push_back(Pnt2f(           -1, 0));
    ebpg->editMFPositions()->push_back(Pnt2f(           -1, 1));
    ebpg->editMFPositions()->push_back(Pnt2f(-blendpixel-1, 1));

    ebpg->setNormalizedX(false);
    ebpg->setNormalizedY(true);
    endEditCP(ebpg);

    // take the viewport
    ViewportPtr vp = gwin->getPort(0);

    beginEditCP(vp);
    vp->editMFForegrounds()->push_back(ebpg);    
    endEditCP  (vp);

// show the whole scene
    mgr->showAll();
    
    // GLUT main loop
    glutMainLoop();

    return 0;
}
Esempio n. 2
0
int main (int argc, char **argv)
{
    osgInit(argc,argv);

    // GLUT init

    glutInit(&argc, argv);
    glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    int winid = glutCreateWindow("OpenSG");
    glutKeyboardFunc(key);
    glutVisibilityFunc(vis);
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutIdleFunc(display);

    // glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );

    glEnable( GL_DEPTH_TEST );
    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );

    // OSG

    SceneFileHandler::the().print();

    // create the graph

    // beacon for camera and light
    NodePtr b1n = Node::create();
    GroupPtr b1 = Group::create();
    beginEditCP(b1n);
    b1n->setCore( b1 );
    endEditCP(b1n);

    // transformation
    NodePtr t1n = Node::create();
    TransformPtr t1 = Transform::create();
    beginEditCP(t1n);
    t1n->setCore( t1 );
    t1n->addChild( b1n );
    endEditCP(t1n);

    cam_trans = t1;

    // light

    NodePtr dlight = Node::create();
    DirectionalLightPtr dl = DirectionalLight::create();

    beginEditCP(dlight);
    dlight->setCore( dl );
    endEditCP(dlight);

    beginEditCP(dl);
    dl->setAmbient( .3, .3, .3, 1 );
    dl->setDiffuse( 1, 1, 1, 1 );
    dl->setDirection(0,0,1);
    dl->setBeacon( b1n);
    endEditCP(dl);

    // root
    root = Node::create();
    GroupPtr gr1 = Group::create();
    beginEditCP(root);
    root->setCore( gr1 );
    root->addChild( t1n );
    root->addChild( dlight );
    endEditCP(root);

    // Load the file

    NodePtr file = NullFC;

    if ( argc > 1 )
        file = SceneFileHandler::the().read(argv[1]);

    if ( file == NullFC )
    {
        std::cerr << "Couldn't load file, ignoring" << std::endl;
        file = makeTorus( .5, 2, 16, 16 );
    }

    file->updateVolume();

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

    std::cout << "Volume: from " << min << " to " << max << std::endl;

    beginEditCP(dlight);
    dlight->addChild( file );
    endEditCP(dlight);

    std::cerr << "Tree: " << std::endl;
//  root->dump();

    // Camera

    cam = PerspectiveCamera::create();
    cam->setBeacon( b1n );
    cam->setFov( deg2rad( 90 ) );
    cam->setNear( 0.1 );
    cam->setFar( 10000 );

    // Background
    SolidBackgroundPtr bkgnd = SolidBackground::create();

    // Foreground
    GrabForegroundPtr fgnd = GrabForeground::create();

    image = Image::create();
    beginEditCP(image);
    image->set(Image::OSG_RGB_PF, 1);
    endEditCP(image);

    fgnd->setImage(image);

    // Viewport

    vp = Viewport::create();
    vp->setCamera( cam );
    vp->setBackground( bkgnd );
    vp->editMFForegrounds()->push_back( fgnd );
    vp->setRoot( root );
    vp->setSize( 0,0, 1,1 );

    // Window
    std::cout << "GLUT winid: " << winid << std::endl;

    GLUTWindowPtr gwin;

    GLint glvp[4];
    glGetIntegerv( GL_VIEWPORT, glvp );

    gwin = GLUTWindow::create();
    gwin->setId(winid);
    gwin->setSize( glvp[2], glvp[3] );

    win = gwin;

    win->addPort( vp );

    win->init();

    // Action

    ract = DrawAction::create();

    // tball

    Vec3f pos( 0, 0, max[2] + ( max[2] - min[2] ) * 1.5 );

    tball.setMode( Trackball::OSGObject );
    tball.setStartPosition( pos, true );
    tball.setSum( true );
    tball.setTranslationMode( Trackball::OSGFree );

    // run...

    glutMainLoop();

    return 0;
}
Esempio n. 3
0
int main(int argc, char **argv)
{
    osgInit(argc,argv);

    // GLUT init
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    
    int id=glutCreateWindow("OpenSG");
    
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    GLUTWindowPtr gwin=GLUTWindow::create();
    gwin->setId(id);
    gwin->init();

    // create the scene
//    NodePtr scene = makeTorus(.5, 2, 16, 16);
    NodePtr scene = Node::create();
    beginEditCP(scene);
    scene->setCore(Group::create());
    endEditCP(scene);

    // create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;

    // tell the manager what to manage
    mgr->setWindow(gwin );
    mgr->setRoot  (scene);
    
    DisplayFilterForegroundPtr fg = DisplayFilterForeground::create();

    beginEditCP(fg);
    // add filter
    endEditCP(fg);

    // take the viewport
    ViewportPtr vp = gwin->getPort(0);

    beginEditCP(vp);
    vp->editMFForegrounds()->push_back(fg);    
    endEditCP  (vp);

    colorFilterPtr = ColorDisplayFilter::create();
    distortionFilterPtr = DistortionDisplayFilter::create();
    resolutionFilterPtr = ResolutionDisplayFilter::create();

    beginEditCP(colorFilterPtr);
    beginEditCP(resolutionFilterPtr);
    beginEditCP(distortionFilterPtr);
    beginEditCP(fg);
    colorFilterPtr->setMatrix(osg::Matrix(0,0,1,0,
                                          1,0,0,0,
                                          0,1,0,0,
                                          0,0,0,1));
    resolutionFilterPtr->setDownScale(0.25);

    distortionFilterPtr->setColumns(2);
    distortionFilterPtr->setRows(2);
    distortionFilterPtr->editMFPositions()->push_back(Vec2f(0,.5));
    distortionFilterPtr->editMFPositions()->push_back(Vec2f(.5,0));
    distortionFilterPtr->editMFPositions()->push_back(Vec2f(.5,1));
    distortionFilterPtr->editMFPositions()->push_back(Vec2f(1,.5));
    
    fg->editMFFilter()->push_back(colorFilterPtr);
    fg->editMFFilter()->push_back(resolutionFilterPtr);
    fg->editMFFilter()->push_back(distortionFilterPtr);

    endEditCP(distortionFilterPtr);
    endEditCP(colorFilterPtr);
    endEditCP(resolutionFilterPtr);
    endEditCP(fg);

    for(UInt32 a=1 ; a<argc ;a++)
    {
        NodePtr file = SceneFileHandler::the().read(argv[a],0);
        if(file != NullFC)
            scene->addChild(file);
        else
            std::cerr << "Couldn't load file, ignoring " << argv[a] << std::endl;
    }
	if ( scene->getNChildren() == 0 )
	{
        scene->addChild(makeTorus( .5, 2, 16, 16 ));
//        scene->addChild(makeBox(.6,.6,.6,5,5,5));
    }

    // show the whole scene
    mgr->showAll();
    
    // GLUT main loop
    glutMainLoop();

    return 0;
}