Example #1
0
// This function creates a cylinder similarly to the teapot
// and the torus above. This time the cylinder is trimmed with
// a circle defined by 9 square-based control points to show
// an example of defining a circle without using infinite control
// points (see [Piegl and Tiller], pp. 299, Ex7.2 (Figure 7.16)) as
// well as to show how to define a rational trimming curve.
NodePtr makeTrimmedCylinder()
{
    SimpleMaterialPtr cylmat =  SimpleMaterial::create();
    beginEditCP(cylmat);
    {
        cylmat->setDiffuse(  Color3f(0.0, 0.8, 0.7));
        cylmat->setAmbient(  Color3f(0.2, 0.2, 0.2));
        cylmat->setEmission( Color3f(0.02, 0.02, 0.02) );
        cylmat->setSpecular( Color3f(0.78, 0.78, 0.78) );
        cylmat->setShininess( 128 );
        cylmat->addChunk(g_fb_chunk);
    }
    endEditCP(cylmat);
    float knots4[4] = {0, 0, 1, 1};
    float knots_circle[8] = {0, 0, 0, 0.5, 0.5, 1, 1, 1};
    float knots_outertrim[7] = {0, 0, 1, 2, 3, 4, 4};
    float knots_trimcircle[12] = {0, 0, 0, 0.25, 0.25, 0.5,
                                  0.5, 0.75, 0.75, 1, 1, 1
                                 };
    float cylcps[10][4] = {
        { 1.00,     0, -.50, 1},
        { 1.00,     0,  .50, 1},
        {    0,  1.00,    0, 0},
        {    0,  1.00,    0, 0},
        {-1.00,     0, -.50, 1},
        {-1.00,     0,  .50, 1},
        {    0, -1.00,    0, 0},
        {    0, -1.00,    0, 0},
        { 1.00,     0, -.50, 1},
        { 1.00,     0,  .50, 1},
    };
    float outertrim_cps[5][2] = {
        {0, 0},
        {1, 0},
        {1, 1},
        {0, 1},
        {0, 0},
    };
    float trimcircle_cps[9][3] = {
        {0.541667, 0.5,      1},
        {0.383016, 0.235702, 0.707107},
        {0.5,      0.333333, 1},
        {0.324091, 0.235702, 0.707107},
        {0.458333, 0.5,      1},
        {0.324091, 0.471405, 0.707107},
        {0.5,      0.666667, 1},
        {0.383016, 0.471405, 0.707107},
        {0.541667, 0.5,      1},
    };
    NodePtr cylnode = makeSurface(10, cylcps, 2, 1, 8, knots_circle,
                                  4, knots4, 0.001, cylmat);
    SurfacePtr s = SurfacePtr::dcast(cylnode->getCore());
    // add outer trimming around the domain
    addTrimCurve(s, 5, outertrim_cps, 1, 7, knots_outertrim, true);
    // add inside circle trimming
    addTrimCurve(s, 9, trimcircle_cps, 2, 12, knots_trimcircle, true);
    return cylnode;

}
Example #2
0
    GeometryPtr createGeo(UInt32 mat)
    {
        GeoIndicesPtr ind = _ind[mat];
        
        FDEBUG(("CreateGeoRec::createGeo: Mat %d: %d inds (%d tris)\n",
                mat, ind->getSize(), ind->getSize()/3));
                
        endEditCP(ind);

        GeometryPtr geo = Geometry::create();

        GeoPLengthsUI32Ptr len = GeoPLengthsUI32::create();
        beginEditCP(len);
        len->push_back(ind->getSize());
        endEditCP(len);

        GeoPTypesUI8Ptr typ = GeoPTypesUI8::create();
        beginEditCP(typ);
        typ->push_back(GL_TRIANGLES);
        endEditCP(typ);

        beginEditCP(geo);
        if(random_color)
        {
            SimpleMaterialPtr mat = SimpleMaterial::create();
            beginEditCP(mat);
            Color3f c(  drand48() * .7f + .3f,
                        drand48() * .7f + .3f,
                        drand48() * .7f + .3f);
            mat->setDiffuse(c);
            mat->setSpecular(Color3f(1.f,1.f,1.f));
            mat->setShininess(10.f);                        
            endEditCP(mat);
            geo->setMaterial(mat);
        }
        else
        {
            geo->setMaterial(MaterialPool::getMaterial(mat));
        }
        geo->setPositions(_pos);
        geo->setIndices(ind);
        geo->setLengths(len);
        geo->setTypes(typ);
        
        geo->getIndexMapping().push_back(Geometry::MapPosition);
        endEditCP(geo);

        calcVertexNormals(geo);
        //calcFaceNormals(geo);
        createSingleIndex(geo);

        return geo;
    }
