Simple opacity effect

How Simple as that. Im trying managed directx 9. I dont want transparent (i still want to see the bitmap). I havent found any samples or documentation on this subject. Any help is much appreciated, or a point in the right direction. Oh, im trying to do this with sprites.


Answer this question

Simple opacity effect

  • ko712000

    hmm, maybe i didnt pass what i wanted to do correctly, i want to do this programatilly, im trying to make a fade in and out effect, and created a 100 pictures with different alpha masks for each different button is to much work. i tried using the color settings when creating the sprite to set and its either on or off, there is no in between which is what im looking for. programticlly changing the alpha channel of the sprite. please help, im completely stumped.

  • nasr0001

    I'm not quite clear on what you want to do, but perhaps you're speaking of something like the button effect in the CustomUI sample (found in the DirectX Sample Browser).  Look at the code for that effect, I think it does what you're looking for.

  • Potyos

    You may need to tinker with your presentation parameters.  I wanted to do the very thing you are attempting to do now; and after a few struggles, figured out that my main problem was that I wasn't using alpha colors in the Draw2D function of the Sprite object.  However, apparently the color values aren't the problem with your source code.

    I also noticed that you are using the Draw method of the Sprite object.  Try using Draw2D (since you are going with a 2D solution in this case).  If you wish to work with the Draw method (for possible 3D implementation later), you may need to change the values of some arguments in that method.  The 3D element adds another factor when drawing textures.  Unfortunately, I haven't begun to deal with 3D texture drawing.

    Ok, so back to the presentation parameters.

    I have my presentation parameters set to the following:



    // Set generic presentation parameters

    PresentParams.SwapEffect = SwapEffect.Discard;

    PresentParams.EnableAutoDepthStencil = true;

    PresentParams.AutoDepthStencilFormat = DepthFormat.D16;

    PresentParams.PresentationInterval = PresentInterval.One;

    PresentParams.Windowed = true;

     



    I then use the following method to draw my sprites with some experimental effects:



    SpriteController.Begin(SpriteFlags.AlphaBlend);

    float xv = 0;

    int av = 0;

    float rv = 0;

    float sv = 0;

    for (int passes = 0; passes <= 50; passes += 1)

    {

    Point NewPoint = new Point((int)xv, SpritePoint1.Y);

    // Ensure the alpha value does not go below 0

    if (av > 255) { av = 255; }

    // Ensure sv does not become negative

    if (sv < 0) { sv = 0; }

    if (sv > 2) { sv = 2; }

    // Make the top most layer full opaque

    if (passes == 50) { av = 255; }

    NewPoint.X = SpritePoint1.X + (int)xv;

    SpriteController.Draw2D(sprite.Texture, new Rectangle(0, 0, sprite.Width, sprite.Height), new Rectangle(0, 0, (int)(sprite.Width * sv), (int)(sprite.Height * sv)), new Point(20, 2), rv, NewPoint, Color.FromArgb(av, 255, 255, 255));

    xv += 0f;

    av += 5;

    rv += 0.01f;

    sv += 0.04f;//2 / (50 / 2);

    }

    //SpriteController.Draw2D(texTest, new Point(0, 0), 0, SpritePoint1, Color.FromArgb(150, 255, 255, 255));

    SpriteController.Draw2D(sprite.Texture, new Rectangle(0, 0, sprite.Width, sprite.Height), new Rectangle(0, 0, sprite.Width, sprite.Height), new Point(0, 0), Color.FromArgb(200, 155, 255, 150));

    //SpriteController.Draw2D(texTest, new Point(0, 0), 0, SpritePoint2, Color.FromArgb(200, 155, 255, 150));

    SpriteController.End();

     



    In addition to all this, I'm using the following method to load my sprite (texture):


    m_texture = TextureLoader.FromFile(device, m_sourceFile, 0, 0, 0, Usage.Dynamic, Format.Unknown, Pool.Default, Filter.None, Filter.None, m_maskColor.ToArgb(), ref m_imageInformation);


     



    The sprite draw method I included above, draws textures with varying opacities and works on my machine.

    Some notes:

    1.  SpriteController is simply an instance object of the Sprite class.

    2.  The

    sprite
     
    object is an instance of a custom SpriteImage class that I made to conveniently store image and texture information to pass to Direct3D methods (such as Sprite.Draw2D). 

    m_texture
     
    is a member variable for my SpriteImage class and that variable gets returned using the Texture property of the SpriteImage class.  Thus,

    sprite.Texture
     
    returns the Texture resource to the Draw2D function properly.

    Hope this works for you and helps.

    If not, try using your code (if not done already) in the Managed DirectX sample project since that project has everything setup properly.  You need to make sure that you have everything setup to properly dispose and restore DirectX resources (such as adding event handlers for the Device.OnDeviceReset() event and the Form.OnPaint() event.

    I'm not sure what your level of DirectX and/or C# is; so hopefully I'm not speaking complete greek here (or should I say "geek", lol geek-speak...)  Anyway, let me know if any of this doesn't work or if you still run into problems getting the varying opacity working for drawing textures.  I'd hate to see a good idea go to waste!

    As I write this now, I realize that your last post was on 8 Aug and it is now almost October; so I'm probably a little late with this, but perhaps some programmer browsing the internet in need of help will come across this and find some benefit.  We shall have to see...  At least that way I won't feel like I've completely wasted my time.

    - Aaron Misner



  • Dgates123

    heres the code that i currently have
                _Device.RenderState.AlphaBlendEnable = True
                _Device.RenderState.SourceBlend = Blend.SourceAlpha
                _Device.RenderState.DestinationBlend = Blend.InvSourceAlpha

     

                renderSprite.Begin(SpriteFlags.AlphaBlend)
                renderSprite.Transform = Matrix.Scaling(CType(screenWidth, Single) / CType(backgroundSource.Width, Single), CType(screenHeight, Single), CType(backgroundSource.Height, Single), 0)
                renderSprite.Draw(backgroundTexture, backgroundSource, ObjectCenter, ObjectCenter, SpriteColor)
                renderSprite.Transform = Matrix.Identity
                _device.RenderState.AlphaBlendEnable = True
                renderSprite.Draw(buttonTextureOff, offSource, UI_Screen.ObjectCenter, New Vector3(0, 0, 1), System.Drawing.Color.FromArgb(100, Color.White))
                _device.RenderState.AlphaBlendEnable = False
                renderSprite.End()


    heres the stuff that i tried for the alpha parts of creating the device

    '_Device.RenderState.AlphaSourceBlend = Blend.SourceAlpha
    '_Device.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha
    '_Device.RenderState.AlphaBlendOperation = BlendOperation.Max
    '_Device.RenderState.AlphaSourceBlend = Blend.SourceAlpha
    '_Device.RenderState.AlphaDestinationBlend = Blend.Zero
    '_Device.RenderState.AlphaTestEnable = True
    '_Device.RenderState.SeparateAlphaBlendEnabled = True
    '
    '_Device.RenderState.AlphaFunction = Compare.Greater
    '_Device.RenderState.AlphaTestEnable = True
    '_Device.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha
    '_Device.RenderState.SourceBlend = Blend.SourceAlpha
    '_Device.TextureState(0).ColorOperation = TextureOperation.Modulate
    '_Device.TextureState(0).ColorArgument1 = TextureArgument.TextureColor
    '_Device.TextureState(0).ColorArgument2 = TextureArgument.Diffuse
    '_Device.TextureState(0).AlphaOperation = TextureOperation.SelectArg1
    '_Device.TextureState(0).AlphaArgument1 = TextureArgument.TextureColor

    'none of the above changes did anything at all to the following results.
    and for rendering the sprite

    'renderSprite.Draw(buttonTextureOff, offSource, UI_Screen.ObjectCenter, New Vector3(0, 0, 1), System.Drawing.Color.FromArgb(100, Color.Transparent)) nothing: all white no opacity
    'renderSprite.Draw(buttonTextureOff, offSource, UI_Screen.ObjectCenter, New Vector3(0, 0, 1), System.Drawing.Color.FromArgb(100, Color.white)) nothing: all white no opacity
    'renderSprite.Draw(buttonTextureOff, offSource, UI_Screen.ObjectCenter, New Vector3(0, 0, 1), System.Drawing.Color.FromArgb(100, nothing)) nothing: all white no transparency
    'renderSprite.Draw(buttonTextureOff, offSource, UI_Screen.ObjectCenter, New Vector3(0, 0, 1), System.Drawing.Color.FromArgb(100, Color.Black)) nothing: all black no transparency
    'renderSprite.Draw(buttonTextureOff, offSource, UI_Screen.ObjectCenter, New Vector3(0, 0, 1), System.Drawing.Color.FromArgb(100, Color.Violet)) nothing: all violet no transparency
    'renderSprite.Draw(buttonTextureOff, offSource, UI_Screen.ObjectCenter, New Vector3(0, 0, 1), System.Drawing.Color.white nothing: all white no transparency
    'renderSprite.Draw(buttonTextureOff, offSource, UI_Screen.ObjectCenter, New Vector3(0, 0, 1), System.Drawing.Color.white) nothing: all white no transparency
    renderSprite.Draw(buttonTextureOff, offSource, UI_Screen.ObjectCenter, New Vector3(0, 0, 1), System.Drawing.Color.FromArgb(0, Color.Violet)) nothing, its not shown at all




    my image textures in the file are all black.
    im using the august sdk. although it did the same with the april as well.



  • djee

    http://msdn.microsoft.com/archive/default.asp url=/archive/en-us/directx9_m_Summer_04/directx/ref/ns/microsoft.directx.direct3d/c/sprite/sprite.asp




  • Simple opacity effect