【问题标题】:Open the depth test, but it doesn't work?开启深度测试,还是不行?
【发布时间】:2018-10-09 13:16:17
【问题描述】:
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);

//显示

glEnable(GL_DEPTH_TEST);

打开深度测试,还是不行? 可以正常显示,但是我打开深度测试,红色三角Z设置为0.5,蓝色设置为0.8,但它们的渲染顺序没有变化?

【问题讨论】:

标签: opengl-es-2.0 depth-testing


【解决方案1】:

多件事:

  • 启用深度测试并不意味着深度测试设置正确。你必须设置它
  • 您必须确保有深度缓冲区
  • 渲染图元时必须启用深度写入和深度测试

使用 openGL,您通常需要调用这些函数才能进行深度测试:

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

并启用深度写入:

glDepthMask(GL_TRUE);

在谷歌上搜索opengl enable depth testing 给了我一些很好的结果。您应该查看 google 在其索引中的多种资源。

【讨论】:

  • 哦。你说得对。另一个可能的问题是缺少深度缓冲区。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-06
相关资源
最近更新 更多