• Advertisement
Sign in to follow this  

SDL+OpenGL+depth buffer trouble

This topic is 4346 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I don't know whether this is an SDL or an OpenGL problem, or perhaps something entirely on my computer alone. So sorry if this is not the right forum :). Anyway, I have set up a basic SDL app to test small OpenGL programs while I go through the Red Book. I've hit the wall with the depth buffer.
#include <SDL.h>
#include <SDL_OpenGL.h>

void draw() {
    static float angle = 0.0f;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0,0.0,-1.0);
    glRotatef(angle,1.0f,0.0f,0.0f);
    angle+=0.3f;
    glBegin(GL_QUADS);
        glColor3f(1.0f,0.0f,0.0f);
        glVertex3f(-0.5f,-0.5f,0.0f);
        glColor3f(0.0f,1.0f,0.0f);
        glVertex3f(-0.5f,0.5f,0.0f);
        glColor3f(0.0f,0.0f,1.0f);
        glVertex3f(0.5f,0.5f,0.0f);
        glColor3f(1.0f,1.0f,0.0f);
        glVertex3f(0.5f,-0.5f,0.0f);
    glEnd();
    glBegin(GL_QUADS);
        glColor3f(1.0f,0.0f,0.0f);
        glVertex3f(-0.5f,-0.5f,1.0f);
        glColor3f(0.0f,1.0f,0.0f);
        glVertex3f(-0.5f,0.5f,1.0f);
        glColor3f(0.0f,0.0f,1.0f);
        glVertex3f(0.5f,0.5f,1.0f);
        glColor3f(1.0f,1.0f,0.0f);
        glVertex3f(0.5f,-0.5f,1.0f);
    glEnd();
}

int main(int argc, char *argv[]) {
    const int xres=640;
    const int yres=480;
    SDL_Event event;
    SDL_Init(SDL_INIT_VIDEO);
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ) ;
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
    SDL_SetVideoMode(xres,yres,0,SDL_OPENGL | SDL_HWSURFACE | SDL_DOUBLEBUF);
    glViewport(0,0,xres,yres);
    glClearColor(0.0f,0.0f,0.0f,0.0f);
    glShadeModel(GL_SMOOTH);
    glClearDepth(1.0);
    glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL );
    glDepthMask( GL_TRUE );

    glMatrixMode(GL_PROJECTION);
    gluPerspective(70.0,(double)xres/yres,0.0,3.0);
    glMatrixMode(GL_MODELVIEW);

    bool done = false;
    while(!done) {
        draw();
        SDL_GL_SwapBuffers();

        while(SDL_PollEvent(&event)) {
            switch(event.type) {       
                case SDL_KEYDOWN:
                    if(event.key.keysym.sym == SDLK_ESCAPE)
                        done=true;
                    break;
                case SDL_QUIT:
                    done=true;
            }
        }
    }
    SDL_Quit();
    return(0);
}



It flickers heavily when the two quads overlap, and it puts the second quad before the first one (it *should* be otherwise, right?). I've tried entering loads of different numbers, commenting out function calls and putting them on other places, etc, etc. Either everything flickers (without the glDepthFunc call), or just the polygon that seems to be misplaced flickers a bit. I don't see it... What part of the magic failed on me? Thank you, -Amarth

Share this post


Link to post
Share on other sites
Advertisement
Your near and far clipping planes seem to be off. Try
gluPerspective(70.0,(double)xres/yres,1.0,30.0);
Never set you zNear clipping plane to zero, bad things happen when you approach infinity.

Share this post


Link to post
Share on other sites
Indeed, that did it... Thank you, I'd never have found that. I don't think I understand the concepts of clipping planes and the depth buffer that well... Oh, this is in the wrong forum by now, so if someone really feels the need to move this around, no problemo.

Anyway, I don't really see it... What happens when the near clipping plane is set to zero? I can somehow imagine that this gives troubles, as it's a projection on one point... But I don't see why it gives troubles here, and not before. Can someone explain? Don't worry about the maths, I've done algebra, numerical analysis, projective geometry and I hope whatever else is involved. I'd *like* to see the maths :).

So... The clipping planes define what z-values are included in the viewing volume. I've read in the Red Book that the depth buffer only uses values between 0.0 and 1.0 (or did I get that wrong?), so does it scale these values so that 0 is the near clipping plane, and 1.0 the far clipping plane? So that if I, for example, set zNear=1.0 and zFar=51.0, then around z=40.0 I'd have a depth buffer value of 0.8? Or did I get that completely wrong? And should I even care?

Share this post


Link to post
Share on other sites
Okay, thanks a lot... I think I got it a bit better now :). Onwards!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement