Thread: Alpha blending rocks

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    Alpha blending rocks

    Check this screenshot out. Sorry for flooding the board with these...but who knows...it might become a game.

    The sky texture is the same as the ground texture..but it is alpha blended with the sky and fog color - producing very good results.

  2. #2
    That's pretty spiffy. I never thought of doing that before.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    now just make another render pass on the ground with that texture again, except this time grayscale, and then have both the cloud texture coords and those texture coords move together, and you can have cloud shadows on the ground

    [edit]
    nice job on the clouds, btw...
    [/edit]
    I came up with a cool phrase to put down here, but i forgot it...

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Changing the u and v values of the texture would force me to lock and unlock the vertex buffer. In order to implement that I will have to re-design my skyplane class to use a user vertex buffer rather than a Direct3D vertex buffer. It will also have to rendered with DrawPrimitiveUP()

    Also a second rendering pass would be very expensive - texture blending could accomplish the same thing.
    Last edited by VirtualAce; 04-04-2004 at 03:58 PM.

  5. #5
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    How hard is is to do alpha with directx? With OpenGL, rendering thigns like glass involves sorting the potentially visible alpha blended primitives by distance, then rendering the furthest away first. in the editor, I have to say that each alpha blended face is actually a portal (so that the BSP thing knows it is see through).

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Alpha is extremely easy with Direct3D.


    Device->SetRenderState(D3DRS_ALPHABLENDENABLE,true);


    That's it. See DirectX is essentially a state machine. Each object you render can have its own states. And while you think that these calls would be expensive...they don't seem to be.


    So if we want fog for our terrain we do this:

    float fogstart=900.0f;
    float fogend=1000.0f;

    Device->SetRenderState(D3DRS_FOGENABLE,true);
    Device->SetRenderState(D3DRS_FOGSTART,*(DWORD *)fogstart);
    Device->SetRenderState(D3DRS_FOGEND,*(DWORD *)fogend);


    Now all vertexes will be fogged according to the D3D formula for vertex fog if they fall within the distance specified.

    Then say we are rendering a skybox and we don't want it fogged because the fog 'piles up' at the corners and makes the skybox very visible, we simply turn the fog off and render.

    Device->SetRenderState(D3DRS_FOGENABLE,false);
    SkyBox.Render(camerapos);


    But instead of making all these calls out in open space between Device->BeginScene() and Device->EndScene() - it is best to make all render states private to the object and or objects that share the same states. That way you can be sure that render states are correctly setup prior to rendering each object.

    If you did not turn alpha blending off before rendering the terrain it would simply render an alpha blended see-through terrain - cool for water effects, but not terrain. So for water you would make a water class and then in its render function you would turn on alpha blending and then turn it off.

    But like I said grouping alpha blended objects would be a good idea too. You could simply create a base template class for alpha blended objects and then derive all alpha blended objects from it. The class would then turn alpha blending on, render all the objects, and turn it off.

    There are so many approaches and methods I haven't even begun to scratch the surface. Direct3D is very powerful but IMO very simple to use.


    By the way, I need some 3D meshes of trees complete with textures and mesh subsets for rendering. As well if anyone has any characters, explosions, or other such objects I'd be very interested in them. I'm currently working on adding a sun and lense flares to the engine.

  7. #7
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169
    looking good bubba
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

  8. #8
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Bubba, what happens in these scenarios:

    1)
    clear screen to black
    render an alpha blended primitive FIRST. Does it ever show up?

    2) render an alpha blended primitive behind the already alpha blended sky, does the primitive behind the sky show up?

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote Originally Posted by Bubba
    Changing the u and v values of the texture would force me to lock and unlock the vertex buffer. In order to implement that I will have to re-design my skyplane class to use a user vertex buffer rather than a Direct3D vertex buffer. It will also have to rendered with DrawPrimitiveUP()

    Also a second rendering pass would be very expensive - texture blending could accomplish the same thing.
    You could just use the Texture Matrix stuff to manipulate your uv coordinates. Just a thought.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That would still require locking the buffer. The vertexes are stored in the buffer so to change them requires a lock. I've already implemented this...and it didn't slow down at all so I'm sticking with it.

    Direct3D is grabbing the vertexes from the vertex buffer which holds the u and v texel coords. I could change the u and v in my class, but that would not change them in the actual buffer D3D is using. Sort of like you can change the data in your sound buffers all you want...but unless the DMA chip is reading from those buffers it doesn't matter - it reads from the memory area you specify, not from the class that holds the data.

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote Originally Posted by Bubba
    That would still require locking the buffer. The vertexes are stored in the buffer so to change them requires a lock. I've already implemented this...and it didn't slow down at all so I'm sticking with it.
    No, it doesn't require a lock. I said Texture Matrix. This is a transformation that is applied to your uv coordinates when they are going down the pipeline. Much like a word transform or camera transform. This is a fixed-function pipeline transformation, no locking is required.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  12. #12
    MrWizard is right. I don't know exactly how D3D does the texture matrix, but I've used it b4 in OGL.

  13. #13
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Not bad man!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Direct3D10 Sprites Alpha Blending
    By DarkMasterBosel in forum Game Programming
    Replies: 0
    Last Post: 01-17-2009, 07:17 AM
  2. Replies: 23
    Last Post: 07-09-2007, 04:49 AM
  3. 2 questions in DirectX
    By X PaYnE X in forum Game Programming
    Replies: 4
    Last Post: 12-02-2004, 09:38 AM
  4. Alpha blending w/o color loss
    By VirtualAce in forum Game Programming
    Replies: 37
    Last Post: 10-27-2004, 12:58 AM
  5. Alpha blending
    By SMurf in forum Game Programming
    Replies: 3
    Last Post: 08-29-2003, 06:50 PM