PDA

View Full Version : OpenGL shader on MESA if statement question



ASUSMASTER
11-03-2015, 07:58 AM
Dear OpenGL community,

I am new in the openGL development world and I am facing some inexplicable ( for me ) issues.

We have shader :



void main()
{
if ( discarded == 1 )
discard;

const float reflectFactor = 0.5;
vec4 fragmentColor;

if ( clippingPlaneCount==2 && (clippingPlane1.x>outposition.x || clippingPlane1.y>outposition.y || clippingPlane2.x<outposition.x || clippingPlane2.y<outposition.y) )
discard;

if ( activeCuttingPlane == 1 && isCuttedByPlane() > 0.5 )
discard;
adaptedNormal = outnormal;
//if ( dot (gl_Normal,outnormal) <0.0 ) adaptedNormal = vec3(-outnormal.x,-outnormal.y,-outnormal.z);
if ( meridionalView == 0.0 )
{
// Reverse normal if back facing
if ( !gl_FrontFacing )
adaptedNormal = -outnormal;
}
...
}



We noticed that when using Mesa we get some "black" surface in the back of the geometry.
We found that the reason was :
// Reverse normal if back facing
if ( !gl_FrontFacing )
adaptedNormal = -outnormal;

Here if we change to :
// Reverse normal if back facing
if ( gl_FrontFacing == false )
adaptedNormal = -outnormal;

I work fine.

Can you tell me why the if statement with ! , where gl_FrontFacing is a bool is not interpreted as in C ?

It might be a noob question but I developed in C, java and C++ and for me this should be ok ...

Note : This code work well on native openGL machine. Mesa is used on remote desktop or when the machine doesn't support OpenGL 2.1 or higher.

Regards

Dark Photon
11-04-2015, 06:31 AM
gl_FrontFacing is derived from the vertex winding order that you've told OpenGL represents the front of a triangle (with glFrontFace()). Is it possible you have this set backwards for your geometry?

In any case, you might also try:


vec3 normal = normalize(gl_NormalMatrix * gl_Normal);

if ( normal.z < 0 )
normal = -normal;


Note here I'm not using our outNormal because I don't know what space this is in or where it comes from. It also concerns me that I see this being a fragment shader and this value isn't being normalized.

ASUSMASTER
11-05-2015, 09:12 AM
Hi Dark Photon,

Thanks for your "work arround" and your quick reply,
We already changed our code a bit, we where passing un-needed paramters.

But my question was more 'technical'.

Why this doesn't work :


if ( !gl_FrontFacing )
adaptedNormal = -outnormal;


And this work fine :


if ( gl_FrontFacing == false )
adaptedNormal = -outnormal;


Regards