示例#1
0
ChunkMaterialPtr vtkOsgConverter::CreateMaterial(bool lit, bool hasTexCoords)
{
	if (_verbose)
		std::cout << "Start CreateMaterial()" << std::endl;

	vtkProperty* prop = _actor->GetProperty();
	double* diffuseColor = prop->GetDiffuseColor();
	double* ambientColor = prop->GetAmbientColor();
	double* specularColor = prop->GetSpecularColor();
	double specularPower = prop->GetSpecularPower();

	double diffuse = prop->GetDiffuse();
	double ambient = prop->GetAmbient();
	double specular = prop->GetSpecular();
	double opacity = prop->GetOpacity();

	float pointSize = prop->GetPointSize();
	float lineWidth = prop->GetLineWidth();
	// int lineStipplePattern = prop->GetLineStipplePattern();

	int representation = prop->GetRepresentation();

	if (_verbose)
	{
		std::cout << "    Colors:" << std::endl;
		std::cout << "      diffuse " << diffuse << " * " << diffuseColor[0] << " " <<
		diffuseColor[1] << " " << diffuseColor[2] << std::endl;
		std::cout << "      ambient " << ambient << " * " << ambientColor[0] << " " <<
		ambientColor[1] << " " << ambientColor[2] << std::endl;
		std::cout << "      specular " << specular << " * " << specularColor[0] << " " <<
		specularColor[1] << " " << specularColor[2] << std::endl;
	}

	PolygonChunkPtr polygonChunk = PolygonChunk::create();
	beginEditCP(polygonChunk);
	{
		if (representation == VTK_SURFACE)
		{
			polygonChunk->setFrontMode(GL_FILL);
			polygonChunk->setBackMode(GL_FILL);
		}
		else if (representation == VTK_WIREFRAME)
		{
			polygonChunk->setFrontMode(GL_LINE);
			polygonChunk->setBackMode(GL_LINE);
		}
		else
		{
			polygonChunk->setFrontMode(GL_POINT);
			polygonChunk->setBackMode(GL_POINT);
		}
	} endEditCP(polygonChunk);

	MaterialChunkPtr osgMaterialChunk = MaterialChunk::create();
	beginEditCP(osgMaterialChunk);
	{
		osgMaterialChunk->setDiffuse(Color4f(diffuseColor[0] * diffuse, diffuseColor[1] *
		                                     diffuse, diffuseColor[2] * diffuse, opacity));
		osgMaterialChunk->setSpecular(Color4f(specularColor[0] * specular,
		                                      specularColor[1] * specular,
		                                      specularColor[2] * specular, 1.0));
		osgMaterialChunk->setAmbient(Color4f(ambientColor[0] * ambient, ambientColor[1] *
		                                     ambient, ambientColor[2] * ambient, opacity));
		osgMaterialChunk->setShininess(specularPower);

		//if(opacity < 1.0)
		//{
		// osgMaterialChunk->setColorMaterial(GL_AMBIENT); // HACK: Opacity does not work with GL_AMBIENT_AND_DIFFUSE
		//osgMaterialChunk->setTransparency(1.0f - opacity);
		//}
		//else
		osgMaterialChunk->setColorMaterial(GL_AMBIENT_AND_DIFFUSE);

		// On objects consisting only of points or lines, dont lit
		if(!lit)
			osgMaterialChunk->setLit(false);
	} endEditCP(osgMaterialChunk);

	ChunkMaterialPtr osgChunkMaterial = ChunkMaterial::create();
	beginEditCP(osgChunkMaterial);
	{
		osgChunkMaterial->addChunk(osgMaterialChunk);
		osgChunkMaterial->addChunk(TwoSidedLightingChunk::create());
		osgChunkMaterial->addChunk(polygonChunk);

		if(pointSize > 1.0f)
		{
			PointChunkPtr pointChunk = PointChunk::create();
			pointChunk->setSize(pointSize);
			osgChunkMaterial->addChunk(pointChunk);
		}

		if(lineWidth > 1.0f)
		{
			LineChunkPtr lineChunk = LineChunk::create();
			lineChunk->setWidth(lineWidth);
			osgChunkMaterial->addChunk(lineChunk);
		}

		// TEXTURE
		if (hasTexCoords)
		{
			vtkTexture* vtkTexture = _actor->GetTexture();
			if (vtkTexture)
			{
				TextureChunkPtr osgTextureChunk = NullFC;
				osgTextureChunk = CreateTexture(vtkTexture);

				if(osgTextureChunk != NullFC)
				{
					if (_verbose)
						std::cout << "    Add TextureChunk" << std::endl;
					osgChunkMaterial->addChunk(osgTextureChunk);
				}

				// Per default EnvMode is set to GL_REPLACE which does not lit the surface
				beginEditCP(osgTextureChunk);
				osgTextureChunk->setEnvMode(GL_MODULATE);
				endEditCP(osgTextureChunk);
			}
		}
	} endEditCP(osgChunkMaterial);

	if (_verbose)
		std::cout << "End CreateMaterial()" << std::endl;

	return osgChunkMaterial;
}
int main (int argc, char **argv)
{
    osgInit(argc,argv);

    // GLUT init
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);

    int id=glutCreateWindow("OpenSG");

    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    GLUTWindowPtr gwin=GLUTWindow::create();
    gwin->setId(id);
    gwin->init();

    // create the scene
    NodePtr scene = Node::create();
    beginEditCP(scene);
        scene->setCore(Group::create());
    endEditCP(scene);

    // create the SimpleSceneManager helper
    _mgr = new SimpleSceneManager;

    // tell the manager what to manage
    _mgr->setWindow(gwin );
    _mgr->setRoot  (scene);

    // create the geometry.
    NodePtr plane = makePlane( 1, 1, 2, 2 );
    NodePtr torus = makeTorus( .2, 1, 16, 8 );

    GeometryPtr plane_geo, torus_geo;
    plane_geo = GeometryPtr::dcast(plane->getCore());
    torus_geo = GeometryPtr::dcast(torus->getCore());

    PolygonChunkPtr pchunk = PolygonChunk::create();
    beginEditCP(pchunk);
        pchunk->setFrontMode(GL_LINE);
        pchunk->setBackMode(GL_LINE);
        pchunk->setOffsetFactor(-1.0);
        pchunk->setOffsetLine(true);
    endEditCP(pchunk);

    // create materials for the plane.
    SimpleMaterialPtr pm1 = SimpleMaterial::create();
    beginEditCP(pm1);
        pm1->setDiffuse( Color3f( 0,1,0 ) );
        pm1->setAmbient( Color3f( 0,1,0 ) );
        pm1->setSpecular( Color3f( 0,0,0 ) );
    endEditCP(pm1);

    SimpleMaterialPtr pm2 = SimpleMaterial::create();
    beginEditCP(pm2);
        pm2->setDiffuse( Color3f( 1,0,0 ) );
        pm2->setAmbient( Color3f( 1,0,0 ) );
        pm2->setSpecular( Color3f( 0,0,0 ) );
        pm2->addChunk(pchunk);
    endEditCP(pm2);

    MultiPassMaterialPtr mppm = MultiPassMaterial::create();
    beginEditCP(mppm);
        mppm->addMaterial(pm1);
        mppm->addMaterial(pm2);
    endEditCP(mppm);

    plane_geo->setMaterial(mppm);

    // create materials for the torus.
    SimpleMaterialPtr tm1 = SimpleMaterial::create();
    beginEditCP(tm1);
        tm1->setDiffuse( Color3f( 0,0,1 ) );
        tm1->setAmbient( Color3f( 0,0,1 ) );
        tm1->setTransparency(0.6);
    endEditCP(tm1);

    SimpleMaterialPtr tm2 = SimpleMaterial::create();
    beginEditCP(tm2);
        tm2->setDiffuse( Color3f( 1,0,0 ) );
        tm2->setAmbient( Color3f( 1,0,0 ) );
        tm2->setSpecular( Color3f( 0,0,0 ) );
        tm2->addChunk(pchunk);
    endEditCP(tm2);

    MultiPassMaterialPtr mptm = MultiPassMaterial::create();
    beginEditCP(mptm);
        mptm->addMaterial(tm1);
        mptm->addMaterial(tm2);
    endEditCP(mptm);

    torus_geo->setMaterial( mptm );

    beginEditCP(scene);
        scene->addChild(plane);
        scene->addChild(torus);
    endEditCP(scene);

    // show the whole scene
    _mgr->showAll();
    
    // GLUT main loop
    glutMainLoop();

    return 0;
}
示例#3
0
int main( int argc, char *argv[] )
{
    osgInit(argc, argv);

    glutInit(&argc, argv);
    glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutCreateWindow("OpenSG");
    glutKeyboardFunc(key);
    // glutReshapeFunc(resize);
    glutDisplayFunc(display);
    // glutMouseFunc(mouse);
    // glutMotionFunc(motion);

    glutIdleFunc(display);

    pImage = Image::create();

    // create the dummy structures

    // the window is needed for the chunks that access GLObjects

    win = GLUTWindow::create();
    win->frameInit(); // test for preliminary calls not messing up GLexts
    win->init();
    
    dact = DrawAction::create();
    dact->setWindow(get_pointer(win));

    win->init();

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 60, 1, 0.1, 10 );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    gluLookAt( 3, 3, 3,  0, 0, 0,   0, 1, 0 );

    glEnable( GL_DEPTH_TEST );
    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );
    glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );

    dlid = glGenLists( 1 );
    glNewList( dlid, GL_COMPILE );
    glutSolidSphere( .8, 8, 8 );
    glEndList();

    dlid2 = glGenLists( 1 );
    glNewList( dlid2, GL_COMPILE );
    glBegin( GL_POLYGON );
    glNormal3f( 0, 1, 0 );
    glColor3f( 1, 1, 1 );
    glTexCoord2f( 0, 0 );
    glVertex3f( -1.5, -1, -1.5 );
    glTexCoord2f( 2, 0 );
    glVertex3f(  1.5, -1, -1.5 );
    glTexCoord2f( 2, 2 );
    glVertex3f(  1.5, -1,  1.5 );
    glTexCoord2f( 0, 2 );
    glVertex3f( -1.5, -1,  1.5 );
    glEnd();
    glEndList();


    Matrix m;

    tchunk1 = TransformChunk::create();
    m.setTranslate( 0, 1, 0 );
    tchunk1->setMatrix( m );

    tchunk2 = TransformChunk::create();
    tchunk2->setMatrix( Matrix::identity() );


    mchunk1 = MaterialChunk::create();
    mchunk1->setDiffuse( Color4f( 1,0,0,0 ) );
    mchunk1->setAmbient( Color4f( 1,0,0,0 ) );
    mchunk1->setShininess( 20 );

    mchunk2 = MaterialChunk::create();
    mchunk2->setDiffuse( Color4f( 0,1,0,0 ) );
    mchunk2->setAmbient( Color4f( 0,1,0,0 ) );
    mchunk2->setShininess( 50 );

    // Texture chunk

