Sign in to follow this  

Object behind another object

This topic is 1795 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 am creating a OpenGL scene and i have problem when two 3d object are on one X, Y position and have different Z positions, because the display order are not controling by the Z position, but the display order in script.
 
I am using C++ and Windows WINAPI.
 
On google i find some solution for glut or linux devices using glEnable(GL_DEPTH_TEST) and glDepthFunc(GL_LEQUAL), but that is not good solution for WINAPI becouse screen start blinking and objects was damaged.
 
Can someone help me ?

Share this post


Link to post
Share on other sites

Your objects, while defined in a 3D space, are still being drawn on a 2D display. So what happens is OpenGL draws the first object it comes across (first in your script) and then draws the second object it comes across (second in your script). I believe GL_DEPTH_TEST is supposed to check where in the 3D world the object is so when it renders the object, it knows what order to do it in. You could do this yourself by simply testing which object is further from the camera and render it first, find the next closest object and render that next, and so on until they're all rendered. I wouldn't recommend it though because as your scene gets more complicated, you'll just run into more issues. But that's what OpenGL is for.

 

Your problem is likely you haven't enabled a depth buffer for your rendering context. Something like this (from http://nehe.gamedev.net/tutorial/creating_an_opengl_window_%28win32%29/13001/):

 

glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

 

If you're having any more issues, posting your code will help.

Edited by akciom

Share this post


Link to post
Share on other sites
Yes I know, I wrote that I tryed glEnable (GL_DEPTH_TEST) and glDepthFunc (GL_LEQUAL) (and I uses glClearDepth(1.0f) and glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)). 
 
I cannot post my code, because is more complex and is always in classes, structures and unions. I can only better explain my problem and post all glEnables and some stuff from my scripts.
 
Ok, where I can  begin..... I have scene/game, who is about Solar System, I am using primitive object sphere and i have 3 object Universe(rotating background), Sun and one planet.
Camera is look on sun and planet is behind sun, but what i see with camera is this(without Depht test): 
 

8Cmdv.jpg

 

 

And when I enable:

glClearDepth(1.0f)

glEnable(GL_DEPTH_TEST)

glDepthFunc(GL_LEQUAL)

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

 

and in loop i have: glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

I see this:
 

tMV86.jpg

Edited by QIZI94

Share this post


Link to post
Share on other sites

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