OpenGL depth test not working properly

I have a simple program to use the depth test. It does not work as expected. The program draws the X, Y axis and the sphere near the origin. If I do not enable GL_DEPTH_TEST, the sphere is drawn above the axis. If I enable GL_DEPTH_TEST, the axis will be drawn over a sphere that I did not expect. Can someone tell me what I did wrong?

void  
glwid::initializeGL()  
{    
    glClearColor (0.0f, 0.0f, 0.0f, 1.0f);      
}  



void  
glwid::resizeGL(int width, int height)  
{  
    glViewport( 0, 0, (GLint)width, (GLint)height );  
    glMatrixMode( GL_PROJECTION );  
    glLoadIdentity();  

    gluPerspective ( 90, (GLint)width/ (GLint)height, 0.0, 200.0 );  
    glMatrixMode( GL_MODELVIEW );  
    glLoadIdentity();  
    glEnable (GL_DEPTH_TEST);  
}  


void  
glwid::paintGL()  
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  

    glMatrixMode (GL_MODELVIEW);  
    glLoadIdentity();  

    gluLookAt (0, 0, 100, 0, 0, 0, 0, 1, 0);  


    //  
    // X axis  
    //  
    glBegin( GL_LINES );  
    qglColor( green );  
    glVertex3f (-100.0,  0, 0. );  
    glVertex3f (100.0, 0, 0. );  
    glEnd();  


    //  
    // Y axis  
    //  
    glBegin( GL_LINES );  
    qglColor( red );  
    glVertex3f (0.0, 100.0, 0. );  
    glVertex3f (0.0, -100, 0. );   
    glEnd();  

    //  
    // sun  
    //  
    glTranslated (5, 0, 20);  
    GLUquadricObj *sphere_quadric = gluNewQuadric();  
    glColor3ub (255, 255, 0);  
    gluQuadricDrawStyle(sphere_quadric, (GLenum)GLU_SMOOTH);  
    gluSphere(sphere_quadric, 10, 36, 36);  
}  
+3
source share
2 answers

I tried my code. The problem is the function resizeGL(). The problem was what you set

gluPerspective ( 90, (GLint)width/ (GLint)height, 0.0, 200.0 );  

0.0 . , 0,01 - . : http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml

(GLint)width/ (GLint)height (GLfloat)width/ (GLfloat)height, .

glEnable(GL_DEPTH_TEST) initializeGL()

+13

Z- 0. Z 20 ( "" ), .

, Z , . , .

: ( ). 100 , . , , .

glDisable(GL_DEPTH_TEST); glEnable (GL_DEPTH_TEST);

0

Source: https://habr.com/ru/post/1787015/


All Articles