VB6 programming: DirectX 2D graphics learning log 18 transparency

VB6 programming: DirectX 2D graphics learning log 18 transparency
tutorial download address : https://download.csdn.net/download/gosub60/13696651 The
source code is as follows, note: a character sticker is required

'---------------------------------
'标题:DirectX教程
'
'描述:透明度
'
'作者:Jacob Roman 翻译:[email protected] QQ:127644712
'
'日期:2005年12月2日
'
'联系人:[email protected]
'---------------------------------

Option Explicit

'2D(已转换和已点燃)顶点格式类型。
Private Type TLVERTEX

    X As Single
    Y As Single
    Z As Single
    RHW As Single
    Color As Long
    Specular As Long
    TU As Single
    TV As Single
    
End Type

'一些颜色深度常数有助于使DX常数更具可读性。
Private Const COLOR_DEPTH_16_BIT As Long = D3DFMT_R5G6B5
Private Const COLOR_DEPTH_24_BIT As Long = D3DFMT_A8R8G8B8
Private Const COLOR_DEPTH_32_BIT As Long = D3DFMT_X8R8G8B8

'2D(转换和点亮)顶点格式。
Private Const FVF_TLVERTEX As Long = D3DFVF_XYZRHW Or D3DFVF_TEX1 Or D3DFVF_DIFFUSE Or D3DFVF_SPECULAR

Private DirectX8 As DirectX8 '主DirectX对象。
Private Direct3D As Direct3D8 '控制3D一切。
Private Direct3D_Device As Direct3DDevice8 '表示硬件渲染
Private Direct3DX As D3DX8

Private Fullscreen_Enabled As Boolean '帮助确定它是否为全屏模式。
Private Running As Boolean '帮助确定主游戏循环是否正在运行。

Private Vertex_List(3) As TLVERTEX '4个顶点将构成一个正方形

Private Texture As Direct3DTexture8

''使用此功能可以更轻松地设置具有所需信息的顶点。
Private Function Create_TLVertex(X As Single, Y As Single, Z As Single, RHW As Single, Color As Long, Specular As Long, TU As Single, TV As Single) As TLVERTEX

    Create_TLVertex.X = X
    Create_TLVertex.Y = Y
    Create_TLVertex.Z = Z
    Create_TLVertex.RHW = RHW
    Create_TLVertex.Color = Color
    Create_TLVertex.Specular = Specular
    Create_TLVertex.TU = TU
    Create_TLVertex.TV = TV
    
End Function

Private Function DirectX_Initialize() As Boolean

    On Error GoTo Error_Handler
    
     Dim Display_Mode As D3DDISPLAYMODE '显示模式说明。
    Dim Direct3D_Window As D3DPRESENT_PARAMETERS 'Backbuffer和视口说明。
    
    Set DirectX8 = New DirectX8 '创建DirectX对象。
    Set Direct3D = DirectX8.Direct3DCreate() '使用DirectX对象创建Direct3D对象。
    Set Direct3DX = New D3DX8
    
    If Fullscreen_Enabled = True Then
    
        '“现在我们正在全屏模式下工作,我们必须设置
         '屏幕分辨率切换为,而不是使用默认屏幕
         '解析度。
        
        Display_Mode.Width = 1024
        Display_Mode.Height = 768
        Display_Mode.Format = COLOR_DEPTH_32_BIT
    
        Direct3D_Window.Windowed = False '该应用程序将处于全屏模式。
        Direct3D_Window.BackBufferCount = 1 '仅1个后缓冲
        Direct3D_Window.BackBufferWidth = Display_Mode.Width '使后缓冲宽度与显示宽度匹配
        Direct3D_Window.BackBufferHeight = Display_Mode.Height '使后缓冲高度与显示高度匹配
        Direct3D_Window.hDeviceWindow = frmMain.hWnd '使用frmMain作为设备窗口。
        
    Else
    
       Direct3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, Display_Mode '使用您当前使用的显示模式
                                                                         '已经在。 如果您感到困惑,我是
                                                                         '在谈论您当前的屏幕分辨率。 ;)
        
        Direct3D_Window.Windowed = True '该应用程序将处于窗口模式。
    
    End If
    
     Direct3D_Window.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC '监视器运行时刷新。
    Direct3D_Window.BackBufferFormat = Display_Mode.Format '设置检索到后缓冲区中的格式。
    
   '使用一些有用的信息以及信息创建渲染设备
     '我们已经设置了Direct3D_Window。
    Set Direct3D_Device = Direct3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, frmMain.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Direct3D_Window)
    
    Direct3D_Device.SetVertexShader FVF_TLVERTEX '设置顶点着色的类型。 (需要)
    
    '设置属性以提高透明度。
    Direct3D_Device.SetRenderState D3DRS_ALPHAREF, 255
    Direct3D_Device.SetRenderState D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL
     '不需要这些行,但能够过滤掉
     '使它们看起来更好的纹理。
    
    Direct3D_Device.SetTextureStageState 0, D3DTSS_MINFILTER, D3DTEXF_POINT
    Direct3D_Device.SetTextureStageState 0, D3DTSS_MAGFILTER, D3DTEXF_POINT

    Exit Function
    
