Animated Direct3D with Matrices

Adapted from this blog entry.



An animated version of Direct3D with Vertices, using matrices.

import clr clr.AddReference("System.Drawing") clr.AddReference("System.Windows.Forms")

clr.AddReferenceByPartialName("Microsoft.DirectX, Version=1.0.2902.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35") clr.AddReferenceByPartialName("Microsoft.DirectX.Direct3D")
 * 1) Using the Strong Name of the assembly

from System import Array, Environment from System import * from System.Drawing import * from System.Windows.Forms import *

from Microsoft.DirectX import * from Microsoft.DirectX.Direct3D import *

class Matrices(Form): def __init__(self): self.ClientSize = Size(400, 300) self.MinimumSize = Size(100, 100) self.Text = "D3D Tutorial 03: Matrices"

self.pause = False self.device = None self.vertexBuffer = 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.device.DeviceReset += self.onResetDevice

self.onCreateDevice(self.device, None) self.onResetDevice(self.device, None)

def onCreateDevice(self, dev, event): self.vertexBuffer = VertexBuffer(           CustomVertex.PositionColored, 3,            dev, Usage.None,            CustomVertex.PositionColored.Format,            Pool.Default) self.vertexBuffer.Created += self.onCreateVertexBuffer self.onCreateVertexBuffer(self.vertexBuffer, None)

def onResetDevice(self, dev, event): dev.RenderState.CullMode = Cull.None dev.RenderState.Lighting = False

def onCreateVertexBuffer(self, vb, event): verts = vb.Lock(0, LockFlags.None) verts[0] = CustomVertex.PositionColored(-1.0, -1.0, 0.0, Color.DarkGoldenrod.ToArgb) verts[1] = CustomVertex.PositionColored(+1.0, -1.0, 0.0, Color.MediumOrchid.ToArgb) verts[2] = CustomVertex.PositionColored(+0.0, +1.0, 0.0, Color.Cornsilk.ToArgb)

vb.Unlock

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

if self.pause: return

self.device.Clear(ClearFlags.Target, Color.Blue, 1.0, 0) self.device.BeginScene self.setupMatrices self.device.SetStreamSource(0, self.vertexBuffer, 0)

self.device.VertexFormat = CustomVertex.PositionColored.Format

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

self.device.EndScene self.device.Present

def setupMatrices(self): iTime = Environment.TickCount % 1000 fAngle = iTime * (2.0 * Math.PI) / 1000.0 self.device.Transform.World = Matrix.RotationY(fAngle)

self.device.Transform.View = Matrix.LookAtLH(           Vector3(0.0, 3.0, -5.0),            Vector3(0.0, 0.0, 0.0),            Vector3(0.0, 1.0, 0.0)        ) self.device.Transform.Projection = Matrix.PerspectiveFovLH(           Math.PI / 4, 1.0, 1.0, 100.0)

matrix = Matrices matrix.Show while (matrix.Created): matrix.render Application.DoEvents
 * 1) A loop to ensure continuous repainting

Back to Contents.