lightmaps + floor reflection working !!!

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.
zillion42
Posts: 324
Joined: Wed Aug 29, 2007 12:32 am
Location: Hamburg, Germany

lightmaps + floor reflection working !!!

Post by zillion42 »

Well,

the thread states help with a lightmapped reflection shader, but actually I haven't 'really' started yet. I've done some researching and had two attempts at it though. The goal would be either a HLSL or GLSL shader that renders lightmaps on top of a real not faked reflection. I read a bit about GLSL and HLSL on lighthouse3d and facewound and I got aquainted with some concepts of it but my experiece so far doesn't even allow me to turn off the bump completly in the realistic waterSceneNode (<-where reflection works really neatly imo), which was my last attempt. I looked at sio2's example and maybe someone can confirm this, the reflections there dont work correctly. I saw that Christian Clavet added some code to the reflections in the waterscenenode to adjust FOV of the reflection camera and scene camera... The problem I see in sio2's example looks just like that, but I haven't been able to fix it accordingly, which was my first attempt.
Now, all the fun I had with physics in irrlicht aside, I'll have to start working again tomorrow. Not so serious because we're only doing a test setup for an indoor scene, but for that a lightmapped + reflection shader would in fact be required.
Well the two best choices for me are either modifying sio2's or elveman's shader and while continuing to do so .... ANY help is appreciated.

Edit:
what I comprehend so far

1. set new render target
2. make a new cam and position it correctly according to reflection plane
3. render to target
4. set old render target and old cam
5. pass render target output as texture to shader and project with camera mapping. (is that what is called mWorldViewProj ?)
6. add lightmaps (using 2nd Set of UV coords) on top of diffuse and reflection to PS output.
7. render
Last edited by zillion42 on Tue Feb 03, 2009 6:34 am, edited 1 time in total.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

That all sounds about right, tell us how you get on with it. ;)
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
skumar
Posts: 201
Joined: Thu Feb 14, 2008 6:24 pm
Location: kerala state india
Contact:

Post by skumar »

Zillion i was also on this thing for some time and i am almost there. But i was bit busy with some design things....

I will say what i was doing....

From elvmans waterscenenode edited by clavet....i took the reflection thing and i was about to use the shader from sio2's demo for making the exact one...

I was able to switch off the waves of the waterSN. And the refection was perfect!......

im busy now also...i will make it soon....or else you can implement this ...not so hard....

use the setWaveHeight() function to zero for making the water flat...

I think we can replace/edit the shaders from sio2 and waterSN to get the final thing
skumar
poulpi33
Posts: 31
Joined: Tue Jan 06, 2009 8:01 pm
Location: Bordeaux, France

Post by poulpi33 »

zillion42 wrote:hey,
After posting in beginners help
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=32135
I saw your current project... studio.exe and must say congratulations, I've been trying to do that for month. I thought maybe you could help me a little in finding a good way to get lightmaps and reflections working together...
awesome work...
zillion42
If you now how to do lightmapping and reflection, doing both is simple, simply add the reflection to the final output. Reflections in the real world are additive, like light.

about lightmapping:
If you have diffuse lighting too, then it depend if your lightmap is a "real" lightmap (mean a source of light) or an ambient occlusion map.
If it is a "real" lightmap then your lightmap will be your ambient light.
If it's an ambient occlusion map, then you need to multiply it with your ambient value.

But in both case, reflections are always added to the whole result.

- lightmap:
output = (color*(lightmap+diffuse)) + (reflection*fresnel)

- ambient occlusion map:
output = (color*((ambient*occlusionMap)+diffuse)) + (reflection*fresnel)

Note that in my irrlicht studio demo, i did not yet code the fresnel for reflection. If you want to see the final result you can see the one i first did in Quest3d: Download
zillion42
Posts: 324
Joined: Wed Aug 29, 2007 12:32 am
Location: Hamburg, Germany

Post by zillion42 »

whoOo...
in fact I'm so darn lazy these last few days I could fall asleep writing forum posts...

I'll post as soon as I get a little further.
Last edited by zillion42 on Thu Jan 29, 2009 3:15 am, edited 1 time in total.
zillion42
Posts: 324
Joined: Wed Aug 29, 2007 12:32 am
Location: Hamburg, Germany

Post by zillion42 »

ok, i've overcome my lazyness a little bit and kind of fixed the giroom example... The problem seems to be the b3d files or rather the meshes not being properly zeroed out before exporting or an export error, or god knows what. The floor is offset by 361 units on Y and the red "le cobusier" thing seems strange aswell...
Anyway I realized that what I needed is more on a per polygon level because a simple floor won't do in many cases, consider a staircase for example. On the other hand one could use environment maps for such purposes, but looking at the hardware ray-tracing examples at least there is a possibility...

sio2's giroom example

edit:
the floor was offset by 419.0f, the whole rendering seems to lag behind, and when I turn on AA the blue poles disappear :roll:
zillion42
Posts: 324
Joined: Wed Aug 29, 2007 12:32 am
Location: Hamburg, Germany

Post by zillion42 »

I tried to make the whole thing a scene node, hoping the onAnimate() would maybe render the reflections more synchronized but it keeps crashing when tying to drawAll() with the passed SceneManger...
any ideas ?
DynamicReflectionNode
CDynamicReflections.h

Code: Select all

//
// Dynamic Reflections.
// sio2 'at' users.sourceforge.net
//

#pragma once

#include <string>
#include <irrlicht.h>
using namespace irr;

class CDynamicReflections : public scene::ISceneNode, video::IShaderConstantSetCallBack
{
public:
	CDynamicReflections(scene::ISceneManager* smgr,
		core::dimension2di RenderTargetSize, 
		scene::ISceneNode *nodeFloor, 
		scene::ISceneNode* parent = 0, 
		s32 id = -1);
	virtual ~CDynamicReflections();
	
	// frame
	virtual void OnRegisterSceneNode();

	virtual void OnAnimate(u32 timeMs);
	
	// renders 
	virtual void render();

	// returns the axis aligned bounding box of terrain
	virtual const core::aabbox3d<f32>& getBoundingBox() const;
	
	virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData);

private:
	f32						m_cameraY; // Y position of the camera
	scene::ISceneNode       *m_reflectNode;
	scene::ICameraSceneNode *m_reflectCam;
	video::ITexture         *m_renderTarget;
	video::IVideoDriver*	_driver;
	scene::ISceneManager*	SceneManager;
	ITimer*					Timer;
};
CDynamicReflections.cpp

Code: Select all

//
// CDynamicReflections.
// sio2 'at' users.sourceforge.net
//

#include "CDynamicReflections.h"

CDynamicReflections::CDynamicReflections(scene::ISceneManager* smgr, 
										 core::dimension2di RenderTargetSize, 
										 scene::ISceneNode *nodeFloor,
										 scene::ISceneNode* parent, 
										 s32 id):
	scene::ISceneNode( parent?parent:smgr->getRootSceneNode(),smgr, id),
						SceneManager(smgr)
					
{
	_driver = SceneManager->getVideoDriver();
	
	scene::ICameraSceneNode *camera = SceneManager->getActiveCamera();
	m_reflectCam = SceneManager->addCameraSceneNode();
	m_cameraY	= f32(camera->getPosition().Y);	

	m_reflectCam->setFarValue(camera->getFarValue());
	m_reflectCam->setFOV(camera->getFOV());
	
	SceneManager->setActiveCamera(camera);

	m_reflectNode = nodeFloor;
	nodeFloor->grab();

	s32 dmat = video::EMT_LIGHTMAP;
	video::E_DRIVER_TYPE edt = _driver->getDriverType();
	video::IGPUProgrammingServices *gpu = _driver->getGPUProgrammingServices();
	dmat = gpu->addHighLevelShaderMaterialFromFiles(
		"floorReflect.hlsl", "vertexMain", video::EVST_VS_1_1,
		"floorReflect.hlsl", "pixelMain", video::EPST_PS_2_0,
		this, video::EMT_LIGHTMAP);

	m_reflectNode->setMaterialType((video::E_MATERIAL_TYPE)dmat);
	m_reflectNode->setMaterialFlag(video::EMF_BACK_FACE_CULLING, true);
	m_reflectNode->setMaterialFlag(video::EMF_LIGHTING, false);
	m_reflectNode->setMaterialFlag(video::EMF_FOG_ENABLE, false);

	m_renderTarget = _driver->addRenderTargetTexture(RenderTargetSize);

	// 0 == diffuse texture
	// 1 == lightmap
	// 2 == reflection texture
	m_reflectNode->setMaterialTexture(2, m_renderTarget);

}

// --------------------------------------------------------------------------

CDynamicReflections::~CDynamicReflections()
{
	m_renderTarget->drop();
	m_reflectNode->drop();
}

// --------------------------------------------------------------------------

void CDynamicReflections::OnRegisterSceneNode()
{
	if (IsVisible)
		SceneManager->registerNodeForRendering(this);

	ISceneNode::OnRegisterSceneNode();
}
// --------------------------------------------------------------------------

