ZBuffer problem with rtt [SOLVED]

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
tom_gamer
Posts: 15
Joined: Sat Jun 01, 2013 8:51 am

ZBuffer problem with rtt [SOLVED]

Post by tom_gamer »

Hello,

I got a zbuffer problem with real time textures. :(

Setting:
-irrlicht 1.8
-OpenGL
-drawing objects with my own shader
-shader based on EMT_TRANSPARENT_ALPHA_CHANNEL
-set in all materials the flags for EMF_ZWRITE_ENABLE and EMF_ZBUFFER
-did a 'smgr->getParameters()->setAttribute(scene::ALLOW_ZWRITE_ON_TRANSPARENT, true);'
--> everything works fine, when i draw to the frame buffer
Image

but now i want to do i bit post processing and write the scene to a rtt:

Code: Select all

 
    screenQuad = new CScreenQuadSceneNode(smgr->getRootSceneNode(),smgr,-1);
    rtt = driver->addRenderTargetTexture(dimension2d<u32>(2048, 1024), "RTT0", ECF_A8R8G8B8);
    screenQuad->getMaterial(0).setTexture(0,rtt);
    screenQuad->getMaterial(0).MaterialType = (E_MATERIAL_TYPE)EMT_SOLID;
 
...

Code: Select all

 
    driver->beginScene(true, true, backgroundColor);
 
    driver->setRenderTarget(rtt, true, true, SColor(0,0,0,255));
    smgr->drawAll();
    driver->setRenderTarget(ERT_FRAME_BUFFER, false, false, SColor(0,0,0,255));
    screenQuad->render();
 
    guienv->drawAll();
    driver->endScene();
 
--> it seems there is no writing to the zbuffer anymore
Image


Is there something special to keep in mind, when using rtt? Do you have any idea?

Regards,
Tom
Last edited by tom_gamer on Mon Oct 14, 2013 3:22 pm, edited 2 times in total.
SpaceCombat
http://sourceforge.net/projects/spacecombatgame
(OpenSource space combat simulation)
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: ZBuffer problem with rtt

Post by mongoose7 »

I'm guessing you don't clear the ZBuffer when changing render targets. There is only one Zbuffer.
tom_gamer
Posts: 15
Joined: Sat Jun 01, 2013 8:51 am

Re: ZBuffer problem with rtt

Post by tom_gamer »

When I have

Code: Select all

driver->setRenderTarget(rtt, true, true, SColor(0,0,0,255));
the second 'true' is for clearing the zbuffer, right?

So, the zbuffer should be cleared, when the scene is drawn.

When I switch back to the frame buffer, I only draw the screen quad, which is not affected by the zbuffer.

Maybe there is a problem with the size of the rtt?
SpaceCombat
http://sourceforge.net/projects/spacecombatgame
(OpenSource space combat simulation)
tom_gamer
Posts: 15
Joined: Sat Jun 01, 2013 8:51 am

Re: ZBuffer problem with rtt

Post by tom_gamer »

I played around a bit...

Even when I set clearzbuffers in each line, it doesn't work.

Code: Select all

 
    driver->beginScene(true, true, backgroundColor);
    driver->clearZBuffer ();
    driver->setRenderTarget(rtt, true, true, SColor(0,0,0,255));
    driver->clearZBuffer ();
    smgr->drawAll();
    driver->clearZBuffer ();
    driver->setRenderTarget(ERT_FRAME_BUFFER, false, false, SColor(0,0,0,255));
    driver->clearZBuffer ();
    screenQuad->render();
    driver->clearZBuffer ();
 
    guienv->drawAll();
    driver->endScene();
 
I also switched from my shader to EMT_SOLID for all objects. Same result. :?

It's obviously related only to the rtt. :(

Today, I'll try to build a minimal example...
SpaceCombat
http://sourceforge.net/projects/spacecombatgame
(OpenSource space combat simulation)
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: ZBuffer problem with rtt

Post by Nadro »

Small example for reproduce this issue will be really useful.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
tom_gamer
Posts: 15
Joined: Sat Jun 01, 2013 8:51 am

Re: ZBuffer problem with rtt

Post by tom_gamer »

And finally, I found it! :)

It did the oldest mistake, since z buffers... :roll:

The distance between near and far plane was too big. (Near=0.5, Far=15000)
I suppose, the frame buffer calculates with 32 bits and the rtt only with 24. Very interesting.

The right solution for me is a logarithmic deph buffer. Have a look at this link: http://outerra.blogspot.de/2009/08/loga ... uffer.html
This works really great for my scene (near objects with high details and in the same scene very far objects). I put it into my shader and found no performance disadvantages.

The only drawback is, that I now have to create a shader for all objects. So, the z-value is calculated right.

Thanks to all posters... :D
SpaceCombat
http://sourceforge.net/projects/spacecombatgame
(OpenSource space combat simulation)
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: ZBuffer problem with rtt [SOLVED]

Post by Nadro »

RTT depth should use the same precision as the main depth buffer (base on SIrrlichtCreationParameters::ZBufferBits parameter). For more info you can check following method:

Code: Select all

GLenum COpenGLDriver::getZBufferBits() const
COpenGLDriver.cpp at line 4710 in Irrlicht v1.8 sources.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
Post Reply