Managed to get FX Shaders to work in Irrlicht......

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.
Post Reply
Gman3344
Posts: 23
Joined: Tue Jul 22, 2008 9:03 pm

Managed to get FX Shaders to work in Irrlicht......

Post by Gman3344 »

As the title says......problem is i had to use Direct3D Matrix Function to set it up, so the model is stuck to the camera when using Irrlicht's FPS camera, so i don't know........anyways, here's how you'd set it up...

Code: Select all

#include "Main_Game.h"
#include "VD.h"
#include <Cg/Cg.h>
#include <IrrCg.h>

#pragma comment(lib,"IrrCg.lib")
#pragma comment(lib,"cg.lib")
#pragma comment(lib,"cgD3D9.lib")
#pragma comment(lib,"cgGL.lib")

using namespace IrrCg;

IrrCg::ICgProgrammingServices* GPU;
//IrrCg::ICgMaterialConstantSetCallBack* MCB;

class MyShaderConstants : public ::IrrCg::ICgMaterialConstantSetCallBack
{
public:
	CGparameter View;
	irr::IrrlichtDevice* Device;
	virtual void OnSetConstants(IrrCg::ICgD3DServices* services,CGprogram Vertex,CGprogram Pixel,SMaterial Material)
	{
		View = cgGetNamedParameter(Vertex,"View");


		irr::core::matrix4 Matrix;

		Matrix = Device->getVideoDriver()->getTransform(E_TRANSFORMATION_STATE::ETS_PROJECTION);
		Matrix *= Device->getVideoDriver()->getTransform(E_TRANSFORMATION_STATE::ETS_VIEW);
		Matrix *= Device->getVideoDriver()->getTransform(E_TRANSFORMATION_STATE::ETS_WORLD);

		services->setMatrix(View,IrrCg::MATRIX_IDENTITY,Matrix);
	}
};


		
int Main_Game::PreludeLevel()
{
	if(this->Device)
	{
		Input_Receiver = new MyEventReceiver();
		Device->setEventReceiver(this->Input_Receiver);
		GPU = new IrrCg::ICgProgrammingServices(this->Device);

		if(GPU)
		{
		//GUI->addMessageBox(L"Shaders are working",L"Yay for shaders!",true,EMBF_OK);
		} 
	}
	

	if(!HavokInit.Initialize_Havok())
	{
		return 1;
	}
	
	this->LoadLevel();
	this->LoadPlayerMesh();
	
	if(VideoDriver->getDriverType() == E_DRIVER_TYPE::EDT_DIRECT3D9)
	{
		this->D3DDevice = VideoDriver->getExposedVideoData().D3D9.D3DDev9;
	}

	ID3DXBuffer* ShaderError;
	if(FAILED(D3DXCreateEffectFromFileA(this->D3DDevice,"Shaders//Shader.fx",NULL,NULL,NULL,NULL,&this->Red,&ShaderError)))
	{
		FILE* File = fopen("Shader.fx","rb");

		if(!File)
		{
			MessageBoxA(NULL,"Shader Not loaded","",MB_OK);
		}
	}

	Red->FindNextValidTechnique(NULL,&this->Technique);
	FPS = SceneManager->addCameraSceneNodeFPS(NULL,100,500);
	
	this->Player_Parent = SceneManager->addCubeSceneNode(2,NULL,NULL);
/*
	if(FPS)
	{
		if(Player_Parent)
		{
			Player_Parent->setMaterialFlag(E_MATERIAL_FLAG::EMF_LIGHTING,false);
			Player_Parent->setPosition(vector3df(0,40,0));
			Player_Mesh_Node->setPosition(vector3df(0,-40,0));
			Player_Mesh_Node->setParent(Player_Parent);
			
		}
	}*/ 
	 
	vector3df PlayerVelocity = Player_Parent->getPosition();

	vector3df Player_Pos = Player_Parent->getAbsolutePosition();

	Player_Pos.Y = 65;
	Player_Pos.Z = 10;
	
	while(Device->run())
	{
		
		//FPS->setPosition(Player_Pos);

	
		
		
		if(Device->isWindowActive())
		{
			
			VideoDriver->beginScene(true,true,SColor(255,0,0,0));

			D3DXMATRIX View;
			D3DXMATRIX Proj;
			D3DXVECTOR3 pEye(0,100,-100);
			D3DXVECTOR3 pAt(0,100,0);
			D3DXVECTOR3 Up(0,1.0,0);
			D3DXMATRIX World;


			D3DXMatrixLookAtLH(&View,&pEye,&pAt,&Up);
			//Render->GetDirect3DDevice9()->SetTransform(D3DTS_VIEW,&View);
			Red->SetMatrix("View",&View);
			D3DXMatrixPerspectiveFovLH(&Proj,45,1.333,1.0,10000.0);
			Red->SetMatrix("Projection",&Proj);
		
			//Render->GetDirect3DDevice9()->SetTransform(D3DTS_PROJECTION,&Proj);

			D3DXMatrixTranslation(&World,0,0,0);
			Red->SetMatrix("World",&World);
			Red->CommitChanges();
			Red->Begin(0,0);
				Red->BeginPass(0);
				Player_Mesh_Node->render();
				Red->EndPass();
			Red->End();
			SceneManager->drawAll();
	
			

			GUI->drawAll();

			VideoDriver->endScene();
		}
	}
		

	
	return 1;
}