void CDynamicReflections::OnAnimate(u32 timeMs)
{
	if (!(m_renderTarget && m_reflectNode && SceneManager))
		return;
	if (!_driver) 
		return;
	
	if(IsVisible)
	{
		_driver->setRenderTarget(m_renderTarget, true, true, video::SColor(255,255,255,255));

		scene::ICameraSceneNode *camera = SceneManager->getActiveCamera();
		f32 posY = m_reflectNode->getPosition().Y;
		
		core::vector3df position=camera->getPosition();
		position.Y=-position.Y+2*posY; //position of the water
		position.Y -= 418.5f;
		m_reflectCam->setPosition(position);


		core::vector3df target=camera->getTarget();
		target.Y=-target.Y+2*posY;
		target.Y -= 418.5f;
		m_reflectCam->setTarget(target);

		SceneManager->setActiveCamera(m_reflectCam);
		m_reflectNode->setVisible(false);
		SceneManager->drawAll();    

		_driver->setRenderTarget(0);
		m_reflectNode->setVisible(true);
		SceneManager->setActiveCamera(camera);
	}

	ISceneNode::OnAnimate(timeMs);
}

// --------------------------------------------------------------------------

void CDynamicReflections::render()
{

}
// --------------------------------------------------------------------------

const core::aabbox3d<f32>& CDynamicReflections::getBoundingBox() const
{
	return m_reflectNode->getBoundingBox();
}
// --------------------------------------------------------------------------

void CDynamicReflections::OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
{
	if (!services) return;
	video::IVideoDriver* driver = services->getVideoDriver();
	if (!driver) return;

	core::matrix4 worldViewProj;
	worldViewProj = driver->getTransform(video::ETS_PROJECTION);         
	worldViewProj *= driver->getTransform(video::ETS_VIEW);
	worldViewProj *= driver->getTransform(video::ETS_WORLD);
	services->setVertexShaderConstant("mWorldViewProj", worldViewProj.pointer(), 16);
}
zillion42
Posts: 324
Joined: Wed Aug 29, 2007 12:32 am
Location: Hamburg, Germany

Post by zillion42 »

OK, I've got it finally... Based on the RealisticWaterSceneNode and just test lightmapped with cinema4d using B3D-Exporter V1.4...
....DOWNLOAD....
Image
Image
Image
DOWNLOAD


noise/noise.h : no such file or directory.
|
\/
fixed...
Last edited by zillion42 on Tue Feb 03, 2009 9:44 am, edited 1 time in total.
Katsankat
Posts: 178
Joined: Sun Mar 12, 2006 4:15 am
Contact:

Post by Katsankat »

noise/noise.h : no such file or directory.
skumar
Posts: 201
Joined: Thu Feb 14, 2008 6:24 pm
Location: kerala state india
Contact:

Post by skumar »

Thanks zillion42 .....

after making a heavy scene i will try integrating that


:D
skumar
netpipe
Posts: 670
Joined: Fri Jun 06, 2008 12:50 pm
Location: Edmonton, Alberta, Canada
Contact:

hehey

Post by netpipe »

the opengl version needs glsl shaders :P
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Now add some bumpmapping and distort the reflection by the bumpmap normals, it will look sweet!
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
zillion42
Posts: 324
Joined: Wed Aug 29, 2007 12:32 am
Location: Hamburg, Germany

Post by zillion42 »

I added some bloom instead using Nils Daumann's postprocessing class
EDIT:
And I added the bump to reflection again...
Image
And I don't really know how many laws I'm breaking to show you this piece of software from my old workplace but, it's a really neat thing to have...In a distant future we need one for irrlicht aswell... Well basically it's a HLSL post processing gui that can adjust the values of saturate/desaturate, bloom, Fog, DoF and Noise. That last one noise is a real enhancement and often underestimated. You can see a bit in the background.
Image
zillion42
Posts: 324
Joined: Wed Aug 29, 2007 12:32 am
Location: Hamburg, Germany

Post by zillion42 »

And...

Quite a big concern for me still, is the inability to use Anti-Aliasing in conjunction with RTT reflection... To make myself more clear, it's not my problem that I can't Anti-Alias my Render Target, the problem is that turning on Anti-Aliasing in the device effectively messes up(destroys) my reflections... AND... I have SOOOOO NOOO idea why that is so, that it makes me pull my hair out while sleeping.

speculation welcome...
skumar
Posts: 201
Joined: Thu Feb 14, 2008 6:24 pm
Location: kerala state india
Contact:

Post by skumar »

So you are there....near the peak... :D

Thank you for taking such an effort and getting results....

So anti aliasing is the problemmm

Why dont you try to blur the reflection rtt....does it have any problems????

in some floor reflection this can help....
skumar
Post Reply