This site was last updated:Monday 13 October 2008, 11:30 GMT

Educational Community

DIRECT3D WITH C#
(1 vote, average: 5.00 out of 5)
Written by Christopher   
Thursday, 12 June 2008 18:23

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:

 

  1. Input assembler: vertex data is read and are fed down the pipeline to continue with the processing.
  2. Vertex shader: Various operations are performed on a single vertex at a time. Operations include transformations, skinning and lighting.
  3. Geometry shader: Primitives are being processed at this stage. A triangle, point or line is considered to be a primitive.
  4. Stream output: Writes the previous results into memory.
  5. Rasterizer: converts primitives into pixels. These pixels are fed into the pixel shader.
  6. Pixel Shader: This stage is for determining the final colour of the pixel to be written to the target.
  7. 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();

}

Trackback(0)
Comments (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 

User Menu

None
Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor KNOGE.com offers free video and text tutorials on various softwares also free resources to improve your economy and start making money online. THIS IS ONLY A TEST AD TO DISPLAY HOW IT MIGHT LOOK Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor

Adobe InDesign CS3 - Working with objects

Objects in Adobe InDesign CS3 are the basic building block of any design that you do. In this video tutorial you will learn how to create those blocks easily in...

InDesign Videos | Christopher

Read More

Adobe InDesign CS3 - Working with Panels & More

In this Adobe InDesign CS3 video tutorial you will learn how to set keys and work with panels, you will also learn how to create customized keyboard shortcuts for optimized...

InDesign Videos | Christopher

Read More

Adobe Photoshop CS3 - Create customized rust on cars

This Adobe Photoshop CS3 video tutorial will teach you how to create your own customized rust on cars for a more grungy and perhaps dusty effect. This technique does also...

Photoshop Videos | Christopher

Read More
100%
-
+
3
Show options