introduction to games graphics development in direct3d ross brown games technology teaching group...

51

Upload: joel-garrett

Post on 26-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader
Page 2: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Introduction to Introduction to Games Graphics Games Graphics Development in Development in Direct3DDirect3D

Introduction to Introduction to Games Graphics Games Graphics Development in Development in Direct3DDirect3D

Ross BrownRoss Brown

Games Technology Teaching Group LeaderGames Technology Teaching Group LeaderVisual and Media Computing Research Group Visual and Media Computing Research Group LeaderLeaderFaculty of Information TechnologyFaculty of Information TechnologyQueensland University of TechnologyQueensland University of Technology

Page 3: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Presentation ContentsPresentation Contents

AimsAims

Direct3D Graphics Direct3D Graphics PipelinePipeline

Modelling ObjectsModelling Objects

Animating ObjectsAnimating Objects

Lighting ObjectsLighting Objects

Projecting ObjectsProjecting Objects

Effects in HLSLEffects in HLSL

Page 4: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

AimsAims

Introduce you to the Introduce you to the main components of a main components of a DirectX 10 programDirectX 10 program

Provide pointers to Provide pointers to further informationfurther information

Page 5: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

OutcomesOutcomes

Basic principles of Direct3DBasic principles of Direct3DGraphics PipelineGraphics Pipeline

Setup and Utility LibrarySetup and Utility Library

Object ModellingObject Modelling

Object AnimationObject Animation

Scene LightingScene Lighting

ProjectionsProjections

HLSL HLSL

Page 6: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

ApproachApproach

Direct3D sample Direct3D sample SimpleHLSL10.cpp SimpleHLSL10.cpp “Tiny” deconstructed“Tiny” deconstructed

Mix of theory and Mix of theory and practicepractice

Will cover key Will cover key Direct3D callsDirect3D calls

Will not cover small Will not cover small detailsdetails

Page 7: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

AssumptionsAssumptions

Some mathsSome maths

Windows programming knowledgeWindows programming knowledge

Assuming familiarity with callback Assuming familiarity with callback functions and event based programmingfunctions and event based programming

GraphicsGraphics tutorial, not games engine tutorial, not games engine tutorialtutorial

VC++ knowledge – games VC++ knowledge – games lingua francalingua franca

Page 8: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Direct3D IntroductionDirect3D Introduction

Part of the Microsoft Part of the Microsoft Direct X multimedia Direct X multimedia architecturearchitecture

Also has reference Also has reference software renderersoftware renderer

DirectX 10 fully DirectX 10 fully integrated into Vistaintegrated into Vista

Exposes Graphics Exposes Graphics Processing Unit (GPU) Processing Unit (GPU) functionality via COM functionality via COM APIAPI

Renderer is Renderer is encapsulated in a encapsulated in a device objectdevice object

http://download.microsoft.com/download/2/2/b/22bfadd8-01b0-4fc4-942b-6e7b1635b214/http://download.microsoft.com/download/2/2/b/22bfadd8-01b0-4fc4-942b-6e7b1635b214/Intro_to_Direct3D10.pptIntro_to_Direct3D10.ppt

Page 9: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

RasterisationStage

GeometryStage

Direct3D Graphics PipelineDirect3D Graphics Pipeline

Is the theoretical Is the theoretical framework for framework for computer graphicscomputer graphics

Defines the processes Defines the processes enacted upon 3D enacted upon 3D geometry to produce geometry to produce a realistic imagea realistic image

Each API Each API implementation has implementation has its own versionits own version

Two stagesTwo stagesGeometry stageGeometry stage

Rasterisation stageRasterisation stage

InputAssembler

VertexShader

Rasterizer/Interpolator

PixelShader

GeometryShader

OutputMerger

Vertex Buffer

Index Buffer

Texture

Texture

Depth/Stencil

Texture

Render Target

Stream Output

http://download.microsoft.com/download/2/2/b/22bfadd8-01b0-4fc4-942b-6e7b1635b214/http://download.microsoft.com/download/2/2/b/22bfadd8-01b0-4fc4-942b-6e7b1635b214/Intro_to_Direct3D10.pptIntro_to_Direct3D10.ppt

Page 10: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Geometry StageGeometry Stage

3D Geometry handed 3D Geometry handed over the bus to over the bus to geometry stagegeometry stage

Transforms to these Transforms to these points to world points to world coordinatescoordinates

Applies lighting to the Applies lighting to the 3D points3D points

Project them to 2D Project them to 2D normalised normalised coordinatescoordinates

CPU

GeometryStage

BusGeometryGeometry

2D 2D Projected Projected PolygonsPolygons

Page 11: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Raster.Stage

FrameBuffer

Rasterisation StageRasterisation Stage

Transformed, lit and Transformed, lit and projected points from projected points from the geometry stage the geometry stage are now turned into are now turned into pixelspixels

Rasterisation turns Rasterisation turns polygons into a series polygons into a series of pixel x, y of pixel x, y coordinates within the coordinates within the framebufferframebuffer

Generates final imageGenerates final image

PixelsPixels

Page 12: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Direct3D Program StructureDirect3D Program Structure

A Direct3D program is thus a set of the A Direct3D program is thus a set of the following steps:following steps:1.1. Direct3D device setupDirect3D device setup

2.2. Modelling of objects as 3D polygonsModelling of objects as 3D polygons

3.3. Transformation of objects into world spaceTransformation of objects into world space

4.4. Lighting of objectsLighting of objects

5.5. Projection of objects to 2D windowProjection of objects to 2D window

6.6. Rasterisation of polygons into framebuffer as Rasterisation of polygons into framebuffer as pixelspixels

Page 13: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Direct3D Device SetupDirect3D Device Setup

DirectX utility toolkit removes complexityDirectX utility toolkit removes complexity

There are a number of key callbacksThere are a number of key callbacks

Other calls superfluous to talk’s Other calls superfluous to talk’s requirementsrequirements

WinMain sets up the callbacks on key WinMain sets up the callbacks on key eventsevents

OnD3D10CreateDevice - device is createdOnD3D10CreateDevice - device is created

MsgProc - handle windows’ event messagesMsgProc - handle windows’ event messages

OnD3D10FrameRender – render the geometry OnD3D10FrameRender – render the geometry to the screento the screen

Page 14: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

WINAPI wWinMain(…) // Program Entrance pointWINAPI wWinMain(…) // Program Entrance point……

DXUTSetCallbackMsgProc( DXUTSetCallbackMsgProc( MsgProc );MsgProc );

DXUTSetCallbackD3D10DeviceCreated( DXUTSetCallbackD3D10DeviceCreated( OnD3D10CreateDevice );OnD3D10CreateDevice );

……DXUTSetCallbackD3D10FrameRender( DXUTSetCallbackD3D10FrameRender(

OnD3D10FrameRender );OnD3D10FrameRender );……

Direct X Device SetupDirect X Device Setup

Page 15: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Modelling Objects as 3D PointsModelling Objects as 3D Points

Concept of Concept of CartesianCartesian coordinatescoordinates

We define vertices v We define vertices v in 3D space via in 3D space via Cartesian coordinate Cartesian coordinate system x, y, zsystem x, y, z

Positive z axis is Positive z axis is pointing into the pointing into the screenscreen

Direct3D is a Direct3D is a Left Left HandHand coordinate coordinate systemsystem

3D points are 3D points are generated by a generated by a modelling package, modelling package, program – or by hand program – or by hand for the seriously for the seriously masochistic masochistic

yy

zz

xx

vv

vvxx

ppzz

vvyy

Page 16: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Modelling of Objects as 3D Modelling of Objects as 3D MeshesMeshes

A simple cube can be A simple cube can be modelled with eight modelled with eight 3D points3D points

Two points make an Two points make an edgeedge

Multiple points form a Multiple points form a polygonpolygon

Polygons formed into Polygons formed into polyhedrons polyhedrons

Polyhedron is split into Polyhedron is split into mesh of trianglesmesh of triangles

yy

xx

v3v3v6v6

v7v7

v8v8

v1v1

v2v2

v4v4v5v5

e1e1

e2e2

e3e3

e4e4

e5e5e6e6

e7e7e8e8

e9e9

Page 17: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Polygon and Vertex NormalsPolygon and Vertex Normals

Each polygon or Each polygon or vertex is given a vertex is given a normal normal

Normals are direction Normals are direction vectors vectors n(x, y, z)n(x, y, z) indicating polygon indicating polygon orientationorientation

Used in lighting Used in lighting calculations and other calculations and other thingsthings

nn11

nn22

nn33

Page 18: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Polygon Texture CoordinatesPolygon Texture Coordinates

