Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
Hi, I am wondering how do I minimize a windowed DirectX application without getting an application error. Like in various tutorials, I captured the OnDeviceLost event, but what do I have to put inside it? The application displays a simple cube without textures and it uses 1 light. There is no rendering loop running. The rendering block is called only if needed (i.e. setting viewer position, setting light color etc.).
  • Leaders
Posted

If you don't post any code or tell us what the program is doing when the error occurs that really doesn't give us much to go on.

 

You need to look at the error details and where the error is thrown to get some idea of why it is being thrown. There may also be DirectX debugging utilities that can give you more info when an error occurs.

[sIGPIC]e[/sIGPIC]
Posted

okay, here it is. I´ve cut the code to the bone so that it still produces the error:

 

Public Class Form1
   Dim D3D As Direct3D.Device
   Dim PP As New PresentParameters()
   Dim vb As VertexBuffer = Nothing
   Dim Cam As New Vector3(0, 0, 10)
   Dim DeviceReady As Boolean = False

   Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

       Dim dm As DisplayMode = Manager.Adapters.Default.CurrentDisplayMode
       With PP
           .Windowed = True
           .SwapEffect = SwapEffect.Discard
           .DeviceWindow = Panel1
           .BackBufferFormat = Format.X8R8G8B8
           .BackBufferWidth = Panel1.Width
           .BackBufferHeight = Panel1.Height
           .EnableAutoDepthStencil = True
           .AutoDepthStencilFormat = DepthFormat.D16
       End With

       Try
           D3D = New Direct3D.Device(0, DeviceType.Hardware, Panel1, CreateFlags.HardwareVertexProcessing, PP)
           AddHandler D3D.DeviceLost, AddressOf Me.OnDeviceLost
           AddHandler D3D.DeviceReset, AddressOf Me.OnResetDevice
           AddHandler D3D.DeviceResizing, AddressOf Me.OnDeviceResizing
       Catch ex As Exception
           MsgBox("Error in creating device! " & ex.Message, MsgBoxStyle.Exclamation, "Some error!")
           Exit Sub
       End Try

       vb = New VertexBuffer(GetType(CustomVertex.PositionNormalColored), 36, D3D, Usage.Dynamic And Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Managed)
       vb.SetData(New Cube(2, 1, 0.5, Color.SkyBlue).GetVerts, 0, LockFlags.None)

       SetupLights()
       DeviceReady = True

       Panel1.Invalidate()
   End Sub

   Private Sub SetupLights()
       With D3D
           .Lights(0).Type = LightType.Point
           .Lights(0).Position = New Vector3(-2, 7, 3)
           .Lights(0).Ambient = System.Drawing.Color.White
           .Lights(0).Attenuation0 = 0.8F
           .Lights(0).Attenuation1 = 0.02F
           .Lights(0).Range = 10000.0F
           .Lights(0).Enabled = True
           With .PresentationParameters
               .BackBufferWidth = Panel1.Width
               .BackBufferHeight = Panel1.Height
           End With
       End With
   End Sub

   Private Sub OnDeviceLost()

   End Sub

   Private Sub OnResetDevice()
      
   End Sub

   Private Sub OnDeviceResizing()
       My.Application.DoEvents()
       SetupLights()
       Panel1.Invalidate()
   End Sub

   Private Sub Render()
       With D3D

           .Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.Black, 1, 0)

           .BeginScene()
           .Transform.View = Matrix.LookAtLH(cam, New Vector3(0, 0, 0), New Vector3(0, 1, 0))
           .Transform.Projection = Matrix.PerspectiveFovLH(0.8, Panel1.Width / Panel1.Height, 1, 1000)

           'drawing cube
           .SetStreamSource(0, vb, 0)
           .VertexFormat = CustomVertex.PositionNormalColored.Format
           .Transform.World = Matrix.Translation(New Vector3(0, 0, 0))
           .RenderState.Lighting = True
           .DrawPrimitives(PrimitiveType.TriangleList, 0, 12)

           .EndScene()

            My.Application.DoEvents()
            .Present()

       End With
   End Sub


   Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
       Render()
   End Sub

