How to render a grey scale image of the Z-buffer

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

How to render a grey scale image of the Z-buffer

Post by robmar »

What is the most effective way to render the Z-buffer to a texture?
Mel
Competition winner
Posts: 2293
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: How to render a grey scale image of the Z-buffer

Post by Mel »

Using shaders. i don't know if there is any other way. You calculate the position of the vertices in the vertex shader, then, you can pass the normalized Z value to the pixel shader, and you can draw it either to the alpha channel of the current render target or to a diferent render target using MRT
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: How to render a grey scale image of the Z-buffer

Post by robmar »

Yes, that´s one way, probably the fastest way as the code runs on the GPU. Also directly readying the memory, but perhaps Irrlicht´s developers know the best way to do it with Irrlicht 1.8? Maybe some inbuilt facility?
teto
Posts: 159
Joined: Thu Dec 03, 2009 9:37 pm
Location: /home
Contact:

Re: How to render a grey scale image of the Z-buffer

Post by teto »

I think I have seen somewhere in 1.8 a function called createDepthMap but dunno if it works or even if it is public. Otherwise shaders.
Using trunk with mingw/gcc 4.6, Windows 7 64 bits driver opengl
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: How to render a grey scale image of the Z-buffer

Post by robmar »

Thanks for that, will look that one up.

The aim is to show a 2D image of the depth buffer in grey scale on an area of the screen.

Would it be possible to draw the depth buffer into the GUI area?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: How to render a grey scale image of the Z-buffer

Post by mongoose7 »

Use draw2DImage and you can draw it wherever you want.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: How to render a grey scale image of the Z-buffer

Post by robmar »

How would that work? You´d need the z-buffer in an IMage, but how?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: How to render a grey scale image of the Z-buffer

Post by mongoose7 »

driver->draw2DImage(ITexture *tex, recti srcrect, recti destrec);

It's in the examples. Demo.cpp?
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: How to render a grey scale image of the Z-buffer

Post by robmar »

yes, but how do you propose to access the Z-buffer, which is an array of floats?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: How to render a grey scale image of the Z-buffer

Post by mongoose7 »

Ah, sorry. As Mel said, you use a vertex shader to save the depth values in a varying, and a pixel shader to write the depth values to the render target. Then you just draw the render target. (Divide the depth values by the far value to get a number between 0 and 1.) I had forgotten the details.
Mel
Competition winner
Posts: 2293
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: How to render a grey scale image of the Z-buffer

Post by Mel »

Other, more creative way, is to render the scene in 2 passes, one for the normal render to one rendertarget, and the other with the objects in black, with a white fog that started in the near value of the camera, and ended in the far value drawn into another rendertarget. It is not as efficient as using shaders and/or MRT, but for instance, is more compatible, shadow mapping is done like this, so it is an affordable way too... at the cost of a 2 passes process
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: How to render a grey scale image of the Z-buffer

Post by robmar »

interesting idea, but if speed is an important, not so good.

shaders would be faster, but if a shader cannot access more than the z-buffer for its own object, then it would be necessary to re-render every object using a special z-buffer shader; very inefficient.

there are methods in opengl to copy the z-buffer to a texture, but how is that supported in IRRLICHT?

OpenGL mehtod: "Copying the depth buffer to a texture is pretty simple in opengl. If you have created a new texture that you haven't called glTexImage* on, you can use glCopyTexImage2D. This will copy pixels from the framebuffer to the texture. To copy depth pixels, you use a GL_DEPTH_COMPONENT format. I'd suggest GL_DEPTH_COMPONENT24."

Code: Select all

int shadowMapWidth = 512;
int shadowMapHeight = 512;
glGenTextures(1, &m_depthTexture);
 
glBindTexture(GL_TEXTURE_2D, m_depthTexture);
 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
 
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadowMapWidth, shadowMapHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 512,512);
 
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: How to render a grey scale image of the Z-buffer

Post by robmar »

If the backbuffer is 1920 x 1080, can opengl (irrlicht) copy it to a texture of the same resolution? Keep reading about powers of 2 and some flag to change to any resolution, wondered if anyone knows if it works okay and fast? I`ve done it with the D3D driver, now for opengl...
Last edited by robmar on Tue Apr 30, 2013 4:30 pm, edited 1 time in total.
Abraxas)
Posts: 227
Joined: Sun Oct 18, 2009 7:24 am

Re: How to render a grey scale image of the Z-buffer

Post by Abraxas) »

http://irrlicht.sourceforge.net/forum/v ... =9&t=48621

I did it here simply by dividing the screen depth by 1024
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: How to render a grey scale image of the Z-buffer

Post by robmar »

sorry, my question was a bit off-topic. I´m trying to find a fast way to bltcopy from the framebuffer, with the opengl driver, to a texture overlay. Can´t find the function to do a copy from the frame buffer to a texture...
Post Reply