Using the Device Material instead of Vertex Color

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
 
You're right about the lights. I've since learned that all materials has to do with the how light is reflected from the surface. Materials don't do anything if Lighting is not turned. And you do need normals if you want shading.

In order to get the coloring that I wanted, I needed to set Lighting to true, and set both the diffuse and emmissive color to the color I wanted. I didn't even have to add a light to the scene, because the emmissive color disregards any lights in the scene. It was critical, even though there were no lights in the scene, to have Lighting turned on.

Thanks for your reply
 
Back
Top