IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

DirectX Discussion :

Transparence avec direct3d managed


Sujet :

DirectX

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2006
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 13
    Points : 13
    Points
    13
    Par défaut Transparence avec direct3d managed
    Salut,
    Avec mes maigres connaissances en DirectX j'ai tenté de voir si j'arrivais à afficher un quad texturé (à partir de vertex) et de le rendre à moitié transparent dans l'espoir de pouvoir améliorer le tout en une ébauche de moteur 2d^^ L'ennui est que je ne comprend pas pourquoi ça ne fonctionne pas je pensais ne pas voir le quad texturé vu que sa couleur alpha est égal à 0 mais à la place j'ai le droit à un carré noir

    Voici pour le code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
     
    using System;
    using System.Windows.Forms;
    using System.Drawing;
    using Microsoft.DirectX;
    using Microsoft.DirectX.Direct3D;
    using System.Threading;
     
    namespace Hmm
    {
        public class AForm : Form
        {
            Device device = null;
            VertexBuffer vertexBuffer = null;
     
            Texture GrassTexture;
            Texture Arbre;
            Sprite Go;
            float Trans = 1.0f;
     
            static void Main()
            {
                AForm form = new AForm();
                form.Size = new Size(800, 600);
                form.InitializeGraphics();
                form.Show();
     
                while (form.Created)
                {
                    form.Render();
                    Application.DoEvents();
                }
            }
     
            public void InitializeGraphics()
            {
                try
                {
                    PresentParameters presentParams = new PresentParameters();
                    presentParams.Windowed = true;
                    presentParams.SwapEffect = SwapEffect.Discard;
                    device = new Device(0,
                    DeviceType.Hardware,
                    this,
                    CreateFlags.HardwareVertexProcessing,
                    presentParams);
                    LoadTextures();
                    device.DeviceReset += new System.EventHandler(this.OnResetDevice);
                    OnResetDevice(device, null);
     
                    Go = new Sprite(device);
                }
                catch (DirectXException e)
                {
                    MessageBox.Show(null,
                    "Error intializing graphics: "
                    + e.Message, "Error");
                    Close();
                }
            }
     
            private void Render()
            {
                if (device == null) //if there's nothing to render with 
                    return;//then don't bother 
     
                //Set the backbuffer to a nice blue 
                device.Clear(ClearFlags.Target,
                System.Drawing.Color.Blue,
                1.0f, 0);
     
                device.RenderState.Lighting = false;
     
                device.BeginScene();
     
                Go.Begin(SpriteFlags.AlphaBlend);
                Go.Draw2D(GrassTexture, new PointF(0.0f, 0.0f), 0f, new PointF(0, 0), Color.White);
                Go.End();
     
                // Use alpha for transparency
                device.SetRenderState(RenderStates.SourceBlend, true);
                device.SetRenderState(RenderStates.DestinationBlend, true);
     
                device.SetStreamSource(0, vertexBuffer, 0);
                device.VertexFormat = CustomVertex.PositionColoredTextured.Format;
     
                device.SetTexture(0, Arbre);
                device.Transform.World = Matrix.RotationZ(0f) * Matrix.Translation(0, 0, 0) * Matrix.Scaling(12, 12, 0);
     
                device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);
     
                device.EndScene();
                device.Present();
            }
     
            public void OnResetDevice(object sender, EventArgs e)
            {
                Device device = (Device)sender;
     
                vertexBuffer =
                new VertexBuffer(typeof(CustomVertex.PositionColoredTextured),
                4,
                device,
                0,
                CustomVertex.PositionColoredTextured.Format,
                Pool.Default);
     
                Matrix Ortho2D;
                Matrix Identity;
     
                Ortho2D = Matrix.OrthoOffCenterLH(0.0f, this.Width, 0, this.Height, -2.0f, 2.0f);
                device.SetTransform(TransformType.Projection, Ortho2D);
     
                Identity = Matrix.Identity;
                device.SetTransform(TransformType.World, Identity);
                device.SetTransform(TransformType.View, Identity);
     
                device.SetRenderState(RenderStates.ZBufferWriteEnable, false);
                device.SetRenderState(RenderStates.ZBufferFunction, true);
                this.device.RenderState.ZBufferEnable = false;
                device.RenderState.Lighting = false;
                device.RenderState.CullMode = Cull.CounterClockwise;
                device.RenderState.AlphaBlendEnable = true;
                device.RenderState.AlphaTestEnable = true;
     
                vertexBuffer.Created +=
                new System.EventHandler(this.OnCreateVertexBuffer);
                this.OnCreateVertexBuffer(vertexBuffer, null);
            }
     
            public void LoadTextures()
            {
                try
                {
     
                    System.Drawing.Bitmap grass = (System.Drawing.Bitmap)
                    System.Drawing.Bitmap.FromFile(@"C:\Grass.bmp");
                    GrassTexture = Texture.FromBitmap(device, grass, 0, Pool.Managed);
                    Arbre = TextureLoader.FromFile(device, "Arbre.png");
                }
                catch
                {
                    MessageBox.Show(this, "An error has occured while trying to load textures");
                }
            }
     
     
            public void OnCreateVertexBuffer(object sender, EventArgs e)
            {
                VertexBuffer buffer = (VertexBuffer)sender;
     
                CustomVertex.PositionColoredTextured[] verts = new CustomVertex.PositionColoredTextured[4];
     
                verts[2].Tu = 0.0f;
                verts[2].Tv = 0.0f;
     
                verts[3].Tu = 0.0f;
                verts[3].Tv = 1.0f;
     
                verts[0].Tu = 1.0f;
                verts[0].Tv = 1.0f;
     
                verts[1].Tu = 1.0f;
                verts[1].Tv = 0.0f;
     
                verts[0].Position = new Vector3(0, 0, 1.0f);
                verts[1].Position = new Vector3(0, 32, 1.0f);
                verts[2].Position = new Vector3(32, 32, 1.0f);
                verts[3].Position = new Vector3(32, 0, 1.0f);
     
                Color _Color = Color.FromArgb(0, Color.White);
                int IColor = _Color.ToArgb();
     
                verts[0].Color = IColor;
                verts[1].Color = IColor;
                verts[2].Color = IColor;
                verts[3].Color = IColor;
     
                buffer.SetData(verts, 0, LockFlags.None);
     
            }
        }
    }
    Merci d'avance pour vos réponse++.

  2. #2
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    Tu devrais relire la doc des paramètres pour l'alpha-blending :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    device.SetRenderState(RenderStates.AlphaBlendEnable, true);
    device.SetRenderState(RenderStates.SourceBlend, Blend.SourceAlpha);
    device.SetRenderState(RenderStates.DestinationBlend, Blend.InvSourceAlpha);

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2006
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 13
    Points : 13
    Points
    13
    Par défaut
    Salut,
    Ouai j'ai relu la doc et je suis tombé sur l'exemple où il disait comment rendre transparent un quad. J'ai fait pareil mais ça ne fonctionne pas correctement, on dirait que quoi que je mette pour la valeur alpha des vertex, direct3d ne le prend pas en compte lorsque je mets tous à 0 la forme en l'occurence ici mon arbre ^^ devient noir. Je me dis que ça doit ètre à cause du fait que dans l'exemple ils utilisaient des PositionColored et non pas des PositionColoredTextured. Mais j'avou ne pas trop savoir quoi faire là
    Dur dur les débuts en MDX

    Voici le code qui à très légérement changé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
     
    using System;
    using System.Windows.Forms;
    using System.Drawing;
    using Microsoft.DirectX;
    using Microsoft.DirectX.Direct3D;
    using System.Threading;
     
    namespace Hmm
    {
        public class AForm : Form
        {
            Device device = null;
            VertexBuffer vertexBuffer = null;
     
            Texture GrassTexture;
            Texture Arbre;
            Sprite Go;
     
            static void Main()
            {
                AForm form = new AForm();
                form.Size = new Size(800, 600);
                form.InitializeGraphics();
                form.Show();
     
                while (form.Created)
                {
                    form.Render();
                    Application.DoEvents();
                }
            }
     
            public void InitializeGraphics()
            {
                try
                {
                    PresentParameters presentParams = new PresentParameters();
                    presentParams.Windowed = true;
                    presentParams.SwapEffect = SwapEffect.Discard;
                    device = new Device(0,
                    DeviceType.Hardware,
                    this,
                    CreateFlags.HardwareVertexProcessing,
                    presentParams);
                    LoadTextures();
                    device.DeviceReset += new System.EventHandler(this.OnResetDevice);
                    OnResetDevice(device, null);
     
                    Go = new Sprite(device);
                }
                catch (DirectXException e)
                {
                    MessageBox.Show(null,
                    "Error intializing graphics: "
                    + e.Message, "Error");
                    Close();
                }
            }
     
            private void Render()
            {
                if (device == null) //if there's nothing to render with 
                    return;//then don't bother 
     
                //Set the backbuffer to a nice blue 
                device.Clear(ClearFlags.Target,
                System.Drawing.Color.Blue,
                1.0f, 0);
     
                device.BeginScene();
     
                Go.Begin(SpriteFlags.AlphaBlend);
                Go.Draw2D(GrassTexture, new PointF(0.0f, 0.0f), 0f, new PointF(0, 0), Color.White);
                Go.End();
     
                device.RenderState.DiffuseMaterialSource = ColorSource.Color1;
     
                device.RenderState.SourceBlend = Blend.SourceAlpha;
                device.RenderState.DestinationBlend = Blend.InvSourceAlpha;
                device.RenderState.AlphaBlendEnable = true;
     
                device.SetStreamSource(0, vertexBuffer, 0);
                device.VertexFormat = CustomVertex.PositionColoredTextured.Format;
     
                device.SetTexture(0, Arbre);
                device.Transform.World = Matrix.RotationZ(0f) * Matrix.Translation(0, 0, 0) * Matrix.Scaling(12, 12, 0);
     
                device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);
     
                device.EndScene();
                device.Present();
            }
     
            public void OnResetDevice(object sender, EventArgs e)
            {
                Device device = (Device)sender;
     
                vertexBuffer =
                new VertexBuffer(typeof(CustomVertex.PositionColoredTextured),
                4,
                device,
                0,
                CustomVertex.PositionColoredTextured.Format,
                Pool.Default);
     
                Matrix Ortho2D;
                Matrix Identity;
     
                Ortho2D = Matrix.OrthoOffCenterLH(0.0f, this.Width, 0, this.Height, -2.0f, 2.0f);
                device.SetTransform(TransformType.Projection, Ortho2D);
     
                Identity = Matrix.Identity;
                device.SetTransform(TransformType.World, Identity);
                device.SetTransform(TransformType.View, Identity);
     
                device.SetRenderState(RenderStates.ZBufferWriteEnable, false);
                device.SetRenderState(RenderStates.ZBufferFunction, true);
                this.device.RenderState.ZBufferEnable = false;
                device.RenderState.Lighting = false;
                device.RenderState.CullMode = Cull.CounterClockwise;
     
                vertexBuffer.Created +=
                new System.EventHandler(this.OnCreateVertexBuffer);
                this.OnCreateVertexBuffer(vertexBuffer, null);
            }
     
            public void LoadTextures()
            {
                try
                {
     
                    System.Drawing.Bitmap grass = (System.Drawing.Bitmap)
                    System.Drawing.Bitmap.FromFile(@"C:\Grass.bmp");
                    GrassTexture = Texture.FromBitmap(device, grass, 0, Pool.Managed);
                    Arbre = TextureLoader.FromFile(device, "Arbre.png");
                }
                catch
                {
                    MessageBox.Show(this, "An error has occured while trying to load textures");
                }
            }
     
     
            public void OnCreateVertexBuffer(object sender, EventArgs e)
            {
                VertexBuffer buffer = (VertexBuffer)sender;
     
                CustomVertex.PositionColoredTextured[] verts = new CustomVertex.PositionColoredTextured[4];
     
                verts[2].Tu = 0.0f;
                verts[2].Tv = 0.0f;
     
                verts[3].Tu = 0.0f;
                verts[3].Tv = 1.0f;
     
                verts[0].Tu = 1.0f;
                verts[0].Tv = 1.0f;
     
                verts[1].Tu = 1.0f;
                verts[1].Tv = 0.0f;
     
                verts[0].Position = new Vector3(0, 0, 1.0f);
                verts[1].Position = new Vector3(0, 32, 1.0f);
                verts[2].Position = new Vector3(32, 32, 1.0f);
                verts[3].Position = new Vector3(32, 0, 1.0f);
     
                Color _Color = Color.FromArgb(255, 255, 255, 255);
                int IColor = _Color.ToArgb();
     
                verts[0].Color = IColor;
                verts[1].Color = IColor;
                verts[2].Color = IColor;
                verts[3].Color = IColor;
     
                buffer.SetData(verts, 0, LockFlags.None);
     
            }
        }
    }
    Bon ben merci d'avance à tous ceux qui prendront le temps de lire ce post

Discussions similaires

  1. Texte en transparence avec TextOut
    Par TigreRouge dans le forum MFC
    Réponses: 2
    Dernier message: 06/06/2005, 22h57
  2. PB de vue utilisant UNION avec ENTERPRISE MANAGER
    Par punglas dans le forum MS SQL Server
    Réponses: 3
    Dernier message: 22/12/2004, 15h18
  3. [ant]: interaction avec le manager de tomcat
    Par sleepy2002 dans le forum Tomcat et TomEE
    Réponses: 2
    Dernier message: 07/10/2004, 15h02
  4. probleme de transparence avec fog
    Par Daedar dans le forum OpenGL
    Réponses: 10
    Dernier message: 03/05/2004, 08h14
  5. Afficher une buffer de pixel avec Direct3D
    Par Black_Daimond dans le forum DirectX
    Réponses: 4
    Dernier message: 27/12/2002, 22h18

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo