smooth texture

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.
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

What exactly do I need to do in my compiler? :D
Newbie to directx programming I guess.
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

Well, what compiler are you using? Easiest bet to test it, would be to take one of the existing materials, and comment the code for it out, and the replace it with this code. For example, in CD3D9MaterialRenderer.h change this -

Code: Select all

//! Solid 2 layer material renderer
class CD3D9MaterialRenderer_SOLID_2_LAYER : public CD3D9MaterialRenderer
{
public:

	CD3D9MaterialRenderer_SOLID_2_LAYER(IDirect3DDevice9* p, video::IVideoDriver* d)
		: CD3D9MaterialRenderer(p, d) {}

	virtual void OnSetMaterial(SMaterial& material, const SMaterial& lastMaterial,
		bool resetAllRenderstates, IMaterialRendererServices* services)
	{
		if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
		{
			pID3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
			pID3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
			pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP,  D3DTOP_DISABLE);

			pID3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);
			pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_BLENDDIFFUSEALPHA);

			pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
		}

		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
	}
};
to this -

Code: Select all

//! Solid 2 layer material renderer
class CD3D9MaterialRenderer_SOLID_2_LAYER : public CD3D9MaterialRenderer
{
public:

	CD3D9MaterialRenderer_SOLID_2_LAYER(IDirect3DDevice9* p, video::IVideoDriver* d)
		: CD3D9MaterialRenderer(p, d) {}

	//virtual void OnSetMaterial(SMaterial& material, const SMaterial& lastMaterial,
	//	bool resetAllRenderstates, IMaterialRendererServices* services)
	//{
	//	if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
	//	{
	//		pID3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	//		pID3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	//		pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP,  D3DTOP_DISABLE);

	//		pID3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);
	//		pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_BLENDDIFFUSEALPHA);

	//		pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
	//	}

	//	services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
	//}

	virtual void OnSetMaterial(SMaterial& material, const SMaterial& lastMaterial,
		bool resetAllRenderstates, IMaterialRendererServices* services) 
	{
		// base texture( texture stage 0, normal operations, but save them to the temp register )
		pID3DDevice->SetTextureStageState( 0, D3DTSS_RESULTARG, D3DTA_TEMP );

		// alpha texture( texture stage 1, we just need to grab the alpha value from the texture at this stage
		pID3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
		pID3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );

		// this the the blend texture, we want to blend this color with the color from texture stage 0( which is
		// in the temp register ) using the alpha we got from texture stage 1.
		pID3DDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_BLENDCURRENTALPHA );
		pID3DDevice->SetTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_TEXTURE );
		pID3DDevice->SetTextureStageState( 2, D3DTSS_COLORARG2, D3DTA_TEMP );

		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
	}

	virtual void OnUnsetMaterial()
	{ 
		// base texture( texture stage 0, return the target to current, rather than the temp register )
		pID3DDevice->SetTextureStageState( 0, D3DTSS_RESULTARG, D3DTA_CURRENT );

		pID3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
		pID3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );

		pID3DDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
		pID3DDevice->SetTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_TEXTURE );
		pID3DDevice->SetTextureStageState( 2, D3DTSS_COLORARG2, D3DTA_CURRENT );
	}
};

Here's the code I used to test this( not i'm using RAW format images for the alpha layer, which is only available in IrrSpintz, you'll have to create a PNG or TGA texture with alpha to use in Irrlicht, since it doesn't support RAW image loading. )

Code: Select all

	scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNodeRAW( "D:\\Terrain\\FreeWorldExports\\terrain\\heightmap16bit.raw" );
	terrain->setMaterialFlag( video::EMF_LIGHTING, false );
	terrain->setMaterialTexture( 0, driver->getTexture( "D:\\Terrain\\FreeWorldExports\\terrain\\Base.bmp" ) );
	driver->setRAWImagerLoaderParams( 512, 512, 8 );
	terrain->setMaterialTexture( 1, driver->getTexture( "D:\\Terrain\\FreeWorldExports\\terrain\\Alpha_alpha.raw" ) );
	terrain->setMaterialTexture( 2, driver->getTexture( "D:\\Terrain\\FreeWorldExports\\terrain\\Alpha.bmp" ) );
	terrain->setMaterialType( video::EMT_TEXTURE_SPLAT_2 );

