// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv)
{
	cout << "Testing screen aligned text generation and file-IO." << endl;
	
    if(argc == 1)
    {
        FFATAL(("Need *.txf or *.ttf font file\n"));
        return -1;
    }

    // OSG init
    osgInit(argc, argv);

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

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

    PathHandler paths;

    paths.push_backPath(".");

    //paths.push_backPath("/home/elmi/wrk/development/texFont");
    // create the scene
    FontStyle   *fontStyle = FontStyleFactory::the().create( paths, argv[1], 
															 0.25);
	cout << fontStyle->getFontName() << endl;

    // create the scene
    NodePtr pTorus = makeTorus( 0.02, 0.55, 16, 16);
	ChunkMaterialPtr mat = ChunkMaterial::create();

    MaterialChunkPtr pmc = MaterialChunk::create();
    pmc->setDiffuse( Color4f( 1,0,0,0.5 ) );
    pmc->setAmbient( Color4f( 0,1,0,0.5 ) );
    pmc->setSpecular( Color4f( 0,0,1,0.5 ) );
	pmc->setLit( true );
    pmc->setShininess( 20 );

	beginEditCP(mat);
	{
	mat->addChunk( pmc );
	}
	endEditCP(mat);

    scene = Node::create();
	GroupPtr group = Group::create();
    beginEditCP( scene );
    {
        scene->setCore( group );
	}
    endEditCP( scene );

	SharedFontStylePtr sfs = SharedFontStyle::create(); 
	sfs->setContainedFontStyle( fontStyle );

	for( int x=0; x<100; x += 20 )
	{
		for( int y=0; y<100; y += 20 )
		{
			for( int z=0; z<100; z += 20 )
			{
				ScreenAlignedTextPtr scaText = ScreenAlignedText::create();
				if( scaText == NullFC )
				{
					exit (2);
				}
				SharedFontStyleWrapperPtr pFSWrapper = SharedFontStyleWrapper::create();
				pFSWrapper->setFStyleContainer( sfs );
				ostringstream cString;
				cString << '(' 
						<< x 
						<< ',' 
						<< y
						<< ','
						<< z
						<< ')'
						<< endl;

				beginEditCP(scaText);
				{
					scaText->setPosition( Vec3f ( x, y, z ) );
					scaText->setFont( pFSWrapper );
					scaText->setVerticalLineDistance( 0.20 );
					scaText->setAlignment( 0 );
					scaText->editMFText()->push_back( cString.str() );
					scaText->setMaterial( mat );
				}
				endEditCP(scaText);
				NodePtr pTextNode = Node::create();
				beginEditCP( pTextNode );
				{
					pTextNode->setCore( scaText );
				}
				beginEditCP( scene );
				{
					scene->addChild( pTextNode );
				}
				endEditCP( scene );	
				cout << "Erzeugt : " 
					 << cString.str() 
					 << endl;
			}
		}
    }

	//NodePtr pCopied = scene;

#if 1
	{
		ofstream outFileStream( "/tmp/text.osg" );

		if( !outFileStream )
		{
			cerr << "cannot open file" << endl;
			exit(2);
		}
		//FILE *pFile = fopen( "isolinien.osg","w" );
		//BINWriter writer( pFile );
		OSGWriter writer( outFileStream );
		writer.write( scene );
	}
#endif

#if 0
	VRMLWriteAction *pWriter = VRMLWriteAction::create();
	scene->dump();
	pWriter->open("allesscheisse.wrl");
	pWriter->write( scene);
	pWriter->close();
	delete pWriter;
#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();

    // GLUT main loop
    glutMainLoop();

    return 0;
}
Beispiel #2
0
// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv)
{
    // OSG init
    osgInit(argc, argv);

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

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

    PathHandler paths;
    paths.push_backPath(".");

    // create the scene
    // build a special txf-font with only a little set of characters
    FontStyle   *fontStyle = FontStyleFactory::the().create(paths, argv[1], 1);
    assert(fontStyle);
    //((TTFontStyle *) fontStyle)->createTXFMap((UChar8 *) ". fps0123456789");
    ((TTFontStyle *) fontStyle)->createTXFMap();

    // write it somewhere
#if 1
    std::ofstream    target;
    target.open("statistics.txf");
    fontStyle->dump(target);
    target.close();
#else
    ostrstream  target;
    fontStyle->dump(target);
#endif
#if 1
    std::ifstream    source;
    source.open("statistics.txf");
#else
#if 0
    // Hack, to get the stuff into memory
    int     bufSize = 100000;
    char    *buffer = new char[bufSize];
    int     numRead;
    FILE    *in = fopen("statistics.txf", "r");

    numRead = fread(buffer, 1, bufSize, in);

    fclose(in);

    istrstream  source(buffer, numRead);
#else
    istrstream  source(target.str(), target.pcount());
#endif
#endif
    TXFFont     *font = new TXFFont("test.txf", source);
    font->initFont();

    fontText.setSize(1);
    font->createInstance(&fontText);

    fontText.setJustifyMajor(MIDDLE_JT);

    lineVec.push_back("0000.00 fps");

    // TXF-Style Texture+Geometry
    n = Node::create();
    txfGeo = Geometry::create();

    ImagePtr  pTxfImg = Image::create();

    if(fontText.fillTXFGeo(*txfGeo, true, lineVec))
    {
        fontText.fillTXFImage(pTxfImg);

        SimpleTexturedMaterialPtr   mat = SimpleTexturedMaterial::create();
        beginEditCP(mat);
        {
            mat->setImage(pTxfImg);
        }

        endEditCP(mat);
        txfGeo->setMaterial(mat);
        beginEditCP(n, Node::CoreFieldMask);
        {
            n->setCore(txfGeo);
        }
    }

    scene = Node::create();

    // add a transformation to make it move
    trans = Transform::create();
    beginEditCP(scene, Node::CoreFieldMask | Node::ChildrenFieldMask);
    {
        scene->setCore(trans);
        scene->addChild(n);
    }

    endEditCP(scene, Node::CoreFieldMask | Node::ChildrenFieldMask);

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

    // GLUT main loop
    glutMainLoop();

    return 0;
}