Each mesh is then Each mesh is then given 2D texture given 2D texture coordinates (s, t) to coordinates (s, t) to enable the draping of enable the draping of images onto the meshimages onto the mesh

t(0,1)t(0,1)

t(0,0)t(0,0)

t(1,1)t(1,1)

t(1,0)t(1,0)

tt

ssTexture Image SpaceTexture Image Space

Mesh in 3D SpaceMesh in 3D Space

Page 19: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Modelling of Objects as 3D Modelling of Objects as 3D MeshesMeshes

DirectX provides the X DirectX provides the X file format to store file format to store triangle meshes and triangle meshes and other informationother information

Direct3D commands Direct3D commands to create the mesh to create the mesh are called to allocate are called to allocate memory for that mesh memory for that mesh within the Direct3D within the Direct3D devicedevice

Done on the CPU sideDone on the CPU side

Mesh is handed to the Mesh is handed to the GPU to be renderedGPU to be rendered

TextureTexture Mesh & Mesh & NormalsNormals

Final Rendered Final Rendered MeshMesh

Page 20: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Mesh {Mesh { 4432;4432; -34.720058;-12.484819;48.088928;,-34.720058;-12.484819;48.088928;, -25.565304;-9.924385;26.239328;,-25.565304;-9.924385;26.239328;,……MeshNormals {MeshNormals { 4432;4432; -0.989571;-0.011953;-0.143551;,-0.989571;-0.011953;-0.143551;, -0.433214;-0.193876;-0.880192;,-0.433214;-0.193876;-0.880192;,……MeshTextureCoords {MeshTextureCoords { 4432;4432; 0.551713;0.158481;,0.551713;0.158481;, 0.442939;0.266364;,0.442939;0.266364;,……

Direct3D File Format - Tiny.xDirect3D File Format - Tiny.x

Page 21: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

// Create our vertex input layout// Create our vertex input layoutconst D3D10_INPUT_ELEMENT_DESC layout[] =const D3D10_INPUT_ELEMENT_DESC layout[] ={{{ L"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, …{ L"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, …{ L"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, …{ L"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, …{ L"TEXCOORD0", 0, DXGI_FORMAT_R32G32_FLOAT, …{ L"TEXCOORD0", 0, DXGI_FORMAT_R32G32_FLOAT, …};};

pd3dDevice->CreateInputLayout(… &g_pVertexLayout…pd3dDevice->CreateInputLayout(… &g_pVertexLayout…

// Load the mesh// Load the meshDXUTFindDXSDKMediaFileCch( str, … L"tiny\\tiny.x“ );DXUTFindDXSDKMediaFileCch( str, … L"tiny\\tiny.x“ );g_Mesh10.Create( pd3dDevice, str, … );g_Mesh10.Create( pd3dDevice, str, … );

Direct3D Mesh Creation CallsDirect3D Mesh Creation Calls

Page 22: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Extra Points on ModellingExtra Points on Modelling

Meshes are stored in complex data Meshes are stored in complex data structures called structures called Scene GraphsScene Graphs

Tree structures containing Tree structures containing MeshesMeshes, , TexturesTextures, , TransformationsTransformations

Scene graph is traversed and preprocessed Scene graph is traversed and preprocessed to optimise the transmission of geometry to optimise the transmission of geometry to the GPUto the GPU

Scene graph becomes a major component Scene graph becomes a major component of a Games Engineof a Games Engine

Tiny mesh demoTiny mesh demo

Page 23: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Transformation – Object to Transformation – Object to WorldWorld

Once the points have Once the points have crossed the bus to the crossed the bus to the GPU, the points GPU, the points undergo a process of undergo a process of geometric geometric transformationtransformation

Each object has been Each object has been modelled in its own modelled in its own Model SpaceModel Space – usually – usually the origin for the sake the origin for the sake of sanityof sanity

You need to perform You need to perform an an Model to WorldModel to World TransformTransform to make to make the object take its the object take its place in the Worldplace in the World

yy

zz

xx

yy

zz

xx

Model SpaceModel Space

World SpaceWorld Space

Page 24: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Transformation – World to Transformation – World to CameraCamera

Then you orient the Then you orient the world to make the world to make the world exist in camera world exist in camera spacespace

Called the Called the View View TransformTransform

Camera view direction Camera view direction and coordinate system and coordinate system is made to match x, y, is made to match x, y, z axisz axis

yy

zz

xx

yycc = = yy

zzcc = z = z

xxcc = = xx

yycc

xxcc zzcc

Page 25: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Animation of ObjectsAnimation of Objects

To animate is to “give To animate is to “give life”life”

Use parameterised Use parameterised transformations that transformations that change over time tchange over time t

Thus for each frame Thus for each frame the object has move a the object has move a little further, just like a little further, just like a moviemovie

Typical Typical transformations are transformations are Translate, RotateTranslate, Rotate and and ScaleScale

yy

zz

xx yy

zz

xxyy

zz

xx

TranslateTranslate

RotateRotate

ScaleScale

Page 26: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

DirectX MatricesDirectX Matrices

Transforms use a 4x4 Transforms use a 4x4 matrix – matrix – homogeneous homogeneous coordinatescoordinates

Transforms are Transforms are updated within updated within onFrameRenderonFrameRender

DirectX provides a DirectX provides a D3DXMatrixD3DXMatrix Class Class

World transformation World transformation is contained in is contained in mWorldmWorld

View transformation is View transformation is contained in contained in mViewmView

x’y’ =z’1

m11 m12 m13

m14

m21 m22 m23

m24

m31 m32 m33

m34

m41 m42 m43

m44

x+1y+1 =z+11

1 0 0 00 1 0 0 0 0 1 01 1 1 1

xyz1

xyz1

Translation ExampleTranslation Example

Page 27: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Lighting ObjectsLighting Objects

Light photons emanate from light sources Light photons emanate from light sources and bounce off into our eyesand bounce off into our eyes

Simulated on a computer to produce the Simulated on a computer to produce the lighting effects we perceivelighting effects we perceive

Computational models of lights are built Computational models of lights are built into GPU hardwareinto GPU hardware

Provide a number of parametersProvide a number of parametersNormalNormal

Light colour and directionLight colour and direction

Material properties – essentially colourMaterial properties – essentially colour

Page 28: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Lighting ModelLighting Model

Integrated into the Integrated into the model model

Vertex has light value Vertex has light value calculatedcalculated

Vertex forms a Vertex forms a position relative to the position relative to the light directionlight direction

Normal is used to Normal is used to calculate orientation calculate orientation of polygonof polygon

If polygon facing the If polygon facing the light then light then brightlybrightly lit lit

If polygon facing away If polygon facing away from light then from light then darkerdarker

NormalNormal

ColorColor

DirectionDirection

NormalNormal

NormalNormal

Page 29: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Polygon Material PropertyPolygon Material Property

Incident light value is multiplied with the Incident light value is multiplied with the Material PropertyMaterial Property, or reflectance of the , or reflectance of the object to produce its colour due to that object to produce its colour due to that lightlight

Summed for all light sources for final Summed for all light sources for final lighting valuelighting value

Properties specified in the model file – Properties specified in the model file – previous Slide Tiny.xprevious Slide Tiny.x

Page 30: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

D3DXVECTOR3 vLightDir[MAX_LIGHTS];D3DXVECTOR3 vLightDir[MAX_LIGHTS];D3DXVECTOR4 vLightDiffuse[MAX_LIGHTS]; // Light colourD3DXVECTOR4 vLightDiffuse[MAX_LIGHTS]; // Light colour

// Material properties of the mesh// Material properties of the meshD3DXCOLOR colorMtrlDiffuse(1.0f, 1.0f, 1.0f, 1.0f);D3DXCOLOR colorMtrlDiffuse(1.0f, 1.0f, 1.0f, 1.0f);D3DXCOLOR colorMtrlAmbient(0.35f, 0.35f, 0.35f, 0);D3DXCOLOR colorMtrlAmbient(0.35f, 0.35f, 0.35f, 0);

Direct3D LightingDirect3D Lighting

Page 31: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Projection of ObjectsProjection of Objects

After polygon mesh After polygon mesh has been transformed has been transformed and lit, it is and lit, it is ProjectedProjected to be shown on the 2D to be shown on the 2D screenscreen

Performed by the Performed by the projection matrix projection matrix mProjmProj

Many projections: Many projections: parallel, oblique...parallel, oblique...

Show Show PerspectivePerspective projection here as projection here as commonly used in commonly used in gamesgames

HorizonHorizon Vanishing Vanishing PointPoint

Perspective Perspective ProjectionProjection

Page 32: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Perspective ProjectionPerspective Projection

Perspective projection Perspective projection was discovered in the was discovered in the renaissance periodrenaissance period

Sense of depth by Sense of depth by shrinking objects x, y shrinking objects x, y by the distance in zby the distance in z

Encoded into 4x4 Encoded into 4x4 mProj mProj matrix as last matrix as last transform for vertices transform for vertices in polygon meshin polygon mesh

Vertices scaled to Vertices scaled to viewport, for viewport, for rasterisation into the rasterisation into the framebufferframebuffer

Tiny demo of Tiny demo of transformationstransformations

Projection Plane

zz = 0

yp

y

P (x, y, z)

z = d

P (x, y, z)

yp

Page 33: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Coordinate Spaces in Direct3D Coordinate Spaces in Direct3D PipelinePipeline

Mworld Mview Mproj Mclip

Clipping Mvs

Divide by W

PPmodelmodel

PPscreenscreen

Model SpaceModel Space World SpaceWorld Space Camera SpaceCamera Space Projection SpaceProjection Space

Clipping SpaceClipping Space Clipping SpaceClipping Space Homogeneous Screen Homogeneous Screen SpaceSpace

Screen SpaceScreen Space

Page 34: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

CModelViewerCamera g_Camera; CModelViewerCamera g_Camera; // Camera object// Camera object// contains viewing // contains viewing // matrices// matrices

D3DXMATRIX mWorldViewProjection;D3DXMATRIX mWorldViewProjection;D3DXMATRIX mWorld;D3DXMATRIX mWorld;D3DXMATRIX mView;D3DXMATRIX mView;D3DXMATRIX mProj;D3DXMATRIX mProj;

g_Camera.SetProjParams( D3DX_PI/4, fAspectRatio, … );g_Camera.SetProjParams( D3DX_PI/4, fAspectRatio, … );

// Get the projection & view matrix from the camera class// Get the projection & view matrix from the camera classmWorld = g_mCenterMesh * *g_Camera.GetWorldMatrix();mWorld = g_mCenterMesh * *g_Camera.GetWorldMatrix();mProj = *g_Camera.GetProjMatrix();mProj = *g_Camera.GetProjMatrix();mView = *g_Camera.GetViewMatrix();mView = *g_Camera.GetViewMatrix();

mWorldViewProjection = mWorld * mView * mProj;mWorldViewProjection = mWorld * mView * mProj;

Direct3D ProjectionDirect3D Projection

Page 35: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Rasterisation of PolygonsRasterisation of Polygons

Concludes the Geometry Stage of pipelineConcludes the Geometry Stage of pipeline

We now enter the final We now enter the final RasterisationRasterisation stage stage of the DirectX pipelineof the DirectX pipeline

So far all the work we have looked at has So far all the work we have looked at has been performed in a been performed in a real valuedreal valued mathematical spacemathematical space

We now have to make those real valued We now have to make those real valued meshes appear within the meshes appear within the discrete valueddiscrete valued framebuffer within the video memoryframebuffer within the video memory

Page 36: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Triangle RasterisationTriangle Rasterisation

The hardware takes The hardware takes the triangles and turns the triangles and turns them into scanlines to them into scanlines to be put into the be put into the framebufferframebuffer

This is why triangles This is why triangles are used – good are used – good mathematical mathematical properties for properties for rasterisationrasterisation

Light colour calculated Light colour calculated in the geometry stage in the geometry stage are linearly are linearly interpolated across its interpolated across its surfacesurface

yy

xx

Light Colour 1Light Colour 1

Light Colour 2Light Colour 2

Light Colour 3Light Colour 3

Pixels GeneratedPixels Generated

Page 37: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Final Texture and Lighting Pixel Final Texture and Lighting Pixel ValueValue

This is where the This is where the texture is sampled, so texture is sampled, so that the surface of the that the surface of the triangle has the image triangle has the image appear upon itappear upon it

So the final colour of So the final colour of the pixel is a the pixel is a combination of the combination of the lighting value and the lighting value and the texture valuetexture value

yy

Page 38: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

HLSL – Short for Eye Candy!HLSL – Short for Eye Candy!

Previously we have Previously we have described the described the components of the components of the DirectX Graphics DirectX Graphics Pipeline as fixed Pipeline as fixed functionsfunctions

Modern video cards Modern video cards from NVIDA and ATI from NVIDA and ATI are now are now programmable via programmable via small functions called small functions called ShadersShaders

This programs are This programs are inserted into the inserted into the geometry and geometry and rasterisation stages of rasterisation stages of the pipelinethe pipeline

InputAssembler

VertexShader

Rasterizer/Interpolator

PixelShader

GeometryShader

OutputMerger

Vertex Buffer

Index Buffer

Texture

Texture

Depth/Stencil

Texture

Render Target

Stream Output

V(x,y,z)V(x,y,z)

V’(x,y,z)V’(x,y,z)

P(x,y)P(x,y)

Page 39: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

HLSL – Just for the Effect!HLSL – Just for the Effect!

Thus the previous stages described are Thus the previous stages described are performed by shadersperformed by shaders

More formally they are known as More formally they are known as Kernel Kernel FunctionsFunctions, and the processors are called , and the processors are called Stream ProcessorsStream Processors

For a while they had to be programmed in For a while they had to be programmed in assembler – first Xboxassembler – first Xbox

Now they are programmed in a number of Now they are programmed in a number of high-level C++ like languageshigh-level C++ like languages

HLSLHLSL is one of those offerings, jointly is one of those offerings, jointly developed by Microsoft and NVIDIAdeveloped by Microsoft and NVIDIA

Page 40: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

HLSL - StructureHLSL - Structure

Two forms of shaders existTwo forms of shaders existVertex – acting on each vertex that is loaded Vertex – acting on each vertex that is loaded into the pipeline, output passed to the pixel into the pipeline, output passed to the pixel shadershader

Pixel – acting on each pixel that is generated in Pixel – acting on each pixel that is generated in the rasterisation stage output is placed into the rasterisation stage output is placed into framebufferframebuffer

Each stored in a .fx file formatEach stored in a .fx file format

Loaded into the Direct3D device on the Loaded into the Direct3D device on the GPU and compiled ready for executionGPU and compiled ready for execution

DirectX 10 is DirectX 10 is ALLALL shaders… shaders…

Page 41: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

HLSLHLSL

Set the parameters to pass across the busSet the parameters to pass across the busFirst get a pointer to the parameter structuresFirst get a pointer to the parameter structures

Set them before you render the meshSet them before you render the mesh

Shaders are automatically called when a Shaders are automatically called when a polygon is renderedpolygon is rendered

Can switch between shaders for visual Can switch between shaders for visual effectseffects

Allows for multipass algorithms via a Allows for multipass algorithms via a layered approach – like Photoshoplayered approach – like Photoshop

Page 42: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

HLSLHLSL

For our example “Tiny”For our example “Tiny”Vertex shader implements transforms and Vertex shader implements transforms and lighting set up by DirectX API on the CPU sidelighting set up by DirectX API on the CPU side

Pixel shader samples the texture used for the Pixel shader samples the texture used for the surface and blends this with the lighting value surface and blends this with the lighting value to give a final pixel value in the framebufferto give a final pixel value in the framebuffer

Page 43: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

// Load and compile shaders as effects// Load and compile shaders as effects

DXUTFindDXSDKMediaFileCch( str, … DXUTFindDXSDKMediaFileCch( str, … L"BasicHLSL10.fx" ));L"BasicHLSL10.fx" ));

D3DX10CreateEffectFromFile( str, D3DX10CreateEffectFromFile( str, … … pd3dDevice, pd3dDevice, … … &g_pEffect10));&g_pEffect10));

// Obtain variables// Obtain variablesg_pLightDir = g_pEffect10->GetVariableByName( g_pLightDir = g_pEffect10->GetVariableByName(

"g_LightDir" )->AsVector();"g_LightDir" )->AsVector();

HLSL – Shader Load HLSL – Shader Load (OnD3D10CreateDevice)(OnD3D10CreateDevice)

Page 44: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

g_pLightDir->SetRawValue( vLightDir, 0, g_pLightDir->SetRawValue( vLightDir, 0, sizeof(D3DXVECTOR3)*MAX_LIGHTS ) );sizeof(D3DXVECTOR3)*MAX_LIGHTS ) );

