Objects being drawn through other objects

I started learning OpenGL from http://www.videotutorialsrock.com/ a few days ago. To learn something, I decided to make an analog clock.

Things went pretty good, I made the arms and made them move the way they should. Then I got the suggestion to cover the middle, since the arms looked funny. So I made a little box to cover it. Ran into some issues because I had screwed up beforehand, but managed to do it. Then I decided to make some kind of background, so I could try out lightning and perhaps textures. This wasn’t a big issue either, just copying the code I made for the little “cover box” and change its parameters.

Also, since my brother didn’t believe the clock was actually 3D (which some might say it’s not, but whatever) I did some keyhandling so the camera rotates left, right, up, down, in and out. That’s about as far as I’ve come at this point.

Now the question. The arms, backgrounds and cover are outputted on different places on the Z axis, like:

cover - arms - background

with a small gap in between. To my understanding, this is what decides which object is in front of something else when rendering. However, I noticed something funny when moving the camera around the back of the clock - looking at it from behind, I still see the arms and the cover, even though they’re supposed to be on the far side (the front) of the background.

I looked around, but can’t find any info on this. So can someone explain why this is? That is the question, sorry for all the background info.

The source (it’s just one file, hopefully properly commented and not too large) can be found here:
http://fryspunkten.se/coding/OGL_Clock.cpp
if anybody feels like trying it out themselves.

Any other questions, just ask. But I would like to solve this before moving on with the program (got big plans for my clock, yay!). And the window which prints the variables all the time is for error checking a problem I had, sorry about that.

I’m not sure if your problem is this but try to put this before drawing the quads, glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

I repeat, I’m not sure if this will solve your problem :o

Hey, thanks for the quick reply!

I read up on it and tried that solution. I put the line you gave before drawing the background (first quad). I could still see the arms through the back of the background. So I put
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_POINT);
in front of the second arm. This actually kind of solved the issue of the second arm being showed from the back. So it can be used as a solution in this case.

But if I have some bigger construction, like a pyramid (instead of the 2D shapes I have now) which is behind a wall, it wouldn’t work, as the “outside” of the pyramid has to have GL_FILL, if anything. So the topic is still open I guess :slight_smile:

It seems like you’ll want to use the depth-buffer?

Try glEnable(GL_DEPTH_TEST) , it is commented out in your code so I’m not sure what you’re after…

There we go, thank you!
I have no idea why it’s commented. Might have been me trying out glEnable(GL_SMOOTH), and for some reason thinking they can’t both be called :S

It’s slightly embarrassing, but at least I (hopefully) won’t be doing that same mistake again. Thanks a bunch :slight_smile: