Title: 3D Graphics APIs Part II: DirectX Graphics rel' 9'0
1Fac. of Comp., Eng. Tech. Staffordshire
University
3D Computer Graphics
3D Graphics APIs Part II DirectX Graphics
(rel. 9.0)
Dr. Claude C. Chibelushi
2Outline
- Introduction
- Programming Basics
- Initialisation
- Polygonal shape modelling
- Geometric transformations
- Hidden surface removal
- Illumination and shading
- Rendering and display
- Summary
3Introduction
- DirectX
- Set of APIs for developing games and other
multimedia applications for MS Windows platform - Supports
- 2-D, 3-D graphics
- sound, music (control of sound mixing, sound
output ) - user input (joystick, keyboard, mouse), force
feedback - multimedia streaming, network communication
- multiplayer games
4Introduction
- DirectX
- DirectX APIs
- DirectX Graphics
- integration of Direct3D (3D graphics) and
DirectDraw (access to video card) - DirectPlay
- multiplayer games via network
- DirectInput
- access to input devices
- DirectX Audio
- integration of DirectSound and DirectMusic
- DirectShow, DirectAnimation
5Introduction
- DirectX
- Portability
- provides hardware abstraction layer (HAL)
- uses software drivers for communication between
multimedia software and computer hardware - Components of DirectX Graphics
- Direct3D (D3D)
- core rendering functions, types, and constants
- Direct3DX (D3DX)
- utility library
6Introduction
- DirectX Graphics sample features
- Programmable vertex and pixel processing
languages - developer can write custom shaders
- Multisampling rendering
- full-scene anti-aliasing, motion blur,
depth-of-field blur - Point sprite support
- particle systems
- Higher-order primitives
- smoother geometric shapes
7Introduction
- DirectX Graphics
- Layered architecture of graphics system
8Programming Basics
- Win32 programming
- run-time requirements for DirectX 9
- Microsoft Windows 98, Windows Me, Windows 2000,
Windows XP - dynamic link libraries d3d.dll
- libraries d3d9.lib, d3dx9dt.lib,
- header files d3d9.h, d3dx9.h,
9Programming Basics
- Typical program steps
- create application window
- create and initialise Direct3D objects
- invoke appropriate instructions / commands for
- event handling
- defining and rendering synthetic worlds
10Programming Basics
- Initialisation of Direct3D
- create and initialise Direct3D objects
- e.g. D3D rendering device, D3D vertex buffer
- COM interface of object used for
- object manipulation
- creation of other objects required for scene
rendering
11Initialisation
- Sample code
- LPDIRECT3D9 g_pD3D NULL
// object used to create D3DDevice - LPDIRECT3DDEVICE9 g_pd3dDevice NULL //
rendering device - g_pD3D Direct3DCreate9( D3D_SDK_VERSION )
- g_pD3D-gtCreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTE
XPROCESSING, d3dpp, g_pd3dDevice )
12Graphics Pipeline
- DirectX 8
- graphics pipeline
13Graphics Pipeline
- DirectX 9 graphics pipeline
14Polygonal shape modelling
- Polygon defined as group of vertices
- support for custom vertex type
- flexible vertex format (FVF) each vertex may
consist of - x, y, z coordinates
- colour
- vertex normal
- texture map coordinates
- light map coordinates
- vertex buffers for geometry storage
15Polygonal shape modelling
- Mesh data architecture
- vertex buffer
- face index buffer face has 3 vertex indexes
- attribute buffer
- materials, textures
- index into attributes table
- adjacency buffer face has index to adjacent faces
16Polygonal shape modelling
- Simple general procedure
- Specify vertex format
- Create array of vertices
- Store array in vertex buffer
- May explicitly specify drawing as
- point list, line list / strip, triangle list /
strip / fan - Alternative
- Use D3DX to load mesh from file
17Polygonal shape modelling
- Definition of custom vertex type sample code
- LPDIRECT3DVERTEXBUFFER9 g_pVB NULL //
buffer to hold vertices - // structure for custom vertex type
- struct CUSTOMVERTEX
-
- FLOAT x, y, z, rhw // vertex position
- D3DXVECTOR3 normal // surface normal for
vertex -
- // custom FVF (describes custom vertex structure)
- define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZD3DFVF_NOR
MAL)
18Polygonal shape modelling
- Definition of custom vertex type sample code
- LPDIRECT3DVERTEXBUFFER9 g_pVB NULL //
buffer to hold vertices - // structure for custom vertex type
- struct CUSTOMVERTEX
-
- FLOAT x, y, z, rhw // vertex position
- DWORD color // vertex color
-
- // custom FVF (describes custom vertex structure)
- define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHWD3DFVF_
DIFFUSE)
19Polygonal shape modelling
- Setting up vertex buffer sample code
- CUSTOMVERTEX g_Vertices
-
- 150.0f, 50.0f, 0.5f, 1.0f, 0xffff0000,
, // x, y, z, rhw, color - 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00,
, - 50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff,
, -
- g_pd3dDevice-gtCreateVertexBuffer(
3sizeof(CUSTOMVERTEX), -
0, D3DFVF_CUSTOMVERTEX, -
D3DPOOL_DEFAULT, g_pVB ) - VOID pVertices
- g_pVB-gtLock( 0, sizeof(g_Vertices),
(BYTE)pVertices, 0 ) ) - memcpy( pVertices, g_Vertices, sizeof(g_Vertices)
)
20Geometric Transformations
- Homogeneous matrices
- 4 x 4 transformation matrices
- declared using structures
- D3DMATRIX, D3DXMATRIX, ...
- vM notation, hence
- left-to-right matrix concatenation order
- see e.g. translation matrix on next slide
21Geometric Transformations
22Geometric Transformations
- Homogeneous matrices
- Sample functions
- D3DXMatrixIdentity(), D3DXMatrixScaling(),
D3DXMatrixTranslation(), D3DXMatrixRotationY(),
D3DXMatrixTransformation(), ... - D3DXMatrixMultiply(), D3DXMatrixTranspose(),
D3DXMatrixInverse(), ...
23Geometric Transformations
- Homogeneous matrices (sample code)
- Declaring and initialising scaling matrix
- D3DMATRIX scMat
- sX, 0.0f, 0.0f, 0.0f,
- 0.0f, sY, 0.0f, 0.0f,
- 0.0f, 0.0f, sZ, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f
24Geometric Transformations
- Homogeneous matrices (sample code)
- Declaring and setting translation matrix
- D3DXMATRIX trMat
- float tx 10.0, ty 0.0, tz 0.0
- D3DXMatrixTranslation (trMat, tx, ty, tz)
25Geometric Transformations
- Homogeneous matrices (sample code)
- Declaring and setting rotation matrix
- D3DXMATRIX rotMat
- float thetaX 30.0 / Pitch /, thetaY 0.0 /
Yaw /, thetaZ 45.0 / Roll / - / e.g. rotation about x-axis /
- D3DXMatrixRotationX(rotMat, thetaX)
- / e.g. rotation about all 3 axes /
- D3DXMatrixRotationYawPitchRoll(rotMat, thetaY,
thetaX, thetaZ)
26Geometric Transformations
- Homogeneous matrices (sample code)
- Matrix concatenation
- D3DXMATRIX resMat
- / declare / initialise matrices rotMat and
trMat / - / rotation followed by translation /
- D3DXMatrixMultiply(resMat, rotMat, trMat)
27Geometric Transformations
- Viewing pipeline
- Three main matrices in pipeline
- world matrix
- model-to-world transformation
- view matrix
- world-to-view transformation
- projection matrix
- projection and viewport transformation
28Geometric Transformations
- Viewing pipeline
- Transformation matrices
- automatic concatenation of pipeline matrices
- order in which matrices defined not important
- but combination order always world x view x
projection - flags used for setting matrix in rendering device
object - D3DTS_WORLD, D3DTS_VIEW, D3DTS_PROJECTION
29Geometric Transformations
- Viewing pipeline
- Specification of camera
- D3DXMatrixLookAtLH()
- defines position, view-plane normal, and view-up
vector - builds left-handed camera coordinate system
- sets left-handed, look-at matrix
- Specification of projection e.g. perspective
- D3DXMatrixPerspectiveFovLH(...)
- sets left-handed perspective projection matrix
30Geometric Transformations
- Viewing pipeline sample code
- (world matrix)
- // Set up world matrix
- D3DXMATRIX matWorld
- D3DXMatrixRotationY( matWorld,
timeGetTime()/150.0f ) // for rotation about
y-axis - g_pd3dDevice-gtSetTransform( D3DTS_WORLD,
matWorld )
31Geometric Transformations
- Viewing pipeline sample code
- (view matrix)
- // Set up our view matrix (give eye point,
look-at point, and up direction) - D3DXMATRIX matView
- D3DXMatrixLookAtLH( matView, D3DXVECTOR3(
0.0f, 3.0f,-5.0f ), - D3DXVECTOR3(
0.0f, 0.0f, 0.0f ), - D3DXVECTOR3(
0.0f, 1.0f, 0.0f ) ) - g_pd3dDevice-gtSetTransform( D3DTS_VIEW,
matView )
32Geometric Transformations
- Viewing pipeline sample code
- (projection matrix)
- // Set up projection matrix (give field of view,
aspect ratio, near and far clipping planes - D3DXMATRIX matProj
- D3DXMatrixPerspectiveFovLH( matProj,
D3DX_PI/4, 1.0f, 1.0f, 100.0f ) - g_pd3dDevice-gtSetTransform( D3DTS_PROJECTION,
matProj )
33Hidden Surface Removal
- Face culling
- enabling / disabling
- g_pd3dDevice-gtSetRenderState( D3DRS_CULLMODE,
D3DCULL_NONE ) // turn off culling - z-buffer
- enabling / disabling
- g_pd3dDevice-gtSetRenderState( D3DRS_ZENABLE, TRUE
) // turn on the zbuffer - initialisation
- specify value for clearing colour, depth, and
stencil buffers - g_pd3dDevice-gtClear(...)
34Illumination and Shading
- Specification of surface orientation / material
- specification of normals
- see polygonal shape modelling
- specification of material
- data type D3DMATERIAL9
- Specification of lights
- data type D3DLIGHT9
35Illumination and Shading
- Lighting enabling / disabling
- g_pd3dDevice-gtSetRenderState( D3DRS_LIGHTING,
FALSE ) // turn off D3D lighting - g_pd3dDevice-gtSetRenderState( D3DRS_AMBIENT,
0xffffffff ) // turn on ambient lighting
36Illumination and Shading
- Sample code (materials)
- // set up material (diffuse and ambient colours)
- D3DMATERIAL9 mtrl
- ZeroMemory( mtrl, sizeof(D3DMATERIAL9) )
- mtrl.Diffuse.r mtrl.Ambient.r
mtrl.Diffuse.g mtrl.Ambient.g 1.0f - mtrl.Diffuse.b mtrl.Ambient.b 0.0f
mtrl.Diffuse.a mtrl.Ambient.a 1.0f - g_pd3dDevice-gtSetMaterial( mtrl )
37Illumination and Shading
- Sample code (lights)
- // set up directional light and set
D3DRS_LIGHTING renderstate to enable lighting - D3DXVECTOR3 vecDir D3DLIGHT9 light
- ZeroMemory( light, sizeof(D3DLIGHT9) )
- light.Type D3DLIGHT_DIRECTIONAL
- light.Diffuse.r 1.0f light.Diffuse.g
1.0f light.Diffuse.b 1.0f - vecDir D3DXVECTOR3(1.0f, 1.0f, 0.5f)
- D3DXVec3Normalize( (D3DXVECTOR3)light.Direct
ion, vecDir ) - light.Range 1000.0f
- g_pd3dDevice-gtSetLight( 0, light )
g_pd3dDevice-gtLightEnable( 0, TRUE ) - g_pd3dDevice-gtSetRenderState( D3DRS_LIGHTING,
TRUE ) - g_pd3dDevice-gtSetRenderState( D3DRS_AMBIENT,
0x00202020 )
38Rendering and Display
- Sample code
-
- // Clear the backbuffer to a blue color
- g_pd3dDevice-gtClear( 0, NULL,
D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0
) - g_pd3dDevice-gtBeginScene() // Begin the
scene - SetupLights() // set up lights and
materials - SetupMatrices() // set up world, view, and
projection matrices - // Draw the triangles in the vertex buffer
- g_pd3dDevice-gtSetStreamSource( 0, g_pVB, 0,
sizeof(CUSTOMVERTEX) ) - g_pd3dDevice-gtSetFVF( D3DFVF_CUSTOMVERTEX )
- g_pd3dDevice-gtDrawPrimitive(
D3DPT_TRIANGLELIST, 0, 1 ) - g_pd3dDevice-gtEndScene() // End the scene
- g_pd3dDevice-gtPresent( NULL, NULL, NULL, NULL
) // send back-buffer content to display
39Rendering and Display
- Sample code
- Mesh display mode
- wireframe
- g_pd3dDevice-gtSetRenderState(D3DRS_FILLMODE,
D3DFILL_WIREFRAME) // wireframe display - solid
- g_pd3dDevice-gtSetRenderState(D3DRS_FILLMODE,
D3DFILL_SOLID) // solid mesh display
40Suggested Reading
- http//www.microsoft.com/directx/default.asp
41Summary
- DirectX set of APIs for MS Windows games and
multimedia applications - Sample features of Direct3D
- polygonal shape modelling flexible vertex format
- hidden surface removal
- back-face culling
- z-buffer / clipping
- illumination and shading
- ambient, diffuse, specular illumination models
- programmable vertex / pixel shaders