Direct3D Initialization

I am trying to initialize Direct3D using this code:

Imports microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Public Class Form1
    Private dev As Microsoft.DirectX.Direct3D.Device = Nothing

    Private Function Init3d() As Boolean
        Try
            Dim pparams As New PresentParameters
            pparams.Windowed = True
            pparams.SwapEffect = SwapEffect.Discard
            dev = New Microsoft.DirectX.Direct3D.Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, pparams)
            Return True
        Catch ex As DirectXException
            Return False
        End Try

    End Function
    Private Sub render()
        dev.Clear(ClearFlags.Target, Color.Blue, 1.0F, 0)
        dev.BeginScene()
        dev.EndScene()
        dev.Present()
    End Sub
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Me.render()
    End Sub
    Sub main()
        If Init3d() = False Then MsgBox("Couldn't init.")

    End Sub

End Class
 

and this tutorial when I debug/run I get this error:

dev.Clear(ClearFlags.Target, Color.Blue, 1.0F, 0)
NullReferenceException was unhandled.
Object Reference not set to an instance of an object.
 

All help is greatly appreciated


Answer this question

Direct3D Initialization

  • Lauriemoore

    Hey, interessting page :) !!

    But still not running, also I downloaded SDK, these samples still not running!!! It fails while creating device... although compiled samples from microsoft SDK are running well... why codes from same SDK won't run

  • abc_acb

    As the error says - dev is 'nothing'.

    The problem could be a couple of things.

    1. Your render code is getting called before the device is set up becuase of the way you have things ordered. Wrapp the code in Paint with

    if Not dev is Nothing

    then render will not be called until the device is properly set up

    2. Put some breakpoints in your Main routine and make sure its getting called at all.



  • Cool_DBA

    I am also trying some manuals from net, I tried also this example repaired as you told, and as I think its right. I have the same problem with sample from http://www.k2wrpg.org/wiki/index.php Direct3DVbDotNet
    still it cannot create device, I tried to change some parameters, nothing matters.

    [code]
    Imports microsoft.DirectX
    Imports Microsoft.DirectX.Direct3D
    Public Class Form1
    Private dev As Microsoft.DirectX.Direct3D.Device = Nothing

    Private Function Init3d() As Boolean
    Try
    Dim pparams As New PresentParameters
    pparams.Windowed = True
    pparams.SwapEffect = SwapEffect.Discard
    dev = New Microsoft.DirectX.Direct3D.Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, pparams)
    Return True
    Catch ex As DirectXException
    Return False
    End Try
    End Function

    Private Sub render()
    dev.Clear(ClearFlags.Target, Color.Blue, 1.0F, 0)
    dev.BeginScene()
    dev.EndScene()
    dev.Present()
    End Sub

    Sub main()
    If Init3d() = False Then
    MsgBox("Couldn't init.")
    else
    Me.render()
    End If
    End Sub

    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
    Call main()
    End Sub
    End Class
    [/code]

  • David Parslow

    Check out the SDK tutorials in VB.Net and start with those http://www.thezbuffer.com/articles/315.aspx

  • Direct3D Initialization