Title: Advanced Game Technology CMPCD3026 CMPSEM044
1Advanced Game TechnologyCMPCD3026-CMPSEM044
Abdennour El Rhalibi Room 723 a.elrhalibi_at_livjm.ac
.uk
2Course Details (Attempt)
- 3D Game Engines Components
- DirectX D3D, 3D Modelling and Rendering
- Meshes, Level Loading and Editing, LOD
- Camera Setting and Animation
- Terrain Rendering
- Spatial Data structure BSP and PVS
- NPC Behaviour and 3D PathFinding A, Flocking,
Scripting - 3D Collision Detection
- Game networking Issues Architecture, Protocol,
Event Synchronisation - Shading languages
- Introduction to Console Programming
33D Graphics with Direct3D 9Lecture 2
4The Features Of DirectX
- Today's games use DirectX and so support many if
not all of the graphical features provided by
this SDK. - Polygons
- Shading
- Textures
- Lights
- Environmental Effects
- Hardware
5D3D Architecture
- Microsoft Direct3D provides device independence
through the hardware abstraction layer (HAL) - HAL is a device-specific interface
- Application doesnt directly access video card
63D Rendering systems
- The modeling-rendering paradigm
- Graphics pipeline
- At the head of the pipeline, all of a model's
vertices are declared relative to a local
coordinate system. - The first stage of the geometry pipeline
transforms a model's vertices from their local
coordinate system to a coordinate system that is
used by all the objects in a scene. - In the next stage, the vertices that describe
your 3-D world are oriented with respect to a
camera. - The next stage is the projection transformation.
In this part of the pipeline, objects are usually
scaled with relation to their distance from the
viewer in order to give the illusion of depth to
a scene. - In the final part of the pipeline, any vertices
that will not be visible on the screen are
removed. This process is called clipping. After
clipping, the remaining vertices are scaled
according to the viewport parameters and
converted into screen coordinates. The resulting
vertices - seen on the screen when the scene is
rasterized - exist in screen space.
7Rendering vs. Rasterization
- Many times these words are used interchangeable,
but there are differences - Rendering
- generating a pixel-by-pixel image based on the
objects (primitives, bitmaps, etc.) currently in
the scene and how they interact - Rasterization
- the converting of geometric entities (primitives)
to pixel assignments - also called scan conversion
- the raster is the array (data structure) of
picture elements (primitives) - Bottom-line
- rendering is a higher-order operation that uses
rasterization
8Summary 20 easy steps ?
- Setup
- Direct3DCreate9()
- GetDeviceCaps()
- GetAdapterCount() and GetAdapterModeCount()
- D3DPRESENT_PARAMETERS
- CreateDevice()
- Resources
- CreateVertexBuffer
- CreateIndexBuffer
- CreateTexture
- CreateVertexShader()
- CreatePixelShader()
- Rendering
- Begin/EndScene()
- SetMaterial(...)
- SetLight(...)
- LightEnable(...)
- SetViewport(...)
- SetTransform(...) - World, view and projection
9Direct3D object
- Direct3D is implemented through Component Object
Model (COM) objects and interfaces. - Direct3D object is the first object that your
application creates and the last object that your
application releases - Functions for enumerating and retrieving
capabilities of a Direct3D device are accessible
through the Direct3D object. This enables
applications to select devices without creating
them - EXLPDIRECT3D9 g_pD3D NULL
- if( NULL (g_pD3D Direct3DCreate9(D3D_SDK_VER
SION))) - return E_FAIL
10D3D Device
- D3D Device is the rendering component of Direct3D
- Encapsulates and stores the rendering state
- Performs transformations and lighting operations
and rasterizes an image to a surface. - Two types of devices HAL and software (HEL)
- You can think of these devices as two separate
drivers, one is a software/reference driver and
the other is the hardware driver - Can have three different vertex processing modes
software vertex processing, hardware vertex
processing, and mixed vertex
11Example Create a D3D Device
- Refer to to Direct3D Tutorial 1
- Step 1. Create a window
- Step 2. Init Direct3D
- Step 3. Handle system messages
- Step 4. Render and display a scene
- Step 5. Shut down and clean up
12Basic Concepts
- DX uses Left Hand Coordinate System. OpenGL uses
Right Hand Coordinate System - Primitives are built using vertices, stored in
vertex buffers - Different types of primitives D3DPT_POINTLIST,
D3DPT_LINELIST, D3DPT_LINESTRIP,
D3DPT_TRIANGLELIST, D3DPT_TRIANGLESTRIP, D3DPT_T
RIANGLEFAN - Each face has a normal associated with it.
Normals are vectors perpendicular to the faces
surface and point the way the face is facing - Face normals are specified using order of
vertices, Clock-Wise or Counter-Clock-Wise - Faces with normal facing away from the camera are
usually backfaced culled
133D Primitives
- 3-dimensional shapes can be composed of 2D
(coplanar) primitives in space (usually
triangles). - Each triangle (or coplanar polygon) on a 3D shape
is called a face. - Smooth surfaces can be achieved by enough
primitives and correct use of shading.
14Making other shapes using Triangles
- How many vertices, edges, triangles to make a
cube? - 8 vertices?
- 18 edges
- 12 triangles
- How about an x-wing fighter?
- 3099 vertices
- 9190 edges
- 6076 triangles
http//gts.sourceforge.net/samples.html
15Graphics Data
- While a scene is being rendered, where should the
vertex data be stored? - storing it in system memory (RAM) requires slow
copying to the video card - E.g. by simply declaring vertices
- default behavior in Direct3D for vertices
- storing it directly in the graphics card is best
- this is where vertex buffers come in
16Vertex Buffers
- Vertex buffers, represented by the
IDirect3DVertexBuffer9 interface, are memory
buffers that contain vertex data - Vertex buffers can contain any vertex type -
transformed or untransformed, lit or unlit - The flexibility of vertex buffers make them ideal
staging points for reusing transformed geometry - Ex Rendering models that use multiple textures
the geometry is transformed only once, and then
portions of it can be rendered as needed,
interleaved with the required texture changes - Vertex buffer is described in terms of its
capabilities if it can exist only in system
memory, if it is only used for write operations,
and the type and number of vertices it can
contain traits described by D3DVERTEXBUFFER_DESC
17Rendering from VB
- Set the stream source by calling the
IDirect3DDevice9SetStreamSource method - d3dDevice-gtSetStreamSource( 0, g_pVB,
sizeof(CUSTOMVERTEX) ) - Inform Direct3D which vertex shader to use by
calling the IDirect3DDevice9SetVertexShader or
IDirect3DDevice9SetFVF method - d3dDevice-gtSetFVF( D3DFVF_CUSTOMVERTEX )
- After setting the stream source and vertex
shader, any draw methods will use the vertex
buffer - d3dDevice-gtDrawPrimitive( D3DPT_TRIANGLELIST, 0,
1 )
18Dealing w/ Vertex Buffer
- Creating Vertex Buffer
- //g_pVB is a variable of type LPDIRECT3DVERTEXBUFF
ER9 - if( FAILED( d3dDevice-gtCreateVertexBuffer(
3sizeof(CUSTOMVERTEX), 0 / Usage /,
D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT,
g_pVB ) ) ) - return E_FAIL
- Accessing Vertex Buffer. Need to lock because VB
can be in device memory - // Lock the buffer to gain access to the vertices
- VOID pVertices
- if(FAILED(g_pVB-gtLock(0, sizeof(g_Vertices),
(BYTE)pVertices, 0 ) ) ) - return E_FAIL
- memcpy(pVertices, g_Vertices, sizeof(g_Vertices))
- g_pVB-gtUnlock()
- Make sure your application properly accesses the
allocated memory. Otherwise, you risk rendering
that memory invalid.
19Making a simple cube in Direct3D
- For a cube, well need 12 triangles
- we could declare construct 36 vertices, each
with its own coordinates - well see a more efficient means using only 8
vertices - load into vertex buffer
- at draw time, provide vertex buffer as triangle
list
20Index Buffers
- Constructing 36 vertices to create a cube using
triangles!! - inefficient use of memory
- a real cube only has 8 vertices (one for each
cube corner) - In a large-scale application, redundant data is
costly - Index buffers are a mechanism for sharing vertex
data among primitives - a buffer that stores indices into vertex data
- What does that mean?
- an array (or vector, etc.) that describes shapes
(e.g., triangles) using indices of constructed
vertices in vertex buffer - groups of 3 indices describe one triangle
21Example, create the index buffer
- vertices of a unit cube
- // vertices of a unit cube
- vertices0 Vertex(-1.0f, -1.0f, -1.0f)
- vertices1 Vertex(-1.0f, 1.0f, -1.0f)
- vertices2 Vertex( 1.0f, 1.0f, -1.0f)
- vertices3 Vertex( 1.0f, -1.0f, -1.0f)
- vertices4 Vertex(-1.0f, -1.0f, 1.0f)
- vertices5 Vertex(-1.0f, 1.0f, 1.0f)
- vertices6 Vertex( 1.0f, 1.0f, 1.0f)
- vertices7 Vertex( 1.0f, -1.0f, 1.0f)
22A Cube Index Buffer Solution
- // define the triangles of the cube
- // front side
- indices0 0 indices1 1 indices2
2 - indices3 0 indices4 2 indices5
3 - // back side
- indices6 4 indices7 6 indices8
5 - indices9 4 indices10 7 indices11
6 - // left side
- indices12 4 indices13 5 indices14
1 - indices15 4 indices16 1 indices17
0 - // right side
- indices18 3 indices19 2 indices20
6 - indices21 3 indices22 6 indices23
7 - // top
- indices24 1 indices25 5 indices26
6
23Advantages Disadvantages of Index Buffers
- What are the advantages?
- more efficient memory management
- What are the disadvantages?
- vertices have to share color data
- vertices have to share normal data
- may result in lighting errors
24How about the same cube multiple times?
- We can construct a single vertex buffer
- Set the world transform for the first cube
- Draw the first cube using our vertex buffer
- Set the world transform for the next cube
- Draw the next cube using our vertex buffer
25Additional Primitives
26Additional Primitives (2)
27Additional Primitives (3)
28Additional Primitives (4)
http//www.codesampler.com/d3dbook/chapter_05/chap
ter_05.htm
29D3D Primitives
- D3DPT_POINTLIST, D3DPT_LINELIST, D3DPT_LINESTRIP,
D3DPT_TRIANGLELIST, D3DPT_TRIANGLESTRIP, D3DPT_T
RIANGLEFAN
30Example Rendering Vertices
- Refer to to Direct3D Tutorial 2
- Step 1. Setup vertex buffer (in C also define
custom vertex type) - Step 2. Render
31Conclusion
- We have quickly overviewed some basic techniques
for 3D graphics in any system. - Use the Direct3D tutorials and documentation to
familiarize yourself with the APIs.