[osg-users] Multitexture: projection a texture on another texture

Ekaterina Fokina ekaterina.fokina at tum.de
Mon Feb 6 05:49:07 PST 2017


Hello dear osg masters,

I am not very expirienced yet with osg and trying to do a simple example of projection a texture on another texture, but somehow it is not working, seems that some settings are wrong. Could you please have a look at the code:
	osg::Geode* geode = new osg::Geode;

	// set up the texture of the base.
	osg::StateSet* stateset = new osg::StateSet();
	osg::Image* image = osgDB::readImageFile("Images/lz.rgb");
	if (image)
	{
		osg::Texture2D* texture = new osg::Texture2D;
		texture->setImage(image);
		stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
	}

	geode->setStateSet(stateset);


	osg::HeightField* grid = new osg::HeightField;
        ...
	geode->addDrawable(new osg::ShapeDrawable(grid));
        
        osg::TexEnv *texenv = new osg::TexEnv;
	texenv->setMode(osg::TexEnv::DECAL);

	int tex_width = 1024;
	int tex_height = 1024;

        //Geometry which has to be projected
	osg::Geode* geode2 = new osg::Geode;
	osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
	vertices->push_back(center);
	vertices->push_back(osg::Vec3(center.x(), center.y() + 10, center.z()));
	vertices->push_back(osg::Vec3(center.x() + 10, center.y() + 10, center.z()));
	vertices->push_back(osg::Vec3(center.x() + 10, center.y(), center.z()));
	osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
	normals->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));

	osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
	colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));//red
        
	osg::ref_ptr<osg::Geometry> quad = new osg::Geometry;
	quad->setVertexArray(vertices.get());
	quad->setNormalArray(normals.get());
	quad->setNormalBinding(osg::Geometry::BIND_OVERALL);
	quad->setColorArray(colors.get());
	quad->setColorBinding(osg::Geometry::BIND_OVERALL);
	quad->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));
	geode2->addDrawable(quad.get());


	osg::Texture2D* texture2 = new osg::Texture2D;
	texture2->setTextureSize(tex_width, tex_height);
	texture2->setInternalFormat(GL_RGBA);
	texture2->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
	texture2->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);

	stateset->setTextureAttributeAndModes(1, texture2, osg::StateAttribute::ON);
	stateset->setTextureAttribute(0, texenv);
	stateset->setTextureAttribute(1, texenv);



	osg::Camera* camera = new osg::Camera;

	float znear = 10;
	float zfar = 30;
	float proj_top = 0.25f*znear;
	float proj_right = 0.5f*znear;
	camera->setProjectionMatrixAsFrustum(-proj_right, proj_right, -proj_top, proj_top, znear, zfar);

	camera->setViewMatrixAsLookAt(osg::Vec3(0.0f, 0.0f, 0.0f) - osg::Vec3(0.0f, 2.0f, 0.0f) * 10, osg::Vec3(0.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 0.0f, 1.0f));
	camera->setViewport(0, 0, tex_width, tex_height);
	//camera->setClearColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.0f)); 
	camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	camera->setRenderOrder(osg::Camera::PRE_RENDER);

	camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
	camera->attach(osg::Camera::COLOR_BUFFER, texture2);
	camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
	camera->addChild(geode2);

	osg::Group* group = new osg::Group;
	group->addChild(geode);
	group->addChild(camera);




Thank you!

Cheers,
Ekaterina

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=70116#70116








More information about the osg-users mailing list