davedwards
Newcomer
- Joined
- Mar 14, 2003
- Messages
- 3
Hi,
I'm having a little difficulty using the material of the device.
As an example, I've modified tutorial 2 from DirectX SDK, to use Transformed vertices instead of TransformedColored vertices, and then I set the Device Material to Red. However, when I render the triangle is not red, but white. Can anyone tell me what I am doing wrong?
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Namespace VerticesTutorial
Public Class Vertices
Inherits Form
' Our global variables for this project
Private device As device = Nothing ' Our rendering device
Private vertexBuffer As vertexBuffer = Nothing
Public Sub New()
Me.ClientSize = New System.Drawing.Size(300, 300)
Me.Text = "Direct3D Tutorial 2 - Vertices"
End Sub 'New
Public Function InitializeGraphics() As Boolean
Try
Dim presentParams As New PresentParameters()
presentParams.Windowed = True
presentParams.SwapEffect = SwapEffect.Discard
device = New Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, presentParams)
AddHandler device.DeviceCreated, AddressOf Me.OnCreateDevice
Me.OnCreateDevice(device, Nothing)
Return True
Catch e As DirectXException
Return False
End Try
End Function 'InitializeGraphics
Public Sub OnCreateDevice(ByVal sender As Object, ByVal e As EventArgs)
Dim dev As Device = CType(sender, Device)
vertexBuffer = New VertexBuffer(GetType(CustomVertex.Transformed), 3, dev, Usage.WriteOnly, CustomVertex.Transformed.Format, Pool.Default)
AddHandler vertexBuffer.Created, AddressOf Me.OnCreateVertexBuffer
Me.OnCreateVertexBuffer(vertexBuffer, Nothing)
End Sub 'OnCreateDevice
Public Sub OnCreateVertexBuffer(ByVal sender As Object, ByVal e As EventArgs)
Dim vb As VertexBuffer = CType(sender, VertexBuffer)
Dim verts As CustomVertex.Transformed() = CType(vb.Lock(0, 0), CustomVertex.Transformed())
verts(0) = New CustomVertex.Transformed(150, 50, 0.5F, 1)
verts(1) = New CustomVertex.Transformed(250, 250, 0.5F, 1)
verts(2) = New CustomVertex.Transformed(50, 250, 0.5F, 1)
vb.Unlock()
End Sub 'OnCreateVertexBuffer
Private Sub Render()
'Clear the backbuffer to a blue color
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0F, 0)
'Begin the scene
device.BeginScene()
' Set the material
Dim mat As New Material()
mat.Ambient = Color.Red
mat.Diffuse = Color.Red
device.Material = mat
device.SetStreamSource(0, vertexBuffer, 0)
device.VertexFormat = CustomVertex.Transformed.Format
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1)
device.EndScene()
device.Present()
End Sub 'Render
Shared Sub Main()
Dim frm As New Vertices()
frm.InitializeGraphics()
frm.Show()
While frm.Created
frm.Render()
Application.DoEvents()
End While
End Sub 'Main
End Class 'Vertices
End Namespace 'VerticesTutorial
I'm having a little difficulty using the material of the device.
As an example, I've modified tutorial 2 from DirectX SDK, to use Transformed vertices instead of TransformedColored vertices, and then I set the Device Material to Red. However, when I render the triangle is not red, but white. Can anyone tell me what I am doing wrong?
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Namespace VerticesTutorial
Public Class Vertices
Inherits Form
' Our global variables for this project
Private device As device = Nothing ' Our rendering device
Private vertexBuffer As vertexBuffer = Nothing
Public Sub New()
Me.ClientSize = New System.Drawing.Size(300, 300)
Me.Text = "Direct3D Tutorial 2 - Vertices"
End Sub 'New
Public Function InitializeGraphics() As Boolean
Try
Dim presentParams As New PresentParameters()
presentParams.Windowed = True
presentParams.SwapEffect = SwapEffect.Discard
device = New Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, presentParams)
AddHandler device.DeviceCreated, AddressOf Me.OnCreateDevice
Me.OnCreateDevice(device, Nothing)
Return True
Catch e As DirectXException
Return False
End Try
End Function 'InitializeGraphics
Public Sub OnCreateDevice(ByVal sender As Object, ByVal e As EventArgs)
Dim dev As Device = CType(sender, Device)
vertexBuffer = New VertexBuffer(GetType(CustomVertex.Transformed), 3, dev, Usage.WriteOnly, CustomVertex.Transformed.Format, Pool.Default)
AddHandler vertexBuffer.Created, AddressOf Me.OnCreateVertexBuffer
Me.OnCreateVertexBuffer(vertexBuffer, Nothing)
End Sub 'OnCreateDevice
Public Sub OnCreateVertexBuffer(ByVal sender As Object, ByVal e As EventArgs)
Dim vb As VertexBuffer = CType(sender, VertexBuffer)
Dim verts As CustomVertex.Transformed() = CType(vb.Lock(0, 0), CustomVertex.Transformed())
verts(0) = New CustomVertex.Transformed(150, 50, 0.5F, 1)
verts(1) = New CustomVertex.Transformed(250, 250, 0.5F, 1)
verts(2) = New CustomVertex.Transformed(50, 250, 0.5F, 1)
vb.Unlock()
End Sub 'OnCreateVertexBuffer
Private Sub Render()
'Clear the backbuffer to a blue color
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0F, 0)
'Begin the scene
device.BeginScene()
' Set the material
Dim mat As New Material()
mat.Ambient = Color.Red
mat.Diffuse = Color.Red
device.Material = mat
device.SetStreamSource(0, vertexBuffer, 0)
device.VertexFormat = CustomVertex.Transformed.Format
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1)
device.EndScene()
device.Present()
End Sub 'Render
Shared Sub Main()
Dim frm As New Vertices()
frm.InitializeGraphics()
frm.Show()
While frm.Created
frm.Render()
Application.DoEvents()
End While
End Sub 'Main
End Class 'Vertices
End Namespace 'VerticesTutorial