Depth data using RTT

Problems building or running the engine, queries about how to use features etc.
Post Reply
natepak
Gnoblar
Posts: 20
Joined: Wed Mar 26, 2008 8:26 pm
x 1

Depth data using RTT

Post by natepak »

Hello,

I'm trying to create a program that will render the depth data to a texture. This is not for shadow mapping, so I need a texture independent of Ogre's shadow functions.

I can create the render texture, but I don't know how to tell Ogre to render depth data to it.

I create the render texture like so:
renderTexture = Ogre::TextureManager::getSingleton().createManual(
"myDepthTextureName",
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
Ogre::TEX_TYPE_2D,
imageWidth, imageHeight, 0,
Ogre::PF_FLOAT16_R,
Ogre::TU_RENDERTARGET);

renderTaget = renderTexture[0]->getBuffer()->getRenderTarget();

I create a camera for the texture:
sceneMgr->createCamera("depthCameraName"); renderTarget->addViewport(camera);


I create a material:
Ogre::MaterialPtr matPtr = Ogre::MaterialManager::getSingleton().create(
"depthMatName", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
matPtr->getTechnique(0)->getPass(0)->setLightingEnabled(false);
matPtr->getTechnique(0)->getPass(0)->createTextureUnitState("myDepthTextureName");

Does anyone know how to proceed from here? I can't figure out how to get Ogre to put depth (z-buffer) data into the texture instead of color data.

Thanks for the help.
User avatar
Assaf Raman
OGRE Team Member
OGRE Team Member
Posts: 3092
Joined: Tue Apr 11, 2006 3:58 pm
Location: TLV, Israel
x 76

Post by Assaf Raman »

Use a shader that color by the depth - and you will get the result you want.
Check out the "Depth of Field.rfx" sample in RenderMonkey (look the Depth sub project).
Watch out for my OGRE related tweets here.
natepak
Gnoblar
Posts: 20
Joined: Wed Mar 26, 2008 8:26 pm
x 1

GLSL depth shader

Post by natepak »

I've tried attaching a vertex and fragment shader that will compute the depth, but all I can get is a white screen.

Here is my vertex shader:

uniform mat4 worldViewProj;
uniform vec4 texelOffsets;
varying vec2 depth;

void main()
{
gl_Position = ftransform();

// fix pixel / texel alignment
gl_Position.xy += texelOffsets.zw * gl_Position.w;

depth.x = gl_Position.z;
depth.y = gl_Position.w;
}


Here is my frag shader:
varying vec2 depth;

void main()
{
float finalDepth = depth.x / depth.y;

// just smear across all components
// therefore this one needs high individual channel precision
gl_FragColor = vec4(finalDepth, finalDepth, finalDepth, 1);
}


This code was taken from the DepthShadowmap example in the ogre sources. Can someone notice what is wrong with the shaders?

Thanks,
-nate
User avatar
SpaceDude
Bronze Sponsor
Bronze Sponsor
Posts: 822
Joined: Thu Feb 02, 2006 1:49 pm
Location: Nottingham, UK
x 3
Contact:

Post by SpaceDude »

Assuming you are new to shaders, first thing to do is check that it's compiling correctly. Check you ogre.log to see if there are any errors.

Secondly, I would guess your values have a range greater than 1. When you save that to a texture or view it on screen all the values higher than 1 get clamped to 1. Likewise values lower than 0 get clamped to 0. Just to check your shader is working I would suggest you try the following line:

gl_FragColor = vec4(0.5, 0.5, 0.5, 1);

You should see a grey screen. If not, try to get that working first, then you can start worrying about your depth map.
User avatar
cyanbeck
Gnoblar
Posts: 8
Joined: Tue Nov 02, 2004 3:20 pm
x 1

Post by cyanbeck »

Hi, natepak,

you could take a look at this thread:
How to render the Z-buffer to a Texture :)
Post Reply