glDepthFunc不起作用

glDepthFunc不起作用

本文介绍了opengl glDepthFunc不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写显示对象隐藏部分的代码.例如:板是较大的多边形对象,而圆柱是较小的多边形对象.圆柱体被板隐藏. (请参见下半部分窗口:圆柱体穿透了板.圆柱体的某些部分被板掩盖了.)图片是由以下代码制作的.

I'm writing code that display hidden part of the object.Here is example : the plate is larger polygonal object, and cylinder is smaller polygonal object. cylinder is hidden by plate. (See the lower half window : cylinder penetrates the plate. some part of the cylinder is hidden by plate. )The image is made by below code.

  1. 绘制板(不将其绘制到RGB缓冲区.仅捕获深度值)
  2. 绘制圆柱体(如果通过深度测试较少":则表示绘制了圆柱体的可见部分(深度较小))

每帧模型都沿y轴旋转.它为我提供了每帧正确的结果.

The model is rotated along y axis for each frame. It gives me a correct result for every frame.

现在,我想将圆柱体的隐藏部分显示为透明.在使用混合之前,我只想显示圆柱的隐藏部分.也就是说,我必须显示圆柱区域,该区域的深度值比板的深度更大.然后,我改变

Now, I'd like to display hidden part of the cylinder as transparent.Before using blending, I want to display only the hidden part of the cylinder. That is, I have to display cylinder's region that have more greater depth values than plate's depth. Then, I just change

    glDepthFunc(GL_LESS);

    glDepthFunc(GL_GREATER);

但是,如果我将其更改为GL_GREATER,它不会给我正确的结果.我在第一帧得到了正确的结果,但是之后,模型消失了. (这意味着该模型不会显示在窗口上.上下视口都显示)我不明白原因.救救我!

However, If I changed it to GL_GREATER, it does not give me a correct result.I got correct result at first frame, but after then, the model is gone. (That means, the model is not displayed on window. Both of upper, and lower viewport)I cannot catch the reason. Help me!

void MyDisplay()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glRotatef(rot, 0, 1, 0);

    glViewport(0,0, width, height/2);
    glEnable(GL_DEPTH_TEST);
    DrawPlate();
    glColor4f(0,0,0,1);
    DrawCylinder();
    glDisable(GL_DEPTH_TEST);

    glViewport(0,height/2, width, height/2);
    glEnable(GL_DEPTH_TEST);

    glClearDepth(1.0);
    //glDrawBuffer(GL_NONE); // No color buffers are written
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    DrawPlate();

    glDepthFunc(GL_LESS);
    // glDepthFunc(GL_GREATER); // doesn't work !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glColor4f(0,0,0,0.5f);
    DrawCylinder();

    delay(1);
    glFlush();
    glutPostRedisplay();
}

推荐答案

嘿,我找到了解决方案.即:一帧后未初始化深度测试(更大).帧后深度变为1,则没有像素通过测试.因此,我必须在第一行输入glDepthFunc(GL_LESS).

Hey guys i found a solution.That is : depth test (greater) does not initialized after one frame.the depth goes to 1 after frame, then no pixel passes the test.Thus I have to input glDepthFunc(GL_LESS) at first line.

这篇关于opengl glDepthFunc不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 02:04