Drawing 2D on Direct3D device-VBForums
Results 1 to 6 of 6

Thread: Drawing 2D on Direct3D device

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2012
    Location
    việt nam (vietnam)
    Posts
    37

    Drawing 2D on Direct3D device

    Err, not a new question, huh?
    For example: I wanna draw, and fill a rectangle, 80% opacity on the device. (You can see this in NFSMW). This is intro so i'll not touch to coordinates yet.
    Also with circles, ellipses. How can I do that (especially when it's it totally opaque)?

  2. #2
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,072

    Re: Drawing 2D on Direct3D device

    Sorry bout the hold up. Been working a lot all week. This source code should help you draw a textured transparent rectangle (such as a sprite with the background removed) with you having the ability to change how alphablended it is so it fades away like a ghost. In your case you wanted it to be 80% opacity, so make it 80 or some other value around there. Use the arrow keys to fade it. Left being more alphablended. Right being less alphablended. However if you are refering to a polygon thats see thru that darkens whatevers in the background, I have code for that as well. But for now heres this:

    vb.net Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Imports Microsoft.DirectX
    5. Imports Microsoft.DirectX.Direct3D
    6.  
    7. Public Class frmMain
    8.     Private Const COLOR_DEPTH_16_BIT As Format = Format.R5G6B5
    9.     Private Const COLOR_DEPTH_24_BIT As Format = Format.A8R8G8B8
    10.     Private Const COLOR_DEPTH_32_BIT As Format = Format.X8R8G8B8
    11.  
    12.     Private Direct3D_Device As Device
    13.  
    14.     Private Fullscreen_Enabled As Boolean
    15.     Private Running As Boolean = True
    16.  
    17.     Private Vertex_List As CustomVertex.TransformedColoredTextured() = New CustomVertex.TransformedColoredTextured(0 To 3) {} 'create an array of vertices
    18.     Private Texture As Texture
    19.     Private Alpha As Integer
    20.  
    21.     Private Function Create_TLVertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal Color As Integer, ByVal TU As Integer, ByVal TV As Integer) As CustomVertex.TransformedColoredTextured
    22.  
    23.         Create_TLVertex.Position = New Vector4(X, Y, Z, 1)
    24.         Create_TLVertex.Color = Color
    25.         Create_TLVertex.Tu = TU
    26.         Create_TLVertex.Tv = TV
    27.  
    28.     End Function
    29.  
    30.     Private Sub DirectX9_Initialize()
    31.  
    32.         Dim Display_Mode As DisplayMode
    33.         Dim Direct3D_Window As PresentParameters = New PresentParameters
    34.  
    35.         If Fullscreen_Enabled = True Then
    36.             Display_Mode.Width = 800
    37.             Display_Mode.Height = 600
    38.             Display_Mode.Format = COLOR_DEPTH_16_BIT
    39.             'Check to see if fullscreen mode is supported before you use it.
    40.             If Manager.CheckDeviceType(0, DeviceType.Hardware, Display_Mode.Format, Display_Mode.Format, False) Then
    41.                 ' Perfect, this is valid
    42.                 Direct3D_Window.Windowed = False
    43.                 Direct3D_Window.BackBufferCount = 1
    44.                 Direct3D_Window.BackBufferWidth = Display_Mode.Width
    45.                 Direct3D_Window.BackBufferHeight = Display_Mode.Height
    46.             Else
    47.                 MessageBox.Show("Your video card doesn't support this screen resolution.", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
    48.             End If
    49.         Else
    50.             Direct3D_Window.Windowed = True
    51.         End If
    52.  
    53.         Direct3D_Window.SwapEffect = SwapEffect.Copy
    54.         Direct3D_Window.BackBufferFormat = Display_Mode.Format
    55.  
    56.         'Create our device
    57.         Direct3D_Device = New Device(0, DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Direct3D_Window)
    58.  
    59.         'Right here will alphablend the polygon
    60.         Direct3D_Device.SetRenderState(RenderStates.AlphaBlendEnable, True)
    61.  
    62.         'Needed for alphablending
    63.         '----------------------------------------------------------------------------------------------------
    64.         Direct3D_Device.SetTextureStageState(0, TextureStageStates.ColorOperation, TextureOperation.Modulate)
    65.         Direct3D_Device.SetTextureStageState(0, TextureStageStates.ColorArgument1, TextureArgument.TextureColor)
    66.         Direct3D_Device.SetTextureStageState(0, TextureStageStates.ColorArgument2, TextureArgument.Diffuse)
    67.  
    68.         Direct3D_Device.SetTextureStageState(0, TextureStageStates.AlphaOperation, TextureOperation.Modulate)
    69.         Direct3D_Device.SetTextureStageState(0, TextureStageStates.AlphaArgument1, TextureArgument.TextureColor)
    70.         Direct3D_Device.SetTextureStageState(0, TextureStageStates.AlphaArgument2, TextureArgument.Diffuse)
    71.  
    72.         Direct3D_Device.SetRenderState(RenderStates.SourceBlend, Blend.SourceAlpha)
    73.         Direct3D_Device.SetRenderState(RenderStates.DestinationBlend, Blend.InvSourceAlpha)
    74.         Direct3D_Device.SetRenderState(RenderStates.BlendOperation, TextureOperation.Add)
    75.         '----------------------------------------------------------------------------------------------------
    76.  
    77.         'These lines are not needed, but it's nice to be able to filter the
    78.         'textures to make them look nicer.
    79.  
    80.         Direct3D_Device.SetSamplerState(0, SamplerStageStates.MinFilter, TextureFilter.Point)
    81.         Direct3D_Device.SetSamplerState(0, SamplerStageStates.MagFilter, TextureFilter.Point)
    82.  
    83.     End Sub
    84.  
    85.     Private Sub Create_Polygon()
    86.  
    87.         Vertex_List(0) = Create_TLVertex(0, 0, 0, System.Drawing.Color.FromArgb(Alpha, 255, 255, 255).ToArgb, 0, 0)
    88.         Vertex_List(1) = Create_TLVertex(100, 0, 0, System.Drawing.Color.FromArgb(Alpha, 255, 255, 255).ToArgb, 1, 0)
    89.         Vertex_List(2) = Create_TLVertex(0, 100, 0, System.Drawing.Color.FromArgb(Alpha, 255, 255, 255).ToArgb, 0, 1)
    90.         Vertex_List(3) = Create_TLVertex(100, 100, 0, System.Drawing.Color.FromArgb(Alpha, 255, 255, 255).ToArgb, 1, 1)
    91.  
    92.     End Sub
    93.  
    94.     Private Sub Draw_Polygon()
    95.  
    96.         Direct3D_Device.VertexFormat = CustomVertex.TransformedColoredTextured.Format
    97.         Direct3D_Device.SetTexture(0, Texture)
    98.         Direct3D_Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List)
    99.  
    100.     End Sub
    101.  
    102.     Private Sub Load_Texture(ByVal Filepath As String, ByVal Transparency_Color As Integer)
    103.  
    104.         Texture = TextureLoader.FromFile(Direct3D_Device, Filepath, 512, 512, 1, Usage.None, Format.A8B8G8R8, Pool.Managed, Filter.Point, Filter.Point, Transparency_Color)
    105.  
    106.     End Sub
    107.  
    108.     Private Sub Render()
    109.  
    110.         Direct3D_Device.Clear(ClearFlags.Target, Color.Black, 1.0, 0)
    111.         Direct3D_Device.BeginScene()
    112.         'Rendering code goes here.
    113.         Create_Polygon()
    114.         Draw_Polygon()
    115.         Direct3D_Device.EndScene()
    116.         Direct3D_Device.Present()
    117.         Application.DoEvents()
    118.  
    119.     End Sub
    120.  
    121.     Private Sub Game_Loop()
    122.  
    123.         Do While Running = True
    124.             Render()
    125.         Loop
    126.  
    127.     End Sub
    128.  
    129.     Private Sub Main()
    130.  
    131.         If MessageBox.Show("Click Yes to go to full screen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
    132.             Fullscreen_Enabled = True
    133.         End If
    134.  
    135.         With Me
    136.             .Show()
    137.             .SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True) 'Do not draw forms background
    138.             .Text = "DirectX Tutorial"
    139.             If Fullscreen_Enabled = True Then .FormBorderStyle = Windows.Forms.FormBorderStyle.None
    140.         End With
    141.  
    142.         DirectX9_Initialize()
    143.         Load_Texture(Application.StartupPath() & "\Sprite1.png", System.Drawing.Color.FromArgb(0, 0, 0, 0).ToArgb)
    144.         Alpha = 255
    145.         Running = True
    146.  
    147.     End Sub
    148.  
    149.     Private Sub Shutdown()
    150.  
    151.         Running = False
    152.         Texture = Nothing
    153.         Direct3D_Device = Nothing
    154.         Application.Exit()
    155.  
    156.     End Sub
    157.  
    158.  
    159.     Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    160.  
    161.         If e.KeyCode = Keys.Escape Then Shutdown()
    162.  
    163.         'Press and hold Left or Right keys to change the Alpha values and watch the image fade away!
    164.         If e.KeyCode = Keys.Left Then
    165.             Alpha -= 5
    166.             If Alpha <= 0 Then Alpha = 0
    167.         End If
    168.  
    169.         If e.KeyCode = Keys.Right Then
    170.             Alpha += 5
    171.             If Alpha >= 255 Then Alpha = 255
    172.         End If
    173.  
    174.     End Sub
    175.  
    176.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    177.  
    178.         Main()
    179.  
    180.     End Sub
    181.  
    182.     Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    183.  
    184.         Shutdown()
    185.  
    186.     End Sub
    187.  
    188.     Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    189.  
    190.         Game_Loop()
    191.  
    192.     End Sub
    193.  
    194.     Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    195.  
    196.         If Fullscreen_Enabled = False Then
    197.             Running = False
    198.             Render()
    199.             Running = True
    200.         End If
    201.  
    202.     End Sub
    203. End Class

    As for circles and ellipses, there should be a built in function as part of DirectDraw build in to DirectX that lets you do both. Research it on Google if you need to but its definitely part of the DirectDraw portion of DirectX.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2012
    Location
    việt nam (vietnam)
    Posts
    37

    Re: Drawing 2D on Direct3D device

    Oh you're back, finally! I've been waiting for you, Jacob.
    But if i want to use Direct3d, not DirectDraw what should i do? I don't want to work with DirectDraw. They dropped it and some DirectDraw game (Age Of Empires 2, RoadRash) seems can't run properly on my computer, even I've ran the DirectDraw Fix on Windows XP.
    I'll search somewhere code to draw the circle. GDI can do this, or even Borland Pascal can do this on 16-bit machine. I think that's all about putting some pixel by order on device, doesn't it? Now we can put the pixels and the rest is algorithm.
    Anyway, thanks for you code.

  4. #4
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,072

    Re: Drawing 2D on Direct3D device

    Yea good point. I don't think you can really mix and match them unless they fixed that. However I do know you can mix and match 2D and 3D in the same rendering pipeline using Direct3D.

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2012
    Location
    việt nam (vietnam)
    Posts
    37

    Re: Drawing 2D on Direct3D device

    Hmm... what about using class Sprite (not writting my own sprite class)? Heard http://www.toymaker.info/Games/html/sprites.html say "A Sprite is the term used to describe a 2D graphics with a number of related functions. In the past hardware acceleration existed on consoles and old computers as sprites were the foundation for all 2D games. E.g. a sprite could be the player character, enemies, power ups etc."

    But tuts never touch it, however, they replaces with their own ClsSprite.

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2012
    Location
    việt nam (vietnam)
    Posts
    37

    Re: Drawing 2D on Direct3D device

    Something on http://en.wikipedia.org/wiki/Midpoint_circle_algorithm might help. I'm trying.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.