//  UChar8 imgdata[] =
//      {  255,0,0,0,  0,255,0,0,  0,0,255,255,  255,255,255,255 };
    UChar8 imgdata[] =
        {  255,0,0,  255,0,0,  255,0,255,
           255,0,0,  255,0,0,  255,0,255,
           255,255,0,  255,255,0,  255,255,255,
           255,255,0,  255,255,0,  255,255,255, };
//  UChar8 limgdata[] =
//      {  0, 128, 64, 255 };
    pImage->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, imgdata );

    if ( argc > 1 )
        pImage->read( argv[1] );

    xchunk1 = TextureChunk::create();
    beginEditCP(xchunk1);
    xchunk1->setImage( pImage ); // NOTE: the image is NOT copied, the variable
                                 // needs to be kept around as long as the 
                                 // texture is used
    xchunk1->setMinFilter( GL_LINEAR );
    xchunk1->setMagFilter( GL_NEAREST );
    xchunk1->setWrapS( GL_REPEAT );
    xchunk1->setWrapT( GL_REPEAT );
    xchunk1->setEnvMode( GL_REPLACE );
    xchunk1->setEnvColor( Color4f(.5,.5,.5,0) );
    xchunk1->setScale( false );
    
//    xchunk1->setShaderOperation(GL_PASS_THROUGH_NV);
    
    endEditCP(xchunk1);

    xchunk1->imageContentChanged();

    beginEditCP(xchunk1);
    xchunk1->setImage( pImage );
    endEditCP(xchunk1);

    // blend chunk

    blchunk = BlendChunk::create();
