How to check for backface in fragment shader

I try to implement two side lighting by first check if the face is back facing.
by

checking the dot product between view vector (0,0,-1) and normal vector (in viewspace)

vec3 viewVector = vec3(0,0,-1)-vec3(0,0,0);
float dotProd = dot(viewVector,normal);

vec4 finalColor;
if(dotProd < 0){
	pointLightByRadius(normal,eyeNorm,lightVec);
	finalColor = diffuseTerm + specularTerm;	
}
else{
	pointLightByRadius(-normal,eyeNorm,lightVec);
	finalColor = diffuseTerm + specularTerm;
}

which is correct for most part, except for flat surface that perpendicular to z-axis will keep changing side when turn the camera.

Can someone help me on this ?

Thank in advance

You could just use gl_FrontFacing. That will tell you if the fragment is from a front or back facing triangle.

Thank , It work like a charm.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.