Example #3
0
// This function creates a torus from a single NURBS surface
// using only 25 control points (of which 12 are unique).
// Similarly to the teapot, this uses the half-circle
// constructed from 3 control points (of which the middle point
// is infinite) which is then mirrored to form a full circle.
// (See the NURBS book [Piegl and Tiller], pp. 296, Ex7.1 (Figure 7.15)
// for details on the half-circle.)
NodePtr makeTorus()
{
    SimpleMaterialPtr torusmat =  SimpleMaterial::create();
    beginEditCP(torusmat);
    {
        torusmat->setDiffuse(  Color3f(1.0, 0.0, 0.2));
        torusmat->setAmbient(  Color3f(0.2, 0.2, 0.2));
        torusmat->setEmission( Color3f(0.02, 0.02, 0.02) );
        torusmat->setSpecular( Color3f(0.78, 0.78, 0.78) );
        torusmat->setShininess( 128 );
        torusmat->addChunk(g_fb_chunk);
    }
    endEditCP(torusmat);
    float knots_circle[8] = {0, 0, 0, 0.5, 0.5, 1, 1, 1};
    float toruscps[25][4] = {
        { 1.00,     0,  .75, 1}, //
        {  .25,     0,    0, 0}, //
        { 1.00,     0, 1.25, 1}, //
        { -.25,     0,    0, 0}, //
        { 1.00,     0,  .75, 1},
        {    0,  1.00,    0, 0}, //
        {    0,   .25,    0, 0}, //
        {    0,  1.00,    0, 0},
        {    0,  -.25,    0, 0}, //
        {    0,  1.00,    0, 0},
        {-1.00,     0,  .75, 1}, //
        { -.25,     0,    0, 0}, //
        {-1.00,     0, 1.25, 1}, //
        {  .25,     0,    0, 0}, //
        {-1.00,     0,  .75, 1},
        {    0, -1.00,    0, 0}, //
        {    0,  -.25,    0, 0},
        {    0, -1.00,    0, 0},
        {    0,   .25,    0, 0},
        {    0, -1.00,    0, 0},
        { 1.00,     0,  .75, 1},
        {  .25,     0,    0, 0},
        { 1.00,     0, 1.25, 1},
        { -.25,     0,    0, 0},
        { 1.00,     0,  .75, 1},
    };
    NodePtr torus = makeSurface(25, toruscps, 2, 2, 8, knots_circle,
                                8, knots_circle, 0.005, torusmat);
    return torus;
}
Example #4
0
void CharacterModel::convertMaterials(std::string configfile)
{
    getMaterials().clear();
    UInt32 mcnt = 0;
    PathHandler ph;
    
    ph.setBaseFile(configfile.c_str());
       
    for(int mid = 0; mid < _coreModel->getCoreMaterialCount(); mid++)
    {
        CalCoreMaterial *coremat = _coreModel->getCoreMaterial(mid);
        SimpleMaterialPtr mat = SimpleMaterial::create();

        beginEditCP(mat);

        CalCoreMaterial::Color &calamb = coremat->getAmbientColor();
        CalCoreMaterial::Color &caldif = coremat->getDiffuseColor();
        CalCoreMaterial::Color &calspec = coremat->getSpecularColor();

        mat->setAmbient(Color3f(calamb.red / 255.0f, calamb.green / 255.0f, calamb.blue / 255.0f));
        mat->setDiffuse(Color3f(caldif.red / 255.0f, caldif.green / 255.0f, caldif.blue / 255.0f));
        mat->setSpecular(Color3f(calspec.red / 255.0f, calspec.green / 255.0f, calspec.blue / 255.0f));
        
        mat->setShininess(coremat->getShininess() * 100.f);
        mat->setLit(true);
        mat->setColorMaterial(GL_NONE);
        
        for(int mapId = 0; mapId < coremat->getMapCount(); mapId++)
        {
            std::string file = coremat->getMapFilename(mapId);
            std::string pfile = ph.findFile(file.c_str());
            SINFO << "Loading texture '" << pfile << "'..." << endLog;

            ImagePtr img = Image::create();
            
             if(!img->read(pfile.c_str()))
            {
                SWARNING << "CharacterModel::convertMaterials: error "
                         << "loading image " << file << endLog;
            }
            else
            {
                // amz with my test scene paladin.cfg all textures were
                // upside down so I disabled the vertical flipping perhaps
                // they fixed the bug in Cal3D?
#if 0
                beginEditCP(img);
                {
                // For some reason Cal3D expects textures upside down ???
                UInt32 bpl = img->getBpp() * img->getWidth();
                UChar8 *t = img->getData(), 
                       *b = t + (img->getHeight() - 1) * bpl,
                        dum;

                for(UInt32 y = img->getHeight() / 2; y > 0; --y)
                {
                    for(UInt32 x = bpl; x > 0; --x, ++t, ++b)
                    {
                        dum = *t;
                        *t = *b;
                        *b = dum;
                    }
                    b -= bpl * 2;
                }
                }
                endEditCP(img);
#endif
                
                TextureChunkPtr tex = TextureChunk::create();
                
                beginEditCP(tex);
                tex->setImage(img);
                tex->setEnvMode(GL_MODULATE);
                endEditCP(tex);
                
                mat->addChunk(tex);
            }
        }
            
        endEditCP(mat);
        
        coremat->setUserData((Cal::UserData)mcnt);
        getMaterials().push_back(mat);
        mcnt ++;
    }    
}
Example #5
0
int main(int argc, char** argv)
{
    std::cout << "osgInit()..." << std::endl;
    osgInit( argc, argv );
    
    std::cout << "glutInit()..." <<  std::endl;
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
    int winID = glutCreateWindow("OpenSG");
    
    glutKeyboardFunc(key);
    glutVisibilityFunc(vis);
    glutReshapeFunc(resize);
    glutDisplayFunc(display);       
    glutMouseFunc(mouse);   
    glutMotionFunc(motion); 
    
    glutIdleFunc(display);
            
    glEnable( GL_DEPTH_TEST );
    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );
    glClearColor( .3, .3, .8, 1 );
    
    std::cout << "Creating objects..." << std::endl;
    
    //BEACON
    NodePtr beaconNode = Node::create();
    GroupPtr beaconCore = Group::create();
    beginEditCP( beaconNode );
    beaconNode->setCore( beaconCore );
    endEditCP( beaconNode );
    
    /*
    //LIGHT
    NodePtr dlightNode = Node::create();
    DirectionalLightPtr dlightCore = DirectionalLight::create();
    beginEditCP(dlightNode);
    dlightNode->setCore(dlightCore);
    endEditCP(dlightNode);
    
    beginEditCP(dlightCore);
    dlightCore->setAmbient( .3, .3, .3, 1 );
    dlightCore->setDiffuse( .5, .5, .5, 1 );
    dlightCore->setDirection( 0, 0 , 1 );
    dlightCore->setBeacon( beaconNode );
    endEditCP(dlightCore);
    */
        
    //TRANSFORM
    NodePtr transNode = Node::create();
    TransformPtr transCore = Transform::create();
    beginEditCP( transNode );
    transNode->setCore( transCore );
    transNode->addChild( beaconNode );
    endEditCP( transNode );
    cam_trans = transCore;
    
    //LOD
    NodePtr lowDetailNode = makeBox( 1,1,1, 1,1,1 );
    GeometryPtr boxCore = GeometryPtr::dcast(lowDetailNode->getCore());
    SimpleMaterialPtr lowDetailMat = SimpleMaterial::create();
    lowDetailMat->setDiffuse( Color3f( 1,0,0 ) );
    lowDetailMat->setAmbient( Color3f( 1,0,0 ) );
    boxCore->setMaterial( lowDetailMat );
    
    NodePtr     medDetailNode = makeSphere( 1, 1 );
    GeometryPtr medSphereCore = GeometryPtr::dcast(medDetailNode->getCore());
    SimpleMaterialPtr medDetailMat = SimpleMaterial::create();
    medDetailMat->setDiffuse( Color3f( 0,1,0 ) );
    medDetailMat->setAmbient( Color3f( 0,1,0 ) );
    medSphereCore->setMaterial( medDetailMat );
    
    NodePtr highDetailNode = makeSphere( 2, 1 );
    GeometryPtr highSphereCore = GeometryPtr::dcast(highDetailNode->getCore());
    SimpleMaterialPtr highDetailMat = SimpleMaterial::create();
    highDetailMat->setDiffuse( Color3f( 0,0,1 ) );
    highDetailMat->setAmbient( Color3f( 0,0,1 ) );
    highDetailMat->setSpecular( Color3f(1, 1, 1) );
    highDetailMat->setShininess( 10 );
    highSphereCore->setMaterial( highDetailMat );
    
    NodePtr lodNode = Node::create();
    DistanceLODPtr lodNodeCore = DistanceLOD::create();
    beginEditCP(lodNode);
    lodNode->setCore(lodNodeCore);
    lodNode->addChild( highDetailNode );
    lodNode->addChild( medDetailNode );
    lodNode->addChild( lowDetailNode );
    endEditCP(lodNode);
    
    beginEditCP(lodNodeCore);
    lodNodeCore->editSFCenter()->setValue( Pnt3f(0, 0, 2) );
    lodNodeCore->editMFRange()->push_back( 4.0 );
    lodNodeCore->editMFRange()->push_back( 8.0 );
    lodNodeCore->editMFRange()->push_back( 11.0 );
    endEditCP(lodNodeCore);
    
        
    //TRANSFORM LOD
    NodePtr transLODNode = Node::create();
    TransformPtr transLODCore = Transform::create();
    transMat.setTranslate( 1, -1, -1 );
    beginEditCP(transLODCore);
    transLODCore->editSFMatrix()->setValue( transMat );
    endEditCP(transLODCore);
    beginEditCP( transLODNode );
    transLODNode->setCore( transLODCore );
    transLODNode->addChild( lodNode );
    endEditCP( transLODNode );
    
    
        
    //ROOT
    root = Node::create();
    GroupPtr rootCore = Group::create();
    beginEditCP(root);
    root->setCore(rootCore);
    root->addChild(transNode);
    root->addChild(transLODNode);
    endEditCP(root);
    
    
    
    camera = PerspectiveCamera::create();
    beginEditCP(camera);
    camera->setBeacon( beaconNode );
    camera->setFov( deg2rad(90) );
    camera->setNear( 0.1 );
    camera->setFar( 10000 );
    endEditCP(camera);
    
    SolidBackgroundPtr background = SolidBackground::create();
    
    viewp = Viewport::create();
    beginEditCP(viewp);
    viewp->setCamera( camera );
    viewp->setBackground( background );
    viewp->setRoot( root );
    viewp->setSize( 0,0, 1,1 );
    endEditCP(viewp);
    
    GLint glVP[4];
    glGetIntegerv( GL_VIEWPORT, glVP );
    
    GLUTWindowPtr gwin = GLUTWindow::create();
    gwin->setId(winID);
    gwin->setSize(glVP[2], glVP[3]);
    
    window = gwin;
    beginEditCP(window);
    window->addPort( viewp );
    window->init();
    endEditCP(window);
    
    drAct = DrawAction::create();
    
    Vec3f pos( 0, 0, 3 );
    tball.setMode( Trackball::OSGObject );
    tball.setStartPosition( pos, true );
    tball.setSum( true );
    tball.setTranslationMode( Trackball::OSGFree );

    glutMainLoop();
    return 0;
}
Example #6
0
int main(int argc, char **argv)
{
    osgInit(argc,argv);

    // GLUT init
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);

    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;

    scene = Node::create();

    // create the material

    // Pass 1: Simple red, shiny, depth-tested

    DepthChunkPtr cl1 = DepthChunk::create();
    beginEditCP(cl1);
    cl1->setEnable(true);
    cl1->setFunc(GL_LEQUAL);
    endEditCP(cl1);

    SimpleMaterialPtr mat = SimpleMaterial::create();
    beginEditCP(mat);
    mat->setDiffuse(Color3f(1,0,0));
    mat->setSpecular(Color3f(.9,.9,.9));
    mat->setShininess(30);
    mat->setLit(true);
    mat->addChunk(cl1);
    endEditCP(mat);

    // Pass 2: Green unlit, without depth testing

    DepthChunkPtr cl2 = DepthChunk::create();
    beginEditCP(cl2);
    cl2->setEnable(false);
    endEditCP(cl2);

    SimpleMaterialPtr mat2 = SimpleMaterial::create();
    beginEditCP(mat2);
    mat2->setDiffuse(Color3f(0,1,0));
    mat2->setLit(false);
    mat2->setTransparency(.8);
    mat2->addChunk(cl2);
    endEditCP(mat2);

    // Bring them together

    MultiPassMaterialPtr mpm = MultiPassMaterial::create();

    beginEditCP(mpm);
    mpm->editMFMaterials()->push_back(mat);
    mpm->editMFMaterials()->push_back(mat2);
    endEditCP(mpm);

    GeometryPtr g1 = makeTorusGeo(0.2, 2, 8, 16);

    beginEditCP(scene);
    scene->setCore(g1);
    endEditCP(scene);

    beginEditCP(g1);

    g1->setMaterial(mpm);

    endEditCP(g1);

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

    // show the whole scene
    mgr->showAll();
    mgr->redraw();

    // GLUT main loop
    glutMainLoop();

    return 0;
}
Example #7
0
NodePtr createScenegraph(){
    //At first we load all needed models from file
    NodePtr w_high = SceneFileHandler::the().read("data/woman_high.wrl");
    NodePtr w_medium = SceneFileHandler::the().read("data/woman_medium.wrl");
    NodePtr w_low = SceneFileHandler::the().read("data/woman_low.wrl");
    
    //we check the result
    if ((w_high == NullFC)&&(w_medium == NullFC)&&(w_low == NullFC)){
        std::cout << "It was not possible to load all needed models from file" << std::endl;
        return NullFC;
    }
    
    //now the LOD core
    DistanceLODPtr lod = DistanceLOD::create();
    beginEditCP(lod, DistanceLOD::CenterFieldMask | DistanceLOD::RangeFieldMask);
        lod->getSFCenter()->setValue(Pnt3f(0,0,0));
        lod->getMFRange()->push_back(200);
        lod->getMFRange()->push_back(500);
    endEditCP(lod, DistanceLOD::CenterFieldMask | DistanceLOD::RangeFieldMask);
    
    //the node containing the LOD core. The three models will be
    //added as its children
    NodePtr lodNode = Node::create();
    beginEditCP(lodNode);
        lodNode->setCore(lod);
        lodNode->addChild(w_high);
        lodNode->addChild(w_medium);
        lodNode->addChild(w_low);
    endEditCP(lodNode);
    
        //create the node with switch core ********************
    SwitchPtr sw = Switch::create();
    beginEditCP(sw, Switch::ChoiceFieldMask);
        //Notice: the first choice is 0
        sw->setChoice(0);
    endEditCP(sw, Switch::ChoiceFieldMask);
    
    NodePtr switchNode = Node::create();
    beginEditCP(switchNode);
        switchNode->setCore(sw);
        switchNode->addChild(lodNode);
    endEditCP(switchNode);
    
    //end witch creation **********************************
    
    NodePtr root = Node::create();
    beginEditCP(root);
        root->setCore(Group::create());
        root->addChild(switchNode);
    endEditCP(root);
    
    // we know want to extract the mesh geometry out of the graph
    // it is sufficent to pass the model only as root for searching
    NodePtr womanGeometry = checkName(w_high);
	GeometryPtr geo;
	if (womanGeometry != NullFC){
		geo = GeometryPtr::dcast(womanGeometry->getCore());
		if (geo == NullFC)
			std::cout << "Casting failed!" << std::endl;
	}else{ 
		std::cout << "No correct geometry node found!" << std::endl;
		//create a dummy object
		geo = makeBoxGeo(0.5,0.5,0.5,1,1,1);
	}
    
    // generating a material *********************************
    
    SimpleMaterialPtr mat = SimpleMaterial::create();
    beginEditCP(mat);
        mat->setAmbient(Color3f(0.2,0.2,0.2));
        mat->setDiffuse(Color3f(0.6,0.3,0.1));
        mat->setSpecular(Color3f(1,1,1));
        mat->setShininess(0.8);
    endEditCP(mat);
    
    beginEditCP(geo, Geometry::MaterialFieldMask);
        geo->setMaterial(mat);
    endEditCP(geo, Geometry::MaterialFieldMask);
    
    // end material generation *******************************
    
    //new node with "old" geometry core referenced
    NodePtr woman = Node::create();
    beginEditCP(woman);
        woman->setCore(geo);
    endEditCP(woman);
    
    //translate it a bit to see both women
    NodePtr womanTrans = Node::create();
    TransformPtr t = Transform::create();
    beginEditCP(t);
        Matrix m;
        m.setIdentity();
        m.setTranslate(Vec3f(0,0,200));
        t->setMatrix(m);
    endEditCP(t); 
    beginEditCP(womanTrans);
        womanTrans->setCore(t);
        womanTrans->addChild(woman);
    endEditCP(womanTrans);
    
    //add it to the root
    beginEditCP(root);
        root->addChild(womanTrans);
    endEditCP(root);
    
    return root;
}
Example #8
0
int main (int argc, char **argv)
{
    osgInit(argc,argv);

    // GLUT init

    glutInit(&argc, argv);
    glutInitDisplayMode( GLUT_RGBA | 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;
    NodePtr file1 = NullFC;

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

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

    file1->updateVolume();

//    file->dump();

//    subRefCP(file);

//    return 0;


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

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


    file = Node::create();
    MaterialGroupPtr testMat = MaterialGroup::create();

    SimpleMaterialPtr defaultMaterial = SimpleMaterial::create();

    beginEditCP(defaultMaterial);
    defaultMaterial->setDiffuse(Color3f(1,.0,.0));
    defaultMaterial->setAmbient(Color3f(0.1,0.1,0.1));
    defaultMaterial->setSpecular(Color3f(1,1,1));
    defaultMaterial->setShininess(20);
    endEditCP  (defaultMaterial);


    testMat->setMaterial(defaultMaterial);

    beginEditCP(file);
    {
//        file->setCore(testMat);
        file->setCore(Group::create());
        file->addChild(file1);
    }
    endEditCP  (file);

    scene_trans      = Transform::create();
    NodePtr sceneTrN = Node::create();

    beginEditCP(sceneTrN);
    {
        sceneTrN->setCore(scene_trans);
        sceneTrN->addChild(file);
        sceneTrN->addChild(makeTorus( .5, 2, 16, 16 ));
    }
    endEditCP(sceneTrN);

    beginEditCP(dlight);
    dlight->addChild(sceneTrN);
    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( 100000 );

    // Background
    SolidBackgroundPtr bkgnd = SolidBackground::create();
    beginEditCP(bkgnd, SolidBackground::ColorFieldMask);
    bkgnd->setColor(Color3f(1,1,1));
    endEditCP(bkgnd, SolidBackground::ColorFieldMask);

    // Viewport

    vp = Viewport::create();
    vp->setCamera( cam );
    vp->setBackground( bkgnd );
    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();
    renact = RenderAction::create();

    // tball

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

    float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6;

    Pnt3f tCenter(min[0] + (max[0] - min[0]) / 2,
                  min[1] + (max[1] - min[1]) / 2,
                  min[2] + (max[2] - min[2]) / 2);

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

    // run...

    glutMainLoop();

    return 0;
}
Example #9
0
// This function creates the classic Utah teapot from 4 NURBS
// surfaces: one surface each for the lid, body, handle and spout.
// The "classic" representation is using 18 rational Bezier patches,
// but in order to show the construction of more complex surfaces
// these have been combined into as few NURBS surfaces as possible.
NodePtr makeTeapot()
{
    SimpleMaterialPtr teapotmat =  SimpleMaterial::create();
    beginEditCP(teapotmat);
    {
        teapotmat->setDiffuse(  Color3f(0.8, 0.8, 0.8));
        teapotmat->setAmbient(  Color3f(0.2, 0.2, 0.2));
        teapotmat->setEmission( Color3f(0.02, 0.02, 0.02) );
        teapotmat->setSpecular( Color3f(0.78, 0.78, 0.78) );
        teapotmat->setShininess( 128 );
        teapotmat->addChunk(g_fb_chunk);
    }
    endEditCP(teapotmat);
    NodePtr teapotroot = makeCoredNode<Group>();
    float knots11[11] = {0, 0, 0, 0, 0.5, 0.5, 0.5, 1, 1, 1, 1};
    float knots14[14] = {0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3};
    float knots_circle[8] = {0, 0, 0, 0.5, 0.5, 1, 1, 1};
    float lidcps[35][4] = {
        { 0.0000, 4.0000, 0.0000, 1.0000},
        { 0.0000, 4.0000, 0.8000, 1.0000},
        { 0.0000, 3.6000, 0.0000, 1.0000},
        { 0.0000, 3.4000, 0.2000, 1.0000},
        { 0.0000, 3.2000, 0.4000, 1.0000},
        { 0.0000, 3.2000, 1.3000, 1.0000},
        { 0.0000, 3.0000, 1.3000, 1.0000},

        {-0.0000, 0.0000, 0.0000, 0.0000},
        {-0.8000, 0.0000, 0.0000, 0.0000},
        {-0.0000, 0.0000, 0.0000, 0.0000},
        {-0.2000, 0.0000, 0.0000, 0.0000},
        {-0.4000, 0.0000, 0.0000, 0.0000},
        {-1.3000, 0.0000, 0.0000, 0.0000},
        {-1.3000, 0.0000, 0.0000, 0.0000},

        { 0.0000, 4.0000,-0.0000, 1.0000},
        { 0.0000, 4.0000,-0.8000, 1.0000},
        { 0.0000, 3.6000,-0.0000, 1.0000},
        { 0.0000, 3.4000,-0.2000, 1.0000},
        { 0.0000, 3.2000,-0.4000, 1.0000},
        { 0.0000, 3.2000,-1.3000, 1.0000},
        { 0.0000, 3.0000,-1.3000, 1.0000},

        { 0.0000, 0.0000, 0.0000, 0.0000},
        { 0.8000, 0.0000, 0.0000, 0.0000},
        { 0.0000, 0.0000, 0.0000, 0.0000},
        { 0.2000, 0.0000, 0.0000, 0.0000},
        { 0.4000, 0.0000, 0.0000, 0.0000},
        { 1.3000, 0.0000, 0.0000, 0.0000},
        { 1.3000, 0.0000, 0.0000, 0.0000},

        { 0.0000, 4.0000, 0.0000, 1.0000},
        { 0.0000, 4.0000, 0.8000, 1.0000},
        { 0.0000, 3.6000, 0.0000, 1.0000},
        { 0.0000, 3.4000, 0.2000, 1.0000},
        { 0.0000, 3.2000, 0.4000, 1.0000},
        { 0.0000, 3.2000, 1.3000, 1.0000},
        { 0.0000, 3.0000, 1.3000, 1.0000},
    };
    NodePtr lid = makeSurface(35, lidcps, 2, 3, 8, knots_circle,
                              11, knots11, 0.005, teapotmat);
    float bodycps[50][4] = {
        { 0.0000, 3.0000, 1.4000, 1.0000},
        { 0.0000, 3.1750, 1.3375, 1.0000},
        { 0.0000, 3.1750, 1.4375, 1.0000},
        { 0.0000, 3.0000, 1.5000, 1.0000},
        { 0.0000, 2.3000, 1.7500, 1.0000},
        { 0.0000, 1.6000, 2.0000, 1.0000},
        { 0.0000, 1.0000, 2.0000, 1.0000},
        { 0.0000, 0.4000, 2.0000, 1.0000},
        { 0.0000, 0.2000, 1.5000, 1.0000},
        { 0.0000, 0.0000, 1.5000, 1.0000},

        {-1.4000, 0.0000, 0.0000, 0.0000},
        {-1.3375, 0.0000, 0.0000, 0.0000},
        {-1.4375, 0.0000, 0.0000, 0.0000},
        {-1.5000, 0.0000, 0.0000, 0.0000},
        {-1.7500, 0.0000, 0.0000, 0.0000},
        {-2.0000, 0.0000, 0.0000, 0.0000},
        {-2.0000, 0.0000, 0.0000, 0.0000},
        {-2.0000, 0.0000, 0.0000, 0.0000},
        {-1.5000, 0.0000, 0.0000, 0.0000},
        {-1.5000, 0.0000, 0.0000, 0.0000},

        { 0.0000, 3.0000,-1.4000, 1.0000},
        { 0.0000, 3.1750,-1.3375, 1.0000},
        { 0.0000, 3.1750,-1.4375, 1.0000},
        { 0.0000, 3.0000,-1.5000, 1.0000},
        { 0.0000, 2.3000,-1.7500, 1.0000},
        { 0.0000, 1.6000,-2.0000, 1.0000},
        { 0.0000, 1.0000,-2.0000, 1.0000},
        { 0.0000, 0.4000,-2.0500, 1.0000},
        { 0.0000, 0.2000,-1.5000, 1.0000},
        { 0.0000, 0.0000,-1.5000, 1.0000},

        { 1.4000, 0.0000, 0.0000, 0.0000},
        { 1.3375, 0.0000, 0.0000, 0.0000},
        { 1.4375, 0.0000, 0.0000, 0.0000},
        { 1.5000, 0.0000, 0.0000, 0.0000},
        { 1.7500, 0.0000, 0.0000, 0.0000},
        { 2.0000, 0.0000, 0.0000, 0.0000},
        { 2.0000, 0.0000, 0.0000, 0.0000},
        { 2.0000, 0.0000, 0.0000, 0.0000},
        { 1.5000, 0.0000, 0.0000, 0.0000},
        { 1.5000, 0.0000, 0.0000, 0.0000},

        { 0.0000, 3.0000, 1.4000, 1.0000},
        { 0.0000, 3.1750, 1.3375, 1.0000},
        { 0.0000, 3.1750, 1.4375, 1.0000},
        { 0.0000, 3.0000, 1.5000, 1.0000},
        { 0.0000, 2.3000, 1.7500, 1.0000},
        { 0.0000, 1.6000, 2.0000, 1.0000},
        { 0.0000, 1.0000, 2.0000, 1.0000},
        { 0.0000, 0.4000, 2.0000, 1.0000},
        { 0.0000, 0.2000, 1.5000, 1.0000},
        { 0.0000, 0.0000, 1.5000, 1.0000},

    };
    NodePtr body = makeSurface(50, bodycps, 2, 3, 8, knots_circle,
                               14, knots14, 0.005, teapotmat);
    float handlecps[35][4] = {
        { 1.5000, 2.8000, 0.0000, 1.0000},
        { 2.5000, 2.8000, 0.0000, 1.0000},
        { 3.0000, 2.8000, 0.0000, 1.0000},
        { 3.0000, 2.2000, 0.0000, 1.0000},
        { 3.0000, 1.6000, 0.0000, 1.0000},
        { 2.6500, 1.0500, 0.0000, 1.0000},
        { 1.9000, 0.6000, 0.0000, 1.0000},

        { 0.0000, 0.0000, 0.3000, 0.0000},
        { 0.0000, 0.0000, 0.3000, 0.0000},
        { 0.0000, 0.0000, 0.3000, 0.0000},
        { 0.0000, 0.0000, 0.3000, 0.0000},
        { 0.0000, 0.0000, 0.3000, 0.0000},
        { 0.0000, 0.0000, 0.3000, 0.0000},
        { 0.0000, 0.0000, 0.3000, 0.0000},

        { 1.6000, 2.5000, 0.0000, 1.0000},
        { 2.3000, 2.5000, 0.0000, 1.0000},
        { 2.7000, 2.5000, 0.0000, 1.0000},
        { 2.7000, 2.2000, 0.0000, 1.0000},
        { 2.7000, 1.9000, 0.0000, 1.0000},
        { 2.5000, 1.5000, 0.0000, 1.0000},
        { 2.0000, 1.0000, 0.0000, 1.0000},

        { 0.0000, 0.0000,-0.3000, 0.0000},
        { 0.0000, 0.0000,-0.3000, 0.0000},
        { 0.0000, 0.0000,-0.3000, 0.0000},
        { 0.0000, 0.0000,-0.3000, 0.0000},
        { 0.0000, 0.0000,-0.3000, 0.0000},
        { 0.0000, 0.0000,-0.3000, 0.0000},
        { 0.0000, 0.0000,-0.3000, 0.0000},

        { 1.5000, 2.8000, 0.0000, 1.0000},
        { 2.5000, 2.8000, 0.0000, 1.0000},
        { 3.0000, 2.8000, 0.0000, 1.0000},
        { 3.0000, 2.2000, 0.0000, 1.0000},
        { 3.0000, 1.6000, 0.0000, 1.0000},
        { 2.6500, 1.0500, 0.0000, 1.0000},
        { 1.9000, 0.6000, 0.0000, 1.0000},
    };

    NodePtr handle = makeSurface(35, handlecps, 2, 3, 8, knots_circle,
                                 11, knots11, 0.005, teapotmat);
    float spoutcps[35][4] = {
        {-1.7000, 0.6000, 0.0000, 1.0000},
        {-3.1000, 0.9000, 0.0000, 1.0000},
        {-2.4000, 2.6000, 0.0000, 1.0000},
        {-3.3000, 3.0000, 0.0000, 1.0000},
        {-3.5250, 3.1250, 0.0000, 1.0000},
        {-3.4500, 3.1500, 0.0000, 1.0000},
        {-3.2000, 3.0000, 0.0000, 1.0000},

        { 0.0000, 0.0000, 0.6600, 0.0000},
        { 0.0000, 0.0000, 0.6600, 0.0000},
        { 0.0000, 0.0000, 0.2500, 0.0000},
        { 0.0000, 0.0000, 0.2500, 0.0000},
        { 0.0000, 0.0000, 0.2500, 0.0000},
        { 0.0000, 0.0000, 0.1500, 0.0000},
        { 0.0000, 0.0000, 0.1500, 0.0000},

        {-1.7000, 1.7000, 0.0000, 1.0000},
        {-2.6000, 1.7000, 0.0000, 1.0000},
        {-2.3000, 2.6000, 0.0000, 1.0000},
        {-2.7000, 3.0000, 0.0000, 1.0000},
        {-2.8000, 3.1000, 0.0000, 1.0000},
        {-2.9000, 3.1000, 0.0000, 1.0000},
        {-2.8000, 3.0000, 0.0000, 1.0000},

        { 0.0000, 0.0000,-0.6600, 0.0000},
        { 0.0000, 0.0000,-0.6600, 0.0000},
        { 0.0000, 0.0000,-0.2500, 0.0000},
        { 0.0000, 0.0000,-0.2500, 0.0000},
        { 0.0000, 0.0000,-0.2500, 0.0000},
        { 0.0000, 0.0000,-0.1500, 0.0000},
        { 0.0000, 0.0000,-0.1500, 0.0000},

        {-1.7000, 0.6000, 0.0000, 1.0000},
        {-3.1000, 0.9000, 0.0000, 1.0000},
        {-2.4000, 2.6000, 0.0000, 1.0000},
        {-3.3000, 3.0000, 0.0000, 1.0000},
        {-3.5250, 3.1250, 0.0000, 1.0000},
        {-3.4500, 3.1500, 0.0000, 1.0000},
        {-3.2000, 3.0000, 0.0000, 1.0000},
    };
    NodePtr spout = makeSurface(35, spoutcps, 2, 3, 8, knots_circle,
                                11, knots11, 0.005, teapotmat);

    beginEditCP(teapotroot);
    teapotroot->addChild(lid);
    teapotroot->addChild(body);
    teapotroot->addChild(handle);
    teapotroot->addChild(spout);
    endEditCP(teapotroot);
    return teapotroot;
}