[osg-users] Multitexture: projection a texture on another texture
Julien Valentin
julienvalentin51 at gmail.com
Mon Feb 6 10:14:01 PST 2017
Hi Ekaterina
I don't understand why you would use TexEnv..
Am I correct?
You want to create a texture2 resulting from viewing geode2 from camera perspective,
And then use this result on geode according camera projection.
If it is what you want you will require osg::TexGen instead (see osgshadow as an example on how to set planes eqs according to camera projection) to generate projective texture coordinates.
Further I've never used glTexenv(seams deprecated since gl3) but i think it could be simplier to mix explicily textures in a custom fragment program ..
Hope it helps
Ekaterina wrote:
> 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=70117#70117
More information about the osg-users
mailing list