Glsl Texture projection HELP[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
oldskoolPunk
Posts: 199
Joined: Wed Nov 29, 2006 4:07 am

Glsl Texture projection HELP[SOLVED]

Post by oldskoolPunk »

Hi

I am still having trouble understanding !

The following image shows how close it "appears".
Image

But if you look you can see that it is like to small? Then when I zoom back it looks like this
Image

Will you look at my code and lead me in the right direction?

Code: Select all


tMatrix.buildTextureTransform(	0,
				irr::core::vector2df(0.5f,0.5f),
				irr::core::vector2df(0.0f,0.0f),
				irr::core::vector2df(1.0f,1.0f)
			     );
		
		
tMatrix *= Graphics::shadowCam->getProjectionMatrix();
tMatrix *= Graphics::shadowCam->getViewMatrix();

//tMatrix.setTextureTranslate(0.5f,0.5f);
//tMatrix.setTextureScale(0.5f,0.5f);
		
services->setVertexShaderConstant("tMatrix", reinterpret_cast<irr::f32*>(&tMatrix), 16);

Code: Select all

// reciever vertex shader

uniform mat4 tMatrix;
varying vec3 TexCoord;

void main()
{
	gl_Position	= ftransform();
	TexCoord	= tMatrix * gl_Vertex;			
}

Code: Select all

// reciever pixel shader

uniform sampler2D renderTarget;
varying vec3 TexCoord;

void main()
{
	float Shad = max(texture2DProj(renderTarget,TexCoord),0.0);
	
	vec4 Color = vec4(0.1,0.95,0.1,1) ;
	if (Shad<1.0) Color *= 0.4;
       gl_FragColor = Color;
}
When I try to transform the texture with

Code: Select all

//tMatrix.setTextureTranslate(0.5f,0.5f);
//tMatrix.setTextureScale(0.5f,0.5f);
I get this
Image

Please help! :D
Last edited by oldskoolPunk on Mon Nov 17, 2008 2:53 pm, edited 1 time in total.
Signature? I ain't signin nuthin!
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Code: Select all

tMatrix.buildTextureTransform(   0,
            irr::core::vector2df(0.5f,0.5f),
            irr::core::vector2df(0.0f,0.0f),
            irr::core::vector2df(1.0f,1.0f)
              );
      
      
tMatrix *= Graphics::shadowCam->getProjectionMatrix();
tMatrix *= Graphics::shadowCam->getViewMatrix(); 
You don't need all of this.

Just do:

Code: Select all

      
tMatrix = Graphics::shadowCam->getProjectionMatrix();
tMatrix *= Graphics::shadowCam->getViewMatrix();
tMatrix *= (AbsoluteTransformationMatrix of scene node); 
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
oldskoolPunk
Posts: 199
Joined: Wed Nov 29, 2006 4:07 am

Post by oldskoolPunk »

Thanks BlindSide

I didnt know I had to multiply the absolute transformation of the scene node.
The scene node of the caster? or the reciever?

I tried both and got similar results.
Image

Code: Select all

irr::core::matrix4 tMatrix;
tMatrix = Graphics::shadowCam->getProjectionMatrix();
tMatrix *= Graphics::shadowCam->getViewMatrix();
tMatrix *= Character::node->getAbsoluteTransformation();
tMatrix.setTranslation(irr::core::vector3df (0.5f,0.5f,0.0f));
tMatrix.setScale(irr::core::vector3df(0.5f,0.5f,1.0f));
		
services->setVertexShaderConstant("tMatrix", reinterpret_cast<irr::f32*>(&tMatrix), 16);
Signature? I ain't signin nuthin!
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

I havent studeied the code in details but watch out when declaring varyings!
varying vec3 TexCoord; <- make sure this has a valid 4th component
Ie: make it a vec 4 since texture2DProj needs the .w to prespective correct the projection. The current code should raise a warning or not compile on some cards since tex2dproj has vec4 as an argument type.
"Irrlicht is obese"

If you want modern rendering techniques learn how to make them or go to the engine next door =p
oldskoolPunk
Posts: 199
Joined: Wed Nov 29, 2006 4:07 am

Post by oldskoolPunk »

Thanks omaremad.

I tried a vec4 for a long time, but recently changed to a vec3, only then did I even see a projection at all.
According to the following sources, texture2DProj will accept a vec3 OR a vec4, in either case it just uses the last coordinate to divide by the s and t.

"The third component of coord is ignored for the vec4 coord variant"

http://www.opengl.org/registry/doc/GLSL ... 1.20.8.pdf

http://www.opengl.org/sdk/docs/tutorial ... turing.php
Signature? I ain't signin nuthin!
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

Hmm never knew it was flexible :oops:

Anyway the fourth coord of TexCoord = tMatrix * gl_Vertex; is definitely different from the third so if you use the float3 you need to put w into the z of Texcoord.
"Irrlicht is obese"

If you want modern rendering techniques learn how to make them or go to the engine next door =p
oldskoolPunk
Posts: 199
Joined: Wed Nov 29, 2006 4:07 am

Post by oldskoolPunk »

ok thank you omaremad.

I changed it back to a vec4, but still getting the same results :(

I have written a test outside of my framework, so it can be easily copied and pasted, if you or anyone else has the time to try to help me out with it.

It is a sphere and a plane, with an arrow showing the location and direction of the light and shadow camera. Here is a screen shot.

Image

And here is the simplified code.

Code: Select all

# include <Irrlicht.h>
#pragma comment (lib,"Irrlicht.lib")


int main()
{
///// make all the Irrlicht pointers ///////
	irr::IrrlichtDevice *device =	irr::createDevice(	irr::video::EDT_OPENGL, 
									irr::core::dimension2d<irr::s32>(800,600),
									32,
									true,
									false,
									true
									);
	irr::video::IVideoDriver *driver		 = device->getVideoDriver();
	irr::scene::ISceneManager *scenemanager  = device->getSceneManager();
	irr::video::IGPUProgrammingServices *gpu = driver->getGPUProgrammingServices();
///////////////////////////////////////////

///// add the lights and cameras /////////
	irr::scene::ILightSceneNode  *light =     scenemanager->addLightSceneNode ();
	light->setPosition(irr::core::vector3df(20,20,0));

	irr::scene::ICameraSceneNode *shadowCam = scenemanager->addCameraSceneNode();
	shadowCam->setPosition(irr::core::vector3df(20,20,0));
	shadowCam->setTarget  (irr::core::vector3df(0,0,0));

	irr::scene::IAnimatedMeshSceneNode *arrowNode = scenemanager->addAnimatedMeshSceneNode(scenemanager->addArrowMesh("arrow",irr::video::SColor(255,50,75,250),irr::video::SColor(255,150,175,250),4,8,2,.6f,.05f,.3f));
	arrowNode->setMaterialFlag(irr::video::EMF_LIGHTING,false);
	arrowNode->setPosition(light->getPosition());
	arrowNode->setRotation(irr::core::vector3df(0,0,135));
	arrowNode->setScale(irr::core::vector3df(3,3,3));

	irr::scene::ICameraSceneNode *renderCam = scenemanager->addCameraSceneNodeFPS();
	renderCam->setPosition(irr::core::vector3df(0,25,-50));
	renderCam->setTarget  (irr::core::vector3df(0,0,0));
/////////////////////////////////////////

//// add a sphere //////////////////////
	irr::scene::IAnimatedMesh *sphereMesh = scenemanager->addSphereMesh("sphere",5.0f,32,32);
	irr::scene::IAnimatedMeshSceneNode *sphereNode = scenemanager->addAnimatedMeshSceneNode(sphereMesh);
	sphereNode->getMaterial(0).DiffuseColor.set(255,250,75,50);
	sphereNode->setPosition(irr::core::vector3df(0,5,0));
///////////////////////////////////////

///// create the render target////////
	irr::video::ITexture *renderTarget = driver->createRenderTargetTexture(irr::core::dimension2di(512,512));



////  build the matrix to hopefully project the shadow  /////////////
	irr::core::matrix4 tMatrix;
	tMatrix=shadowCam->getProjectionMatrix();// the light's projection
	tMatrix *= shadowCam->getViewMatrix();  //  the lights view
	tMatrix *= sphereNode->getAbsoluteTransformation();// the sceneNode's position 
	tMatrix.setTranslation(irr::core::vector3df(0.5f,0.5f,0.0f));// translation and scale
	tMatrix.setScale(irr::core::vector3df(0.5f,0.5f,1.0f));     // to make it 0-1
////////////////////////////////////

/////add the plane/////////////////
	irr::s32 shader;
	shader = gpu->addHighLevelShaderMaterialFromFiles(	"VERT.vert",
														"main",
														irr::video::EVST_VS_2_0,
														"FRAG.frag",
														"main",
														irr::video::EPST_PS_2_0,
														NULL,
														irr::video::EMT_SOLID,
														0
														);
				

	irr::video::SMaterial planeMaterial;
	planeMaterial.MaterialType = (irr::video::E_MATERIAL_TYPE)shader;
	planeMaterial.DiffuseColor.set(255,50,250,75);
	planeMaterial.setTexture(0,renderTarget); // set the renderTarget texture
	planeMaterial.setTextureMatrix(0,tMatrix);// set the lights view projection matrix

	irr::scene::IAnimatedMesh *planeMesh = scenemanager->addHillPlaneMesh("plane",irr::core::dimension2df(100.0f,100.0f),irr::core::dimension2d<irr::u32>(1,1),&planeMaterial);
	irr::scene::IAnimatedMeshSceneNode *planeNode = scenemanager->addAnimatedMeshSceneNode (planeMesh);
/////////////////////////////////////

	while (device->run())
	{
		driver->beginScene(true,true,0);

//////// shadow pass ////////////
		scenemanager->setActiveCamera(shadowCam);
		driver->setRenderTarget(renderTarget, true, true, irr::video::SColor(255,255,255,255));
		shadowCam->render();
		sphereNode->render();

///////  render pass ///////////
		scenemanager->setActiveCamera(renderCam);
		driver->setRenderTarget(0,true,true,0);
		scenemanager->drawAll();

		driver->endScene();
	}

	device->drop();
}
These are the only 2 external files, the shader for projecting the texture. SUper simple.

Code: Select all

//  vertex shader

varying vec4 TexCoord;

void main()
{
	gl_Position		= ftransform();
	TexCoord	    = gl_TextureMatrix[0] * gl_Vertex;
}

Code: Select all

//  pixel shader

uniform sampler2D renderTarget;
varying vec4 TexCoord;
varying vec4 Color;

void main()
{
	vec4 Light = texture2DProj(renderTarget,TexCoord);

	gl_FragColor = Light;
}
Please take just a few moments with this example? I made it as short as I could. What more am I missing?
Signature? I ain't signin nuthin!
oldskoolPunk
Posts: 199
Joined: Wed Nov 29, 2006 4:07 am

Post by oldskoolPunk »

I added these lines to the shadow rendering pass

Code: Select all

//////// shadow pass ////////////
scenemanager->setActiveCamera(shadowCam);
driver->setRenderTarget(renderTarget,true,true,irr::video::SColor(255,255,255,255));

driver->setTransform(irr::video::ETS_PROJECTION,shadowCam->getProjectionMatrix());
driver->setTransform(irr::video::ETS_VIEW,shadowCam->getViewMatrix());
driver->setTransform(irr::video::ETS_WORLD,shadowCam->getAbsoluteTransformation());

shadowCam->render();
sphereNode->render();
And my vertex shader now looks like this, to show whats going on

Code: Select all

//  vertex shader

varying vec4 TexCoord;


void main()
{
	gl_Position		= gl_ModelViewProjectionMatrix * gl_Vertex;
	TexCoord	    = gl_TextureMatrix[0] * gl_Vertex;
}
So if gl_Position is the model * view * projection of the render camera multiplied by the position of the vertex, then it seems that TexCoord should be the model * view * projection of the light multiplied by the vertex.
So I build it like this in the test code above.

Code: Select all

////  build the matrix to hopefully project the shadow  /////////////
irr::core::matrix4 tMatrix;
tMatrix=shadowCam->getProjectionMatrix();// the light's projection
tMatrix *= shadowCam->getViewMatrix();  //  the lights view
tMatrix *= sphereNode->getAbsoluteTransformation();// the sceneNode's position

tMatrix.setTranslation(irr::core::vector3df(0.5f,0.5f,0.0f));
tMatrix.setScale(irr::core::vector3df(0.5f,0.5f,1.0f));
But still nothing???
Eperimenting gives varied results.
Image
Image
Image
Image

Help :)
Signature? I ain't signin nuthin!
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Stop doing that weird texture transform thing and do the multiplying/adding in the shader. (I don't think you are shifting by the right amount btw.)

Take a look at this:

Code: Select all

SMPos.xy  = SMPos.xy / SMPos.w / 2.0 + vec2(0.5, 0.5);
Thats what I do to the tex coords. The texcoords are just gl_Vertex multiplied by the world matrix of the object and the projection and view of the shadow camera like you were doing.

Cheers

EDIT: Remember to use normal texture2D with this not texture2DProj as you are already doing the perspective divide here.

Tell me how that works out.

EDIT2: If you are adament on using the matrix to shift and scale the texcoords then I think you have to do it before multiplying by projection, etc.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
oldskoolPunk
Posts: 199
Joined: Wed Nov 29, 2006 4:07 am

Post by oldskoolPunk »

Thank you Blindside!

So now I understand that you are dividing the depth with the x and y cool.
OK so I added your code to it here

Code: Select all

//  vertex shader

uniform mat4 vMatrix;
uniform mat4 tMatrix;

varying vec3 TexCoord;


void main()
{
	gl_Position		= vMatrix * gl_Vertex;
	TexCoord	    = tMatrix * gl_Vertex;
	TexCoord.xy     = TexCoord.xy / TexCoord.z / 2.0 + vec2(0.5, 0.5);
	
}
I also added a callback function, creating both matrices myself, similar to the way you did it in your effects suite.

Code: Select all

class MyShaderCallBack : public irr::video::IShaderConstantSetCallBack
{
public:

	virtual void OnSetConstants(irr::video::IMaterialRendererServices* services, irr::s32 userData)
	{
		irr::video::IVideoDriver *driver=services->getVideoDriver();

		irr::s32 renderTarget = 0;
		services->setPixelShaderConstant("renderTarget", reinterpret_cast <irr::f32*>(&renderTarget), 1);

		irr::core::matrix4 vMatrix;
		vMatrix = driver->getTransform(irr::video::ETS_PROJECTION);			
		vMatrix *= driver->getTransform(irr::video::ETS_VIEW);
		vMatrix *= driver->getTransform(irr::video::ETS_WORLD);
		services->setVertexShaderConstant("vMatrix", vMatrix.pointer(), 16);

		irr::core::matrix4 tMatrix;
		tMatrix = shadowCam->getProjectionMatrix();			
		tMatrix *= shadowCam->getViewMatrix();
		tMatrix *= sphereNode->getAbsoluteTransformation();
		services->setVertexShaderConstant("tMatrix", tMatrix.pointer(), 16);
	}
}callback;
Im still getting this :(
Image

I tried doing the transformation before everything in the program, but I got this
Image
Using gl_TextureMatrix[0] got me this (which is why I decided to start passing both matrices myself
Image
Heres another random stab
Image
Signature? I ain't signin nuthin!
oldskoolPunk
Posts: 199
Joined: Wed Nov 29, 2006 4:07 am

Post by oldskoolPunk »

It is now 241 am here so I have to get a cfew hours sleep .

Here is where I am at I will pick back up tomorrow during football. :D

Code: Select all

# include <Irrlicht.h>
#pragma comment (lib,"Irrlicht.lib")

irr::scene::ICameraSceneNode *shadowCam;
irr::scene::IAnimatedMeshSceneNode *sphereNode;

class MyShaderCallBack : public irr::video::IShaderConstantSetCallBack
{
public:

	virtual void OnSetConstants(irr::video::IMaterialRendererServices* services, irr::s32 userData)
	{
		irr::video::IVideoDriver *driver=services->getVideoDriver();

		irr::s32 renderTarget = 0;
		services->setPixelShaderConstant("renderTarget", reinterpret_cast <irr::f32*>(&renderTarget), 1);

		irr::core::matrix4 vMatrix;
		vMatrix = driver->getTransform(irr::video::ETS_PROJECTION);			
		vMatrix *= driver->getTransform(irr::video::ETS_VIEW);
		vMatrix *= driver->getTransform(irr::video::ETS_WORLD);
		services->setVertexShaderConstant("vMatrix", vMatrix.pointer(), 16);

		irr::core::matrix4 tMatrix;
		tMatrix = shadowCam->getProjectionMatrix();			
		tMatrix *= shadowCam->getViewMatrix();
		tMatrix *= sphereNode->getAbsoluteTransformation();
		services->setVertexShaderConstant("tMatrix", tMatrix.pointer(), 16);
	}
}callback;


int main()
{
///// make all the Irrlicht pointers ///////
	irr::IrrlichtDevice *device =	irr::createDevice(	irr::video::EDT_OPENGL, 
									irr::core::dimension2d<irr::s32>(800,600),
									32,
									true,
									false,
									true
									);
	irr::video::IVideoDriver *driver		 = device->getVideoDriver();
	irr::scene::ISceneManager *scenemanager  = device->getSceneManager();
	irr::video::IGPUProgrammingServices *gpu = driver->getGPUProgrammingServices();
///////////////////////////////////////////

///// add the lights and cameras /////////
	irr::scene::ILightSceneNode  *light =     scenemanager->addLightSceneNode ();
	light->setPosition(irr::core::vector3df(20,20,0));

	shadowCam = scenemanager->addCameraSceneNode();
	shadowCam->setPosition(light->getPosition());
	shadowCam->setTarget  (irr::core::vector3df(0,0,0));

	irr::scene::IAnimatedMeshSceneNode *arrowNode = scenemanager->addAnimatedMeshSceneNode(scenemanager->addArrowMesh("arrow",irr::video::SColor(255,50,75,250),irr::video::SColor(255,150,175,250),4,8,2,.6f,.05f,.3f));
	arrowNode->setMaterialFlag(irr::video::EMF_LIGHTING,false);
	arrowNode->setPosition(light->getPosition());
	arrowNode->setRotation(irr::core::vector3df(0,0,135));
	arrowNode->setScale(irr::core::vector3df(3,3,3));

	irr::scene::ICameraSceneNode *renderCam = scenemanager->addCameraSceneNodeFPS();
	renderCam->setPosition(irr::core::vector3df(0,25,-50));
	renderCam->setTarget  (irr::core::vector3df(0,0,0));
/////////////////////////////////////////

//// add a sphere //////////////////////
	irr::scene::IAnimatedMesh *sphereMesh = scenemanager->addSphereMesh("sphere",5.0f,32,32);
	sphereNode = scenemanager->addAnimatedMeshSceneNode(sphereMesh);
	sphereNode->getMaterial(0).DiffuseColor.set(255,250,75,50);
	sphereNode->setPosition(irr::core::vector3df(0,5,0));
	shadowCam->setTarget(sphereNode->getPosition());
///////////////////////////////////////

///// create the render target////////
	irr::video::ITexture *renderTarget = driver->createRenderTargetTexture(irr::core::dimension2di(512,512));

/////add the plane/////////////////
	irr::s32 shader;
	shader = gpu->addHighLevelShaderMaterialFromFiles(	"VERT.vert",
														"main",
														irr::video::EVST_VS_2_0,
														"FRAG.frag",
														"main",
														irr::video::EPST_PS_2_0,
														&callback,
														irr::video::EMT_SOLID,
														0
														);
				

	irr::video::SMaterial planeMaterial;
	planeMaterial.MaterialType = (irr::video::E_MATERIAL_TYPE)shader;
	planeMaterial.DiffuseColor.set(255,50,250,75);
	planeMaterial.setTexture(0,renderTarget); // set the renderTarget texture
//	planeMaterial.setTextureMatrix(0,tMatrix);// set the lights view projection matrix

	irr::scene::IAnimatedMesh *planeMesh = scenemanager->addHillPlaneMesh("plane",irr::core::dimension2df(100.0f,100.0f),irr::core::dimension2d<irr::u32>(1,1),&planeMaterial);
	irr::scene::IAnimatedMeshSceneNode *planeNode = scenemanager->addAnimatedMeshSceneNode (planeMesh);
/////////////////////////////////////

	while (device->run())
	{
		driver->beginScene(true,true,0);

//////// shadow pass ////////////
		scenemanager->setActiveCamera(shadowCam);
		driver->setRenderTarget(renderTarget, true, true, irr::video::SColor(255,255,255,255));
		//driver->setTransform(irr::video::ETS_PROJECTION,shadowCam->getProjectionMatrix());
		//driver->setTransform(irr::video::ETS_VIEW,shadowCam->getViewMatrix());
		//driver->setTransform(irr::video::ETS_WORLD,shadowCam->getAbsoluteTransformation());
		shadowCam->render();
		sphereNode->render();

///////  render pass ///////////
		scenemanager->setActiveCamera(renderCam);
		driver->setRenderTarget(0,true,true,0);
		scenemanager->drawAll();

		driver->endScene();
	}

	device->drop();
}

Code: Select all

//  vertex shader

uniform mat4 vMatrix;
uniform mat4 tMatrix;

varying vec4 TexCoord;


void main()
{
	gl_Position		= vMatrix * gl_Vertex;
	TexCoord	    = tMatrix * gl_Vertex;
	TexCoord.xy     = TexCoord.xy / TexCoord.w / 2.0 + vec2(0.5, 0.5);
	
}

Code: Select all

//  pixel shader

uniform sampler2D renderTarget;
varying vec4 TexCoord;

void main()
{
	
	vec4 Light = texture2D(renderTarget,TexCoord);
	gl_FragColor =  Light;
}
Last edited by oldskoolPunk on Sun Nov 16, 2008 4:40 pm, edited 1 time in total.
Signature? I ain't signin nuthin!
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Read my post again, you missed some obvious things.

1. You don't need to do both the texture scaling/shifting and pixel shader scaling/shifting, don't do the scaling/shifting using the texture matrix, I told you to take that off remember? Just do it like I suggested in the first post.

2. W component, not Z component divide!

Cheers
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
oldskoolPunk
Posts: 199
Joined: Wed Nov 29, 2006 4:07 am

Post by oldskoolPunk »

AAhh yes your right oops.

I have edited the previous post, because my paste contained some things I was trying. But the first image is what I get following your instructions exactly.
Image

I am going to try some more.
Signature? I ain't signin nuthin!
oldskoolPunk
Posts: 199
Joined: Wed Nov 29, 2006 4:07 am

Post by oldskoolPunk »

Hey!

Thanks to some tips from Blindside (he pretty much rewrote the fragemt shader) its working now :D

Image

Firstly, adding the matrix to the node and retrieving it in the shader with gl_TextureMatrix[0] gives me different results. I have to pass the matrix in a callback (sadly)

Second, Blindside moved the texture coordinate conversion to the fragment shader.

Here is the completed test code from above.

Code: Select all

# include <Irrlicht.h>
#pragma comment (lib,"Irrlicht.lib")

irr::scene::ICameraSceneNode *shadowCam; //this is the shadow camera
										// I have to make it global
										// for the shader callback
class MyShaderCallBack : public irr::video::IShaderConstantSetCallBack
{
public: 
	virtual void OnSetConstants(irr::video::IMaterialRendererServices* services, irr::s32 userData)
	{
		irr::video::IVideoDriver *driver=services->getVideoDriver();
//////// This is where I pass the ModelViewProjection of the light ////
///////  to the shader. //////////////////////////////////////////////
		irr::core::matrix4 tMatrix;
		tMatrix = shadowCam->getProjectionMatrix();			
		tMatrix *= shadowCam->getViewMatrix();
		tMatrix *= driver->getTransform(irr::video::ETS_WORLD);
		services->setVertexShaderConstant("tMatrix", tMatrix.pointer(), 16);
	}
}callback;
  
int main()
{
///// make all the Irrlicht pointers ///////
	irr::IrrlichtDevice *device =	irr::createDevice(	irr::video::EDT_OPENGL, 
									irr::core::dimension2d<irr::s32>(800,600),
									32,
									true,
									false,
									true
									);
	irr::video::IVideoDriver *driver		 = device->getVideoDriver();
	irr::scene::ISceneManager *scenemanager  = device->getSceneManager();
	irr::video::IGPUProgrammingServices *gpu = driver->getGPUProgrammingServices();
///////////////////////////////////////////
 
///// add the lights and cameras /////////
	irr::scene::ILightSceneNode  *light =     scenemanager->addLightSceneNode ();
	light->setPosition(irr::core::vector3df(20,20,0));
 
	shadowCam = scenemanager->addCameraSceneNode();      //this is the
	shadowCam->setPosition(light->getPosition());       // shadow camera
	shadowCam->setTarget  (irr::core::vector3df(0,0,0));
 
	irr::scene::IAnimatedMeshSceneNode *arrowNode = scenemanager->addAnimatedMeshSceneNode(scenemanager->addArrowMesh("arrow",irr::video::SColor(255,50,75,250),irr::video::SColor(255,150,175,250),4,8,2,.6f,.05f,.3f));
	arrowNode->setMaterialFlag(irr::video::EMF_LIGHTING,false);
	arrowNode->setPosition(light->getPosition());
	arrowNode->setRotation(irr::core::vector3df(0,0,135));
	arrowNode->setScale(irr::core::vector3df(3,3,3));
 
	irr::scene::ICameraSceneNode *renderCam = scenemanager->addCameraSceneNodeFPS();
	renderCam->setPosition(irr::core::vector3df(0,25,-50));
	renderCam->setTarget  (irr::core::vector3df(0,0,0));
 
//// add a sphere //////////////////////
	irr::scene::IAnimatedMesh		   *sphereMesh = scenemanager->addSphereMesh("sphere",5.0f,32,32);
	irr::scene::IAnimatedMeshSceneNode *sphereNode = scenemanager->addAnimatedMeshSceneNode(sphereMesh);
	sphereNode->getMaterial(0).DiffuseColor.set(255,250,75,50);
	sphereNode->setPosition(irr::core::vector3df(0,5,0));
	shadowCam->setTarget(sphereNode->getPosition());

///// create the render target////////
	irr::video::ITexture *renderTarget = driver->createRenderTargetTexture(irr::core::dimension2di(512,512));

/////add the plane/////////////////
	irr::s32 shader;
	shader = gpu->addHighLevelShaderMaterialFromFiles(	"VERT.vert",
														"main",
														irr::video::EVST_VS_2_0,
														"FRAG.frag",
														"main",
														irr::video::EPST_PS_2_0,
														&callback,
														irr::video::EMT_SOLID,
														0
														);
 
 
	irr::video::SMaterial planeMaterial;
	planeMaterial.MaterialType = (irr::video::E_MATERIAL_TYPE)shader;//set the shader
	planeMaterial.DiffuseColor.set(255,50,250,75);//make it green
	planeMaterial.setTexture(0,renderTarget); // set the renderTarget texture
 
	irr::scene::IAnimatedMesh *planeMesh = scenemanager->addHillPlaneMesh("plane",irr::core::dimension2df(100.0f,100.0f),irr::core::dimension2d<irr::u32>(1,1),&planeMaterial);
	irr::scene::IAnimatedMeshSceneNode *planeNode = scenemanager->addAnimatedMeshSceneNode (planeMesh);

	while (device->run())
	{
		driver->beginScene(true,true,0);
 
//////// shadow pass ////////////
		scenemanager->setActiveCamera(shadowCam);
		driver->setRenderTarget(renderTarget, true, true, irr::video::SColor(255,255,255,255));
		sphereNode->getMaterial(0).DiffuseColor.set(255,0,0,0);
		shadowCam->render();
		sphereNode->render();
///////////////////////////////
 
///////  render pass ///////////
		scenemanager->setActiveCamera(renderCam);
		driver->setRenderTarget(0,true,true,0);
		sphereNode->getMaterial(0).DiffuseColor.set(255,250,75,50);
		scenemanager->drawAll();
///////////////////////////////
 
		driver->endScene();
	}
 
	device->drop();
}

Code: Select all

//  vertex shader
uniform mat4 tMatrix;

void main()
{
	gl_Position = ftransform();
	gl_TexCoord[0] = tMatrix * gl_Vertex;
}

Code: Select all

//  pixel shader
 
uniform sampler2D renderTarget;
 
void main()
{
	vec4 Light = vec4(1,1,1,1); 
    vec2 smTCoords = gl_TexCoord[0].xy / gl_TexCoord[0].w / 2.0 + vec2(0.5, 0.5);
	vec2 clampedSMPos = clamp(smTCoords, vec2(0.0, 0.0), vec2(1.0, 1.0));
	if(clampedSMPos.x == smTCoords.x && clampedSMPos.y == smTCoords.y) Light = texture2D(renderTarget,smTCoords);
 
	gl_FragColor =  Light;
}
Signature? I ain't signin nuthin!
Post Reply