int Main_Game::LoadLevel()
{
	this->Level_Mesh = SceneManager->getMesh("Huge_Level//Level.x");

	if(Level_Mesh)
	{
		Level_Mesh_Node = SceneManager->addAnimatedMeshSceneNode(Level_Mesh);

		if(Level_Mesh_Node)
		{
			Level_Mesh_Node->setMaterialFlag(E_MATERIAL_FLAG::EMF_LIGHTING,false);
			
		}
	}
	return 1;
}

int Main_Game::LoadPlayerMesh()
{
	Player_Mesh = (ISkinnedMesh*)SceneManager->getMesh("Player//Alyx.x");

	if(Player_Mesh)
	{
		Player_Mesh_Node = SceneManager->addAnimatedMeshSceneNode(Player_Mesh);

		if(Player_Mesh_Node)
		{
			Player_Mesh_Node->setMaterialFlag(E_MATERIAL_FLAG::EMF_LIGHTING,false);
		
		}
	}
	return 1;
}

Ignore the whole Shader callback crap i was tring to use IrrCg and didn't go so good, so hope someone gets something out of it. and if Anyone has a way to use Irrlicht Matrces rather the D3DXMATRIX i'd love to hear it.

Oh Also it seems that it gets clipped when you turn the camera and look at another mesh, not sure it it's Irrlicht or the way The FPS camera is made or something.
Gman3344
Posts: 23
Joined: Tue Jul 22, 2008 9:03 pm

Post by Gman3344 »

It's amazing no one responded yet to this......I tried to convert between matrices and that didn't good so well.
night_hawk
Posts: 153
Joined: Mon Mar 03, 2008 8:42 am
Location: Suceava - Romania
Contact:

Post by night_hawk »

3 things:
1. This is in the wrong section :D That's a possible reason to no answer...
2. Awesome! I'll have to check this out.
3. Could you post the .fx?
Gman3344
Posts: 23
Joined: Tue Jul 22, 2008 9:03 pm

Post by Gman3344 »

.fx file:

Code: Select all


float4x4 View : WorldViewProjection;

struct VS_INPUT 
{
   float4 Position : POSITION0;
   
};

struct VS_OUTPUT 
{
   float4 Position : POSITION0;
   
};

VS_OUTPUT vs_main( VS_INPUT Input )
{
   VS_OUTPUT Output;

   
   Output.Position = mul(Input.Position,View);
   
   return( Output );
   
}




float4 ps_main() : COLOR0
{   
   return( float4( 1.0f, 0.0f, 0.0f, 1.0f ) );
   
}




//--------------------------------------------------------------//
// Technique Section for Effect Group 1
//--------------------------------------------------------------//
technique Position
{
   pass Pass_0
   {
      VertexShader = compile vs_2_0 vs_main();
      PixelShader = compile ps_2_0 ps_main();
   }

}

as you can see.....it ain't much, really
Post Reply