End Class

Public Class Cube
   Public X As Single
   Public Y As Single
   Public Z As Single
   Public [Color] As Color

   Public Sub New(ByVal Length As Single, ByVal Width As Single, ByVal Height As Single, ByVal CubeColor As Color)
       X = Length
       Y = Width
       Z = Height
       [Color] = CubeColor
   End Sub

   Public Function GetVerts() As CustomVertex.PositionNormalColored()
       'This gets the vertexes of the cube.  Each face is composed of 2 triangles.
       'Each triangle has 3 vertexes, so each face needs 6 vertexes; i.e. some vertexes
       'are written twice.  The order in which you build the vertexes determine which 
       'side gets filled. You plot vertexes counter-clockwise for sides facing the camera
       'and clockwise for ones facing "away".  

       'Also, in order for each side to be lit, you need to specify the 
       'normal for each vertex.  This is the same number for all vertexes on each side.  
       'Setting the normal improperly can give some interesting effects in lighting.

       Dim X2 As Single = X / 2
       Dim Y2 As Single = Y / 2
       Dim Z2 As Single = Z / 2

       Dim verts(35) As CustomVertex.PositionNormalColored
       'Front face.
       verts(0) = New CustomVertex.PositionNormalColored(New Vector3(-X2, Y2, Z2), New Vector3(0, 0, 1), [Color].ToArgb())
       verts(1) = New CustomVertex.PositionNormalColored(New Vector3(-X2, -Y2, Z2), New Vector3(0, 0, 1), [Color].ToArgb())
       verts(2) = New CustomVertex.PositionNormalColored(New Vector3(X2, Y2, Z2), New Vector3(0, 0, 1), [Color].ToArgb())
       verts(3) = New CustomVertex.PositionNormalColored(New Vector3(-X2, -Y2, Z2), New Vector3(0, 0, 1), [Color].ToArgb())
       verts(4) = New CustomVertex.PositionNormalColored(New Vector3(X2, -Y2, Z2), New Vector3(0, 0, 1), [Color].ToArgb())
       verts(5) = New CustomVertex.PositionNormalColored(New Vector3(X2, Y2, Z2), New Vector3(0, 0, 1), [Color].ToArgb())

       'Back face.  This is facing away from the camera, so vertices should be clockwise order.
       verts(6) = New CustomVertex.PositionNormalColored(New Vector3(-X2, Y2, -Z2), New Vector3(0, 0, -1), [Color].ToArgb())
       verts(7) = New CustomVertex.PositionNormalColored(New Vector3(X2, Y2, -Z2), New Vector3(0, 0, -1), [Color].ToArgb())
       verts(8) = New CustomVertex.PositionNormalColored(New Vector3(-X2, -Y2, -Z2), New Vector3(0, 0, -1), [Color].ToArgb())
       verts(9) = New CustomVertex.PositionNormalColored(New Vector3(-X2, -Y2, -Z2), New Vector3(0, 0, -1), [Color].ToArgb())
       verts(10) = New CustomVertex.PositionNormalColored(New Vector3(X2, Y2, -Z2), New Vector3(0, 0, -1), [Color].ToArgb())
       verts(11) = New CustomVertex.PositionNormalColored(New Vector3(X2, -Y2, -Z2), New Vector3(0, 0, -1), [Color].ToArgb())

       'Top face.
       verts(12) = New CustomVertex.PositionNormalColored(New Vector3(-X2, Y2, Z2), New Vector3(0, 1, 0), [Color].ToArgb())
       verts(13) = New CustomVertex.PositionNormalColored(New Vector3(X2, Y2, -Z2), New Vector3(0, 1, 0), [Color].ToArgb())
       verts(14) = New CustomVertex.PositionNormalColored(New Vector3(-X2, Y2, -Z2), New Vector3(0, 1, 0), [Color].ToArgb())
       verts(15) = New CustomVertex.PositionNormalColored(New Vector3(-X2, Y2, Z2), New Vector3(0, 1, 0), [Color].ToArgb())
       verts(16) = New CustomVertex.PositionNormalColored(New Vector3(X2, Y2, Z2), New Vector3(0, 1, 0), [Color].ToArgb())
       verts(17) = New CustomVertex.PositionNormalColored(New Vector3(X2, Y2, -Z2), New Vector3(0, 1, 0), [Color].ToArgb())

       'Bottom face. This is facing away from the camera, so vertices should be clockwise order.
       verts(18) = New CustomVertex.PositionNormalColored(New Vector3(-X2, -Y2, Z2), New Vector3(0, -1, 0), [Color].ToArgb())
       verts(19) = New CustomVertex.PositionNormalColored(New Vector3(-X2, -Y2, -Z2), New Vector3(0, -1, 0), [Color].ToArgb())
       verts(20) = New CustomVertex.PositionNormalColored(New Vector3(X2, -Y2, -Z2), New Vector3(0, -1, 0), [Color].ToArgb())
       verts(21) = New CustomVertex.PositionNormalColored(New Vector3(-X2, -Y2, Z2), New Vector3(0, -1, 0), [Color].ToArgb())
       verts(22) = New CustomVertex.PositionNormalColored(New Vector3(X2, -Y2, -Z2), New Vector3(0, -1, 0), [Color].ToArgb())
       verts(23) = New CustomVertex.PositionNormalColored(New Vector3(X2, -Y2, Z2), New Vector3(0, -1, 0), [Color].ToArgb())

       'Left face.
       verts(24) = New CustomVertex.PositionNormalColored(New Vector3(-X2, Y2, Z2), New Vector3(-1, 0, 0), [Color].ToArgb())
       verts(25) = New CustomVertex.PositionNormalColored(New Vector3(-X2, -Y2, -Z2), New Vector3(-1, 0, 0), [Color].ToArgb())
       verts(26) = New CustomVertex.PositionNormalColored(New Vector3(-X2, -Y2, Z2), New Vector3(-1, 0, 0), [Color].ToArgb())
       verts(27) = New CustomVertex.PositionNormalColored(New Vector3(-X2, Y2, -Z2), New Vector3(-1, 0, 0), [Color].ToArgb())
       verts(28) = New CustomVertex.PositionNormalColored(New Vector3(-X2, -Y2, -Z2), New Vector3(-1, 0, 0), [Color].ToArgb())
       verts(29) = New CustomVertex.PositionNormalColored(New Vector3(-X2, Y2, Z2), New Vector3(-1, 0, 0), [Color].ToArgb())

       'Right face. This is facing away from the camera, so vertices should be clockwise order.
       verts(30) = New CustomVertex.PositionNormalColored(New Vector3(X2, Y2, Z2), New Vector3(1, 0, 0), [Color].ToArgb())
       verts(31) = New CustomVertex.PositionNormalColored(New Vector3(X2, -Y2, Z2), New Vector3(1, 0, 0), [Color].ToArgb())
       verts(32) = New CustomVertex.PositionNormalColored(New Vector3(X2, -Y2, -Z2), New Vector3(1, 0, 0), [Color].ToArgb())
       verts(33) = New CustomVertex.PositionNormalColored(New Vector3(X2, Y2, -Z2), New Vector3(1, 0, 0), [Color].ToArgb())
       verts(34) = New CustomVertex.PositionNormalColored(New Vector3(X2, Y2, Z2), New Vector3(1, 0, 0), [Color].ToArgb())
       verts(35) = New CustomVertex.PositionNormalColored(New Vector3(X2, -Y2, -Z2), New Vector3(1, 0, 0), [Color].ToArgb())
       Return verts
   End Function
End Class

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...