Jump to content
  • Advertisement
Sign in to follow this  
NMO

OpenGL Depth buffer sharing

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

Hi,

I have written a Qt GUI application in Linux which contains 4 OpenGL windows. Every window has it's own OpenGL stuff in it, means every window has it's own render function. I need the depth values from every OpenGL window and I'm retrieving them with glReadPixels(). The problem is that I get wrong depth buffer results when I'm requesting the value of one OpenGL window. Afaik the depth buffer is implemented in hardware so my OpenGL windows are sharing the same depth buffer. I think the problem is that one OpenGL window is overriding the depth value of one other window. So how do I get the depth value of one specific window?

Share this post


Link to post
Share on other sites
Advertisement
I think I haven't explained my problem well enough so I'll try to make it clearer with a better explanation:

I've written an application where I can translate or rotate meshes inside my OpenGL windows. Thats pretty similar to what Blender or 3dsMax or some other modeling app does. So for example I click with the mouse on a mesh to select it an then hold the mouse to move it around in my 3d world. To get the depth of a mesh I use following code:

float OpenGLWidget::getDepthFromMouseCoordinates(int x, int y) {
GLint viewport[4];
GLfloat z;
glGetIntegerv (GL_VIEWPORT, viewport);
render();
glReadPixels( x, viewport[3]-y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z );
printf("depth of pixel: %f\n", z);
fflush(stdout);
return z;
}


That works pretty well with ONE OpenGL window. But i have four windows to observe the view from different angles. The problem is now that all four windows are writing into the depth buffer. So lets say i click into Window1 and get the depth of the mouse coordinate with the above code snippet. The problem is that another window (lets say Window2) has overwritten the depth buffer in the meantime so that glReadPixels() is no longer valid for Window1.

Share this post


Link to post
Share on other sites
I've never heard of glMakeCurrent(). Is part of the OpenGL starndard? Whats the purpose of glMakeCurrent(), especially for my problem? Maybe you could give me a short explanation.

Share this post


Link to post
Share on other sites
You can have several OpenGL contexts in your process. However, each can be current only to a single thread (OpenGL and its contexts aren't thread-safe). By calling wglMakeCurrent, glxMakeCurrent, QGLContext::makeCurrent and similar, depending on your platform, you select a context for all subsequent OpenGL calls in that thread. AFAIK a similar mechanism is a part of every single OpenGL implementation on platforms with multiple threads, processes and windows (which is basically everything out there). The OpenGL standard doesn't deal with such things much.
It might not be relevant to your problem but is definitely necessary to know.

Btw, when calling QGLContext::create(), you can specify a shareContext instance. The same goes for QGLWidget::GLWidget() constructors (shareWidget argument), which I suppose you use. This mechanism is in place for sharing of OpenGL objects (textures, other objects, display lists and stuff). This means that all enabled contexts will have access to those resources (OpenGL objects). Be sure to avoid using any in multiple threads at the same time, not thread-friendly.

I suggest rendering into OpenGL framebuffer objects (render-to-texture) instead of using a swap-chain (rendering directly onto widget surfaces) and then presenting what you rendered on your widgets. That way, you will have complete control of what's going where. QT4 has some really nice wrappers for all the stuff you need but it can be done with pure OpenGL too, the same way.

Share this post


Link to post
Share on other sites

Did you remember to call glMakeCurrent for the context you want to use?


There is no such GL function.
There is wglMakeCurrent which is one of the wiggle functions : There is an app for that
http://www.opengl.org/wiki/Platform_specifics:_Windows

Share this post


Link to post
Share on other sites
Blah, sorry for the mistake, haven't slept enough, I meant wglMakeCurrent. And as pcmaster said, QGLContext seems to have a method called makeCurrent which does it platform independetly.

Share this post


Link to post
Share on other sites
Yes thats the solution. I only hat to insert QGLWidget::makeCurrent() at the beginning of my selection stuff and now it works fine!

Thank you!

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!