在启动深度测试glEnable(GL_DEPTH_TEST)之后,z值较小的片元如果比z值较大的片元晚绘制,z值较小的片源将会被丢弃,如下:
CELL::matrix4 matRot;
matRot.translate(0,0,-1);-----------Z值较大
CELL::matrix4 MVP1 = matProj*matView*matRot;
glUniformMatrix4fv(_shader._MVP,1,false,MVP1.data());
glVertexAttribPointer(_shader._position,3, GL_FLOAT, false, sizeof(Vertex),&vertexs[0].x);
glVertexAttribPointer(_shader._uv,2, GL_FLOAT, false, sizeof(Vertex),&vertexs[0].u);
glVertexAttribPointer(_shader._color,4, GL_FLOAT, false, sizeof(Vertex),&vertexs[0].r);
glDrawArrays(GL_TRIANGLES,0,sizeof(vertexs)/sizeof(vertexs[0]));
matRot.translate(-1,0,-2);----------Z值较小
MVP1 = matProj*matView*matRot;
glUniformMatrix4fv(_shader._MVP,1,false,MVP1.data());
glDrawArrays(GL_TRIANGLES,0,sizeof(vertexs)/sizeof(vertexs[0]));
如果代码改为
CELL::matrix4 matRot;
matRot.translate(0,0,-2);-----------Z值较小
CELL::matrix4 MVP1 = matProj*matView*matRot;
glUniformMatrix4fv(_shader._MVP,1,false,MVP1.data());
glVertexAttribPointer(_shader._position,3, GL_FLOAT, false, sizeof(Vertex),&vertexs[0].x);
glVertexAttribPointer(_shader._uv,2, GL_FLOAT, false, sizeof(Vertex),&vertexs[0].u);
glVertexAttribPointer(_shader._color,4, GL_FLOAT, false, sizeof(Vertex),&vertexs[0].r);
glDrawArrays(GL_TRIANGLES,0,sizeof(vertexs)/sizeof(vertexs[0]));
matRot.translate(-1,0,-1);----------Z值较大
MVP1 = matProj*matView*matRot;
glUniformMatrix4fv(_shader._MVP,1,false,MVP1.data());
glDrawArrays(GL_TRIANGLES,0,sizeof(vertexs)/sizeof(vertexs[0]));