I'm doing some more testing, it's basically blending but for some reason, the 2nd texture( in the 3rd stage ) isn't being blended in properly, I'm looking at it.
Image
Strong99
Admin
Posts: 687
Joined: Fri Mar 31, 2006 7:06 pm
Location: Netherlands
Contact:

Post by Strong99 »

you mean this was all code needed? i going to check it out, in the dll right?

man irrlicht needs this in next release i quess

[edit] owh you posted more thnx
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Why can't the alpha channel of the texture be used? Having a three texture renderer might be more flexible, but just the basic functionality even with smooth blending should be there, right?
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

It could be, but that's not what they asked for.
Image
Strong99
Admin
Posts: 687
Joined: Fri Mar 31, 2006 7:06 pm
Location: Netherlands
Contact:

Post by Strong99 »

i got a terrain without light enabled and the model uses only yhe 3rd texture. the strangest is that with more than 6 lights the program crashes since this change.
Strong99
Admin
Posts: 687
Joined: Fri Mar 31, 2006 7:06 pm
Location: Netherlands
Contact:

Post by Strong99 »

crash fixed, but i still have that or the 2nd texture (alpha map) won't work or that 3rd texture is placed over everything.
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

In order to use the D3DTA_TEMP register, you need to have the D3DPMISCCAPS_TSSARGTEMP in your D3DCAPS, maybe you don't?
Image
Strong99
Admin
Posts: 687
Joined: Fri Mar 31, 2006 7:06 pm
Location: Netherlands
Contact:

Post by Strong99 »

and how can i do that?
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

Start -> Programs -> Microsoft DirectX -> Utilities -> DirectX Caps Viewer
Image
Strong99
Admin
Posts: 687
Joined: Fri Mar 31, 2006 7:06 pm
Location: Netherlands
Contact:

Post by Strong99 »

i have under :

DirectX Graphics Adapters >
RADEON 9600 SERIES >
D3D Device Types >
Reference >
Caps >
PrimitiveMiscCaps

what i do the 3rd texture stays ontop. i use an tga, do i have to set it first with transparency before applying on the terrain?
Strong99
Admin
Posts: 687
Joined: Fri Mar 31, 2006 7:06 pm
Location: Netherlands
Contact:

Post by Strong99 »

after changing some stuff i get this:

Image
Strong99
Admin
Posts: 687
Joined: Fri Mar 31, 2006 7:06 pm
Location: Netherlands
Contact:

Post by Strong99 »

and to exlude other coords i did

Code: Select all

node->getMaterial(0).Texture1 = texture1;
node->getMaterial(0).Texture2 = texture2;
node->getMaterial(0).Texture3 = texture3;
node->getMaterial(0).MaterialType = video::EMT_SOLID_2_LAYER;
but that doesn't work, i get than a gray surface
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

that screenshot looks like its working. It looks like you scaled the texture coordinates of your alpha layer. Oh, I can't believe I forgot about texture coords. What scenenode/mesh are you using to render that stuff???? Problem is, we're using a material which has 3 texture stages but Irrlicht meshes only support 1 or 2 texture coordinates. You can, in DX tell a texture stage to use texture coordinates of another texture stage. I don't have DX on this laptop I'm on at work yet( just got a new laptop ), I'll try to look the call up on the web and let you know.
Image
Strong99
Admin
Posts: 687
Joined: Fri Mar 31, 2006 7:06 pm
Location: Netherlands
Contact:

Post by Strong99 »

i use IScenenode and IAnimatedMeshSceneNode's,

the second ( alpha mapping ) needs to fit in the model, the 3rd is allowed to be tiled
Post Reply