0
m_Program = new GLProgram;
m_Program->initWithFilenames(“shader/gldemo.vsh”, “shader/gldemo.fsh”);
m_Program->link();
//set uniform locations
m_Program->updateUniforms();

glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

//create vbo
glGenBuffers(1, &vertexVBO);
glBindBuffer(GL_ARRAY_BUFFER, vertexVBO);

auto size = Director::getInstance()->getVisibleSize();
float vertercies[] = {
                    0,0,0.5,   //first point 
                   -1, 1,0.5,   
                    -1, -1,0.5,
                    -0.3,0,0.8,   
                    1, 1,0.8,   
                    1, -1,0.8};

float color[] = { 1, 0,0, 1,  1,0,0, 1, 1, 0, 0, 1,
                0, 1,0, 1,  0,1,0, 1, 0, 1, 0, 1};

glBufferData(GL_ARRAY_BUFFER, sizeof(vertercies), vertercies, GL_STATIC_DRAW);
//vertex attribute "a_position"
GLuint positionLocation = glGetAttribLocation(m_Program->getProgram(), "a_position");
glEnableVertexAttribArray(positionLocation);
glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);

//set for color
glGenBuffers(1, &colorVBO);

glBindBuffer(GL_ARRAY_BUFFER, colorVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(color), color, GL_STATIC_DRAW);

GLuint colorLocation = glGetAttribLocation(m_Program->getProgram(), "a_color");
glEnableVertexAttribArray(colorLocation);
glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);

//for safty
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

//show enter image description here

glEnable(GL_DEPTH_TEST);

Open the depth test, but it doesn't work? It can be displayed normally, but I open the depth test, the red triangle Z is set to 0.5, the blue is set to 0.8, but their rendering order has not changed?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
jerryln
  • 3
  • 1
  • Possible duplicate of [Can't get depth testing to work in OpenGL](https://stackoverflow.com/questions/9045843/cant-get-depth-testing-to-work-in-opengl) – neam Apr 29 '18 at 05:39

1 Answers1

0

Multiple things:

  • Enabling depth testing does not mean that the depth testing is set correctly. You have to setup it
  • You have to make sure there is a depth buffer
  • You have to enable depth writing and depth testing when rendering your primitives

With openGL, you typically need to call those functions in order to have depth testing working:

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

and to enable depth writing:

glDepthMask(GL_TRUE);

And just searching for opengl enable depth testing on google gave me some neat results. You should look on the multiple resources google have in its index.

neam
  • 894
  • 9
  • 19