Direct3D with Vertices

Adapted from this blog entry.



import clr clr.AddReference("System.Drawing") clr.AddReference("System.Windows.Forms") clr.AddReference("Microsoft.DirectX.Direct3D")

from System import Array from System.Drawing import * from System.Windows.Forms import * from Microsoft.DirectX.Direct3D import *

class Vertices(Form):

def __init__(self): self.MinimumSize = Size(100, 100) self.Text = "D3D Tutorial 02: Vertices" self.vertexBuffer = None self.device = None self.Paint += lambda *_: self.render self.initializeGraphics

def initializeGraphics(self): presentParams = PresentParameters presentParams.Windowed = True presentParams.SwapEffect = SwapEffect.Discard

self.device = Device(0, DeviceType.Hardware, self, CreateFlags.SoftwareVertexProcessing, presentParams)

self.onCreateDevice(self.device, None)

def onCreateDevice(self, device, event): self.vertexBuffer = VertexBuffer(           CustomVertex.TransformedColored, 3,            self.device, Usage.None,            CustomVertex.TransformedColored.Format,            Pool.Default)

self.vertexBuffer.Created += self.onCreateVertexBuffer

self.onCreateVertexBuffer(self.vertexBuffer, None)

def onCreateVertexBuffer(self, vb, event): stm = vb.Lock(0, 0, LockFlags.None) verts = Array.CreateInstance(CustomVertex.TransformedColored, 3) verts[0] = CustomVertex.TransformedColored(150, 50, 0.5, 1, Color.Aqua.ToArgb) verts[1] = CustomVertex.TransformedColored(250, 250, 0.5, 1, Color.Brown.ToArgb) verts[2] = CustomVertex.TransformedColored( 50, 250, 0.5, 1, Color.LightPink.ToArgb)

stm.Write(verts) vb.Unlock

def render(self): if not self.device: print "error" return

self.device.Clear(ClearFlags.Target, Color.Blue, 1.0, 0)

self.device.BeginScene

self.device.SetStreamSource(0, self.vertexBuffer, 0)

self.device.VertexFormat = CustomVertex.TransformedColored.Format

self.device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1)

self.device.EndScene self.device.Present

Application.Run(Vertices)

Back to Contents.