Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


Rendering the depth buffer?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Limedark   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 March 2012 - 02:25 PM

Well, the question is very simple, so I hope that the answer might also be (I am going for "glass half full" mentality): I want to see my OpenGL graphics rendered not by their color or textures, but by their z-buffer value. I.e. closer pixels are darker, distant are brighter (or vice versa if easier). Like this image from Wikipedia: http://en.wikipedia....le:Z_buffer.svg (the lower one, of course). I already have the code to draw the things I want, but it draws it in color and textures. I want to see the Z-buffer instead.

What are my options?

Sponsor:

#2 zacaj   Members   -  Reputation: 643

Like
0Likes
Like

Posted 03 March 2012 - 02:34 PM

Are you using shaders? If so, youll need to get the Z value. Usually you can pass this in from the vertex shader. Then you can do gl_FragColor.rgb=vec3(pow(Z,50)); Not sure if its possible without shaders.

#3 Limedark   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 March 2012 - 03:07 PM

I'm not using shaders. I thought that since the Z-buffer exists somewhere (I assume, or things would overlap, and my clearing the depth buffer would affect nothing), I should be able to simply(??) swap it for the color buffer. I clear the two things in the same line (glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)), so they should be using similar logic... or am I making wild assumptions?

#4 zacaj   Members   -  Reputation: 643

Like
0Likes
Like

Posted 03 March 2012 - 03:15 PM

You're probably going to need to somehow copy the depths buffer into the color buffer or a texture. I'm not an expert with non shader opengl, so I can't really help you there.

#5 mhagain   Crossbones+   -  Reputation: 8140

Like
0Likes
Like

Posted 03 March 2012 - 03:42 PM

Fog.

Set some fog, either linear or exp, then draw your objects untextured (or with white textures). It won't be an exact replica of what the depth buffer contains, but it will give you the visual result you want.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#6 Limedark   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 March 2012 - 05:34 PM

The buffer copy or swap is something I've looked at, but found no syntax on. If anyone has the slightest thread on this, I'm all ears!

The fog is not going to do it, since it is not just to fake the look, it's an actual depiction of distances that I need :-/ However, the basic idea miiiight have something to it, if I can find no other option...

#7 swiftcoder   Senior Moderators   -  Reputation: 10238

Like
0Likes
Like

Posted 03 March 2012 - 07:13 PM

You should just just be able to allocate a texture with an internal format of GL_DEPTH_COMPONENT, and call glCopyTexSubImage2D to copy the contents of the depth buffer into that texture. After that, you can render a fullscreen quad with the texture, or whatever else you want...

However, unless you are restricted to an ancient version of OpenGL, I would recommend using an FBO (Frame Buffer Object), to render the depth buffer directly to a texture in the first place. Or even a shader, to render the z coordinate into the colour buffer, and save having to render multiple passes at all.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#8 Limedark   Members   -  Reputation: 100

Like
0Likes
Like

Posted 04 March 2012 - 05:16 AM

You should just just be able to allocate a texture with an internal format of GL_DEPTH_COMPONENT, and call glCopyTexSubImage2D to copy the contents of the depth buffer into that texture. After that, you can render a fullscreen quad with the texture, or whatever else you want...


This looks like exactly what I was thinking. However, I cannot find the proper code for it, and I fear guessing it from scratch will end in utter failure (I am not a veteran). Is there an example or perhaps even a tutorial of this exact method somewhere?

However, unless you are restricted to an ancient version of OpenGL, I would recommend using an FBO (Frame Buffer Object), to render the depth buffer directly to a texture in the first place. Or even a shader, to render the z coordinate into the colour buffer, and save having to render multiple passes at all.


Partly due to my not being veteran, but also because of a long and boring story about what I'm doing (which I'll spare you), I would like to at least try without FBOs or shaders first, if there is a way?

#9 sjaakiejj   Members   -  Reputation: 130

Like
0Likes
Like

Posted 04 March 2012 - 03:25 PM

I thought that since the Z-buffer exists somewhere (I assume, or things would overlap, and my clearing the depth buffer would affect nothing), I should be able to simply(??) swap it for the color buffer.


The thing with that is that, for as far as I'm aware, the z-buffer is implemented (either by hardware or software) on the GPU. This means that the only direct way to get access to it is via shaders.

#10 swiftcoder   Senior Moderators   -  Reputation: 10238

Like
0Likes
Like

Posted 04 March 2012 - 04:22 PM

The thing with that is that, for as far as I'm aware, the z-buffer is implemented (either by hardware or software) on the GPU. This means that the only direct way to get access to it is via shaders.

AFAIK, shaders can't access the depth buffer directly, either. FBOs are the only way to directly render to a texture (which texture can then be used by a shader).

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS