Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello, I has tries a C++ Tutorial in VB.Net to rewrite, but I see only a blue window.

 

Can someone say to me, what wrong I make?

 

In the attachment is the original cpp-file of the sample and the complete VB.Net project.

 

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D



Namespace Direct3DTest2
   Public Class MyGraphicsSample
       Inherits GraphicsSample 'Alles von Class GraphicsSample erben

       Public AnzahlObjekte As Integer = 2048
       Public vbObjekte As VertexBuffer
       Public ibObjekte As IndexBuffer
       Public vCameraPos As Vector3 = New Vector3(0, 0, 0)
       Public CameraWinkel As Single = 0
       Public Sichtfeld As Single = Math.PI / 4

       Public Sub New()
       End Sub 'New

       Protected Overrides Sub FrameMove()

       End Sub 'FrameMove

       Public Sub RenderScene()
           Device.Transform.View = Matrix.LookAtLH(vCameraPos, New Vector3(vCameraPos.X + Math.Sin(CameraWinkel), vCameraPos.Y, vCameraPos.Z + Math.Cos(CameraWinkel)), New Vector3(0, 1, 0))
           Me.Text = vCameraPos.X & "x" & vCameraPos.Y & "x" & vCameraPos.Z & "Winkel:" & 360 * CameraWinkel / Math.PI
           Dim Aspect As Single
           Aspect = Device.PresentationParameters.BackBufferWidth / Device.PresentationParameters.BackBufferHeight
           Device.Transform.Projection = Matrix.PerspectiveFovLH(CameraWinkel, Aspect, 0.1, 250)
           Device.Transform.World = Matrix.Identity



           Device.SetStreamSource(0, vbObjekte, 0)
           Device.Indices = ibObjekte
           Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, AnzahlObjekte * 8, 0, AnzahlObjekte * 12)
       End Sub 'RenderScene

       Protected Overrides Sub Render()
           Device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Drawing.Color.Blue.ToArgb, 1, 0)
           Device.BeginScene()
           RenderScene()
           Device.EndScene()
       End Sub 'Render

       Protected Overrides Sub InitializeDeviceObjects()
           Try
               Device.VertexFormat = VertexFormats.Position Or VertexFormats.Diffuse Or VertexFormats.Texture1

               Device.RenderState.Lighting = False
               Device.RenderState.CullMode = Cull.None
               Device.RenderState.DitherEnable = True

               Device.SamplerState(0).MinFilter = TextureFilter.Linear
               Device.SamplerState(0).MagFilter = TextureFilter.Linear
               Device.SamplerState(0).MipFilter = TextureFilter.Linear

               Dim tmpTextur As Texture
               tmpTextur = TextureLoader.FromFile(Device, "Texture.bmp", 256, 256, 1, 0, Format.Unknown, Pool.Managed, Filter.None, Filter.Linear, 0)
               Device.SetTexture(0, tmpTextur)

               vbObjekte = New VertexBuffer(GetType(CustomVertex.PositionColoredTextured), AnzahlObjekte * 8, Device, 0, CustomVertex.PositionColoredTextured.Format, Pool.Managed)
               ibObjekte = New IndexBuffer(GetType(Short), AnzahlObjekte * 36, Device, 0, Pool.Managed)

               Dim Verts() As CustomVertex.PositionColoredTextured
               Dim Indices() As Short

               Verts = DirectCast(vbObjekte.Lock(0, LockFlags.NoSystemLock), CustomVertex.PositionColoredTextured())
               Indices = DirectCast(ibObjekte.Lock(0, LockFlags.NoSystemLock), Short())

               Dim n, m As Integer
               Dim tmpPos As New Vector3
               Dim StartVertex, StartIndex As Integer
               For n = 0 To AnzahlObjekte - 1
                   tmpPos.X = 250 * Rnd() + 20
                   tmpPos.Y = 250 * Rnd() + 20
                   tmpPos.Z = 250 * Rnd() + 20

                   StartVertex = n * 8
                   StartIndex = n * 36

                   Verts(StartVertex + 0) = New CustomVertex.PositionColoredTextured(tmpPos.X + -1, tmpPos.Y + 1, tmpPos.Z + -1, 0, Rnd, Rnd)
                   Verts(StartVertex + 1) = New CustomVertex.PositionColoredTextured(tmpPos.X + -1, tmpPos.Y + 1, tmpPos.Z + 1, 0, Rnd, Rnd)
                   Verts(StartVertex + 2) = New CustomVertex.PositionColoredTextured(tmpPos.X + 1, tmpPos.Y + 1, tmpPos.Z + 1, 0, Rnd, Rnd)
                   Verts(StartVertex + 3) = New CustomVertex.PositionColoredTextured(tmpPos.X + 1, tmpPos.Y + 1, tmpPos.Z + -1, 0, Rnd, Rnd)
                   Verts(StartVertex + 4) = New CustomVertex.PositionColoredTextured(tmpPos.X + -1, tmpPos.Y + -1, tmpPos.Z + -1, 0, Rnd, Rnd)
                   Verts(StartVertex + 5) = New CustomVertex.PositionColoredTextured(tmpPos.X + -1, tmpPos.Y + -1, tmpPos.Z + 1, 0, Rnd, Rnd)
                   Verts(StartVertex + 6) = New CustomVertex.PositionColoredTextured(tmpPos.X + 1, tmpPos.Y + -1, tmpPos.Z + 1, 0, Rnd, Rnd)
                   Verts(StartVertex + 7) = New CustomVertex.PositionColoredTextured(tmpPos.X + 1, tmpPos.Y + -1, tmpPos.Z + -1, 0, Rnd, Rnd)

                   Indices(StartIndex + 0) = 0 : Indices(StartIndex + 1) = 3 : Indices(StartIndex + 2) = 7
                   Indices(StartIndex + 3) = 0 : Indices(StartIndex + 4) = 7 : Indices(StartIndex + 5) = 4
                   Indices(StartIndex + 6) = 2 : Indices(StartIndex + 7) = 5 : Indices(StartIndex + 8) = 1
                   Indices(StartIndex + 9) = 2 : Indices(StartIndex + 10) = 5 : Indices(StartIndex + 11) = 6
                   Indices(StartIndex + 12) = 1 : Indices(StartIndex + 13) = 0 : Indices(StartIndex + 14) = 4
                   Indices(StartIndex + 15) = 1 : Indices(StartIndex + 16) = 4 : Indices(StartIndex + 17) = 5
                   Indices(StartIndex + 18) = 3 : Indices(StartIndex + 19) = 2 : Indices(StartIndex + 20) = 6
                   Indices(StartIndex + 21) = 3 : Indices(StartIndex + 22) = 6 : Indices(StartIndex + 23) = 7
                   Indices(StartIndex + 24) = 0 : Indices(StartIndex + 25) = 1 : Indices(StartIndex + 26) = 2
                   Indices(StartIndex + 27) = 0 : Indices(StartIndex + 28) = 2 : Indices(StartIndex + 29) = 3
                   Indices(StartIndex + 30) = 6 : Indices(StartIndex + 31) = 5 : Indices(StartIndex + 32) = 4
                   Indices(StartIndex + 33) = 6 : Indices(StartIndex + 34) = 4 : Indices(StartIndex + 35) = 7

               Next n

               vbObjekte.Unlock()
               ibObjekte.Unlock()

           Catch
               Dim e As New MediaNotFoundException
               HandleSampleException(e, ApplicationMessage.ApplicationMustExit)
               Throw e
           End Try
       End Sub 'InitializeDeviceObjects

       Protected Overrides Sub RestoreDeviceObjects(ByVal sender As System.Object, ByVal e As System.EventArgs)
       End Sub 'RestoreDeviceObjects

       Shared Sub Main()
           Dim d3dApp As New MyGraphicsSample

           If d3dApp.CreateGraphicsSample() Then
               d3dApp.Run()
           End If
       End Sub 'Main

       Private Sub MyGraphicsSample_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
           Select Case e.KeyCode
               Case Keys.Left
                   CameraWinkel -= elapsedtime * Math.PI / 8
               Case Keys.Right
                   CameraWinkel += elapsedtime * Math.PI / 8
               Case Keys.Up
                   vCameraPos.X += Math.Sin(CameraWinkel) * 5 * elapsedtime
                   vCameraPos.Z += Math.Cos(CameraWinkel) * 5 * elapsedtime
               Case Keys.Down
                   vCameraPos.X -= Math.Sin(CameraWinkel) * 5 * elapsedtime
                   vCameraPos.Z -= Math.Cos(CameraWinkel) * 5 * elapsedtime
               Case Keys.PageDown
                   Sichtfeld += Math.PI * 10 / 360 * elapsedtime
               Case Keys.PageUp
                   Sichtfeld -= Math.PI * 10 / 360 * elapsedtime
           End Select
           If Sichtfeld <= 0 Then Sichtfeld = Math.PI * 0.1 / 360
           If Sichtfeld >= Math.PI / 2 Then Sichtfeld = Math.PI * 179.9 / 360
       End Sub
   End Class 'MyGraphicsSample
End Namespace 'Direct3DTest2

direct3dtest2_kap_02_5_buffer_new.zip

Posted

In Render(), after Device.EndScene you need Device.Present().

What's happening is that the screen is indeed being cleared, but the output produced by DrawIndexedPrimitives is not being put onto the screen.

Posted

The Device.Present() is in 'Sub RenderEnvironment()

 

That cannot be the error, because otherwise a blue window would also not be indicated.

 

   Public Sub Render3DEnvironment()
       If deviceLost Then
           Try
               ' Test the cooperative level to see if it's okay to render
               device.TestCooperativeLevel()
           Catch e As DeviceLostException
               ' If the device was lost, do not render until we get it back
               isHandlingSizeChanges = False
               isWindowActive = False
               Return
           Catch e As DeviceNotResetException
               ' Check if the device needs to be resized.
               ' If we are windowed, read the desktop mode and use the same format for
               ' the back buffer
               If windowed Then
                   Dim adapterInfo As GraphicsAdapterInfo = graphicsSettings.AdapterInfo
                   graphicsSettings.WindowedDisplayMode = Manager.Adapters(adapterInfo.AdapterOrdinal).CurrentDisplayMode
                   presentParams.BackBufferFormat = graphicsSettings.WindowedDisplayMode.Format
               End If

               ' Reset the device and resize it
               device.Reset(device.PresentationParameters)
               EnvironmentResized(device, New System.ComponentModel.CancelEventArgs())
           End Try
           deviceLost = False
       End If

       ' Get the app's time, in seconds. Skip rendering if no time elapsed
       Dim fAppTime As Single = DXUtil.Timer(DirectXTimer.GetApplicationTime)
       Dim fElapsedAppTime As Single = DXUtil.Timer(DirectXTimer.GetElapsedTime)
       If 0.0F = fElapsedAppTime And frameMoving Then
           Return
       End If
       ' FrameMove (animate) the scene
       If frameMoving Or isSingleStep Then
           ' Store the time for the app
               appTime = fAppTime
               elapsedTime = fElapsedAppTime
           ' Frame move the scene
           FrameMove()

           isSingleStep = False
       End If

       ' Render the scene as normal
       Render()

       UpdateStats()

       Try
           ' Show the frame on the primary surface.
           device.Present()
       Catch e As DeviceLostException
           deviceLost = True
       End Try
   End Sub 'Render3DEnvironment

