Results 1 to 5 of 5

Thread: DirectX8 - Material Alpha Blending

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    118

    DirectX8 - Material Alpha Blending

    NB : The attached VB app is set at a resolution of 1600 x 1200 which may not work. If so just change the screen res via code ty.

    Hi all

    I know there r many ways of alpha blending in DirectX8 but i really need to know how to do it via material properties. Basically i want to be able to change the value of diffuse.a / ambient.a (belonging to DIRECT3DMATERIAL8) and thus change the value ofalpha and thus the translucency of a primitive (triangle). I have made a program but it doesnt do the job although it is set up and probably just needs 1 or 2 lines changed to make it to work. If any1 can fiddle with the app and get it workng i would be greatful otherwsie any help on material based alpha blending would be much appreciated.

    Yours sincerely

    Daniel Lewis

    PS I am fully aware that there r many other types of alpha blending and I know how to do them I just need to know how to do this 1 as well now (its much simpler than the other types Im sure I just cant get it working)

    PPS The stuff on this particular subject on www.directx4vb.com doesnt work!!!!
    Attached Files Attached Files

  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Didnt look at your project, but try these:
    Code:
    D3DDevice.SetRenderState D3DRS_ALPHABLENDENABLE, 1
    D3DDevice.SetRenderState D3DRS_AMBIENT, &Hffffffff
    The first sets blending on, the second sets the ambient light to full white.

    Z.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    118
    Thanks for trying Z but no luck on this 1. Here is the info on this subject on www.directx4vb.com

    As you can see from the code given .Col is not a property of
    D3DMATERIAL8. So I know its wrong the question is what is the right way of doing it. I underatnd if this 1 will be too time consuming for you to help me on Z. This isnt essential to my project but would be useful.

    ---------- DIRECTX4VB ----------------------------------------------------

    This is a nice simple one this, but useful to know about. The first (per-vertex) technique only work with manually lit vertices - where you can specify the colour. So what happens when you're letting Direct3D do the lighting for you? You use the material properties! You have no where near the same amount of control (no interpolation across a triangle), but it's useful for making an entire model/buffer a certain colour or opacity (the alpha value is measured as opacity - best seen in paint packages), it's also excellent for motion blur (should you like that sort of thing). Here's how we configure our material, apply it to the device, and then make sure Direct3D uses the alpha component:

    D3DDevice.SetRenderState D3DRS_ALPHABLENDENABLE, 1
    D3DDevice.SetRenderState D3DRS_DESTBLEND, D3DBLEND_SRCCOLOR
    D3DDevice.SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
    Dim mat as D3DMATERIAL8
    mat.Col.A=AlphaVal 'on a 0.0 to 1.0 scale
    mat.Col.R = 1: mat.Col.G = 1: mat.Col.B = 1 'material color doesn't affect texture or lighting colour
    mat.Ambient = mat.Col
    mat.Diffuse = mat.Col

    D3DDevice.SetMaterial mat

    '//Render geometry here!

    D3DDevice.SetRenderState D3DRS_ALPHABLENDENABLE, 0

    Not too complicated really, just make sure that you disable alpha blending after you're finished (otherwise other objects will get alpha-blended as well).

    --------------------------------------END DIRECTX4VB --------------------

  4. #4
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Got it! Just change your Render Code to this:
    Code:
    With Material
                .Ambient.a = AlphaValue
                .Ambient.r = 1
                .Ambient.g = 1
                .Ambient.b = 1
                
                .diffuse = .Ambient
            
            End With
        
            D3DDevice.SetMaterial Material
        
            D3DDevice.SetRenderState D3DRS_ALPHABLENDENABLE, 1
                    D3DDevice.SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
            D3DDevice.SetRenderState D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA
            D3DDevice.SetTextureStageState 0, D3DTSS_COLORARG1, D3DTA_TEXTURE
            D3DDevice.SetTextureStageState 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE
            D3DDevice.SetTextureStageState 0, D3DTSS_COLOROP, D3DTOP_MODULATE
            D3DDevice.SetTextureStageState 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE
            D3DDevice.SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1
            
            D3DDevice.SetTexture 0, Gold
            
            D3DDevice.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Test1(0), Len(Test1(0))
                    D3DDevice.SetVertexShader FVFUnlitVertexWS
                    
            D3DDevice.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Test2(0), Len(Test2(0))
            
            D3DDevice.SetRenderState D3DRS_ALPHABLENDENABLE, 0
    The 5 calls to SetTextureStageState are the really important ones, especially the last two. What was going on was that since you had a texture, the alpha was defaulting to the embedded texture alpha values, so the calls set up the blending to use the diffuse(material) color.

    Z.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    118

    THANK YOU

    Z

    Wow man u r the best! Its works perfectly. I knew is was something to do with setrenderstate or settexturestagestate but i just didnt know what (hence being able to alter the blending values in the project).

    That is excellent. In my college project I want to use lights (sparcely) so I really needed this to work so that I could alpha blend unlit vertices. Things r looking up now . I really do appreciate your help.

    I also removed the textures used and the blending still worked perfectly. Amazing! I was thinking maybe I wont need textures for things such as glass (but then again who has white glass?).

    CHEERS Z (big thumbs up)

    If theres anything I could do to help u out just let me know

    Dan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width