Sign in to follow this  

Z problems with 2d rendering

This topic is 3709 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

Hello! I'm having some fun with render-to-texture (using framebuffers) so I decided to render a 2D game I made on the surface of a rotating cube. It works perfectly fine, except if I turn off Z writing while rendering the 2D game, then I get weird flickering (kind of Z fighting) on the faces of the cube. I can't understand this behavior, the 2D game does absolutely not need to Z write (neither does it need to Z test) in order to render properly as the elements are rendered back to front. Here's what my rendering code does :
- Setup orthographics projection : glOrtho()
- Setup render-to-texture : glBindFramebufferEXT()
- Clear colorbuffer : glClear(GL_COLOR_BUFFER_BIT)
- Disable Z testing : glDisable(GL_DEPTH_TEST)
- Disable Z writing : glDepthMask(GL_FALSE)   <-- Works OK with GL_TRUE
- Render 2D game
- Setup render-to-backbuffer : glBindFramebufferEXT(,0)
- Setup perspective projection : gluPerspective()
- Clear colorbuffer : glClear(GL_COLOR_BUFFER_BIT)
- Clear depthbuffer : glClear(GL_DEPTH_BUFFER_BIT)
- Enable Z testing : glEnable(GL_DEPTH_TEST)
- Enable Z writing : glDepthMask(GL_TRUE)
- Render textured cube
Here how it looks like (best case, sometimes I only see some flickering on the edges and no faces) : Does someone understand what's going on?

Share this post


Link to post
Share on other sites
Quote:
Original post by Erik Rufelt
I don't think glDepthMask(GL_FALSE) is needed, once depth-testing is disabled writing is also disabled. Try not using it at all, even though it shouldn't matter as far as I know.


Even if disabling depth writing wasn't needed, why would disabling it create such a bug?

My conception of zbuffering was a 2-state thing :
- ztesting : on/off
- zwriting : on/off
If I understand well, the following would be more accurate :
- zbuffering : off/test_only/test_and_write

Share this post


Link to post
Share on other sites
When disabling the z-testing are you still binding a zbuffer to render to your texture ? I've heard that not binding a zbuffer to your target, even if you don't use it at all, produces render artefacts on some driver (don't remember if it was ATI or NVIDIA).

Also ensure that rendering to your texture has been finished before rendering to your cube (glFinish).

--
Ashaman

Share this post


Link to post
Share on other sites
Quote:
Original post by Ashaman73
When disabling the z-testing are you still binding a zbuffer to render to your texture ? I've heard that not binding a zbuffer to your target, even if you don't use it at all, produces render artefacts on some driver (don't remember if it was ATI or NVIDIA).

Also ensure that rendering to your texture has been finished before rendering to your cube (glFinish).

--
Ashaman


No need to call glFinish

Share this post


Link to post
Share on other sites
FYI: glDisable(GL_DEPTH_TEST) means all fragments pass the depth test and the depth value of the fragment will be not written to regardless of glDepthMask(). To get it so that depth always passed and the depth values are updated use glDepthFunc(GL_ALWAYS) together with glDepthMask(GL_TRUE).

By the way, what hardware are you running on? out of morbid curiosity are you sure that have you done glClear(GL_DEPTH_BIT) before rendering the cube (because if you do not the depth values from the previous frame will obscure your cube)

edit: massive correction.

[Edited by - kRogue on October 16, 2007 4:58:20 AM]

Share this post


Link to post
Share on other sites

This topic is 3709 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this