Posted

At a quick glance ....

 

you have choosen vertex type of

 

PositionColoredTextured

 

That means every vertex has an x,y and z postion in 3d space and little subtriangles make the object up from that.

 

Also the vertex has a color, which I haven't seen being set anywhere ( only a quick glance ).

 

Also and most important is that a texture is mapped onto the object using UV.

 

UV work like x and y, a total area of a texture is 1.0f ( 1 in a float value ) so if you use 50.0f in both U and V the texture would tile 50 times.

 

as your no using color, maybe use

 

PositionTextured

 

The big missing peice is when you render the scene you are not setting the render state to be using your texture that is referenced in the Vertex objects before drawing your primate object. You set this in the initalize objects, but it's not really the right place for it.

 

 

in C# it would look something like this

 

device.SetTexture(0,texture);
		device.TextureState[0].ColorOperation = TextureOperation.Modulate;
		device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;
		device.TextureState[0].ColorArgument2 = TextureArgument.Diffuse;
		device.TextureState[0].AlphaOperation = TextureOperation.Disable;

remember that directx is a state machine, so you set and change the render state as you draw stuff.

 

also when using the z axis you should turn on the z buffer in Initalization

 

device.RenderState.ZBufferEnable = true;

 

I don't know if this is a VB / C# difference, but when you create a vertex buffer for the primate object you usual have an event handler that is called when the device has finished creating the vertex buffer, after which time you then fill the vertex information.

 

hope this helps.

 

:)

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...