[osg-users] Capturing all visible nodes

Robert Osfield robert.osfield at gmail.com
Wed Jan 23 00:51:53 PST 2019


Hi Issac,

On Wed, 23 Jan 2019 at 01:16, Isaac Wolf <ijwolf8 at gmail.com> wrote:
> Robert, the end goal is to take a picture of an object in real life and apply it as a texture to a 3D model by orienting the geometry in the program to be roughly the same as in the picture.
>
> My plan was to capture all the points currently visible, retrieve their locations on the screen, and then apply those values as texture coordinates to the model. As if the XY screen space is the UV texture space.
>
> I understand that this will result in skewed textures once the model is rotated, but that is just fine for the purposes of the project.
>
> The "only visible points" portion of this is important, because the user could be zoomed in on a model, and the texture should only be applied to that space.

What you describe is texture projection, and just so happens that
OpenGL/OSG support it out of the box, no need to go intersecting the
scene and generating texcoords yourself, it can all be done down on
the GPU automatically for you :-)

What you'll need is an eye linear texgen, osg::TexGenNode.  The
osgspotlight example shows how it's done - look at the
createSpotLightNode() function.

    // create tex gen.

    osg::Vec3 up(0.0f,0.0f,1.0f);
    up = (direction ^ up) ^ direction;
    up.normalize();

    osg::TexGenNode* texgenNode = new osg::TexGenNode;
    texgenNode->setTextureUnit(textureUnit);
    osg::TexGen* texgen = texgenNode->getTexGen();
    texgen->setMode(osg::TexGen::EYE_LINEAR);
    texgen->setPlanesFromMatrix(osg::Matrixd::lookAt(position,
position+direction, up)*
                                osg::Matrixd::perspective(angle,1.0,0.1,100)*
                                osg::Matrixd::translate(1.0,1.0,1.0)*
                                osg::Matrixd::scale(0.5,0.5,0.5));


    group->addChild(texgenNode);

You'll need to learn a bit about texgen and the OSG's positional state
API around it, but once you understand it it should just fall in to
place, you'll be able to easily update the projection of the photo to
change the projection and move the viewer's camera independently.
There are several examples that use TexGenNode so have a look at them.

Cheers,
Robert.


More information about the osg-users mailing list