virtual void  initialize(void)
	{
		// Check necessary stuff
		if(_win == NullFC)
		{
			FWARNING(("SceneManager::initialize: window not set, "
					  "ignoring!\n"));
			return;
		}
	    
		// the rendering action
		_ownAction = RenderAction::create();
		_action = _ownAction;

		// the camera and light beacon
		NodePtr cartN = Node::create();
		_cart = Transform::create();

		beginEditCP(cartN);
			cartN->setCore(_cart);
		endEditCP(cartN);

		// the headlight
		_internalRoot = Node::create();
		_headlight    = DirectionalLight::create();

		addRefCP(_internalRoot);
		beginEditCP(_internalRoot);
			_internalRoot->setCore(_headlight);
			_internalRoot->addChild(cartN);
		endEditCP(_internalRoot);

		beginEditCP(_headlight);
			_headlight->setAmbient  (.3, .3, .3, 1);
			_headlight->setDiffuse  ( 1,  1,  1, 1);
			_headlight->setSpecular ( 1,  1,  1, 1);
			_headlight->setDirection( 0,  0,  1);
			_headlight->setBeacon   (cartN);
		endEditCP(_headlight);

		// the camera
		_camera = PerspectiveCamera::create();
		addRefCP(_camera);
		beginEditCP(PerspectiveCameraPtr::dcast(_camera));
			PerspectiveCameraPtr::dcast(_camera)->setBeacon(cartN);
			PerspectiveCameraPtr::dcast(_camera)->setFov   (deg2rad(60.f));
			PerspectiveCameraPtr::dcast(_camera)->setNear  (0.1f);
			PerspectiveCameraPtr::dcast(_camera)->setFar   (10000.f);
		endEditCP(PerspectiveCameraPtr::dcast(_camera));

		// need a viewport?
		if(_win->getPort().size() == 0)
		{
			SolidBackgroundPtr bg = SolidBackground::create();
			beginEditCP(bg);
				bg->setColor(Color3f(0, 0, 0));
			endEditCP(bg);

			ViewportPtr vp = Viewport::create();
			beginEditCP(vp);
				vp->setCamera                  (_camera);
				vp->setRoot                    (_internalRoot);
				vp->setSize                    (0,0, 1,1);
				vp->setBackground              (bg);
			endEditCP(vp);

			beginEditCP(_win);
				_win->addPort(vp);
			endEditCP(_win);
		}

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


    glEnable( GL_NORMALIZE );
    glEnable( GL_DEPTH_TEST );

	// OSG

	// 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();
	headlight = DirectionalLight::create();

	beginEditCP(dlight);
	dlight->setCore( headlight );
	endEditCP(dlight);
	
	beginEditCP(headlight);
	headlight->setAmbient( .3, .3, .3, 1 );
	headlight->setDiffuse( 1, 1, 1, 1 );
	headlight->setSpecular( 1, 1, 1, 1 );
	headlight->setDirection(0,0,1);
	headlight->setBeacon( b1n);
	endEditCP(headlight);

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

	// Create the scene

    NodePtr sroot = Node::create();

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

    GroupPtr gr = Group::create();

    beginEditCP(sroot);
    sroot->setCore(gr);
    endEditCP(sroot);

    // a transform to move it around
 
    NodePtr tnode = Node::create();
    tr = Transform::create();
    beginEditCP(tnode);
    tnode->setCore( tr );
    endEditCP(tnode);

    beginEditCP(sroot);
    sroot->addChild(tnode);
    endEditCP(sroot);

    // the billboard
 
    NodePtr bnode = Node::create();
    bill = Billboard::create();
    beginEditCP(bnode);
    bnode->setCore( bill );
    endEditCP(bnode);

    beginEditCP(tnode);
    tnode->addChild(bnode);
    endEditCP(tnode);
  
    // a geometry to billboard
    NodePtr geo = makeTorus( .2, 1, 16, 3);

    beginEditCP(bnode);
    bnode->addChild( geo );
    endEditCP(bnode);
 
    // a geometry to lead the render action somewhere else
    geo = makePlane( 2, 2, 2, 2 );

    beginEditCP(sroot);
    sroot->addChild( geo );
    endEditCP(sroot);

    dlight->updateVolume();
        
    Vec3f min,max;
    dlight->getVolume().getBounds( min, max );
	
    std::cout << "Volume: from " << min << " to " << max << std::endl;

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

	// Camera
	PerspectiveCameraPtr cam = PerspectiveCamera::create();

	cam->setBeacon( b1n );
	cam->setFov( deg2rad( 60 ) );
	cam->setNear( .1 );
	cam->setFar( 20. );

	// Background
	GradientBackgroundPtr bkgnd = GradientBackground::create();
	
	bkgnd->addLine( Color3f( 0,0,0 ), 0 );
	bkgnd->addLine( Color3f( .5,.5,0 ), 0.5 );
	bkgnd->addLine( Color3f( .7,.7,1 ), 0.5 );
	bkgnd->addLine( Color3f( 0,0,1 ), 1 );

	// Viewport

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

	// Actions
	
	dact = DrawAction::create();
	ract = RenderAction::create();

	// tball

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

	tball.setMode( Trackball::OSGObject );
	tball.setStartPosition( pos, true );
	tball.setSum( true );
	tball.setTranslationMode( Trackball::OSGFree );
    tball.setTranslationScale(10.);
	// run...
	
	glutMainLoop();
	
    return 0;
}
Esempio n. 3
0
int main(int argc, char **argv)
{
    for(int i = 0; i < argc; i++)
    {
        std::cout << "Param " << i << ":" << argv[i] << std::endl;
    }

    OSG::osgInit(argc, argv);

    const char    *inFileName = "tie.bin";

    if(argc > 1)
        inFileName = argv[1];

    std::ifstream in(inFileName, std::ios::binary);
    if(!in)
    {
        std::cerr <<
            "ERROR: Cannot open file " <<
            inFileName <<
            "" <<
            std::endl;
        return -1;
    }

    OSG::BINLoader loader(in);
    loader.read();
    in.close();
    std::cout <<
        "MAIN: " <<
        getContainerId(loader.getRootNode()) <<
        " is root" <<
        std::endl;

    // 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_NORMALIZE);
    glDisable(GL_LIGHTING);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glClearColor(0.5, 0.5, 0.5, 1.);

    // OSG
    //    OSGSceneFileHandler::the().print();
    //    FhsFile::touch();
    // 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();
    headlight = DirectionalLight::create();

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

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

    // 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     fileRoot = Node::create();

    //  OSGActivateColMatPtr colMat = OSGActivateColMat::create();
    GroupPtr    gr = Group::create();

    beginEditCP(fileRoot);

    //  fileRoot->setCore(colMat);
    fileRoot->setCore(gr);
    endEditCP(fileRoot);

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

    //   for(UInt32 numFiles = 1; numFiles < argc; numFiles++)
    //    {
    //       file = SceneFileHandler::the().read(argv[1]);
    file = loader.getRootNode();
    beginEditCP(fileRoot);
    fileRoot->addChild(file);
    fileRoot->invalidateVolume();
    endEditCP(fileRoot);

    //    }
    dlight->updateVolume();

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

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

    //std::cerr << "Tree: " << std::endl;
    //root->dump();
    // Camera
    PerspectiveCameraPtr    cam = PerspectiveCamera::create();

    cam->setBeacon(b1n);
    cam->setFov(deg2rad(60));
    cam->setNear(1.);
    cam->setFar(100000.);

    // Background
    GradientBackgroundPtr   bkgnd = GradientBackground::create();

    bkgnd->addLine(Color3f(0, 0, 0), 0);
    bkgnd->addLine(Color3f(.5, .5, 0), 0.5);
    bkgnd->addLine(Color3f(.7, .7, 1), 0.5);
    bkgnd->addLine(Color3f(0, 0, 1), 1);

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

    // Actions
    dact = DrawAction::create();
    ract = RenderAction::create();

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

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

    // run...
    glutMainLoop();

    return 0;
}