Esempio n. 1
0
float PruneGraphOp::getSize(const NodePtr& node)
{
#ifndef OSG_2_PREP
    const DynamicVolume &vol = node->editVolume(true);
#else
    const BoxVolume     &vol = node->editVolume(true);
#endif
    if (_method == VOLUME)
    {
        return vol.getScalarVolume();
    }
    else if (_method == SUM_OF_DIMENSIONS)
    {
        Pnt3f min, max;
        vol.getBounds(min, max);
        Vec3f diff = max - min;
        return diff[0] + diff[1] + diff[2];
    }
    else
    {
        SWARNING << "Unknown size calculation method" << std::endl;
        return 0;
    }
}
Esempio n. 2
0
void keyboard(unsigned char k, int x, int y)
{
    switch(k)
    {
        case 27:
        {
            OpenSGDeformActionManager::Destroy();
            osgExit();
            exit(0);
        }
        case '1':
        {
            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
            break;
        }
        case '2':
        {
            glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
            break;
        }
        case '3':
        {
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
            break;
        }
        case '5':
        {
            chairIndex = (chairIndex + 1) % 6;
            model = chairNodes[chairIndex];
            cout << "Selected model is: ";
            if (getName(model))
                cout << getName(model);
            cout << " " << model << endl;
            break;
        }
        case '6':
        {
            ax *= -1;
            break;
        }
        case '7':
        {
            if (ax < 0)
                ax = -1;
            else
                ax = 1;
            break;
        }
        case '8':
        {
            if (ax < 0)
                ax = -2;
            else
                ax = 2;
            break;
        }
        case '9':
        {
            if (ax < 0)
                ax = -3;
            else
                ax = 3;
            break;
        }
        case 'p':
        {
#if OSG_MAJOR_VERSION >= 2
			traverse(scene, boost::bind(printSceneGraph,_1));
#else
            traverse(scene, osgTypedFunctionFunctor1CPtrRef
                <Action::ResultE, NodePtr>(printSceneGraph));
#endif 
            break;
        }
        case 'h':
        {
            printUsage();
            break;
        }
        case 'i':
        {
            gmtl::AABoxf aabb;
            osgdam = OpenSGDeformActionManager::GetInstance();
            if (osgdam != 0)
            {
                osgdam->insertLattice(model, 2, 2, 2, 3);
            }
            break;
        }
        case 'j':
        {
            osgdam = OpenSGDeformActionManager::GetInstance();
            if(osgdam != 0)
            {
                gmtl::AABoxf aabb;
                Pnt3f aabbMin, aabbMax;
#if OSG_MAJOR_VERSION >= 2
				model->editVolume(true).getBounds(aabbMin, aabbMax);
#else
				model->getVolume(true).getBounds(aabbMin, aabbMax);
#endif               

                for (int i = 0; i < 3; i++)
                {
                    aabbMin[i] = aabbMin[i] - 1.0f;
                    aabbMax[i] = aabbMax[i]/2 + 1.0f;
                }
                aabb = gmtl::AABoxf(gmtl::Vec3f(aabbMin[0], aabbMin[1], aabbMin[2]),
                                    gmtl::Vec3f(aabbMax[0], aabbMax[1], aabbMax[2]));

                osgdam->insertLattice(model, 2, 2, 2, 3, true, aabb);
            }
            break;
        }
        case 'q':
        {
            NodePtr tmp = model;

            while (tmp != NullFC)
            {
                TransformPtr tcore = TransformPtr::dcast(tmp->getCore());
                if (tcore != NullFC)
                {
                    Matrix mm = tcore->getMatrix();
                    Matrix tm = Matrix::identity();

                    tm.setScale(1,2,1);
                    mm.mult(tm);

                    beginEditCP(tcore, Transform::MatrixFieldMask);
                        tcore->setMatrix(mm);
                    endEditCP(tcore, Transform::MatrixFieldMask);

                    mm = tcore->getMatrix();
                    break;
                }
                tmp = tmp->getParent();
            }
            break;
        }
        default:
            break;
    }
    if (osgdam != 0)
    {
        switch(k)
        {
            case '4':
            {
                all = !all;
                osgdam->setShowAll(model, all);
                break;
            }
            case 'e':
            {
                doExecute = !doExecute;
                osgdam->setInstantExecution(model, doExecute);
                break;
            }
            case 'r':
            {
                osgdam->removeLattice(model);
                break;
            }
            case 't':
            {
                osgdam->taper(model, 1.0f, ax);
                break;
            }
            case 'g':
            {
                osgdam->twist(model, gmtl::Math::deg2Rad(5.0f), ax);
                break;
            }
            case 'b':
            {
                osgdam->bend(model, gmtl::Math::deg2Rad(5.0f), 0.5f, ax);
                break;
            }
            case 'v':
            {
                osgdam->bend(model, -gmtl::Math::deg2Rad(5.0f), 0.0f, ax);
                break;
            }
            case 'c':
            {
                osgdam->bend(model, -gmtl::Math::deg2Rad(5.0f), 1.0f, ax);
                break;
            }
            case 'f':
            {
                osgdam->bend(model, -gmtl::Math::deg2Rad(5.0f), 0.5f, ax);
                break;
            }
            case 'd':
            {
                gmtl::Matrix44f dMatrix;
                dMatrix[0][0] = 0.5f;
                dMatrix[1][1] = 1.0f;
                dMatrix[2][2] = -1.5f;
                osgdam->deform(model, dMatrix, false);
                break;
            }
            case 'l':
            {
                selectPoints.clear();
                selectPositions.clear();
                osgdam->unselectLatticeCellPoints(model);
                break;
            }
            case 'm':
            {
                osgdam->selectLatticeCellPoint(mgr, model, x, y, true);
                break;
            }
            case 'n':
            {
                osgdam->unselectLatticeCellPoint(mgr, model, x, y);
                break;
            }
            case '-':
            {
                gmtl::Vec3f pos;
                selectPositions.clear();
                selectPoints = osgdam->getSelection(model);
                for (size_t i = 0; i < selectPoints.size(); ++i)
                {
                    osgdam->getPoint(model, selectPoints[i], pos);
                    if (ax == 1)
                        pos[0] -= 0.5f;
                    else if (ax == 2)
                        pos[1] -= 0.5f;
                    else
                        pos[2] -= 0.5f;
                    selectPositions.push_back(pos);
                }
                osgdam->setPoints(model, selectPoints, selectPositions);
                break;
            }
            case '+':
            {
                gmtl::Vec3f pos;
                selectPositions.clear();
                selectPoints = osgdam->getSelection(model);
                for (size_t i = 0; i < selectPoints.size(); ++i)
                {
                    osgdam->getPoint(model, selectPoints[i], pos);
                    if (ax == 1)
                        pos[0] += 0.5f;
                    else if (ax == 2)
                        pos[1] += 0.5f;
                    else
                        pos[2] += 0.5f;
                    selectPositions.push_back(pos);
                }
                osgdam->setPoints(model, selectPoints, selectPositions);
                break;
            }
            case 'u':
            {
                osgdam->undo(model);
                break;
            }
            case 'x':
            {
                osgdam->executeFfd(model);
                break;
            }
            case 'o':
            {
                osgdam->dump(model);
                break;
            }
            default:
                break;
        }
    }
}
int main(int argc, char **argv)
{
//    enableFPE();

#ifdef __sgi 	
	signal(SIGSEGV, (void (*)(int))sighand);
	signal(SIGTRAP, (void (*)(int))sighand);
	signal(SIGBUS, (void (*)(int))sighand);
#endif
   
    // OSG init
    osgInit(argc,argv);

    if (argc > 1 && ! strcmp(argv[1],"-bench"))
    {
        runBench = true;
        argc--;
        argv++;
        glutInitWindowPosition(0,0);
        glutInitWindowSize(600,600);
    }

    if (argc > 1 && ! strcmp(argv[1],"-test"))
    {
        testSet = true;
        doMotion = false;
        argc--;
        argv++;
    }

    // GLUT init
    int winid = setupGLUT(&argc, argv);

    // the connection between GLUT and OpenSG
    GLUTWindowPtr gwin= GLUTWindow::create();
    gwin->setId(winid);
    gwin->init();

    // create the scene
    NodePtr scene = Node::create();    
    NodePtr pnode = Node::create();
    
    ComponentTransformPtr trans = ComponentTransform::create();
    beginEditCP(scene);
    scene->setCore(trans);
    scene->addChild(pnode);
    endEditCP(scene);
    
    beginEditCP(trans);
    trans->setTranslation(Vec3f(2,0,0));
    trans->setRotation(Quaternion(Vec3f(0,1,0),Pi/2));
    trans->setScale(Vec3f(2,2,2));
    endEditCP(trans);
    
    
    particles = Particles::create();
    beginEditCP(pnode);
    pnode->setCore(particles);
    endEditCP(pnode);

    numParticles = 100;
    
    if (argc > 1)
    {
        numParticles=atoi(argv[1]);
    }
    
    beginEditCP(particles);

    pnts=GeoPositions3f::create();
    secpnts=GeoPositions3f::create();
    
    addRefCP(pnts);
    addRefCP(secpnts);
    
    GeoColors3fPtr  cols  = GeoColors3f::create();
    GeoNormals3fPtr norms = GeoNormals3f::create();

    MFPnt3f* p=pnts->editFieldPtr();
    MFPnt3f* sp=secpnts->editFieldPtr();
    MFVec3f *size=particles->editMFSizes();

    indices = particles->editMFIndices();
    
    velocities=new Vec3f [numParticles];
    
    beginEditCP(pnts);
    beginEditCP(secpnts);
    beginEditCP(cols);
    if(!testSet)
    {
        for(UInt32 i=0; i < numParticles; ++i)
        {
            Pnt3f pnt(osgrand(),osgrand(),osgrand());
            indices->push_back(i);  
            p->push_back(pnt);  
            sp->push_back(pnt);  
            velocities[i].setValues(osgrand()/30.f/2, osgrand()/30.f/2, osgrand()/30.f/2);
            cols->editFieldPtr()->push_back( 
                Color3f(osgrand()/2.f + .5f,osgrand()/2.f + .5f,osgrand()/2.f + .5f) );
            size->push_back(
                Vec3f(osgrand()/20.f+0.05,osgrand()/20.f+0.05,osgrand()/20.f+0.05));
        }
    }
    else
    {
        Pnt3f   tpos[] = 
        { Pnt3f(.5,.5,.5), Pnt3f (.5,.5,.7), Pnt3f(.5,.5,.9), Pnt3f(.7,.5,.5), 
          Pnt3f(.5,.7,.5), Pnt3f (-1000,-1000,-1000) };
        
        Pnt3f   tsecpos[] = 
        { Pnt3f(0,0,0), Pnt3f(0,0,0), Pnt3f(0,0,0), Pnt3f(0,0,0), 
          Pnt3f(0,0,0) };
        
        Vec3f   tvel[] = 
        { Vec3f(0,0,0), Vec3f(0,0,0), Vec3f(0,0,0), Vec3f(0,0,0), 
          Vec3f(0,0,0) };
        
        Color3f tcol[] = 
        { Color3f(1,0,0), Color3f(0,1,0), Color3f(0,0,1), Color3f(1,1,0), 
          Color3f(1,0,1), Color3f(0,1,1), Color3f(1,1,1) };
        
        Vec3f   tsize[] = 
        { Vec3f(.1,0,0), Vec3f(.1,0,0), Vec3f(.1,0,0), Vec3f(.1,0,0), 
          Vec3f(.1,0,0) };

        for(UInt32 i=0; tpos[i][0] > -1000; ++i)
        {
            indices->push_back(i);  
            p->push_back(tpos[i]);  
            sp->push_back(tsecpos[i]);  
            velocities[i].setValue(tvel[i]);
            cols->editFieldPtr()->push_back(tcol[i]);
            size->push_back(tsize[i]);
        }
       
    }
    endEditCP(pnts);
    endEditCP(secpnts);
    endEditCP(cols);

    beginEditCP(norms);
    norms->push_back(Vec3f(0,1,0));
    endEditCP(norms);

    particles->setPositions( pnts );
    particles->setSecPositions( secpnts );
    particles->setColors( cols );
    particles->setNormals( norms );
                    
    endEditCP(particles);
 
    // set volume static to prevent constant update
    beginEditCP(pnode, Node::VolumeFieldMask);
#ifndef OSG_2_PREP
    Volume &v = pnode->editVolume(false).getInstance();
#else
    Volume &v = pnode->editVolume(false);
#endif
    v.setEmpty();
    v.extendBy(Pnt3f(0,0,0));
    v.extendBy(Pnt3f(1,1,1));
    v.setStatic();
#ifndef OSG_2_PREP
    pnode->editVolume(false).instanceChanged();
#endif
    endEditCP  (pnode, Node::VolumeFieldMask);
  
    SimpleTexturedMaterialPtr tm;

    tm = SimpleTexturedMaterial::create();

    UChar8 imgdata[] =
        {  255,255,255,  255,0,0,  255,0,255,
           255,0,0,  255,0,0,  255,255,255 };
    ImagePtr pImage = Image::create();

    if (argc > 2)
    {
        pImage->read(argv[2]);
    }
    else
    {
        pImage->set(Image::OSG_RGB_PF, 3, 2, 1, 1, 1, 0, imgdata);
    }
    
    beginEditCP(tm);
    tm->setDiffuse( Color3f( 1,1,1 ) );
    tm->setLit( false );

    tm->setImage( pImage );
    tm->setEnvMode( GL_MODULATE );
    
    BlendChunkPtr bl=BlendChunk::create();
    
    beginEditCP(bl);
    bl->setSrcFactor(GL_SRC_ALPHA);
    //bl->setDestFactor(GL_ONE_MINUS_SRC_ALPHA);
    bl->setDestFactor(GL_ONE);
#if 0
    bl->setAlphaFunc(GL_EQUAL);
    bl->setAlphaValue(1);   
#endif
    endEditCP(bl);
   
    tm->addChunk(bl);
    
    endEditCP(tm);

    particles->setMaterial( tm );

#if 0
    // write it, just for testing

    std::ofstream out("test.osg");
    OSGWriter w(out);
    
    w.write(scene);
    
#endif

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

    //mgr->setHighlight(scene);
    
    // GLUT main loop
    glutMainLoop();

    return 0;
}