   How to do Multipass Texture Blending
  Hi I am new to Direct3D.I have two textures created with D3DFMT_A8R8G8B8.I draw the first one withdevice->SetTexture(0,texture1);device->SetTextureStage(0,COLORARG1,TEXTURE);device->SetTextureStage(0,COLOROP,SELECTARG1);device->SetTexture(1,texture2);device->SetTextureStage(1,COLORARG1,CURRENT);device->SetTextureStage(1,COLORARG2,TEXTURE);device->SetTextureStage(1,COLOROP,???????);When I only draw one texture with stage 0, I get the picture.When I do both, as written I Recive a whole page covered with a color from the second texture(I have tried  using D3DTOP_MODULATEALPHA_ADDCOLOR but then I get the whole picture covered with one color. -Is (A1 rgb + A2 rgb * A1 a) mean that for every pixel we do this calculation?AS I understand I need to use Multipass Texture Blending (Direct3D 9)I tried to go by the directions in msdn but still with no luck.can you explain step 3 for me. what is render the scene mean?To perform multipass texture blending in a C/C++ application.1.Set a texture in texture stage 0 by calling the IDirect3DDevice9::SetTexture method. 2.Select the desired color and alpha blending arguments and operations with the IDirect3DDevice9::SetTextureStageState method. The default settings are well-suited for multipass texture blending. 3.Render the appropriate objects in the scene. 4.Set the next texture in texture stage 0. 5.Set the D3DRS_SRCBLEND and D3DRS_DESTBLEND render states to adjust the source and destination blending factors as needed. The system blends the new textures with the existing pixels in the render-target surface according to these parameters. 6.Repeat Steps 3, 4, and 5 with as many textures as needed. Please If anybody knows what to do. I woule be greatfull.meir.[Edited by - mezik on May 6, 2009 8:24:21 AM]
  You need to specify a color operation for stage 1 defining how to combine arg1 and arg2 such as MODULATE for example.EDIT: Here is what you find reading the SDK doc:D3DTSS_COLOROPTexture-stage state is a texture color blending operation identified by one member of the D3DTEXTUREOP enumerated type. The default value for the first texture stage (stage 0)is D3DTOP_MODULATE; for all other stages the default is D3DTOP_DISABLE.SDK Doc SetTextureStageState;  What effect do you want? Assuming you want to use the texture alpha from the first texture to blend with the second:device->SetTexture(0,texture1);device->SetTextureStage(0,COLORARG1,TEXTURE);device->SetTextureStage(0,COLOROP,SELECTARG1);device->SetTexture(1,texture2);device->SetTextureStage(1,COLORARG1,CURRENT);device->SetTextureStage(1,COLORARG2,TEXTURE);device->SetTextureStage(1,COLOROP,MODULATE);;  Hi and thanks for your replyI am trying to draw an overlay bitmap, which would have some text on it and some drawn symbols such as a cross in the middle of the frame, and other meters that would be drawn.My program displays video frames from a camera, with some image enhancments. On the video I would present the bitmap overlay.So MODULATE does not seem to be the right one. If I understand it multiplys the color values of each pixel of one texture with the other.I have tried your code snippet but only got a darken image, no overlay.and also I recive a bitmap in the size of video frame that I am presenting.And the Idea is that were the transparancy is 100% we would see the video, and were it is not transparant then We would have the overlay data.This is doen in any game, now while I am writing the repaly there is an advertisment for Visual Adrenaline, there is somthing moving in the background and a picture of a girl and some text on top of the live backgournd.Thanks in advancemeir   ;  Ah, okay. Once you start asking the right questions you will get the right answers :)In such a scenario you would not want to use multiple textures but alpha blending. You could also combine both but it might be a mess to get things blended correctly. I assume your first texture holds a frame of the video while the second texture holds the overlay? Then just draw the first texture on the screen and do a second pass using the second texture with color or alpha blending. Either set the alpha channel of the second texture to mark areas of the image as transparent or put in a white color in those areas that should be transparent if your overlay elements would not use the white color. Then you enable color blending or alpha blending and get the desired effect.   ;  ThanksWell I see know I am more understoodI am trying to understand your answer.you wrote"Then just draw the first texture on the screen and do a second pass using the second texture with color or alpha blending."Well to draw the video I:device->setTexture(0,videoTexture);device->SetTextureStageStage(0,COLORARG1,texture)device->SetTextureStageStage(0,colorOp,arg1)now what is the second pass?do I need to call drawIndexPrimative?I did thisdevice->setTexture(1,overlayTexture);Now what do you mean with color or alpha blending?you wrote" Either set the alpha channel of the second texture to mark areas of the image as transparent"Yes this is what I do I have diffrent alpha values accross the bitmap.(RGBA) of every pixel is diffrent some have A=0 some have A=1But know I am stuckI set ALPHBLENDABLE TRUE, and SetRenderState(SRCBLEND,INVDESTALPHA)SetRenderState(DESTBLEND,DESTALPHA)with no color or alpha op specified, I get a black frame. nothing.I tried using several SRCBLEND DESTBLEND values. got nothing.Tried some colorOP or ALPHAOP nothingSO what do you mean with "Then you enable color blending or alpha blending and get the desired effect."Thanks again.Meir.P.S.When I set the video texture again in stage 1SetTexture(1,videoTexture) and ARg1 = texture and colorOp is selectArg1I do not see the video only a full frame with one color that is changing all the time.as if it takes the first pixels color and fills the whole frame with it.What is this?Thankss[Edited by - mezik on May 7, 2009 5:21:42 AM];  Okay, just set your video texture to texture stage 0 und leave all texture stage states to their respective defaults. Now issue your draw call. This should lead to your video image displaying exclusively.Now, extend your app by issuing a second draw call. Now put the overlay texture to stage 0 and adjust the texture stage states to do alpha blending with alpha values from your texture like so:m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);  m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);See also the MSDN DX docu for alpha blending from texture.This tells DX to take the color from the texture and from diffuse (for stage 0 this corresponds to the pixel already in the back buffer which is in this case your video image) and modulate (multiply) the RGB color components. This also sets up the alpha operation. The alpha values are again taken from your overlay texture and the back buffer and get multiplied. Now the most important part:m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);This tells DX how to use the alpha value. In this case I assume you have the alpha channel of your overlay texture (src because it is about to be rendered now) set to 0.0 (0) where you would like to have the video image (dst because its already in the back buffer) shining through and to 1.0 (255) where the overlay should be rendered. The settings above make DX take the src pixel (from your overlay texture) and multiply this by its own alpha value. To this result DX adds the dst pixel (from the back buffer => your video image) multiplied by the inverse (1.0 - a) of the src pixel's alpha value. Its easy to see that an alpha value of 0.0 "nulls out" the color from your overlay while an alpha value of 1.0 "nulls out" the color from the video image in the back buffer. See also MSDN DX docu for blend values;  Wow thanksI realy appreciate your full and detailed help.I would try this out.What you are explaining is similar to D3DTOP_BLENDTEXTUREALPHA which I have tried using. but with no success.Thanks allot.Just one question. why do I go back every time to stage 0 couldn't I have doen this in two stages? why to do stage 0 draw and then again stage 0.Well that is how I tried it and got no result. One last question What does D3DTSS_TEXCOORDINDEX  with D3DTSS_TCI_PASSTHRU mean.Again thanks allot!!!!!!!!!!!!!!!!!;  The first pass is required to fill the back buffer so that the second pass can sample the existing pixels there as DEST values to do alpha blending with the SRC values from the second draw pass.You might be able to do this with a single pass as well, but I have never tried this with the fixed function pipeline (as opposed to using shaders) so I can't give you a helping hand for that and I doubt that this second pass will be a performance issue. Also, because for the past years I have been using pixel shaders for such pixel operations I cannot really tell you about the PASSTHROUGH state. Usually D3DTSS_TEXCOORDINDEX is used to say which texture coordinate set a certain texture stage should use as a vertex can have multiple texture coordinates. You can OR the D3DTSS_TCI_XXX values to this D3DTSS_TEXCOORDINDEX indicating that Direct3D should automatically generate texture coordinates for you. THis is required for fixed function environment mapping. However, according to the docs the PASSTHROUGH value seems to deactivate this automatic texture coord generation which sounds to me as if the behavior is the same as if you would omit this PASSTHROUGH value totally.So sorry, I am lost about that.   ;  Thanks again.I went over your code snippet and your explanations, and I managed to draw overlay. now I understad what is the src and what is the dst. which was abit fuzzy.So thank you. One thing there is colorarg CURRENT you wrote to me to use DIFFUSE which would use the already drawn video frame as the dest for the new drawing calculation. So what is CURRENT? Thanks againmeir   