Home | Lehre | Videos | Texte | Vorträge | Software | Person | Impressum, Datenschutzerklärung | Blog
First Steps with Managed DirectX
What is Managed DirectX?
-
DirectX: Microsoft's API for fast graphics (DirectX Graphics, formerly
Direct3D and DirectDraw), audio processing (DirectSound), networked gaming
(DirectPlay), etc., written in C.
-
Managed DirectX: Object-oriented wrapper for DirectX in .NET
-
Allows DirectX to be used with C#, Visual Basic .NET, C++ with Managed
Extensions, etc.
Basic drawing using Managed DirectX
Build a Direct3D application using the SharpDevelop wizard and add the
following:
// A class member to hold our vertices
private VertexBuffer vb;
// ...
// Initialization
vb = new VertexBuffer(typeof(CustomVertex.PositionColored), 6,
device, Usage.WriteOnly, 0, Pool.Managed);
CustomVertex.PositionColored[] v = (CustomVertex.PositionColored[])
vb.Lock(0, 0);
v[0] = new CustomVertex.PositionColored(0.5f, -0.5f, 0.0f, Color.Black.ToArgb());
v[1] = new CustomVertex.PositionColored(0.0f, 1.0f, 0.0f, Color.Red.ToArgb());
v[2] = new CustomVertex.PositionColored(-0.5f, 0.2f, 0.0f, Color.Green.ToArgb());
v[3] = new CustomVertex.PositionColored(0.7f, -0.6f, 0.0f, Color.Beige.ToArgb());
v[4] = new CustomVertex.PositionColored(0.8f, 0.5f, 0.0f, Color.Yellow.ToArgb());
v[5] = new CustomVertex.PositionColored(0.9f, 0.8f, 0.0f, Color.Black.ToArgb());
vb.Unlock();
// ...
// Drawing
device.BeginScene(); // already present in the generated code
device.SetStreamSource(0, vb, 0);
device.VertexFormat = CustomVertex.PositionColored.Format;
device.RenderState.Lighting = false;
device.RenderState.CullMode = Cull.None;
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2); // try
other members of PrimitiveType
device.EndScene(); // already present in the generated code
// ...
// Release (e.g., in DeleteDeviceObjects)
vb.Dispose();
Debugging DirectX programs
-
When the DirectX SDK is installed, DirectX can be set to the (fast) retail
version or the (slower) debug version using its control panel. The command-line
tool dbmon.exe (included with the SDK) will display diagnostic output of
the debug version.
-
The d3d9.dll may be overriden by a fake d3d9.dll. This is the operating
principle of D3DSpy.exe, a complex debugging tool also inculded with the
SDK. It offers for instance step-by-step execution and access to matrices.
Projection, Linear Perspective
So now we can paint using DirectX. But how does the z coordinate work?
Sensing depth (and how to simulate depth)
-
Overlapping (i.e. hidden surfaces)
-
Foreshortening [perspektivische Verkürzung]
-
Shadows
-
Shading
-
Stereoscopic viewing: disparity, vergence
-
aerial perspective [Luftperspektive] = Haze [Dunst]
-
Depth of field [Tiefenschärfe, Schärfentiefe]
-
Accomodation (tension of eye lens)
-
Motion parallax (changes due to moving viewer)
-
Object motion
-
etc.
Projections: How to turn 3D space into 2D images
-
Linear projections: lines in 3D are converted to lines (or segments of
lines) in 2D
-
Parallel projections: parallel lines in 3D are converted to parallel lines
in 2D
-
Orthogonal projection (in technical drawing this means: orthogonal projection
along a principal direction): project perpendicular to the image
plane, e.g., as in the "Front", "Top", and "Left" views in standard 3D
software
-
Isometric projection: The units on the x, y, and z axes have the same length.
-
etc.
-
Linear perspective: Project using rays converging in a common vantage point.
Consequences: foreshortening; vanishing points [Fluchpunkte] for parallel
lines that are not parallel to the image plane. Construction. Demo: field
of view
-
Non-linear projections