- Forward


Direct3D Basics
with Examples in C/C++


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

The Direct3D Architecture
Back SMYC Forward
  • Primitives:
    • Raw vertices are stored in vertex memory buffers
    • Geometric primitives (points, lines, triangles, and polygons) reference the raw vertices
  • The Pipeline:
    • Tessellation Unit
    • Vertex Processing Unit
    • Geometry Processing Unit (clipping, culling and rasterization)
    • Pixel Processor
Direct3D Concepts
Back SMYC Forward
  • Device:
    • The rendering component
    • Contains a transformation module, lighting module, and rasterization module
    • Created using DXUT or
      HRESULT IDirect3D9::CreateDevice(UINT adapter, D3DDEVTYPE type, HWND window, DWORD flags, D3DPRESENT_PARAMETERS* params, IDirect3DDevice9** returnedDevice)
  • Device Types:
    • Hardware accelerated rasterization; both hardware and software vertex processing (D3DDEVTYPE_HAL)
    • Reference device used for debugging that includes every feature (D3DDEVTYPE_REF)
Direct3D Concepts (cont.)
Back SMYC Forward
  • Processing Vertex Data:
    • D3DCREATE_SOFTWARE_VERTEXPROCESSING
    • D3DCREATE_HARDWARE_VERTEXPROCESSING
    • D3DCREATE_MIXED_VERTEXPROCESSING
  • Render States:
    • Alpha Blending State
    • Ambient Lighting State
    • Antialiasing State
    • Depth Buffering State
    • Fog State
    • Lighting State
    • Outline and Fill State
    • Per-Vertex Color State
    • Shading State
    • Texture State
Coordinates
Back SMYC Forward
  • System:
    • Left-handed Cartesian coordinates
  • Converting from Right-Handed:
    • Re-order vertices (e.g., a right-handed triangle with vertices \(v_0\), \(v_1\), \(v_2\) becomes a left-handed triangle with vertices \(v_0\), \(v_2\), \(v_1\))
    • Multiply \(z\) values by -1
Vertices
Back SMYC Forward
  • Vertex Elements:
    • Position
    • Normal
    • Color (D3DCOLOR_ARGB)
  • Process:
    • Specify a custom struct for a vertex
    • Specify a corresponding custom Flexible Vertex Format (FVF)
  • FVF Codes:
    • Untransformed position: D3DFVF_XYZ
    • Color Components: D3DFVF_DIFFUSE, D3DFVF_SPECULAR
    • Normal: D3DFVF_NORMAL
    • Point Size: D3DFVF_PSIZE
Vertices (cont.)
Back SMYC Forward

An Example

directx9examples/basics/cube/cube.cpp (Fragment: vertices)
 
Vertices (cont.)
Back SMYC Forward
  • Vertex Components:
    • One or more vertex elements stored in a buffer (IDirect3DVertexBuffer9)
  • Process:
    • Create the vertex buffer
      HRESULT CreateVertexBuffer(UINT size, DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9** vertexBuffer, NULL)
    • Fill the vertex buffer with (a copy of) the vertices
Vertices (cont.)
Back SMYC Forward

Creating a Vertex Buffer

directx9examples/basics/cube/cube.cpp (Fragment: createBuffer)
 
Points, Lines and Polygons
Back SMYC Forward
  • Primitives:
    • Point list, line list, line strip, triangle list, triangle strip, triangle fan
  • A Triangle Strip:
    • triangle-strip
  • A Triangle Fan:
    • triangle-fan
Points, Lines and Polygons
Back SMYC Forward
  • Nonindexed Rendering:
    • HRESULT IDirect3DDevice9::DrawPrimitive(D3DPRIMITIVETYPE type, UINT startVertex, UINT primitiveCount)
  • Indexed Rendering:
    • HRESULT IDirect3DDevice9::DrawIndexedPrimitive(D3DPRIMITIVETYPE type, INT offset, UINT minIndex, UINT numVertices, UINT startIndex, UINT count)
Points, Lines and Polygons (cont.)
Back SMYC Forward
  • Two 2D Triangles (Nonindexed):
    • opengl-nonindexed
    • device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, // startVertex 2); // primitiveCount
  • Two 2D Triangles (Indexed):
    • opengl-indexed
    • device->DrawIndexPrimitive(D3DPT_TRIANGLELIST, 0, // offset 0, // minIndex 4, // numVertices (in total) 0, // startIndex 2); // primitiveCount
The Rendering Process
Back SMYC Forward
  • Starting the "Scene":
    • IDirect3DDevice9::BeginScene()
  • Ending the "Scene":
    • IDirect3DDevice9::EndScene()
  • Putting the "Scene" in the Off-Screen Buffer:
    • IDirect3DDevice9::Present(NULL, NULL, NULL, NULL)
Rendering (cont.)
Back SMYC Forward
  • Binding Components/Buffers:
    • Vertex components/buffers are bound to a device data stream using
      HRESULT IDirect3DDevice9::SetStreamSource(UNIT streamNumber, IDirect3DVertexBuffer9* streamData, UINT offset, UINT stride)
  • Identify the Fixed Function Vertex Shader:
    • For basic usage, this involves passing the FVF code
      HRESULT IDirect3DDevice9::SetFVF(DWORD fvf)
Rendering (cont.)
Back SMYC Forward

An Example

directx9examples/basics/cube/cube.cpp (Fragment: render)
 
Viewing
Back SMYC Forward
  1. World Matrix:
    • Transformations to perform in model space
  2. View Matrix:
    • The "camera" for the scene
  3. Projection Matrix
    • How geometry is transformed from 3D view space to 2D viewport space
Viewing (cont.)
Back SMYC Forward
  • Declaring a Matrix:
    • D3DXMATRIX
  • Transforms:
    • D3DXMatrixRotation*(), D3DXMatrixReflect(), D3DXMatrixScaling(), D3DXMatrixTranslation(), etc...
  • Applying the Transform:
    • HRESULT IDirect3DDevice9::SetTransform(D3DTRANSFORMSTATETYPE type, CONST D3DMATRIX* m)
    • where type is D3DTS_VIEW, D3DTS_PROJECTION, or D3DTS_WORLD
Viewing (cont.)
Back SMYC Forward
  • Defining the View Transformation Matrix:
    • Is usually done using a "convenience" method
  • Examples:
    • D3DXMATRIX* D3DXMatrixLookAtLH(D3DXMATRIX* mOut, CONST D3DXVECTOR3* eye, CONST D3DXVECTOR3* at, CONST D3DXVECTOR3* up) where the LH indicates "left-handed"
    • D3DXMATRIX* D3DXMatrixRotationYawPitchRoll(D3DXMATRIX* mOut,FLOAT yaw, FLOAT pitch, FLOAT roll) where the pitch is around \(x\), the yaw is around \(y\), and the roll is around \(z\)
The Projection Transform
Back SMYC Forward
  • Orthographic:
    • D3DXMATRIX* D3DXMatrixOrthoLH(D3DXMATRIX* mOut, FLOAT w, FLOAT h, FLOAT znear, FLOAT zfar)
  • Perspective:
    • D3DXMATRIX* D3DXMatrixPerspectiveFovLH(D3DXMATRIX* mOut, FLOAT fovy, FLOAT aspect, FLOAT znear, FLOAT zfar)
Viewing (cont.)
Back SMYC Forward

An Example

directx9examples/basics/cube/cube.cpp (Fragment: projection)
 
Transformations
Back SMYC Forward

Using a Mouse Callback

directx9examples/basics/cube/cube.cpp (Fragment: mousecallback)
 
There's Always More to Learn
Back -