Home | Lehre | Videos | Texte | Vorträge | Software | Person | Impressum, Datenschutzerklärung | Blog RSS

First Steps with Managed DirectX

What is Managed DirectX?

Basic drawing using Managed DirectX

Include references to the following DLLs: Microsoft.DirectX, Microsoft.DirectX.Direct3D, Microsoft.DirectX.Direct3DX

// class members
private Device device;
private VertexBuffer vertices;

// Initialization, for instance in the constructor of the Form object
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.Opaque, true);
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.AutoDepthStencilFormat = DepthFormat.D24X8;
presentParams.EnableAutoDepthStencil = true;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
vertices = new VertexBuffer(typeof(CustomVertex.PositionColored), 3, device, Usage.WriteOnly, 0, Pool.Managed);
CustomVertex.PositionColored[] v = (CustomVertex.PositionColored[])vertices.Lock(0, LockFlags.None);
v[0] = new CustomVertex.PositionColored(0.6f, 0.1f, 0.0f, Color.Green.ToArgb());
v[1] = new CustomVertex.PositionColored(-0.9f, 0.8f, 0.0f, Color.Red.ToArgb());
v[2] = new CustomVertex.PositionColored(0.2f, -0.6f, 0.0f, Color.Yellow.ToArgb());
vertices.Unlock();

// Drawing, typically in the OnPaint function of the Form object
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Blue, 1.0f, 0);
device.BeginScene();
device.SetStreamSource(0, vertices, 0);
device.VertexFormat = CustomVertex.PositionColored.Format;
device.RenderState.Lighting = false;
device.RenderState.CullMode = Cull.None;
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
device.EndScene();
device.Present();

Debugging DirectX programs

Projection, Linear Perspective

So now we can draw triangles using DirectX. But how does the z coordinate work?

Sensing depth (and how to simulate depth)

Projections: How to turn 3D space into 2D images