Page 1 of 1

[Solved]set material to weapon with irrlicht's sample shader

Posted: Thu Mar 20, 2008 6:00 am
by doqkhanh
Hi there,
I want apply shader to my character's weapon. So that, I try to modified Irrlicht's sample to apply it in my case.

First, with DirectX9, this class is same to Irrlicht's sample without OpenGL shader condition code.

Code: Select all

class MyShaderCallBack : public video::IShaderConstantSetCallBack
{
public:

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

		// set inverted world matrix
		// if we are using highlevel shaders (the user can select this when
		// starting the program), we must set the constants by name.

		core::matrix4 invWorld = driver->getTransform(video::ETS_WORLD);
		invWorld.makeInverse();

		services->setVertexShaderConstant("mInvWorld", invWorld.pointer(), 16);
	
		// set clip matrix

		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);
		
		// set camera position
		core::vector3df pos = mainCam->getAbsolutePosition();
		
		services->setVertexShaderConstant("mLightPos", reinterpret_cast<f32*>(&pos), 3);
	
		// set light color 
		video::SColorf col(0.0f,1.0f,1.0f,0.0f);

		services->setVertexShaderConstant("mLightColor", reinterpret_cast<f32*>(&col), 4);

		// set transposed world matrix
		core::matrix4 world = driver->getTransform(video::ETS_WORLD);
		world = world.getTransposed();

		services->setVertexShaderConstant("mTransWorld", world.pointer(), 16);	
	}
};
Second, my own code to apply Irrlicht's shader to my weapon:

Code: Select all

//Load axe mesh
	scene::IAnimatedMeshSceneNode* weapon = smgr->addAnimatedMeshSceneNode(smgr->getMesh("Data\\model\\ax\\ax1.3ds"));
	
	//If load ok
	if(weapon)
	{
		//Set texture
		weapon->setMaterialTexture( 0, driver->getTexture("Data\\model\\ax\\PLYWOOD.jpg") );
		//weapon->setMaterialType( video::EMT_SOLID );

		//Lighting and get joint
		//weapon->setMaterialFlag(irr::video::EMF_LIGHTING, true);
		irr::scene::ISceneNode* hand = ninja->getMesh()->getXJointNode("Joint17");
		
		//Make it smaller
		weapon->setScale(vector3df(0.07f, 0.07f, 0.07f));
		
		if(hand)
		{
			//Set relative postion
			weapon->setPosition(core::vector3df(0.0f,-1.1f,0.0f));
			weapon->setRotation(core::vector3df(0.0f,0.0f,90.0f));
			
			//Attack weapon to joint
			hand->addChild(weapon);

			MyShaderCallBack* scc = new MyShaderCallBack();
					
			s32 newMaterialType = driver->getGPUProgrammingServices()->addShaderMaterialFromFiles(
				"Data\\shaders\\hlsl\\d3d9.vsh",
				"Data\\shaders\\hlsl\\d3d9.psh", scc, video::EMT_TRANSPARENT_ADD_COLOR);

			scc->drop();

			weapon->setMaterialTexture(0, driver->getTexture("Data\\model\\ax\\plywood.jpg"));
			weapon->setMaterialFlag(video::EMF_LIGHTING, false);
			weapon->setMaterialType((video::E_MATERIAL_TYPE)newMaterialType);


		}
	}

Everything ok, complied ok, load texture ok, run vertex shader ok, my pixel shader has been disabled.

But, Irrlicht's shader sample run OK in my computer - a quite old laptop.
Loaded mesh: Data\ninja.b3d
Could not open file of texture: PLYWOOD.JPG
Loaded texture: Data\model\ax\PLYWOOD.JPG
Loaded mesh: Data\model\ax\ax1.3ds
Could not create pixel shader.
Loaded mesh: Data\pointer.b3d
Loaded texture: Data\point.bmp

Posted: Thu Mar 20, 2008 8:20 am
by bitplane
Does you shader work outside Irrlicht, for example in RenderMonkey?

Posted: Thu Mar 20, 2008 8:38 am
by doqkhanh
bitplane wrote:Does you shader work outside Irrlicht, for example in RenderMonkey?
I am using Irrlicht's shader file in media folder.

Posted: Sat Mar 22, 2008 3:35 am
by doqkhanh
Oh, terribly sorry. Your engine and your shader work well. But, cause my texture is a solid texture --> my weapon became transparent.