本帖最后由 苍茫 于 2015-5-22 08:41 编辑
绘制了个三角形- '//******************** in modDX8 module
- Option Explicit
- Public g_DX8 As DirectX8
- Public g_D3D As Direct3D8
- Public g_D3DDevice As Direct3DDevice8
- Public g_VB As Direct3DVertexBuffer8
- Private Type CUSTOMVERTEX
- x As Single 'x in screen space.
- y As Single 'y in screen space.
- z As Single 'normalized z.
- rhw As Single 'normalized z rhw.
- color As Long 'vertex color.
- End Type
- Private Const D3DFVF_CUSTOMVERTEX = (D3DFVF_XYZRHW Or D3DFVF_DIFFUSE)
- Public Function InitDevice(hwnd As Long) As Boolean
- Set g_DX8 = New DirectX8
-
- 'to create a Direct3D object
- Set g_D3D = g_DX8.Direct3DCreate()
- If g_D3D Is Nothing Then InitDevice = False: Exit Function
-
- 'retrieve the current display mode
- Dim mode As D3DDISPLAYMODE
- g_D3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, mode
-
-
- 'filling in the fields of the D3DPRESENT_PARAMETERS object
- Dim d3dpp As D3DPRESENT_PARAMETERS
- With d3dpp
- .BackBufferFormat = mode.Format
- .SwapEffect = D3DSWAPEFFECT_COPY_VSYNC
- .Windowed = 1
- End With
-
- 'to use Direct3D8.CreateDevice to create the Direct3D device
- Set g_D3DDevice = g_D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, _
- D3DCREATE_SOFTWARE_VERTEXPROCESSING, d3dpp)
- If g_D3DDevice Is Nothing Then InitDevice = False: Exit Function
- End Function
- Public Sub DestroyDevice()
- Set g_D3DDevice = Nothing
- Set g_D3D = Nothing
- Set g_DX8 = Nothing
- Set g_VB = Nothing
- End Sub
- Public Sub RenderAndDisplay()
- 'to clear the viewport
- g_D3DDevice.Clear 0, ByVal 0, D3DCLEAR_TARGET, &HFF, 1#, 0
-
- 'begin the scene
- g_D3DDevice.BeginScene
-
- 'render the scene
- Call Render
- 'end the scene
- g_D3DDevice.EndScene
-
- 'display it
- g_D3DDevice.Present ByVal 0, ByVal 0, 0, ByVal 0
- End Sub
- Public Sub Render()
- 'fill three vertices with the points of a triangle
- Dim Vertices(2) As CUSTOMVERTEX
- With Vertices(0): .x = 150: .y = 50: .z = 0.5: .rhw = 1: .color = &HFFFF0000: End With
- With Vertices(1): .x = 250: .y = 250: .z = 0.5: .rhw = 1: .color = &HFF00FF00: End With
- With Vertices(2): .x = 50: .y = 250: .z = 0.5: .rhw = 1: .color = &HFF00FFFF: End With
-
- 'create a vertex buffer as shown
- Set g_VB = g_D3DDevice.CreateVertexBuffer(20 * 3, _
- 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT)
- If g_VB Is Nothing Then Exit Sub
-
- 'fill with data
- D3DVertexBuffer8SetData g_VB, 0, 20 * 3, 0, Vertices(0)
-
- 'render the scene
- g_D3DDevice.SetStreamSource 0, g_VB, 20
-
- g_D3DDevice.SetVertexShader D3DFVF_CUSTOMVERTEX
-
- g_D3DDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, 1
- End Sub
复制代码- '//****** in form1 module
- Option Explicit
- Private Sub Form_Load()
- Call InitDevice(Me.hwnd)
- Timer1.Interval = 40
- End Sub
- Private Sub Form_Unload(Cancel As Integer)
- Call DestroyDevice
- End Sub
- Private Sub Timer1_Timer()
- Call RenderAndDisplay
- End Sub
复制代码 怎么旋转角度呢 ?
下面这段代码 我不知道对不对,我是看MSDN, 也不清楚这段代码该插入到哪?
Dim matWorld As D3DMATRIX
D3DXMatrixRotationY matWorld, Timer * 4
g_D3DDevice.SetTransform D3DTS_WORLD, matWorld
|