I'm trying to create a GLSL diffuse shader, but I'm getting a problem, some faces looks like they become transparent and the back faces are shown in front, it's really confusing and I wonder how to fix this problem. I guess that the problem is with the direction of the normals, but I'm not sure.
This is my .frag shader code:
#version 330
in vec3 FragPos;
in vec3 Normals;
in vec2 TexCoord;
uniform vec4 AlbedoColor;
out vec4 FragColor;
const int numLights = 1;
uniform vec3 lightPositions[numLights];
uniform vec3 lightColors[numLights];
uniform vec3 viewPos;
uniform vec3 diffuse;
void main()
{
// Perform lighting calculations here
vec3 result = vec3(0);
for (int i = 0; i \< numLights; i++) {
// Diffuse
vec3 norm = normalize(Normals);
vec3 lightDir = normalize(lightPositionsi - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColors[i];
result += diffuse;
}
result = result * AlbedoColor.rgb;
FragColor = vec4(result,1.0);
}
If the problem is with the normals, here's the code that generates the mesh:
new Vertex(-0.5f, -0.5f, -0.5f, new Vector3(-1.0f, -1.0f, -1.0f), new Vector2(0.0f, 0.0f)), // bottom-left-back
new Vertex(-0.5f, 0.5f, -0.5f, new Vector3(-1.0f, 1.0f, -1.0f), new Vector2(0.0f, 1.0f)), // top-left-back
new Vertex( 0.5f, 0.5f, -0.5f, new Vector3( 1.0f, 1.0f, -1.0f), new Vector2(1.0f, 1.0f)), // top-right-back
new Vertex( 0.5f, -0.5f, -0.5f, new Vector3( 1.0f, -1.0f, -1.0f), new Vector2(1.0f, 0.0f)), // bottom-right-back
new Vertex(-0.5f, -0.5f, 0.5f, new Vector3(-1.0f, -1.0f, 1.0f), new Vector2(0.0f, 0.0f)), // bottom-left-front
new Vertex(-0.5f, 0.5f, 0.5f, new Vector3(-1.0f, 1.0f, 1.0f), new Vector2(0.0f, 1.0f)), // top-left-front
new Vertex( 0.5f, 0.5f, 0.5f, new Vector3( 1.0f, 1.0f, 1.0f), new Vector2(1.0f, 1.0f)), // top-right-front
new Vertex( 0.5f, -0.5f, 0.5f, new Vector3( 1.0f, -1.0f, 1.0f), new Vector2(1.0f, 0.0f)) // bottom-right-front
Note that the Normals direction is the second paramether. The vertex position is the first, and the third paramether is the location of the texture (it's not in use for now).
Here's some screenshots of what the current result is right now:
The problem was that I did not enable the Depth Test.
In case someone is also with this problem, if you're using OpenGL in C#, you can do "GL.Enable(EnableCap.DepthTest);" And it's going to work fine.
Thank you @user253751 for fixing my problem!