Sign in to follow this  
Erik Sintorn

Can't write depth values in shader

Recommended Posts

Hello, Im writing an application that will do some computations and leave the results in the depth buffer. The fragment shader will need to come up with a depthvalue that will be written if no other fragment has already written a better one. However, I can't seem to write depths at all! I have a framebuffer set up with one color buffer and one depthbuffer. Depth test is disabled and depthmask is set to GL_TRUE. I clear the depthbuffer like: glClearDepth(0.5); glClear(GL_DEPTH_BUFFER_BIT); Then I draw my quad with the following fragment shader: void main() { gl_FragDepth = 0.60; gl_FragColor = vec4(2.3,1,1,1); } But when I read back the depth buffer with: float *buffer3 = new float[size * size]; glReadBuffer(GL_COLOR_ATTACHMENT0_EXT); glReadPixels(0, 0, size, size, GL_DEPTH_COMPONENT, GL_FLOAT, buffer3); Every element in buffer3 is still 0.5. Why is this? Thanks for any help, Erik

Share this post


Link to post
Share on other sites
Disabling the depth test implicitly disables depth writes as well. Instead of disabling the depth test set glDepthFunc(GL_ALWAYS);.

Enigma

Share this post


Link to post
Share on other sites
Arrr.. So far so good, but while I can now write an arbitrary value between 0 and 1.0 to the z buffer in the fragment shader, writing anything outside of this range discards the whole pixel! Why is this and isn't there some way to be allowed to write any 24bit float in the z-buffer?

Share this post


Link to post
Share on other sites
That is because how the depthbuffer is specified, 0 being the near clipplane and 1 the farplane, any value not in that range lacks meaning.

Remember that the actual value stored in the depthbuffer is a non-linear transformation of a objects distance from the nearplane, if you need more info about that gogle is your friend.

The only "buffer" that opengl will not enforce the [0..1] range (as it must to per specification), are float colorbuffer formats.

Share this post


Link to post
Share on other sites

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