Original Post
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. 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
#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);
}