DirectX Samples in Boo
| Excerpt |
|---|
These are examples that use the Managed DirectX API for .NET. |
And of course these run on Windows only. For a cross-platform alternative for 3D graphics, see OpenGL 3D samples.
Textures Example
This is the code from tutorial 5 included with the DirectX SDK, converted to boo (mostly converted automatically by the boo sharpdevelop addin).
| Code Block |
|---|
namespace BooDirect3D.Sample.Textures
import System
import System.Drawing from System.Drawing
import System.Windows.Forms from System.Windows.Forms
import Microsoft.DirectX from Microsoft.DirectX
import Microsoft.DirectX.Direct3D from Microsoft.DirectX.Direct3D
import Microsoft.DirectX.Direct3D from Microsoft.DirectX.Direct3DX
import System.IO
class MyWindow(Form):
_device as Device = null
_vertexBuffer as VertexBuffer = null
_texture as Texture = null
_presentParams = PresentParameters()
_pause = false
def constructor():
self.ClientSize = System.Drawing.Size(400, 300)
self.Text = 'Direct3D Tutorial 5 - Textures'
def InitializeGraphics() as bool:
try:
_presentParams.Windowed = true
_presentParams.SwapEffect = SwapEffect.Discard
_presentParams.EnableAutoDepthStencil = true
_presentParams.AutoDepthStencilFormat = DepthFormat.D16
_device = Device(0, DeviceType.Hardware,
self,
CreateFlags.SoftwareVertexProcessing,
_presentParams)
//or if you have an old boo version, use this instead:
//(_presentParams,))
_device.DeviceReset += OnResetDevice
self.OnCreateDevice(_device, null)
self.OnResetDevice(_device, null)
_pause = false
return true
except ex as DirectXException:
MessageBox.Show(ex.ToString())
return false
def OnCreateDevice(sender, e as EventArgs):
dev as Device = sender
_vertexBuffer = VertexBuffer(
CustomVertex.PositionNormalTextured,
100, dev, Usage.WriteOnly,
CustomVertex.PositionNormalTextured.Format,
Pool.Default)
_vertexBuffer.Created += OnCreateVertexBuffer
self.OnCreateVertexBuffer(_vertexBuffer, null)
def OnResetDevice(sender, e as EventArgs):
dev as Device = sender
dev.RenderState.CullMode = Cull.None
dev.RenderState.Lighting = false
dev.RenderState.ZBufferEnable = true
if not File.Exists("banana.bmp"):
MessageBox.Show("Can't find banana.bmp file.")
Environment.Exit(-1)
_texture = TextureLoader.FromFile(dev, "banana.bmp")
def OnCreateVertexBuffer(sender, e as EventArgs):
vb as VertexBuffer = sender
verts as (CustomVertex.PositionNormalTextured) = vb.Lock(0, LockFlags.None)
i as int = 0
while i < 50:
rawArrayIndexing:
theta as single = (2 * Math.PI * i) / 49.0
verts[2 * i].Position = Vector3(Math.Sin(theta), -1, Math.Cos(theta))
verts[2 * i].Normal = Vector3(Math.Sin(theta), 0, Math.Cos(theta))
verts[2 * i].Tu = i / 49.0
verts[2 * i].Tv = 1
verts[2 * i + 1].Position = Vector3(Math.Sin(theta), 1, Math.Cos(theta))
verts[2 * i + 1].Normal = Vector3(Math.Sin(theta), 0, Math.Cos(theta))
verts[2 * i + 1].Tu = i / 49.0
verts[2 * i + 1].Tv = 0
++i
vb.Unlock()
private def SetupMatrices():
_device.Transform.World = Matrix.RotationAxis(
Vector3(Math.Cos(Environment.TickCount / 250.0),
1, Math.Sin(Environment.TickCount / 250.0)),
Environment.TickCount / 1000.0)
_device.Transform.View = Matrix.LookAtLH(
Vector3(0, 3, -5),
Vector3(0, 0, 0),
Vector3(0, 1, 0))
_device.Transform.Projection = Matrix.PerspectiveFovLH(
Math.PI / 4.0, 1, 1, 100)
def Render(): //main drawing loop
if _pause:
return
_device.Clear(ClearFlags.Target | ClearFlags.ZBuffer,
System.Drawing.Color.Blue, 1, 0)
_device.BeginScene()
SetupMatrices()
rawArrayIndexing:
_device.SetTexture(0, _texture)
_device.TextureState[0].ColorOperation = TextureOperation.Modulate
_device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor
_device.TextureState[0].ColorArgument2 = TextureArgument.Diffuse
_device.TextureState[0].AlphaOperation = TextureOperation.Disable
_device.SetStreamSource(0, _vertexBuffer, 0)
_device.VertexFormat = CustomVertex.PositionNormalTextured.Format
_device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, (4 * 25) - 2)
_device.EndScene()
_device.Present()
protected override def OnPaint(e as PaintEventArgs):
self.Render()
protected override def OnKeyPress(e as KeyPressEventArgs):
if cast(int,e.KeyChar) == cast(int,Keys.Escape):
self.Dispose()
//(if you subscribe to keypress event instead, check e.KeyCode==Keys.Escape
protected override def OnResize(e as System.EventArgs):
_pause = ((self.WindowState == FormWindowState.Minimized) or not self.Visible)
//main section:
using frm = MyWindow():
if not frm.InitializeGraphics():
MessageBox.Show('Could not initialize Direct3D. This tutorial will exit.')
return
frm.Show()
while frm.Created:
frm.Render()
Application.DoEvents()
|
