Can't write depth values in shader

Started by
4 comments, last by Erik Sintorn 18 years, 7 months ago
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
Advertisement
Disabling the depth test implicitly disables depth writes as well. Instead of disabling the depth test set glDepthFunc(GL_ALWAYS);.

Enigma
Yah, that's it. Thank you very much.
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?
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.
Allrigt, it's not a big problem. Thanks so much for your fast answers.

This topic is closed to new replies.

Advertisement