[osg-users] [OSG 3.6.3 Core Profile GL 3+] Weird clipping behaviour in Fragment Shader

Glenn Waldron gwaldron at gmail.com
Thu Sep 19 07:53:32 PDT 2019


 GL_CLIP_PLANE0 is deprecated in GL3+ and unavailable in CORE profile.

Instead, use GL_CLIP_DISTANCE0, pass your clip plane into your shader in a
uniform, and use it to set gl_ClipDistance[0].

Example:
https://github.com/gwaldron/osgearth/blob/master/src/osgEarth/ClipPlane.glsl


Glenn Waldron / osgEarth


On Thu, Sep 19, 2019 at 3:44 AM Dan johansson <johansson_dan at hotmail.com>
wrote:

> Hi,
>
> New user here, doing a fairly simple program test so far with core profile
> settings.
>
> I'm rendering the program through Qt's QOpenGLWidget, even though i don't
> think this is causing any particular issues.
>
> I'm setting up my Cube model and personal shaders like this
>
>
> Code:
>
> OSGModel::OSGModel()
> {
>     modelPAT_ = new osg::PositionAttitudeTransform;
>     modelGeode_ = CubeCreator::buildCube();
>
>     osg::StateSet* stateset = modelGeode_->getOrCreateStateSet();
>     osg::ref_ptr<osg::Program> shaderProgram = new osg::Program;
>     osg::Shader* vShader = osgDB::readShaderFile(osg::Shader::VERTEX,
> "../VertexShader.glsl");
>     osg::Shader* fShader = osgDB::readShaderFile(osg::Shader::FRAGMENT,
> "../FragmentShader.glsl");
>
>     if (!vShader && !fShader)
>     {
>         osg::notify(osg::NOTICE) << "Effect Not Supported !" << std::endl;
>     }
>     shaderProgram->addShader(vShader);
>     shaderProgram->addShader(fShader);
>
>     //wireframe mode
>     //activateWireframeMode(stateset);
>
>     stateset->setAttributeAndModes(shaderProgram,
> osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
>     stateset->setMode(GL_CLIP_PLANE0, osg::StateAttribute::ON);
>     stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
>
>     auto lightColor = osg::Vec3f(0.5f, 0.2f, 0.35f);
>     auto lightSourcePosition = osg::Vec3f(1.2f, 1.0f, 2.0f);
>     // add uniforms
>
>     osg::ref_ptr<osg::Uniform> uniformLightColor = new
> osg::Uniform(osg::Uniform::FLOAT_VEC3, "UlightColor");
>     osg::ref_ptr<osg::Uniform> uniformLightSourcePosition = new
> osg::Uniform(osg::Uniform::FLOAT_VEC3, "UlightPosition");
>
>     uniformLightColor->setElement(0, lightColor);
>     uniformLightSourcePosition->setElement(0, lightSourcePosition);
>
>     stateset->addUniform(uniformLightColor.get());
>     stateset->addUniform(uniformLightSourcePosition.get());
>     //adds normal vectors to the model
>     modelGeode_->accept(geodeNormals); //Node Visitor
>     modelGeode_->addChild(geodeNormals.toNormalsGeomtery(osg::Vec4(1.0f,
> 0.0f, 0.0f, 1.0f)));
>
>     modelPAT_->addChild(modelGeode_);
> }
>
>
>
>
> where i activate the first clip plane GL_CLIP_PLANE0 for the shader. The
> uniforms are parsed for future implementation of lightning effects.
>
> For experiment, i wanted to pass the vertex position for the fragment
> shader but for some reason the model is clipped at x = 0. This can be
> removed by not enable CLIP_PLANE0 but i don't understand why this effect
> occurs as i havent specified the clip plane and do no calculation for
> gl_ClipDistance[0].
>
>
>
>
>
>
> Following is my vertex and fragment shader
>
>
> Code:
>
> #version 330 core
>
> out vec4 ourPosition;
> out vec3 ourNormal;
>
> //lightsource uniforms
> uniform vec3 UlightColor;
> uniform vec3 UlightPosition;
>
> out vec3 lightResult;
>
> void main (void)
> {
>     ourNormal   = gl_NormalMatrix * gl_Normal;
>     ourPosition = gl_ModelViewMatrix *  gl_Vertex;
>     gl_Position = gl_ProjectionMatrix  * ourPosition;
> }
>
>
>
>
>
> Code:
>
> #version 330 core
>
> //object color
> vec3 objectColor = vec3(1.0f, 0.5f, 0.31f);
> in vec4 ourPosition;
>
> void main(void)
> {
>     gl_FragColor = vec4(ourPosition.xyz, 1.0f);
> }
>
>
>
>
> Any leads to why this is happening and what i can do to solve it would be
> appreciated. Thanks for reading, i will supply more details if necessary.
>
> Best regards Dan
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=76676#76676
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20190919/efd58a58/attachment.html>


More information about the osg-users mailing list