g_pLightDiffuse->SetFloatVectorArray( g_pLightDiffuse->SetFloatVectorArray( (float*)vLightDiffuse, 0, MAX_LIGHTS ) );(float*)vLightDiffuse, 0, MAX_LIGHTS ) );

g_pmWorldViewProjection->SetMatrix( g_pmWorldViewProjection->SetMatrix( (float*)&mWorldViewProjection ) );(float*)&mWorldViewProjection ) );

g_pmWorld->SetMatrix( (float*)&mWorld ) );g_pmWorld->SetMatrix( (float*)&mWorld ) );

g_pfTime->SetFloat( (float)fTime ) );g_pfTime->SetFloat( (float)fTime ) );

g_pnNumLights->SetInt( g_nNumActiveLights ) );g_pnNumLights->SetInt( g_nNumActiveLights ) );

HLSL – Set Parameters HLSL – Set Parameters ((onFrameRenderonFrameRender))

Page 45: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

pd3dDevice->IASetInputLayout( g_pVertexLayout );pd3dDevice->IASetInputLayout( g_pVertexLayout );

// Apply the technique contained in the effect // Apply the technique contained in the effect pRenderTechnique->GetDesc( &TechDesc );pRenderTechnique->GetDesc( &TechDesc );

ID3D10EffectPass *pCurrentPass = ID3D10EffectPass *pCurrentPass = pRenderTechnique->GetPassByIndex( iPass );pRenderTechnique->GetPassByIndex( iPass ); pCurrentPass->Apply( 0 );pCurrentPass->Apply( 0 );

