[osg-users] ClipNode Opposite Behavior

Erik Hensens ehensens at hunter.com
Thu Jul 23 14:58:04 PDT 2015


Thank you both for your input! I'm learning a lot.

I'm new to fragment shaders, so I think that maybe I'm doing something obviously wrong...

It seems that what I'm doing below should be pretty simple, but it doesn't work. When I reverse the if (fDist < rad) to if (fDist > rad) then the entire thing disappears. Otherwise (the way it is written below), nothing disappears.

Thanks in advance for your help!


Code:

// The group node that contains the test geode
osg::ref_ptr<osg::Group> pTestGroup = new osg::Group;

// The test geode
osg::ref_ptr<osg::Geode> pTestGeode = new osg::Geode;

// The geode's geometry
osg::ref_ptr<osg::Geometry> pTestGeometry = new osg::Geometry;

// The geometry's test vertices
osg::ref_ptr<osg::Vec3Array> pTestVerticesArray = new osg::Vec3Array;
pTestVerticesArray->push_back(osg::Vec3(0.0f, -2.0f, -2.0f));
pTestVerticesArray->push_back(osg::Vec3(0.0f, -2.0f, 2.0f));
pTestVerticesArray->push_back(osg::Vec3(0.0f, 2.0f, 2.0f));
pTestVerticesArray->push_back(osg::Vec3(0.0f, 2.0f, -2.0f));

// Set this as the vertex array
pTestGeometry->setVertexArray(pTestVerticesArray);

// Define the primitive set
osg::ref_ptr<osg::DrawArrays> pTestDrawArrays = new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, static_cast<GLsizei>(pTestVerticesArray->size()));
pTestGeometry->addPrimitiveSet(pTestDrawArrays);

// Normalize
osg::ref_ptr<osg::StateSet> pTestStateSet = pTestGeometry->getOrCreateStateSet();
pTestStateSet->setMode(GL_NORMALIZE, osg::StateAttribute::ON);

// Add the test geometry to the geode
pTestGeode->addDrawable(pTestGeometry);

// Add the geode to the group
pTestGroup->addChild(pTestGeode);

// The fragment shader program source code
std::string szFragSource(
"uniform vec3 center;\n"
"uniform float rad;\n"
"void main()\n"
"{\n"
"float fDistX = gl_FragCoord.x - center.x;\n"
"float fDistY = gl_FragCoord.y - center.y;\n"
"float fDistZ = gl_FragCoord.z - center.z;\n"
"float fDist = sqrt(fDistX * fDistX + fDistY * fDistY + fDistZ * fDistZ);\n"
"if (fDist < rad) discard;\n"
"}\n"
);

// Create the fragment shader
osg::ref_ptr<osg::Shader> pFragmentShader = new osg::Shader(osg::Shader::FRAGMENT, szFragSource.c_str());

// Create the fragment program
osg::ref_ptr<osg::Program> pProgram = new osg::Program;

// Add the shader to the program
pProgram->addShader(pFragmentShader);

// Get the group node's stateset
osg::StateSet *pStateSet = pTestGroup->getOrCreateStateSet();

// The center of the sphere that I want to define the region that is not drawn
osg::Vec3 tCenter(0.0f, 0.0f, 0.0f);

// The radius of the sphere that I want to define the region that is not drawn
float fRadius = 1.0f;

// Add the center and radius uniforms
osg::ref_ptr<osg::Uniform> pCenterUniform = new osg::Uniform("center", tCenter);
osg::ref_ptr<osg::Uniform> pRadiusUniform = new osg::Uniform("rad", fRadius);
pStateSet->addUniform(pCenterUniform);
pStateSet->addUniform(pRadiusUniform);

// Enact this fragment shader on the stateset
pStateSet->setAttributeAndModes(pProgram);





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








More information about the osg-users mailing list