Animated Object with Lights

Adapted from this blog entry.



A moving object, with a light source.

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

clr.AddReference("Microsoft.DirectX, Version=1.0.2902.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35") clr.AddReference("Microsoft.DirectX.Direct3D")
 * 1) .NET Framework 1.1 DirectX

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

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

class Lights(Form): def __init__(self): self.ClientSize = Size(400, 300) self.Text = "D3D Tutorial 04: Lights"

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 presentParams.EnableAutoDepthStencil = True presentParams.AutoDepthStencilFormat = DepthFormat.D16

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.PositionNormal, 100,            dev,            Usage.WriteOnly, # ?? ???? ??            CustomVertex.PositionNormal.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 = True dev.RenderState.ZBufferEnable = True

def onCreateVertexBuffer(self, vb, event): verts = vb.Lock(0, LockFlags.None) for i in xrange(50): theta = (2 * Math.PI * i) / 49 sin_theta = Math.Sin(theta) cos_theta = Math.Cos(theta) verts[2 * i] = CustomVertex.PositionNormal(               Vector3(sin_theta, -1, cos_theta),  # Position                Vector3(sin_theta,  0, cos_theta)   # Normal            ) verts[2 * i + 1] = CustomVertex.PositionNormal(               Vector3(sin_theta, 1, cos_theta),   # Position                Vector3(sin_theta, 0, cos_theta)    # Normal            )

vb.Unlock

def setupLights(self): col = Color.White mtrl = Material mtrl.Diffuse = col mtrl.Ambient = col self.device.Material = mtrl

tick_step = Environment.TickCount / 250.0 self.device.Lights[0].Type      = LightType.Directional self.device.Lights[0].Diffuse   = Color.DarkTurquoise self.device.Lights[0].Direction = Vector3(                Math.Cos(tick_step), 1.0, Math.Sin(tick_step))

self.device.Lights[0].Enabled = True

self.device.RenderState.Ambient = Color.FromArgb(0x202020)

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

if self.pause: return

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

self.device.BeginScene

self.setupLights self.setupMatrices

self.device.SetStreamSource(0, self.vertexBuffer, 0) self.device.VertexFormat = CustomVertex.PositionNormal.Format self.device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, (4*25)-2) self.device.EndScene self.device.Present

def setupMatrices(self): tick_step  = Environment.TickCount / 250.0 roll_step  = Environment.TickCount / 3000.0 axis       = Vector3(Math.Cos(tick_step), 1, Math.Sin(tick_step)) self.device.Transform.World = Matrix.RotationAxis(axis, roll_step)

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)

lights = Lights lights.Show

while (lights.Created): lights.render Application.DoEvents
 * 1) A loop to ensure continuous repainting

Back to Contents.