[osg-users] Capturing all visible nodes

Isaac Wolf ijwolf8 at gmail.com
Thu Jan 24 11:34:40 PST 2019


Rob,

This was a huge help! Thank you so much! A few questions for you though. I have the following code implemented based off examples:


Code:

osg::StateSet* createSpotLightDecoratorState(unsigned int textureUnit)
	{
		osg::StateSet* stateset = new osg::StateSet;

		// set up spot light texture
		osg::Texture2D* texture = new osg::Texture2D();
		texture->setImage(osgDB::readImageFile("numbered_colors.png"));
		texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER);
		texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER);
		texture->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_BORDER);
		stateset->setTextureAttributeAndModes(textureUnit, texture, osg::StateAttribute::ON);

		// set up tex gens
		stateset->setTextureMode(textureUnit, GL_TEXTURE_GEN_S, osg::StateAttribute::ON);
		stateset->setTextureMode(textureUnit, GL_TEXTURE_GEN_T, osg::StateAttribute::ON);
		stateset->setTextureMode(textureUnit, GL_TEXTURE_GEN_R, osg::StateAttribute::ON);
		stateset->setTextureMode(textureUnit, GL_TEXTURE_GEN_Q, osg::StateAttribute::ON);
		return stateset;
	}

osg::Node* createSpotLightNode(osg::ref_ptr<osg::Camera> camera, unsigned int textureUnit)
	{
		osg::Group* group = new osg::Group;
		osg::Vec3f eye, center, up;

		camera->getViewMatrixAsLookAt(eye, center, up);

		osg::Matrixd viewM = camera->getViewMatrix();
		osg::Matrixd projM = camera->getProjectionMatrix();
		osg::Matrixd windowM = camera->getViewport()->computeWindowMatrix();
		osg::Matrixd worldToScreen = viewM * projM * windowM;

		// create tex gen.
		float scaleVal = 0.5f, translateVal = 1.0f;
		osg::TexGenNode* texgenNode = new osg::TexGenNode;
		texgenNode->setTextureUnit(textureUnit);
		osg::TexGen* texgen = texgenNode->getTexGen();
		texgen->setMode(osg::TexGen::EYE_LINEAR);
		texgen->setPlanesFromMatrix(viewM*projM*
			osg::Matrixd::translate(translateVal, translateVal, translateVal)*
			osg::Matrixd::scale(scaleVal, scaleVal, scaleVal));
		group->addChild(texgenNode);

		return group;
	}





Which is called in the main animation loop by:


Code:

		curGroup->setStateSet(createSpotLightDecoratorState(1));
		curGroup->addChild(createSpotLightNode(camera, 1));




This projects my png on to the body nicely based on the camera location; however, I'd like it to project as if the camera is at some distance x closer to the body. So instead of the texture filling the entire screen, it fills only a certain percentage. I tried just using the setViewMatrix() to move the camera (eye - center) / 2 closer to the body, projecting the texture, then setting the camera back to the original position. But that seems like a very hacky way to do it, and I didn't get exactly the results I wanted!

The second issue I am having is that the portion of the model that is not hit by the projected texture disappears completely. I am assuming this as something to do with the stateset, but I am not sure what to do differently.

Finally, how to I turn off projecting through surfaces? i.e. if I am looking at the front of a cube, it only projects onto the front face, and not onto the back face as well.

Thanks again Robert, I appreciate all the work you put in to answering so many questions on this forum!

Regards,

Isaac

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







More information about the osg-users mailing list