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, Delphi .NET, etc.
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
-
After the DirectX SDK is installed, DirectX can be set to the (fast) retail
version or the (slower) debug version using its control panel. The debug
version sends warnings and error messages to a debug monitor, for instance
the output window of Visual Studio. Be sure to enable "Unmanaged Debugging"
in the properties of the project.
-
PIX, a utility contained in the DirectX SDK, allows to capture all DirectX
commands in real time and to display statistics to aid in optimization.
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)
-
Overlapping (i.e. hidden surfaces)
-
Foreshortening [perspektivische Verkürzung]
-
Shadows [Schatten]
-
Shading [Schattierung]
-
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 [Fluchtpunkte] for parallel
lines that are not parallel to the image plane. Construction. Demo: field
of view
-
Non-linear projections