#ifdef GL_EXT_blend_color
    blchunk->setSrcFactor( GL_CONSTANT_ALPHA );
    blchunk->setDestFactor( GL_ONE_MINUS_CONSTANT_ALPHA );
#endif
    blchunk->setColor( Color4f( .5,.5,.5,0.1 ) );
    blchunk->setEquation(GL_FUNC_SUBTRACT);

    std::cout << "BlendChunk is trans:" << blchunk->isTransparent() << std::endl;
    
    // texture transform chunk

    txchunk = TextureTransformChunk::create();
    beginEditCP(txchunk);
    txchunk->setMatrix( Matrix(4,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1) );
    endEditCP(txchunk);

    // polygon chunk

    pchunk1 = PolygonChunk::create();
    {
    UInt32 stipple[32] = {
        0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff,
        0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff,
        0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff,
        0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff,
        0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff,
        0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff,
        0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff,
        0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff
        };

    pchunk1->editMFStipple()->clear();
    for ( int i = 0; i < 32; i++ )
        pchunk1->editMFStipple()->push_back( stipple[i] );
    }

    pchunk1->setFrontMode(GL_LINE);
    pchunk1->setBackMode(GL_FILL);
    
    pchunk2 = PolygonChunk::create();
    {
    UInt32 stipple[32] = {
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0x00000000, 0x00000000, 0x00000000, 0x00000000,
        };

    pchunk2->editMFStipple()->clear();
    for ( int i = 0; i < 32; i++ )
        pchunk2->editMFStipple()->push_back( stipple[i] );
    }


    lichunk1 = LineChunk::create();
    lichunk1->setSmooth(true);
    lichunk1->setStipplePattern(0xf0f0);

    lichunk2 = LineChunk::create();
    lichunk2->setStippleRepeat(5);
    lichunk2->setStipplePattern(0xaaaa);

    glutMainLoop();

    return 0;
}