Sign in to follow this  
chosendl

Lighting distorts my vertex

Recommended Posts

Hi there i am having a strange problem. Whenever i try and apply lighting to scene, it completely disrupts my triangle. (it looks like a leaf that was eaten by a caterpillar!) Does anybody know whats wrong?
Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim Device As Microsoft.DirectX.Direct3D.Device
    Dim Disp As DisplayMode = Microsoft.DirectX.Direct3D.Manager.Adapters(0).CurrentDisplayMode
    Dim mcaps = Manager.GetDeviceCaps(0, DeviceType.Hardware)

#Region " Windows Form Designer generated code "
#End Region

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

        InitializeGraphics(Me, Disp, True)

        Me.Show()
        Dim myvertb = New Direct3D.VertexBuffer(GetType(CustomVertex.PositionNormal), 3, Device, Usage.WriteOnly, CustomVertex.PositionNormal.Format, Pool.Managed)

        Device.Transform.World = Matrix.Identity
        Device.Transform.View = Matrix.Identity
        Device.Transform.Projection = Matrix.PerspectiveFovLH(Math.PI / 3.0, 1.0, 0.1, 1000.0)
        Device.RenderState.CullMode = Cull.None
        Device.RenderState.ZBufferEnable = True
        Dim mtrl As Direct3D.Material = New Direct3D.Material()
        mtrl.Ambient = System.Drawing.Color.White
        mtrl.Diffuse = System.Drawing.Color.White
        Device.Material = mtrl
        Device.Lights(0).Type = LightType.Directional
        Device.Lights(0).Diffuse = System.Drawing.Color.DarkTurquoise
        Device.Lights(0).Direction = New Vector3(-5, -5, 5)
        Device.RenderState.Lighting = False
        Device.RenderState.Ambient = System.Drawing.Color.FromArgb(0, 20, 20, 20)

        Dim verts(3) As CustomVertex.PositionNormal
        verts(0) = New CustomVertex.PositionNormal()
        verts(0).X = -10
        verts(0).Y = 0
        verts(0).Z = 10
        verts(1) = New CustomVertex.PositionNormal()
        verts(1).X = 0
        verts(1).Y = -10
        verts(1).Z = 10
        verts(2) = New CustomVertex.PositionNormal()
        verts(2).X = 5
        verts(2).Y = 5
        verts(2).Z = 200

        myvertb.setdata(verts, 0, Direct3D.LockFlags.None)

        Device.Clear(ClearFlags.Target, Color.Blue, 1, 0)
        Device.BeginScene()
        Device.SetStreamSource(0, myvertb, 0)
        Device.VertexFormat = CustomVertex.PositionNormal.Format
        Device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1)

        Device.EndScene()
        Device.Present()
        Application.DoEvents()

    End Sub

    Private Function InitializeGraphics(ByVal frm As Control, ByVal Disp As DisplayMode, ByVal Windowed As Boolean) As Boolean
        Dim PParams As PresentParameters = New PresentParameters()
        PParams.Windowed = False
        PParams.SwapEffect = SwapEffect.Discard
        PParams.AutoDepthStencilFormat = DepthFormat.D16
        PParams.BackBufferCount = 1
        PParams.BackBufferFormat = Disp.Format
        PParams.BackBufferHeight = 768
        PParams.BackBufferWidth = 1024
        PParams.MultiSample = MultiSampleType.None
        PParams.MultiSampleQuality = 0
        PParams.EnableAutoDepthStencil = True
        PParams.PresentFlag = PresentFlag.DiscardDepthStencil
        PParams.FullScreenRefreshRateInHz = Disp.RefreshRate
        PParams.PresentationInterval = PresentInterval.Immediate

        REM THIS ERRORS NO MORE
        Device = New Device(0, DeviceType.Hardware, Me.Handle, CreateFlags.HardwareVertexProcessing, PParams)

sgBox("yebo")
    End Function
End Class
EDIT: Please remember to use 'source' tags. [Edited by - jollyjeffers on October 2, 2006 7:17:14 AM]

Share this post


Link to post
Share on other sites
Alright, so I still cant get it right. I have tried looking for an answer all over the net!
Here is the latest code i have tried and a picture of what the problem looks like.

The lighting appears to be working, the triangle is yellowish, but its still scattered and dissolved, what could be wrong?




Imports Microsoft.directx
Imports Microsoft.directx.Direct3D

Public Class Form1
Inherits System.Windows.Forms.Form

Dim Device As Microsoft.DirectX.Direct3D.Device
Dim Disp As DisplayMode = Microsoft.DirectX.Direct3D.Manager.Adapters(0).CurrentDisplayMode
Dim mcaps = Manager.GetDeviceCaps(0, DeviceType.Hardware)

#Region " Windows Form Designer generated code "
#End Region

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

InitializeGraphics()

Me.Show()
Dim myvertb = New Direct3D.VertexBuffer(GetType(CustomVertex.PositionNormal), 3, Device, Usage.WriteOnly, CustomVertex.PositionNormal.Format, Pool.Managed)

Device.Transform.World = Matrix.Identity
Device.Transform.View = Matrix.Identity
Device.Transform.Projection = Matrix.PerspectiveFovLH(Math.PI / 3.0, 1.0, 1, 1000)
Device.RenderState.CullMode = Cull.None
Device.RenderState.ZBufferEnable = True
Dim mtrl As Direct3D.Material = New Direct3D.Material()
mtrl.Ambient = System.Drawing.Color.FromArgb(0, 250, 250, 250)
Device.Material = mtrl
Device.RenderState.Lighting = True
Device.RenderState.Ambient = System.Drawing.Color.FromArgb(0, 250, 250, 20)

Dim verts(3) As CustomVertex.PositionNormal
verts(0) = New CustomVertex.PositionNormal()
verts(0).X = -5
verts(0).Y = 0
verts(0).Z = 6
verts(1) = New CustomVertex.PositionNormal()
verts(1).X = 0
verts(1).Y = -5
verts(1).Z = 6
verts(2) = New CustomVertex.PositionNormal()
verts(2).X = 3
verts(2).Y = 3
verts(2).Z = 6

myvertb.setdata(verts, 0, Direct3D.LockFlags.None)

Device.Clear(ClearFlags.Target, Color.Blue, 1, 0)
Device.BeginScene()
Device.SetStreamSource(0, myvertb, 0)
Device.VertexFormat = CustomVertex.PositionNormal.Format
Device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1)

Device.EndScene()
Device.Present()
Application.DoEvents()

End Sub

Private Function InitializeGraphics() As Boolean
Dim PParams As PresentParameters = New PresentParameters()
PParams.Windowed = True
PParams.SwapEffect = SwapEffect.Discard
PParams.AutoDepthStencilFormat = DepthFormat.D16
PParams.EnableAutoDepthStencil = True
PParams.PresentFlag = PresentFlag.DiscardDepthStencil

Device = New Device(0, DeviceType.Hardware, Me.Handle, CreateFlags.HardwareVertexProcessing, PParams)

End Function
End Class

Share this post


Link to post
Share on other sites
OK update:

I have discovered that lighting has nothing to do with my problem. It is when i try to use the autodepthstencil that my triagle primitive goes through the meat grinder, otherwise it works fine when i disable it.
For now i can carry on, but when i need to start using z-buffers, this is going to be a big problem.

Any one got any ideas as to what the problem is?

Share this post


Link to post
Share on other sites
You need to clear the depth buffer as well as the backbuffer. In C++, this is done by passing D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET to IDirect3DDevice::Clear(), I imagine the C#/VB equivalent is similar.

Share this post


Link to post
Share on other sites

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