Error_Handler:
    
    MsgBox "初始化DirectX时发生错误!", vbCritical
    
    Close_Program
    
    DirectX_Initialize = False


End Function




Private Sub Create_Polygon()

    Vertex_List(0) = Create_TLVertex(0, 0, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 0, 0)
    Vertex_List(1) = Create_TLVertex(100, 0, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 1, 0)
    Vertex_List(2) = Create_TLVertex(0, 100, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 0, 1)
    Vertex_List(3) = Create_TLVertex(100, 100, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 1, 1)

End Sub

Private Sub Load_Texture()

    Dim File_Path As String
    Dim Width As Long
    Dim Height As Long
    Dim Transparency_Color As Long
    
    File_Path = App.Path & "\Sprite1.bmp"

    Width = 256
    Height = 256
    
    Transparency_Color = D3DColorRGBA(0, 0, 0, 255)

    Set Texture = Direct3DX.CreateTextureFromFileEx(Direct3D_Device, _
                                                    File_Path, _
                                                    Width, Height, _
                                                    0, _
                                                    0, _
                                                    D3DFMT_A8R8G8B8, _
                                                    D3DPOOL_MANAGED, _
                                                    D3DX_FILTER_POINT, _
                                                    D3DX_FILTER_POINT, _
                                                    Transparency_Color, _
                                                    ByVal 0, _
                                                    ByVal 0)

End Sub

Private Sub Game_Loop() '游戏循环

    Do While Running = True
        
        DoEvents '允许事件发生,以便程序不会锁定。
        
         '------------------------------------------------- ---
         'DirectX会自动为您处理帧速率
         '这使其运行(最多)与监视器一样快
'         刷新率高,因此您无需在其中添加额外的代码
'         降低循环速度并以一定数量的帧运行
         '每秒。
         '------------------------------------------------- ---
        
         '清除后缓冲。
        Direct3D_Device.Clear 0, ByVal 0, D3DCLEAR_TARGET, D3DColorRGBA(0, 255, 0, 0), 1#, 0
            
            Direct3D_Device.BeginScene
            
                '就在这里将赋予多边形透明度
                Direct3D_Device.SetRenderState D3DRS_ALPHATESTENABLE, True
                '设置纹理
                Direct3D_Device.SetTexture 0, Texture
            
                '绘制多边形
                Direct3D_Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0))
            
            Direct3D_Device.EndScene
        
        '将后缓冲区翻转到窗体窗口中。
        Direct3D_Device.Present ByVal 0, ByVal 0, 0, ByVal 0
        
    Loop

End Sub



