hello.
I’m trying to create a double face shader in opengl, i do test with this shader on a model .
#ifdef GL_ES
precision highp float;
#endif
uniform vec3 CameraModelPosition;
uniform vec3 ColorFront;
uniform vec3 ColorBack;
varying vec3 vLightWeighting;
varying vec3 vNormal;
varying vec3 transformedNormal;
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
void main(void) {
gl_FragColor = vec4(ColorFront.xyz,1.0);
float Dir = dot(transformedNormal,CameraModelPosition);
if(gl_FrontFacing){
// vec4 textureColor = texture2D(uSampler, vec2(vTextureCoord.s,vTextureCoord.t));
// gl_FragColor = vec4(textureColor.rgb, textureColor.a);
gl_FragColor = vec4(1.0, 1.0, 0.0,1.0);
}
else {
gl_FragColor = vec4(1.0, 0.0, 0.0,1.0);
}
}
but this is the result:
how is possible that i look at a triangle that is in my camera space and gl_frontFacing returns false?
using the Dir and test if is >0 works, but when moves the camera some faces disappearing and reappear, there is a lot of instability.
But i think that i’m searching if a face is in front of the camera or not(the gl_Frontfacing is more complex and is related to the winding order), then is correct the Dir, so the formula must be correct , this is my complete shader:
<script id=“Script2” type=“x-shader/x-vertex”>
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat3 uNMatrix;
uniform vec3 uAmbientColor;
uniform vec3 uLightingDirection;
uniform vec3 uLightingDirection2;
uniform vec3 uColor;
//uniform vec3 uDirectionalColor;
varying vec3 vLightWeighting;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
vec3 uDirectionalColor = vec3(1,1,1);
vec3 transformedNormal = uNMatrix * aVertexNormal;
float directionalLightWeighting = max(dot(transformedNormal, uLightingDirection), 0.0) ;
vLightWeighting = uDirectionalColor * directionalLightWeighting * uColor;
float directionalLightWeighting2 = max(dot(transformedNormal, uLightingDirection2), 0.0) ;
vLightWeighting += uDirectionalColor * directionalLightWeighting2 * uColor ;
}
</script>
<script id=“DoubleColorf” type=“x-shader/x-fragment”>
#ifdef GL_ES
precision highp float;
#endif
uniform vec3 CameraModelPosition;
uniform vec3 ColorFront;
uniform vec3 ColorBack;
varying vec2 vTextureCoord;
varying vec3 vLightWeighting;
varying vec3 vNormal;
varying vec3 transformedNormal;
void main(void) {
float Dir = dot(transformedNormal,CameraModelPosition);
//float Dir = -10.0;
if(Dir > (CameraModelPosition.z * 1.0)-100.0){
//gl_FragColor = vec4(1.0,0.0,0.0,1.0);
gl_FragColor = vec4(ColorFront.xyz,1.0);
}
else {
gl_FragColor = vec4(ColorBack.xyz,1.0);
}
}
and this is my formula :
float Dir = dot(transformedNormal,CameraModelPosition);
i trying
if(Dir > (CameraModelPosition.z -DELTA)
for correct, but how big must be the delta?
is corrct put the z of camera in the test?
thanks.
thanks.