depth buffer as texture?

I dont quite remember, how cn you access depth buffer and can you apply it as a texture to a node?
Example code would be nice :slight_smile:

Just like any other texture.

See my “shaderless shadows” sample on the forums. It does exactly that, except that it enables the shadow filter (by setting the texture filter type to Texture.FTShadow).

import direct.directbase.DirectStart
from pandac.PandaModules import *
import sys

# setup scene
env = loader.loadModel('environment')
env.reparentTo(render)

winprops = WindowProperties.size(512,512) 
props = FrameBufferProperties()
props.setRgbColor(1)
props.setAlphaBits(1)
props.setDepthBits(1) 

LBuffer = base.graphicsEngine.makeOutput(
	base.pipe, "offscreen buffer", -2,
	props, winprops,
	GraphicsPipe.BFRefuseWindow,
	base.win.getGsg(), base.win)

if (LBuffer == None):
	self.t=addTitle("Buffer failed to setup")
	sys.exit()

Ldepthmap = Texture()
LBuffer.addRenderTexture(Ldepthmap,
	GraphicsOutput.RTMBindOrCopy,
	GraphicsOutput.RTPDepthStencil)
Ldepthmap.setWrapU(Texture.WMClamp)
Ldepthmap.setWrapV(Texture.WMClamp)
#Ldepthmap.setMinfilter(Texture.FTShadow)
#Ldepthmap.setMagfilter(Texture.FTShadow) 

cm = CardMaker('card')
cm.setFrame(-0.5,1,-1,0.5)
card = aspect2d.attachNewNode(cm.generate())
card.setTexture(Ldepthmap)

run()

No luck

What do you mean? What’s the result?

white

Depth textures aren’t linear for perspective lenses. It doesn’t surprise me that it appears white (while there are probably minor different shades of white). To make sure that it’s working correctly, you could make the frustum tight and see if it’s still white.

For the record, if you want to get the depth texture for the main camera, it’s inadvisable to set up a buffer just to render the depth of the scene a second time. You can probably just use the existing depth texture, as you’re applying it to an object anyway. Not 100% sure, though.

You mean the near and far plane? I tried various numbers, it still seems completely white.

How can I access that?

Through base.win. It’s a GraphicsOutput, too.

And whats it called?
Sorry, but Im not sure where to look for that. I searched the ShowBase reference…

It’s called base.win.

You can call base.win.addRenderTexture(), for instance, to copy the output of the main window to a texture.

Sorry, I thought it i had to search something below base.win ><
So, I dont really understand…

I don’t understand what’s not to understand.

>>> base.win
<libpanda.GraphicsWindow object at 0xdeadbeef>
>>> isinstance(base.win, GraphicsOutput)
True
>>> help(base.win.addRenderTexture)

Sorry, the problem is I dont understand the classesvery well yet. I read the manual pages and reference, but.

If you mean I should replace LBuffer with base.win in the above example, then I tried it out. I get a black texture like this

Ldepthmap = Texture()
base.win.addRenderTexture(Ldepthmap,
	GraphicsOutput.RTMBindOrCopy,
	GraphicsOutput.RTPDepthStencil)
Ldepthmap.setWrapU(Texture.WMClamp)
Ldepthmap.setWrapV(Texture.WMClamp)

PS. help(base.win.addRenderTexture) returns

'more' is not recognized as an internal or external command,
operable program or batch file.

Hello,

create a new camera for your buffer with :

gbuffer_cam = base.makeCamera(LBuffer)

you can then reparent it to your main camera or something else :

gbuffer_cam.reparentTo(base.cam) 

you can use this to display buffers :

loadPrcFileData('', 'show-buffers 1')
import direct.directbase.DirectStart 
from pandac.PandaModules import * 
import sys 
loadPrcFileData('', 'show-buffers 1')
# setup scene 
env = loader.loadModel('environment') 
env.reparentTo(render) 

winprops = WindowProperties.size(512,512) 
props = FrameBufferProperties() 
props.setRgbColor(1) 
props.setAlphaBits(1) 
props.setDepthBits(1) 

LBuffer = base.graphicsEngine.makeOutput( 
   base.pipe, "offscreen buffer", -2, 
   props, winprops, 
   GraphicsPipe.BFRefuseWindow, 
   base.win.getGsg(), base.win) 

if (LBuffer == None): 
   self.t=addTitle("Buffer failed to setup") 
   sys.exit() 

Ldepthmap = Texture() 
LBuffer.addRenderTexture(Ldepthmap, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPDepth)
Ldepthmap.setWrapU(Texture.WMClamp) 
Ldepthmap.setWrapV(Texture.WMClamp) 
#Ldepthmap.setMinfilter(Texture.FTShadow) 
#Ldepthmap.setMagfilter(Texture.FTShadow) 

gbuffer_cam = base.makeCamera(LBuffer)

cm = CardMaker('card') 
cm.setFrame(-0.5,1,-1,0.5) 
card = aspect2d.attachNewNode(cm.generate()) 
card.setTexture(Ldepthmap) 

run()