// Render the mesh with the applied technique// Render the mesh with the applied techniqueg_Mesh10.Render( pd3dDevice );g_Mesh10.Render( pd3dDevice );

HLSL – Rendering with Shaders HLSL – Rendering with Shaders (onFrameRender)(onFrameRender)

Page 46: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

VS_OUTPUT RenderSceneVS( float4 vPos : POSITION,VS_OUTPUT RenderSceneVS( float4 vPos : POSITION, float3 vNormal : NORMAL,float3 vNormal : NORMAL, float2 vTexCoord0 : …float2 vTexCoord0 : …

Output.Position = mul(vAnimatedPos, Output.Position = mul(vAnimatedPos, g_mWorldViewProjection);g_mWorldViewProjection); vTotalLightDiffuse += g_LightDiffuse[i] * vTotalLightDiffuse += g_LightDiffuse[i] * max(0,dot(vNormalWorldSpace, g_LightDir[i]));max(0,dot(vNormalWorldSpace, g_LightDir[i])); Output.Diffuse.rgb = g_MaterialDiffuseColor * Output.Diffuse.rgb = g_MaterialDiffuseColor * vTotalLightDiffuse + g_MaterialAmbientColor *vTotalLightDiffuse + g_MaterialAmbientColor * g_LightAmbient; g_LightAmbient; return Output; // Pass light and coordinate info to return Output; // Pass light and coordinate info to // Pixel shader// Pixel shader

HLSL – Vertex Shader HLSL – Vertex Shader (HLSL10.fx)(HLSL10.fx)

Page 47: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

PS_OUTPUT RenderScenePS( VS_OUTPUT In,PS_OUTPUT RenderScenePS( VS_OUTPUT In, … … ) )

// Get value from texture and multiply with light// Get value from texture and multiply with light

Output.RGBColor = g_MeshTexture.Sample(Output.RGBColor = g_MeshTexture.Sample( MeshTextureSampler, In.TextureUV) MeshTextureSampler, In.TextureUV) * In.Diffuse;* In.Diffuse; return Output; // Final value in frame bufferreturn Output; // Final value in frame buffer

HLSL – Pixel Shader (HLSL10.fx)HLSL – Pixel Shader (HLSL10.fx)

Page 48: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

Final OutputFinal Output

Page 49: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

More Eye Candy!More Eye Candy! www.nvidia.comwww.nvidia.com

Page 50: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

ResourcesResources

www.microsoft.com/directxwww.microsoft.com/directx

developer.nvidia.comdeveloper.nvidia.com

developer.ati.comdeveloper.ati.com

www.gamedev.netwww.gamedev.net

Beginning Direct3D Game Programming, Beginning Direct3D Game Programming, Second Edition (Game Programming) - Second Edition (Game Programming) - Wolfgang EngelWolfgang Engel

Shader X Series – Wolfgang EngelShader X Series – Wolfgang Engel

Page 51: Introduction to Games Graphics Development in Direct3D Ross Brown Games Technology Teaching Group Leader Visual and Media Computing Research Group Leader

© 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.

The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation.

MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.