简体   繁体   English

在OpenGL中进行深度测试

[英]Depth Test in opengl

My program refuses to do depth testing. 我的程序拒绝进行深度测试。 The two sphere objects are always drawn in the order they are created, not according to their position. 这两个球体对象始终按其创建顺序绘制,而不是根据其位置绘制。 Sphere alpha is positioned at (0, 0, 1) and Sphere beta is positioned (0, 0, -10), yet OpenGL still draws beta on top of alpha. 球体alpha定位在(0,0,1),球体beta定位在(0,0,-10),但是OpenGL仍然在alpha上方绘制beta。 I set depth test to enabled in my program. 我在程序中将深度测试设置为启用。

Nothing appears to work. 似乎没有任何作用。 I want OpenGL to do depth test automatically on any objects drawn in the window. 我希望OpenGL对窗口中绘制的任何对象自动进行深度测试。 Any help or advise would be greatly appreciated. 任何帮助或建议,将不胜感激。 Here is the full code. 这是完整的代码。

#include "GL/freeglut.h"
#include "GL/gl.h"
#include "GL/glu.h"

const int SPHERE_RES = 200;   
double Z_INIT = -28.0;       
double RADIUS = 2;          
double Red[3] = {1, 0, 0};   
double Blue[3] = {0, 0, 1};  

using namespace std;


/*
 * Method handles resize of the window
*/ 

void handleResize (int w, int h) {    

    glViewport(0, 0, w, h); 
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    double ratio = (float)w/ (float)h;
    gluPerspective(45.0, ratio, 1.0, 100.0);
}


/*
 * Color and depth is enabled and in this method
*/

void configureColor(void)
{

     glClearColor(1.0f, 1.0f, 1.0f, 0.0f); //Set background to white
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear window.
     glDepthFunc(GL_ALWAYS);
     glEnable(GL_DEPTH_TEST);

     glEnable(GL_COLOR_MATERIAL);        
     glEnable(GL_LIGHTING);
     glEnable(GL_LIGHT0);

}

void display (void) {    

     configureColor();

     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();


     GLfloat sun_direction[] = { 0.0, 0.0, -1.0, 0.0};
     glLightfv(GL_LIGHT0, GL_POSITION, sun_direction);
     GLUquadric* quad =  gluNewQuadric();

     //first sphere is drawn
     glColor3f(Red[0], Red[1], Red[2]);
     glPushMatrix();
         glLoadIdentity();
         glTranslatef(0, 0, Z_INIT);
         glTranslatef(0, 0, 1.0);
         gluSphere(quad, RADIUS, SPHERE_RES, SPHERE_RES);
     glPopMatrix();

     //second sphere is supposed to be drawn behind it, 
     //but it is drawn on top. 
    glColor3f(Blue[0], Blue[1], Blue[2]);
     glPushMatrix();
         glLoadIdentity();
         glTranslatef(0, 0, Z_INIT);
         glTranslatef(0, 0, -10.0);
         gluSphere(quad, RADIUS, SPHERE_RES, SPHERE_RES);
     glPopMatrix();

    free(quad);
     glFlush();
} 

int main(int argc, char** argv)
{

    glutInit(&argc, argv); //initializes the GLUT
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(600,600);
    glutInitWindowPosition(100,100);
    glutCreateWindow("OpenGL - First window demo");  
    glutReshapeFunc(handleResize);
    glutDisplayFunc(display);

    glutMainLoop();    
    return 0;
}

I am using Ubuntu 14.04 operating system. 我正在使用Ubuntu 14.04操作系统。

glDepthFunc(GL_ALWAYS);

This is the reason you see the spheres in the order they are drawn. 这就是您按绘制顺序看到球体的原因。 Setting the depth function to GL_ALWAYS simply means all depth tests always pass, for any fragment, be it closer or farther. 深度函数设置为GL_ALWAYS只是意味着,对于任何片段,无论更近或更远,所有深度测试都始终通过。

You need GL_LESS for the result you want. 您需要GL_LESS以获得所需的结果。 A fragment having depth lesser than the one in the frame buffer wins ; 深度小于帧缓冲区的片段获胜 ; the closer (lesser z) one wins over the farther (greater z) one. 越近(z越小)胜过越远(z越大)。

You can either call glDepthFunc(GL_LESS) or comment out glDepthFunc(GL_ALWAYS) since GL_LESS is the default. 您可以调用glDepthFunc(GL_LESS)或注释掉glDepthFunc(GL_ALWAYS)因为GL_LESS是默认设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM