# Computer graphics/2013-2014/Laboratory 14

Quick links: front; laboratories agenda, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, JOGL template.

## Brief introduction in Managed DirectX 9
### Overview

In this lab we will discuss some general issues concerning with the creating of a simple DX scene and the draw of certain elements inside of it. The code will be written in C# as it closely resembles Java.
NOTE: This lab is only intended to provide a quick intro to Managed DX and comparison with OGL and must not be seen as a tutorial by itself.

### Creating a simple Hello World application

Before proceeding we need to add references to the DirectX libraries which we are going to use. For this go to the Project menu, and select Add reference. In the list that comes up, pick Microsoft.DirectX and Microsoft.DirectX.Direct3D. Also add the following lines to you using-block, so your program can use the referenced libraries:
Each Managed DX application basically requires:
* an initialization method where linking to the Direct3D.Device object is accomplished (similar to the OGL init function)
* a rendering method where the scene is being handled (similar with the OGL display function)
* a method handling device resets. This event occurs for example when the window is being reshaped (similar with the OGL reshape function)
Additionally we could require some user interaction by using the mouse, the keyboard, the joystick etc.

#### Initialization of the DX device

Initialization of the DX device is usually done inside a custom built method which needs to be called before rendering the scene. The next fragment of code shows how we can achieve this:

#### Rendering

Rendering a DX scene requires overriding the OnPaint method:

#### Device resetting

The OnResetDevice event handler is a good place to create and initialize any Direct3D related objects, which may become invalid during a device reset:

### Projection and ModelView in Managed DirectX

As (J)OGL, DX also comes equipped with methods for dealing with setting up a projection, model (world) and view matrix. Both of their methods are quite similar:
NOTE: The previous code created a perspective projection. Additionally one can create an orthographic one by using the Matrix.OrthoOffCenterLH, Matrix.OrthoLH methods or their RH (right hand) counterparts.
The model matrix can be manipulated in the same way as in (J)OGL by applying rotations, translations and scaling on the scene objects. The following fragment of code shows how we can achieve these:

### Dealing with vertices

As (J)OGL, DX relies on vertices as the building blocks of the scene elements. In contrast to (J)OGL DX vertices can be defined in multiple ways: as transformed (screen coordinates), untransformed (world coordinates), with textures or with colors etc:
* TransformedColored
* TransformedTextured
* PositionColored
* PositionTextured
* PositionNormalTextured
* PositionNormalColored
* etc.
The primitives which can be built from vertices are also similar with the (J)OGL ones:
* PrimitiveType.LineList (GL_LINES)
* PrimitiveType.LineStrip (GL_LINE_STRIP)
* PrimitiveType.PointList (GL_POINTS)
* PrimitiveType.TriangleFan (GL_TRIANGLE_FAN)
* PrimitiveType.TriangleList (GL_TRIANGLES)
* PrimitiveType.TriangleStrip (GL_TRIANGLE_STRIP)
The following code fragment show how we can build triangle from PositionColored vertices:
Similar with OGL's display lists DX offers VertexBuffers. A vertex buffer can be created as follows:
IMPORTANT: Lighting must be off if we want our primitives to be visible. If using lighting we need to specify normals to our vertices (use PositionNormalColored, PositionNormalTextured etc.).

### Lights, Materials and Textures

The following fragment of code shows how we can add lighting, materials and textures to scene objects.

### Adding spheres to the scene

Spheres are some of the easiest 3D objects to draw in either OGL (using GLU) or DX. The following example shows how we can add a sphere to a scene and add a material and texture to it.

### Blending

Blending usually means mixing color components of several objects in the scene. DX achieves this as easily as (J)OGL:

## Other issues

The previous sections were meant to show how we can achieve in DX most of the things shown in the previous (J)OGL laboratories. However they were not presented in much detail.
However topics such as camera movement, particle engines, world boxes were omitted as they can be easily achieved with the already known issues plus some translation of OGL code to DX.

## Links

* Managed DX9 tutorial
* Gabriel Iuhasz, iuhasz.gabriel@info.uvt.ro