// move the particles
void idle(void)
{
    if(doMotion)
    {    
        beginEditCP(pnts);
        beginEditCP(secpnts);

        MFPnt3f *p=pnts->editFieldPtr();
        MFPnt3f *sp=secpnts->editFieldPtr();

        for(UInt32 i=0; i<p->size(); ++i)
        {
            Pnt3f pos=(*p)[i];

            pos+=velocities[i];

            if(pos[0]<0 || pos[0]>1)
            {
                pos[0]-=velocities[i][0];
                velocities[i][0]=-velocities[i][0];
            }
            if(pos[1]<0 || pos[1]>1)
            {
                pos[1]-=velocities[i][1];
                velocities[i][1]=-velocities[i][1];
            }
            if(pos[2]<0 || pos[2]>1)
            {
                pos[2]-=velocities[i][2];
                velocities[i][2]=-velocities[i][2];
            }

            (*sp)[i] = pos;
        }

        endEditCP(pnts);
        endEditCP(secpnts);

        beginEditCP(particles,
                    Particles::PositionsFieldMask|Particles::SecPositionsFieldMask);
        particles->setPositions(secpnts);
        particles->setSecPositions(pnts);   
        endEditCP  (particles,
                    Particles::PositionsFieldMask|Particles::SecPositionsFieldMask);

        std::swap(pnts,secpnts);    
    }
    
    if(doIndices)
    {
        beginEditCP(particles, Particles::IndicesFieldMask);
        indices->resize(pnts->getSize() / 2);
        for(UInt32 i = 0; i < pnts->getSize() / 2; ++i)
        {
            (*indices)[i] = static_cast<UInt32>(osgrand() * 2 - 1) * pnts->getSize();
        }
        endEditCP  (particles, Particles::IndicesFieldMask);     
    }
     
    glutPostRedisplay();
}
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;
}