Page 13 of 56

Posted: Mon Feb 16, 2009 3:30 am
by BlindSide
What do you see if you draw2DImage the depthMap?

Posted: Mon Feb 16, 2009 3:51 am
by Halifax
Here's the code I use in the loop:

Code: Select all

effect->update();
                videoDriver->draw2DImage( effect->getDepthMapTexture(), position2di( 0,0 ) );
And here's the output:
Image
I made sure the shader wasn't outputing those values. It outputs vec4( 0.0, 1.0, 0.0, 1.0 ).

So as you can see, apparently the depth texture is the same as the backbuffer or something like that. Or I am doing something wrong.

Posted: Mon Feb 16, 2009 5:48 am
by Halifax
Well BlindSide, after our talk in IRC, I decided to have a look for myself through the code. Here's the image first, of the working implementation:
Image
I have a vague explanation for the reason this was messing up, but nothing clear. To fix the code, you just swap the section that renders the depth buffer above the section that renders the post processing routines:

Code: Select all

if(DepthPass)
	{
		driver->setRenderTarget(DepthRTT, true, true, SColor(0xffffffff));

		// Set max distance constant for depth shader.
		depthMC->FarLink = smgr->getActiveCamera()->getFarValue();

		for(u32 i = 0;i < DepthPassArray.size();++i)
		{
			BufferMaterialList.set_used(0);

			for(u32 g = 0;g < DepthPassArray[i]->getMaterialCount();++g)
				BufferMaterialList.push_back(DepthPassArray[i]->getMaterial(g).MaterialType);

			DepthPassArray[i]->setMaterialType((E_MATERIAL_TYPE)Depth);
			DepthPassArray[i]->OnAnimate(device->getTimer()->getTime());
			DepthPassArray[i]->render();

			for(u32 g = 0;g < DepthPassArray[i]->getMaterialCount();++g)
				DepthPassArray[i]->getMaterial(g).MaterialType = (E_MATERIAL_TYPE)BufferMaterialList[g];
		}
	}
	
	if(PostProcessingRoutinesSize)
	{
		bool Alter = 0;
		ScreenQuad.getMaterial().setTexture(1, ScreenRTT);
		ScreenQuad.getMaterial().setTexture(2, DepthRTT);
		for(u32 i = 0;i < PostProcessingRoutinesSize;++i)
		{
			ScreenQuad.getMaterial().MaterialType = (E_MATERIAL_TYPE)PostProcessingRoutines[i];

			Alter = !Alter;
			ScreenQuad.getMaterial().setTexture(0, i == 0 ? ScreenRTT : ScreenQuad.rt[int(!Alter)]);
			driver->setRenderTarget(i >= PostProcessingRoutinesSize - 1 ?
				outputTarget : ScreenQuad.rt[int(Alter)], true, true, ClearColour);
			ScreenQuad.render(driver);
		}
	}
In the old code, the depth buffer was the last thing rendered. Thus it was a frame behind the actual framebuffer.

Given that it's the last render target enabled, the only reasoning I can possibly find is that beginScene() must annihilate the depth buffer, thus making it unusable for the post processing routines, because it used to go:

Code: Select all

beginScene
       |
postProcess
       |
renderDepth
There must be another explanation, because that certainly doesn't explain why the draw2DImage for the depth buffer doesn't work. The only reason I can think for that is because the depth buffer is still bound from setRenderTarget, and trying to use it with draw2DImage isn't possible.

Oh well, it works, and that's the only explanations I could think of.

Posted: Mon Feb 16, 2009 8:04 am
by BlindSide
Thanks Halifax, that may have taken me a while to track down. I'll try and look more closely at the cause of this.

Cheers, I hope the depth buffer is useful.

Posted: Mon Feb 16, 2009 9:19 am
by Halifax
BlindSide wrote:Thanks Halifax, that may have taken me a while to track down. I'll try and look more closely at the cause of this.

Cheers, I hope the depth buffer is useful.
Yeah, definitely. I'll be posting my SSAO work soon. It definitely needs some help. :(

