Sign in to follow this  

DirectX 8.0 With VB.NET Impossible You Say?

This topic is 3589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Oh it's very possible. This code is very raw form. I just wanted a testbed for now a didn't want to download 200 plus megs over the phone line probably taking 20 hours or so,and then opening the file and getting a corrupted file message(which has happened before and kills). This will get you started and will load and display an x file. The code as is,is setup to load the famous ""tiny.x"" model. You can use any .x that you want but you might not see it unless you change the vectors. If you use the tiny model it will look as though you were lying on the ground and she is standing over you. This code is just to get you started,looking around the net I noticed that people were trying to get dx8 working with vb.net but could not so you will find a problem when you run the code. The resizing has to be worked on,if you resize your form the model will come at you. I'm not looking to optimize the code right now because I just need a testbed so rather than wait I thought I would put it up so others can use DirectX 8.0 with VB.NET. So here it is. /////////////// First Make A Com Reference to DxVBLibA(Direct X 8 for Visual Basic Type Library) ////////////// Then just use this code(Reminder>>In Very Raw Form)
Imports System.IO
Imports System.Math
Imports DxVBLibA
Imports DxVBLibA.CONST_D3DXMESH
Imports System.Runtime.InteropServices


Public Class Form1
    Inherits System.Windows.Forms.Form
    Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (ByVal Destination As D3DMATRIX, ByVal Length As Integer)


Public Structure VBNETX
        Dim numMaterials As Long
        Dim mesh As D3DXMesh
        Dim g_MeshMaterials() As D3DMATERIAL8
        Dim g_MeshTextures() As Direct3DTexture8
End Structure

