Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

opengl glDepthFunc doesn't work

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. draw plate (not draw it to RGB buffer. only catch the depth values)
  2. draw cylinder (if depth test 'less' passes : that means visible part of the cylinder (smaller depth) is drawn)

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); 

to

    glDepthFunc(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!

enter image description here

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();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...