Try to make "texture splatting" problem

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
trg
Posts: 10
Joined: Tue Sep 20, 2005 7:33 pm

Try to make "texture splatting" problem

Post by trg »

Hi!

I'm trying to make texture splatting. The setMaterialType() function with EMT_TRANSPARENT_ALPHA_CHANNEL parameter only affect for the self-texture alpha channel, and in this case the alpha channel will be scaled if the texture scaled.

Code: Select all

terrain->setMaterialTexture(1, m_driver->getTexture("alphamap.tga"));	
		terrain->setMaterialTexture(0, m_driver->getTexture("grass.jpg"));
		terrain->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL );
Texture splat seperates texture in a layer and the alphamap for transparency in another layer. So we can do blend with a large terrain with multiple repeat tiled texture and only one alphamap file, for example.

To do this in DX9 first, I replaced function CD3D9MaterialRenderer_TRANSPARENT_ALPHA_CHANNEL::OnSetMaterial() on CD3D9MaterialRender.h by:

Code: Select all

virtual void OnSetMaterial(SMaterial& material, const SMaterial& lastMaterial,
		bool resetAllRenderstates, IMaterialRendererServices* services) 
	{
		if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
		{
		
			// trg takes code from texture splating - Nate Glasser		
			// Allow multiple passes to blend together correctly, like Irrlicht did
			pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
			pID3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
			pID3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

			pID3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
			pID3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
			pID3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

			// Prevent some ugliness with the alphamaps
			pID3DDevice->SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
			pID3DDevice->SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);

			pID3DDevice->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
			pID3DDevice->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
			pID3DDevice->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
			
			// Alphamap: take the alpha from the alphamap, we don't care about the color
			pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
			pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);

			// Texture: take the color from the texture, take the alpha from the previous stage
			pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
			pID3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
			pID3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
			pID3DDevice->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT);			
		}

		//material.ZWriteEnable = false;
		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
	}

To use, using modified code from the terrain tutorial example:

Code: Select all

terrain->setMaterialTexture(0, m_driver->getTexture("alphamap.tga"));		
		terrain->setMaterialTexture(1, m_driver->getTexture("grass.jpg"));			

		terrain->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL );
		terrain->scaleTexture(1.0f, 1.0f);
alphamap.tga is an alpha channel enabled image.

The result sounds good ! The terrain will be transparent by the transparency defined in the 1st alphamap, and the texture painted by the 2nd texture map.

But I have only one problem: when set scaleTexture() other than 1.0f, such as 2.0f for example, the terrain is tiled OK with 2x2 tiled texture and alpha, but only one cell is painted by the "grass" texture. The rest 3 cells are not painted and seem solid.

I uploaded my binary here if you want to test:
http://vine.phpwebhosting.com/~bjohnsto ... rlicht.zip

Can anyone help me to paint all 2x2 tiled texture and so on?
Thanks!


Image
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

The bad textures look like what i get when my mesh coordinates are wrong in my .x files, but i didnt read ur whole post im afraid so dont know if you're using .x files (or similar) or not
Image Image Image
trg
Posts: 10
Joined: Tue Sep 20, 2005 7:33 pm

Post by trg »

I'm using only flat file .bmp for heightmap (don't care in this case), .jpg for texture and .tga for alphamap.
sRc
Posts: 431
Joined: Thu Jul 28, 2005 1:44 am
Location: Salt Lake City, Utah
Contact:

Post by sRc »

woah that's a funky camera you've got going on there o_O

basically it's stopped tiling, it's repeating the pixels from the edges. if you put a border around the texture you can see it clearly:

(I added a pink one in this instance because it would be clear to see)
Image
Image

I used to know what do do in DirectX commands when this happens, but I forgot now, so I can't be of much more help then that >_>
trg
Posts: 10
Joined: Tue Sep 20, 2005 7:33 pm

Post by trg »

Noone reply until your reply, so I 've to take a round on some CG forums :D

Come back I don't know how did you research my small program without my source code :D I will upload the source soon, now I'm in work at office.

The funky camera based on RPG : when click on a point in the terrain, the player will move there. Zoom by scroll button. Rotate camera by hold down right mouse button and drag.

I'm not good at DX, hope you remember the right DX command in this case.

I will post the entire source later when I come home.
sRc
Posts: 431
Joined: Thu Jul 28, 2005 1:44 am
Location: Salt Lake City, Utah
Contact:

Post by sRc »

trg wrote:Come back I don't know how did you research my small program without my source code :D I will upload the source soon, now I'm in work at office.
well I didn't really need to research much, it's a behavior I was familiar with because I had run into it myself a couple years back programming hard DirectX. I simply needed to edit the texture a bit to demonstrate the problem.

yeah post your source code when you get home so I can see if it's something in there making it go wrong.
The funky camera based on RPG : when click on a point in the terrain, the player will move there. Zoom by scroll button. Rotate camera by hold down right mouse button and drag.
ahh ok now i understand the cnotrols. It does some crazy stuff when it's zoomed all the way out tho :shock: :lol:
trg
Posts: 10
Joined: Tue Sep 20, 2005 7:33 pm

Post by trg »

The wrong zoom function is based on the camera tut which I applied when searching on forum: instead of moving the camera near or far from target (more exactly zooming function), it changes FOV so the camera looks crazy.

The source code includes modification in CD3D9MaterialRender.h and the example.
http://vine.phpwebhosting.com/~bjohnsto ... at-src.zip

The DX texture splat commands I copied from
http://www.gamedev.net/columns/hardcore/splatting/
http://www.gamedev.net/columns/hardcore ... /page4.asp <-- the demo source code.
Post Reply