Private Sub Close_Program()

    Running = False '这有助于程序退出游戏循环。
    
    '卸载所有DirectX对象。
    
    Set Texture = Nothing
    Set Direct3DX = Nothing
    Set Direct3D_Device = Nothing
    Set Direct3D = Nothing
    Set DirectX8 = Nothing
    
    Unload Me '卸载窗体
    
    End ''结束程序
        
        '尽管上方的Unload语句退出了程序,但是您
         '这样做后将导致自动化错误?
         'END 命令 将有助于防止这种情况,并彻底结束该应用程序。

End Sub


Private Sub Form_Activate()

    frmMain.Caption = "DirectX教程:带有彩色顶点的纹理映射"
    
    DirectX_Initialize '初始化DirectX和Direct3D。
    
    Create_Polygon '创建多边形。
    
    Load_Texture '从文件加载纹理贴图
    
    Running = True '全部初始化。 现在可以激活游戏循环了。
    
    Game_Loop
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    If KeyCode = vbKeyEscape Then '如果用户按Esc键...
    
        Close_Program
    
    End If

End Sub

Private Sub Form_Load()

    '窗口完全加载之前将触发此事件。
    
    If MsgBox("单击“是”进入全屏(推荐)", vbQuestion Or vbYesNo, "选项") = vbYes Then Fullscreen_Enabled = True

End Sub

Private Sub Form_Unload(Cancel As Integer)

    Close_Program

End Sub

源码总结如下:透明度
一:在DX初始化模块中增加如下代码:

```vbnet
'设置属性以提高透明度。
    Direct3D_Device.SetRenderState D3DRS_ALPHAREF, 255
    Direct3D_Device.SetRenderState D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL

[SetRenderState] : Set the rendering state parameters of a single device.

Function prototype in VB6:

Sub SetRenderState(stateType As CONST_D3DRENDERSTATETYPE, value As Long)

Parameter 1: stateType: the state variable of the device being modified. This parameter can be any member of the D3DRENDERSTATETYPE enumeration type.

Parameter 2: value: the new value of the device rendering state to be set. The meaning of this parameter depends on the value specified for parameter 1: stateType. For example, if the parameter 1: stateType is D3DRS_SHADEMODE, the second parameter will be a member of the D3DSHADEMODE enumeration type.

D3DSHADEMODE: Define constants describing the supported shading modes. The C++ prototype code is as follows:

typedef enum D3DSHADEMODE { 
  D3DSHADE_FLAT         = 1,
  D3DSHADE_GOURAUD      = 2,
  D3DSHADE_PHONG        = 3,
  D3DSHADE_FORCE_DWORD  = 0x7fffffff
} D3DSHADEMODE, *LPD3DSHADEMODE;

D3DSHADE_FLAT: Flat shading mode. The color and specular reflection component of the first vertex in the triangle are used to determine the color and specular reflection component of the face. These colors remain constant throughout the triangle; that is, they will not be interpolated. The specular reflection Alpha is interpolated.
The first vertex of the triangle used in the plane shading mode is defined in the following way.
For the triangle list, the first vertex of triangle i is i * 3.
For triangle strips, the first vertex of triangle i is vertex i.
For a triangular fan, the first vertex of triangle i is vertex i+1.
The members of this enumerated type define the value of the D3DRS_SHADEMODE rendering state.

D3DSHADE_GOURAUD: Gouraud shading mode. The color of the face and the specular component are determined by linear interpolation between all three vertices of the triangle.

D3DSHADE_PHONG: Not supported.

D3DSHADE_FORCE_DWORD: Force this enumeration to be compiled into a 32-bit size. Without this value, some compilers will allow this enum to be compiled to sizes other than 32 bits. This value is not used.

Two: The following sentence is added to the module Game_Loop'game loop:

'就在这里将赋予多边形透明度
                Direct3D_Device.SetRenderState D3DRS_ALPHATESTENABLE, True

Guess you like

Origin blog.csdn.net/gosub60/article/details/111185892