'''''WINDOWS FORM DESIGNER GENERATED CODE

Dim dx As DirectX8
Dim d3d As Direct3D8
Dim d3dx As New D3DX8
Dim dispmode As D3DDISPLAYMODE
Dim d3dpp As D3DPRESENT_PARAMETERS
Dim d3ddev As Direct3DDevice8
Dim matWorld As D3DMATRIX
Dim matproj As D3DMATRIX
Dim matView As D3DMATRIX
Dim cmMesh As CONST_D3DXMESH

Dim tinyMesh As VBNETX

<DllImport("DX8VB.DLL", EntryPoint:="VB_D3DXMatrixIdentity", SetLastError:=True, CharSet:=System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
    Public Shared Sub D3DXMatrixIdentity(ByRef MOut As DxVBLibA.D3DMATRIX)

    End Sub
    <DllImport("DX8VB.DLL", EntryPoint:="VB_D3DXMatrixRotationX", SetLastError:=True, CharSet:=System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
    Public Shared Sub D3DXMatrixRotationX(ByRef MOut As DxVBLibA.D3DMATRIX, ByVal angle As Single)

    End Sub
    <DllImport("DX8VB.DLL", EntryPoint:="VB_D3DXMatrixRotationY", SetLastError:=True, CharSet:=System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
    Public Shared Sub D3DXMatrixRotationY(ByRef MOut As DxVBLibA.D3DMATRIX, ByVal angle As Single)

    End Sub
    <DllImport("DX8VB.DLL", EntryPoint:="VB_D3DXMatrixRotationZ", SetLastError:=True, CharSet:=System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
    Public Shared Sub D3DXMatrixRotationZ(ByRef MOut As DxVBLibA.D3DMATRIX, ByVal angle As Single)

    End Sub
    <DllImport("DX8VB.DLL", EntryPoint:="VB_D3DXMatrixMultiply", SetLastError:=True, CharSet:=System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
    Public Shared Sub D3DXMatrixMultiply(ByRef MOut As DxVBLibA.D3DMATRIX, ByRef M1 As DxVBLibA.D3DMATRIX, ByRef M2 As DxVBLibA.D3DMATRIX)

    End Sub
    <DllImport("DX8VB.DLL", EntryPoint:="VB_D3DXMatrixTranslation", SetLastError:=True, CharSet:=System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
    Public Shared Sub D3DXMatrixTranslation(ByRef MOut As DxVBLibA.D3DMATRIX, ByVal x As Single, ByVal y As Single, ByVal z As Single)

    End Sub
    <DllImport("DX8VB.DLL", EntryPoint:="VB_D3DXMatrixLookAtLH", SetLastError:=True, CharSet:=System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
    Public Shared Sub D3DXMatrixLookAtLH(ByRef MOut As DxVBLibA.D3DMATRIX, ByRef VEye As DxVBLibA.D3DVECTOR, ByRef VAt As DxVBLibA.D3DVECTOR, ByRef VUp As DxVBLibA.D3DVECTOR)

    End Sub
    <DllImport("DX8VB.DLL", EntryPoint:="VB_D3DVertexBuffer8SetData", SetLastError:=True, CharSet:=System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
    Public Shared Sub D3DVertexBuffer8SetData(ByRef IBuffer As DxVBLibA.Direct3DVertexBuffer8, ByVal Offset As Long, ByVal Size As Long, ByVal Flags As Long, ByVal Data As LITVERTEX)

    End Sub
    <DllImport("DX8VB.DLL", EntryPoint:="VB_D3DIndexBuffer8SetData", SetLastError:=True, CharSet:=System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
       Public Shared Sub D3DIndexBuffer8SetData(ByRef IBuffer As DxVBLibA.Direct3DIndexBuffer8, ByVal Offset As Long, ByVal Size As Long, ByVal Flags As Long, ByVal Data As Short)

    End Sub
    <DllImport("DX8VB.DLL", EntryPoint:="VB_D3DXMatrixScaling", SetLastError:=True, CharSet:=System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
       Public Shared Sub D3DXMatrixScaling(ByRef MOut As DxVBLibA.D3DMATRIX, ByVal x As Single, ByVal y As Single, ByVal z As Single)

    End Sub
    <DllImport("DX8VB.DLL", EntryPoint:="VB_D3DXMatrixPerspectiveFovLH", SetLastError:=True, CharSet:=System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
          Public Shared Sub D3DXMatrixPerspectiveFovLH(ByRef MOut As DxVBLibA.D3DMATRIX, ByVal fovy As Single, ByVal aspect As Single, ByVal zn As Single, ByVal zf As Single)

    End Sub

    Private Function MakeVector(ByRef X As Single, ByRef Y As Single, ByRef Z As Single) As DxVBLibA.D3DVECTOR
        MakeVector.x = X : MakeVector.y = Y : MakeVector.z = Z
    End Function

 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load


        InitDirect3D()
        
        defaultSetting()
        '''''''////////LOAD MESHES//////
        cube = loadMesh(Application.StartupPath & "\cube.x", True, True, Application.StartupPath)
        spaceball = loadMesh(Application.StartupPath & "\SpaceSphere.x", True, True, Application.StartupPath)
        whiteship = loadMesh(Application.StartupPath & "\WhiteShip.x", True, True, Application.StartupPath)
        tinyMesh = loadMesh(Application.StartupPath & "\tiny.x", True, True, Application.StartupPath)
        teapot = loadMesh(Application.StartupPath & "\teapot.x", True, True, Application.StartupPath)
        
        Timer1.Enabled = True
        Timer1.Interval = 100
        Timer1.Start()

    End Sub

 Private Sub InitDirect3D()
        

        'create the DirectX object
        dx = New DirectX8

        'create the Direct3D object
        d3d = dx.Direct3DCreate()
        If d3d Is Nothing Then
            MsgBox("Error initializing Direct3D!")
            Shutdown()
        End If
       

        d3d.GetAdapterDisplayMode(CONST_D3DCONST.D3DADAPTER_DEFAULT, dispmode)
       
        d3dpp.BackBufferWidth = Me.Width
        d3dpp.BackBufferHeight = Me.Height
        d3dpp.BackBufferFormat = dispmode.Format
        d3dpp.BackBufferCount = 1
        d3dpp.MultiSampleType = CONST_D3DMULTISAMPLE_TYPE.D3DMULTISAMPLE_NONE
        d3dpp.SwapEffect = CONST_D3DSWAPEFFECT.D3DSWAPEFFECT_DISCARD
        d3dpp.hDeviceWindow = Me.Handle.ToInt32
        d3dpp.Windowed = 1
        d3dpp.EnableAutoDepthStencil = 1
        d3dpp.AutoDepthStencilFormat = CONST_D3DFORMAT.D3DFMT_D16
        'd3dpp.flags = 0
        'd3dpp.FullScreen_RefreshRateInHz = CONST_D3DCONST.D3DPRESENT_RATE_DEFAULT
        'd3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;




        d3d.CheckDeviceFormat(CONST_D3DCONST.D3DADAPTER_DEFAULT, CONST_D3DDEVTYPE.D3DDEVTYPE_HAL, d3dpp.BackBufferFormat, CONST_D3DUSAGEFLAGS.D3DUSAGE_DEPTHSTENCIL, CONST_D3DRESOURCETYPE.D3DRTYPE_SURFACE, d3dpp.AutoDepthStencilFormat)





        d3ddev = d3d.CreateDevice(CONST_D3DCONST.D3DADAPTER_DEFAULT, CONST_D3DDEVTYPE.D3DDEVTYPE_HAL, Me.Handle.ToInt32, CONST_D3DCREATEFLAGS.D3DCREATE_SOFTWARE_VERTEXPROCESSING, d3dpp)
        'd3ddev.SetVertexShader(FVF_LVERTEX)
        d3ddev.SetRenderState(DxVBLibA.CONST_D3DRENDERSTATETYPE.D3DRS_LIGHTING, 0) '//Disable lighting.
        d3ddev.SetRenderState(DxVBLibA.CONST_D3DRENDERSTATETYPE.D3DRS_ZENABLE, 1)
        d3ddev.SetRenderState(DxVBLibA.CONST_D3DRENDERSTATETYPE.D3DRS_CULLMODE, DxVBLibA.CONST_D3DCULL.D3DCULL_NONE)
        d3ddev.SetRenderState(DxVBLibA.CONST_D3DRENDERSTATETYPE.D3DRS_FILLMODE, DxVBLibA.CONST_D3DFILLMODE.D3DFILL_SOLID)
        D3DXMatrixIdentity(matWorld)
        d3ddev.SetTransform(CONST_D3DTRANSFORMSTATETYPE.D3DTS_WORLD, matWorld)

        D3DXMatrixLookAtLH(matView, MakeVector(0, 0, -300), MakeVector(0, 0, 0), MakeVector(0, 1, 0))
        d3ddev.SetTransform(DxVBLibA.CONST_D3DTRANSFORMSTATETYPE.D3DTS_VIEW, matView)


        D3DXMatrixPerspectiveFovLH(matproj, 45.0F, Me.Width / Me.Height, 1.0F, 2000.0F)
        d3ddev.SetTransform(CONST_D3DTRANSFORMSTATETYPE.D3DTS_PROJECTION, matproj)

        If d3ddev Is Nothing Then
            MsgBox("Error creating the Direct3D device!")
            Shutdown()
        End If

    End Sub

 Public Function loadMesh(ByVal filesrc As String, Optional ByVal texture As Boolean = False, Optional ByVal materiala As Boolean = False, Optional ByRef directory As String = Nothing) As VBNETX
        Dim mtrlBuffer As D3DXBuffer
       
        Dim strTexName As String
        loadMesh.mesh = d3dx.LoadMeshFromX(filesrc, cmMesh.D3DXMESH_MANAGED, d3ddev, Nothing, mtrlBuffer, loadMesh.numMaterials)

        ReDim loadMesh.g_MeshMaterials(loadMesh.numMaterials)
        ReDim loadMesh.g_MeshTextures(loadMesh.numMaterials)

        For i = 0 To loadMesh.numMaterials - 1
            If materiala Then
                d3dx.BufferGetMaterial(mtrlBuffer, i, loadMesh.g_MeshMaterials(i))
                loadMesh.g_MeshMaterials(i).ambient = loadMesh.g_MeshMaterials(i).diffuse
            End If

            If texture Then
                strTexName = d3dx.BufferGetTextureName(mtrlBuffer, i)

                
                If strTexName <> "" Then
                    loadMesh.g_MeshTextures(i) = d3dx.CreateTextureFromFile(d3ddev, directory & "\" & strTexName)
                End If
            End If
        Next
        mtrlBuffer = Nothing

    End Function

 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
      

        d3ddev.Clear(0, System.IntPtr.Zero, CONST_D3DCLEARFLAGS.D3DCLEAR_TARGET Or CONST_D3DCLEARFLAGS.D3DCLEAR_ZBUFFER, System.Drawing.Color.Blue.ToArgb, 1, 0)

        d3ddev.BeginScene()
       
        ' Dim dx4 As Integer
        ' For dx4 = 0 To cube.numMaterials - 1
        ' d3ddev.SetTexture(0, cube.g_MeshTextures(dx4))
        ' d3ddev.SetMaterial(cube.g_MeshMaterials(dx4))
        ' cube.mesh.DrawSubset(dx4)
        ' Next dx4
        'Dim dx3 As Integer
        'For dx3 = 0 To whiteship.numMaterials - 1
        'd3ddev.SetTexture(0, whiteship.g_MeshTextures(dx3))
        'd3ddev.SetMaterial(whiteship.g_MeshMaterials(dx3))
        'whiteship.mesh.DrawSubset(dx3)
        'Next dx3
        'Dim dx2 As Integer
        'For dx2 = 0 To whiteship.numMaterials - 1
        'd3ddev.SetTexture(0, whiteship.g_MeshTextures(dx2))
        'd3ddev.SetMaterial(whiteship.g_MeshMaterials(dx2))
        'whiteship.mesh.DrawSubset(dx2)
        'Next dx2
        Dim dx As Integer
        For dx = 0 To tinyMesh.numMaterials - 1
            d3ddev.SetTexture(0, tinyMesh.g_MeshTextures(dx))
            d3ddev.SetMaterial(tinyMesh.g_MeshMaterials(dx))
            tinyMesh.mesh.DrawSubset(dx)
        Next dx
        'Dim dx1 As Integer
        'For dx1 = 0 To teapot.numMaterials - 1
        'd3ddev.SetTexture(0, teapot.g_MeshTextures(dx1))
        'd3ddev.SetMaterial(teapot.g_MeshMaterials(dx1))
        'teapot.mesh.DrawSubset(dx1)
        'Next dx1

        d3ddev.EndScene()
        d3ddev.Present(System.IntPtr.Zero, System.IntPtr.Zero, 0, System.IntPtr.Zero)

    End Sub

  Public Sub resetDevice()
        Dim w, h As Integer
        w = Me.Width
        h = Me.Height
        If h = 0 Then
            h = 1
        End If
        d3dpp.BackBufferHeight = 0
        d3dpp.BackBufferWidth = 0
        d3ddev.Reset(d3dpp)
    End Sub
    Public Sub defaultSetting()


        d3ddev.SetRenderState(DxVBLibA.CONST_D3DRENDERSTATETYPE.D3DRS_LIGHTING, 0) '//Disable lighting.
        d3ddev.SetRenderState(DxVBLibA.CONST_D3DRENDERSTATETYPE.D3DRS_ZENABLE, 1)
        d3ddev.SetRenderState(DxVBLibA.CONST_D3DRENDERSTATETYPE.D3DRS_CULLMODE, DxVBLibA.CONST_D3DCULL.D3DCULL_NONE)
        D3DXMatrixIdentity(matWorld)
        d3ddev.SetTransform(CONST_D3DTRANSFORMSTATETYPE.D3DTS_WORLD, matWorld)

        D3DXMatrixPerspectiveFovLH(matproj, 45.0F, Me.Width / Me.Height, 1.0F, 2000.0F)
        d3ddev.SetTransform(CONST_D3DTRANSFORMSTATETYPE.D3DTS_PROJECTION, matproj)


    End Sub
    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        If Not (d3ddev Is Nothing Or Me.WindowState = FormWindowState.Minimized) Then
            resetDevice()
            defaultSetting()
        End If
    End Sub
End Class



Have a good day. Steve [Edited by - Steve5050 on February 13, 2008 10:15:27 AM]

Share this post


Link to post
Share on other sites

This topic is 3589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this