Luiz Augusto Wendt
Luiz Augusto Wendt

Reputation: 23

Depth Buffer seems to not work - OpenGL Shader

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:

Upvotes: 2

Views: 1371

Answers (2)

a useless dragon
a useless dragon

Reputation: 67

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.

Upvotes: 2

SurvivalMachine
SurvivalMachine

Reputation: 8356

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

Upvotes: 2

Related Questions