2

I'm using openGL with GLFW and GLEW. I'm rendering everything using shaders but it seems like the depth buffer doesn't work. The shaders I'm using for 3D rendering are:

Vertex Shader

#version 410\n
layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec2 vt
uniform mat4 view, proj, model;
out vec2 texture_coordinates;
void main() {
    texture_coordinates = vt;
    gl_Position = proj * view * model* vec4(vertex_position, 1.0);
};

Fragment Shader

#version 410\n
in vec2 texture_coordinates;
uniform sampler2D basic_texture;
out vec4 frag_colour;
void main() {
    vec4 texel = texture(basic_texture, vec2(texture_coordinates.x, 1 - texture_coordinates.y));
    frag_colour = texel;
};

and I'm also enabling the depth buffer and cull face

glEnable(GL_DEPTH_BUFFER);
glDepthFunc(GL_NEVER);

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);

This is how it is looking:

2 Answers2

2

You're not enabling depth testing. Change glEnable(GL_DEPTH_BUFFER); into glEnable(GL_DEPTH_TEST); This error could have been detected using glGetError().

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
  • @LuizAugustoWendt: Also, you used `glDepthFunc(GL_NEVER)`, which means the depth test will *never pass*. So all fragments ought to be culled. The only reason something appeared on the screen is because you enabled the wrong thing. – Nicol Bolas Jul 01 '16 at 13:44
  • Yep, I was using never just to test if it the depth test was working :~ – Luiz Augusto Wendt Jul 01 '16 at 15:01
2

Like SurvivalMachine said, change GL_DEPTH_BUFFER to GL_DEPTH_TEST. And also make sure that in your main loop you are calling glClear(GL_DEPTH_BUFFER_BIT) before any drawing commands.