INTRODUCTION
Direct 3D is an API used for displaying applications with rich multimedia content. You can add 2D/3D graphics, video and sound in your application. You can also create 3D engines for video games. Direct Graphics is the portion of API that deals with the graphics of a direct 3D application. It is oriented towards 3D design but it also works well for 2D graphics. In order to use sound you have to use the functions of Direct Sound.
DIRECT3D PROCESSES
Direct3D is something like the GDI/GDI++ we have talked about in a previous tutorial. Its aim is to sit between the device drivers and your application in order to make easier the use of the device. It is a thin abstract layer comparable to GDI. However, Direct3D yields much better results in rendering and performance than GDI.
Direct3D contains processes to transform a group of vertices, textures, buffers and state into a screen image. A general flowchart of these processes is given below:
- Input assembler: vertex data is read and are fed down the pipeline to continue with the processing.
- Vertex shader: Various operations are performed on a single vertex at a time. Operations include transformations, skinning and lighting.
- Geometry shader: Primitives are being processed at this stage. A triangle, point or line is considered to be a primitive.
- Stream output: Writes the previous results into memory.
- Rasterizer: converts primitives into pixels. These pixels are fed into the pixel shader.
- Pixel Shader: This stage is for determining the final colour of the pixel to be written to the target.
- Output merger: finalizes the output result.
CREATING APPLICATION WINDOW
The first thing any Microsoft® Windows® application must do when run is to create an application window. To do this, the first call in the Main() function of the following sample code is to the constructor of an application-defined CreateDevice class that sets the size of the display window, the caption of the window, and the window icon. CreateDevice is created from the System.Windows.Forms.Form class used in the Microsoft .NET Framework to represent a window in an application.
using System;
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace DeviceTutorial
{
public class CreateDevice : Form
{
// Global variables for this project
Device device = null; // Rendering device
public CreateDevice()
{
// Set the initial size of form
this.ClientSize = new System.Drawing.Size(400,300);
// And its caption
this.Text = "Direct3D Tutorial 01: CreateDevice";
// Load icon from the resources of the .exe
this.Icon = new Icon(this.GetType(), "directx.ico");
}
}
}
INITIALIZING THE DIRECT3D OBJECT
After you create the application window, you are ready to initialize the Direct3D object that you will use to render the scene. This process includes creating the object, setting the presentation parameters, and finally creating the Direct3D device.
public bool InitializeGraphics()
{
try
{
// Create a PresentParameters object
PresentParameters presentParams = new PresentParameters();
// Don't run full screen
presentParams.Windowed = true;
// Discard the frames
presentParams.SwapEffect = SwapEffect.Discard;
// Instantiate a device
device = new Device(0,DeviceType.Hardware,this,CreateFlags.SoftwareVertexProcessing,presentParams);
return true;
}
catch { return false; }
}
RENDERING THE 3D OBJECT
The application is kept running in a loop using the Application .DoEvents method, which here takes as its argument a CreateDevice object called frm. DoEvents runs a standard Windows application message loop on the current thread.
static void Main()
{
using (CreateDevice frm = new CreateDevice());
{
if (!frm.InitializeGraphics()) // Initialize Direct3D
{
MessageBox.Show("Could not initialize Direct3D. This tutorial will exit.");
return();
}
frm.Show();
// While the form is still valid, render and process messages
while(frm.Created)
{
frm.Render();
Application.DoEvents();
}
}
}
While the created CreateDevice Form object is valid, an application-defined Render method is called to render the Direct3D object. First the viewport (the open window) is set to be a uniform blue color with the Device.Clear method. Scene rendering is begun using the Device.BeginScene method. Rendering is completed and the scene is ended with successive calls to the EndScene and Present methods.
private void Render()
{
if (device == null)
return;
//Clear the backbuffer to a blue color (ARGB = 000000ff)
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
//Begin the scene
device.BeginScene();
// Rendering of scene objects can happen here
//End the scene
device.EndScene();
device.Present();
}
discuss this topic to forum