Posted: Mon Feb 16, 2009 10:36 am
by Pyritie
Bleh, this is going to be a pain to translate into jirr D:

Posted: Mon Feb 16, 2009 4:00 pm
by 9YkKsvXM
-

Posted: Mon Feb 16, 2009 9:06 pm
by Halifax
Nate_D wrote:okay so I am trying to output the depth map texture.

I have enabled the depth pass and added the room node to the depth pass using these lines of code...

effect->enableDepthPass(true);
effect->addNodeToDepthPass(room);

and then in the runloop I am trying to get the depth map texture and set it as the texture for a cube just to see if it works...

irr::video::ITexture* test = effect->getDepthMapTexture();
cube_node->setMaterialTexture(0, test)

so far the cube is completely black, if I don't enable the depthpass however it is completely white. Am I doing this completely the wrong way?
You have to apply the changes that I spoke of about 3 posts above you or so. If you don't have the time or the know-how, then you could just wait for BlindSide to put it into the main build. To get it working now, replace your two files with these:
effectWrapper.h (13.5 KB)
effectWrapper.cpp (17.6 KB)

The code you posted above should work fine. Mind you, the depth map texture is encoded in the red and green channels, so you are going to get a crazy looking texture. So the best thing to do would be to output it in a texture by using a fragment shader with the following(OpenGL):

Code: Select all

gl_FragColor = texture2D( DepthMapSampler, gl_TexCoord[0].st );
With that code you will get the grayish depth map that I displayed above.

By the way, BlindSide, addPostProcessingEffectFromFile doesn't return the material identification number. It's an easy fix, but I just wanted to tell you, because without that you can't use removePostProcessingEffect because it requires an identification number.

Posted: Mon Feb 16, 2009 10:51 pm
by 9YkKsvXM
-

Posted: Tue Feb 17, 2009 12:32 am
by Halifax
Alright, I'll have to take a run through that code sometime this week Nate_D. If a get a chance, I'll try to help you out, but for now maybe someone else can offer their opinion.

Posted: Thu Feb 19, 2009 4:29 pm
by 9YkKsvXM
-

Posted: Thu Feb 19, 2009 5:58 pm
by 9YkKsvXM
-

Posted: Thu Feb 19, 2009 6:00 pm
by BlindSide
Ok sorry for the trouble I fixed this.

It turned out I wasn't setting the render target back to 0 after the depth pass, that's why you HAD to have a post processing pass after it to set the rendertarget to something else. Nate Dog's example didn't work because although he swapped the two sections around he didn't actually have a post processing effect applied.

Anyway this means theres no reason to swap the two sections around anymore (Although I'm not sure why I put the depth pass after the post processing path, you may want to experiment and see which arrangement keeps animations up to date, probably Halifax way is better anyway).

Here is the fixed file: http://irrlichtirc.g0dsoft.com/paste/345
(If you find it difficult to copy goto the bottom and there is a text box with the plain text version.)

Cheers

Posted: Thu Feb 19, 2009 6:31 pm
by Pyritie
BlindSide wrote: Here is the fixed file: http://irrlichtirc.g0dsoft.com/paste/345
(If you find it difficult to copy goto the bottom and there is a text box with the plain text version.)

Cheers
Could you upload it somewhere else too? That site's always down for me when I need it most. :(

Posted: Mon Mar 09, 2009 10:38 pm
by Prott
Hello,
first - Thank you for this great code. It`s very usefull.

But I have a problem. When I try to compile my application, I get this error:

Code: Select all

error LNK2019: unresolved external symbol "public: void __thiscall effectHandler::addPostProcessingEffectFromFile(class irr::core::string<char,class irr::core::irrAllocator<char> > const &)" (?addPostProcessingEffectFromFile@effectHandler@@QAEXABV?$string@DV?$irrAllocator@D@core@irr@@@core@irr@@@Z) referenced in function _main
I have following files in my project (using C++ .net 2005):
CScreenQuad.h
CShaderPre.cpp
CShaderPre.h
effectWrapper.cpp
effectWrapper.h
effectWrapperCB.h
main.cpp
Is there any other file which needs to be included or linked to the project?

Thanks again.