   single pass alpha texturing
  Im having some trouble getting to grips with the whole SetTextureStageState.i'm currently working on a terrain engine and producing a mesh using mid-point displacment.However i recently decided to texture the mesh, which was easy with one texture but looked rubbish so i decided to try and load two, i think im getting close but i cant see what is wrong with this code, it makes sense if i think about it but it doesnt work properly.I load a tga file with an alpha channel into stage 0 and then a grass bmp into stage 1.d3ddev->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);d3ddev->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);d3ddev->SetTextureStageState(1,D3DTSS_COLORARG1,D3DTA_TEXTURE);d3ddev->SetTextureStageState(1,D3DTSS_COLORARG2,D3DTA_CURRENT);d3ddev->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_MODULATE);for(int n = 0; n < m_numberOfTextureData; n++){d3ddev->SetTexture(m_textureData[n].textureStage, m_textureData[n].Base);}d3ddev->SetStreamSource(0, pVB, 0, sizeof(TerrainVertex));d3ddev->SetFVF(D3DFVF_TERRAINVERTEX);d3ddev->SetIndices(pIB);the output kind of works, it blends together the alpha channel from the tga and the colour from the grass, which is weird it seems to make sense the way it is o.o   
  For each stage, it's best practice to set both the color op and the alpha op, even if you're just selecting an argument or the current register.  Otherwise you have no idea what the state could be.Assuming for now that the uninitialized states are their default values, what's happening in stage 0 is that the texture is modulated with the diffuse color and the alpha channel is passed through.  In stage 1, you modulate that with the stage 1 texture.  But beyond that the behavior is undefined, since the default value for the stage 1 alpha op is to be disabled, which isn't allowed unless the color op is also disabled :)  Hence why you shouldn't make any assumptions about state.It's hard to say what else is happening without any screenshots, or knowing what the textures are.   ;  it looks as though it is only taking the colour from the first pixel and not the whole image, if i render the mesh with one texture it renders perfectly :S[/img]its rendering the right area but it just doesnt look right it should have this texture on it[/img]   ;  This is most likely a texture coordinate issue.  By default the FFP is going to look at whatever data is bound to TEXCOORDn to get the texture coordinates for state n, otherwise it uses (0,0) if that's not available.  To change this behavior, use the following call:d3ddev->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);This call instructs the device to use the stage 0 texture coordinates for stage 1.   ;  ah ok cheers, im going to add another co-ordinate pair that should solve this problem right? o.oAfter adding the texture my FPS drops to around 32 im not doing anything special but the terrain is rendered brute force, 129 x 129 does this sound normal ? running on a mid range pc(x4400, x1800xl, 2gig ram)?   ;  Ok so that works with two textures and i can get it too work with 4 textures but i'm calling draw indexed primitive twice, is there no other way i can do this, like blend the alpha and the grass and say the alpha and the rocks, then blend the output from the rocks with the output from the grass?   ;  if you use programmable shaders you should get a lot more flexibility (with reduced compatability). At the moment it looks like your using the fixed function pipeline which is very limited.Also sorting your models by texture can help improve performance. Although thats prob not an issue yet with 4 textures.EDIT:also how big are your textures? your alpha texture especially? You may also have mip-maps generated automatically which will increase your texture memory usage a lot if your using big textures.;  my textures are 256 x 256 and my alpha textures are 32 x 32, yeah i read that you get more flexibility in the PP but i would just like to offer both features. Is it possible to combine multiple alpha and texture maps in one pass its a lot easier with multiple passes i cant seem to get it going with a single pass, this is what i've got so fard3ddev->SetTexture(m_textureData[0].textureStage, m_textureData[0].Base);d3ddev->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);d3ddev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);d3ddev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);d3ddev->SetTexture(m_textureData[1].textureStage, m_textureData[1].Base);d3ddev->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);d3ddev->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);d3ddev->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);d3ddev->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);d3ddev->SetTexture(m_textureData[2].textureStage, m_textureData[2].Base);d3ddev->SetTextureStageState(2, D3DTSS_TEXCOORDINDEX, 0);d3ddev->SetTextureStageState(2, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);d3ddev->SetTextureStageState(2, D3DTSS_COLORARG2, D3DTA_CURRENT);d3ddev->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_ADD);it doesnt really work mind you i've got my textures loaded like so, 0 - texture map, 1 - texture, 2 - different texture map, 3 - different texturei would like to offer the full 8 texture's if its possible in teh fixed pipeline.   ;  how many alpha textures are you using? Are these per tile or an set over the entire terrain? if they are using some sort of tiling then i would suggest packing your alpha textures together to make fewer textures of 256x256. This will mean less texture swapping and will be much faster. graphics cards work best with textures around 256x256.where are your alpha textures placed in the texture stages?With one alpha texture stored as colour (A8R8G8B8), you can have 4 alphas for 4 different textures within one texture (if that makes sense, each channel is an alpha). To use more than 4 you would prob need 2 textures for alpha (which would allow 8 alphas for 8 textures) but then you would be using 2 texture stages for alpha textures and have only 6 left for your other textures. I am not sure if you can seperate alpha from the different channels and use them for blending in the fixed funtion pipeline though.I would do 4 textures each time in a multi-pass way...but that wasnt the question.hope this helps;  At the moment im just using 2 alpha textures one for each Texture to be mapped, no textures are tiled at the moment there just placed on. Im experimenting with terrain.Every second stage is an alpha texture basically the alpha texture follows the Mapped texture and it works great for 1 :S or more if i multipass the problem is multipassing gives me horrible framerate i figured this was because i was brute forcing the terrain.Yeah thats a great idea, using the individual channel alphas i've just been using a general alpha for the image, i'll give that a shot after i get this working >.>.Thanks for the info, maybe theres a problem with my terrain making algorithm thats duplicating vertices or indices. I cant see how but it might explain the framerate.