vertices finalised and lighting begun

46
Vertices Finalised and Lighting Begun Paul Taylor 2010

Upload: kacia

Post on 23-Feb-2016

31 views

Category:

Documents


0 download

DESCRIPTION

Vertices Finalised and Lighting Begun. Paul Taylor 2010. World, View and Projection?. Per Object. Local. World. Per Render. View. Projection. The Vertex Structure . D3D10_INPUT_ELEMENT_DESC colourVertexLayout [] = { // Vertex position { " POSITION", // SemanticName - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Vertices Finalised and Lighting Begun

Vertices Finalised and Lighting Begun

Paul Taylor 2010

Page 2: Vertices Finalised and Lighting Begun

World, View and Projection?

World

View

Projection

Local

Per Object

Per Render

Page 3: Vertices Finalised and Lighting Begun

The Vertex Structure D3D10_INPUT_ELEMENT_DESC colourVertexLayout[] ={// Vertex position{ "POSITION", // SemanticName0, // SemanticIndex used for matrix, etcDXGI_FORMAT_R32G32B32_FLOAT, // Format0, // InputSlot 0 – 15, allows up to 16 vertex buffers per render0, // AlignedByteOffsetD3D10_INPUT_PER_VERTEX_DATA, // InputSlotClass0 },// Vertex color{ "COLOR", // SemanticName0, // SemanticIndexDXGI_FORMAT_R32G32B32A32_FLOAT, 0, // InputSlot12, // AlignedByteOffsetD3D10_INPUT_PER_VERTEX_DATA, // InputSlotClass0 },

};

http://msdn.microsoft.com/en-us/library/bb173059%28v=VS.85%29.aspx

Page 4: Vertices Finalised and Lighting Begun

Why Semantic Names?struct PS_INPUT{

float4 Pos : SV_POSITION; float4 Color : COLOR0;};

//--------------------------------------------------------------------------------------// Vertex Shader//--------------------------------------------------------------------------------------PS_INPUT VS( float4 Pos : POSITION, float4 Color : COLOR ){

PS_INPUT psInput;

Pos = mul( Pos, World ); Pos = mul( Pos, View ); psInput.Pos = mul( Pos, Projection );

psInput.Color = Color;

return psInput; }

Page 5: Vertices Finalised and Lighting Begun

Using SemanticIndex{ L"mTransform", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0,

D3D10_INPUT_PER_INSTANCE_DATA, 1 }, { L"mTransform", 1, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 16,

D3D10_INPUT_PER_INSTANCE_DATA, 1 },{ L"mTransform", 2, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 32,

D3D10_INPUT_PER_INSTANCE_DATA, 1 }, { L"mTransform", 3, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 48,

D3D10_INPUT_PER_INSTANCE_DATA, 1 },}

Per Instance Data?

http://msdn.microsoft.com/en-us/library/ee416415%28VS.85%29.aspx

Page 6: Vertices Finalised and Lighting Begun

Semantics

• Each Vertex Layout must have matching Semantic names with the Shader Technique used:

ShaderEffect->GetTechniqueByName• You can have multiple effects in a shader file,

but it’s easier to have multiple shaders

Page 7: Vertices Finalised and Lighting Begun

Understanding Vertex BuffersVertex Structure

The format of each Vertex passed to the Vertex Shader

Element DescriptorArray of

Element Descriptors(For each Element in a Vertex)

Element Descriptor

Element Descriptor

Position( 3x Float)

Colour ( 4x Float)

Normal( 3x Float)

Render Technique Render Pass Pointer

Render Effect

Page 8: Vertices Finalised and Lighting Begun

Create an Input Layout

• For each type of Vertex Buffer you will need a matching Input Layout

Array of Element Descriptors

Render Pass Pointer

CreateInputLayout(...) InputLayout*

Page 9: Vertices Finalised and Lighting Begun

Using Vertex Layouts

• This will enable the layoutfor each type of buffer youuse. (Type being elementstructure)*Topology is independent

IASetInputLayout(...)

InputLayout*

Page 10: Vertices Finalised and Lighting Begun

The next issue is using Vertex Buffers

We know a Vertex Buffer is just a big list of vertices

• This list needs to match the Vertex Layout

Page 11: Vertices Finalised and Lighting Begun

(Vertex) Buffer Descriptor

• How big is the buffer• How is it to be accessed (CPU vs GPU)• How it is to be used

Page 12: Vertices Finalised and Lighting Begun

Create a Vertex Buffer (Per Object)

A Vertex buffer can hold different types of vertices, but usually holds just one type.

Usually the same Vertex type is used for many different components of an object

Sphere, Arms, Legs, Etc

CreateBuffer(...)

Buffer Descriptor

PC Data Buffer

GPU Buffer Pointer

Page 13: Vertices Finalised and Lighting Begun

Now we can Draw Vertex Buffers

• Draw(...)– Vertex Count– Start Vertex Location

• The means that Multiple Topologies can be drawn from a single buffer

• For a contiguous object, this is logical

Page 14: Vertices Finalised and Lighting Begun

Topology Settings

This is how the Polygons are generated from your Data Buffer

The important ones for now are:Point ListLine ListTriangle ListLine StripTriangle Strip

Page 15: Vertices Finalised and Lighting Begun

Point ListA B C D E F G H

AB

C

DE

F

G

H

Page 16: Vertices Finalised and Lighting Begun

Line ListA B C D E F G H

AB

C

DE

F

G

H

Page 17: Vertices Finalised and Lighting Begun

Triangle ListA B C D E F G H

A

B

C

D

EF

G

H

?

Page 18: Vertices Finalised and Lighting Begun

Line StripA B C D E F G H

A

B

C

D

EF

G

H

Page 19: Vertices Finalised and Lighting Begun

Triangle StripA B C D E F G H

A

B

C

D

EF

G

H

What about LH VS RH Winding?

Page 20: Vertices Finalised and Lighting Begun

Index Buffers

We store each index as a DWORDWith larger Vertices we can save a lot of room

on both the Computer and the Video Card

Create a buffer with D3D10_BIND_INDEX_BUFFER

DrawIndexed(...)

Page 21: Vertices Finalised and Lighting Begun

In Summary we have:

• Vertex Layouts (Per Vertex Type)• Vertex Buffers (~Per Object)• Topology Settings (As needed)

• Possibly Index Buffers (~Per Object)

Page 22: Vertices Finalised and Lighting Begun

Vertex StructureThe format of each Vertex

passed to the Vertex Shader

Element DescriptorArray of Element Descriptors

(For each Element in a Vertex)

Element Descriptor

Element Descriptor

Position( 3x Float)

Colour ( 4x Float)

Normal( 3x Float)

Render Technique

Render Pass PointerRender Effect

.FX File

CreateInputLayout(...) InputLayout*IASetInputLayout(...)

CreateBuffer(...)

Buffer DescriptorPC Data Buffer

GPU Buffer Pointer

IASetVertexBuffers(...)

IASetPrimitiveTopology(...) Draw(...)

Pass->Apply()

Page 23: Vertices Finalised and Lighting Begun

What can we use a 3D Texture for?

• Terrain– At high vertical displacement very little stretch

• Other Solutions to terrain:– Multi-texturing

Page 24: Vertices Finalised and Lighting Begun

OpenGL 4.0

• Supports a full parity of Dx11 features– Main deficiencies in GL3 were:• Tessellation Shaders• Compute Shaders

Page 25: Vertices Finalised and Lighting Begun

The Problem with lighting

• I can’t teach you exactly how to light your game.– In Dx9 you did it their way– In Dx10 you do it your way

• In essence we will cover how to perform lighting as typically done in games.– It will be up to you to merge these concepts with

future rendering techniques

Page 26: Vertices Finalised and Lighting Begun

Basic Lighting and Shading

Page 27: Vertices Finalised and Lighting Begun

The Important Properties of Light

• It can Reflect, bend, spread and scatter

Page 28: Vertices Finalised and Lighting Begun

Most Common Light Sources

• Global Illumination• Light Located at Infinity (Distant Light)• Light Located Locally– Uniform Light (Ambient)– Point Source • Intensity is L(p,P0) = (1 / |p-p0|2 ) L(P0)

– Spotlights (Directional Lights)

Page 29: Vertices Finalised and Lighting Begun

Typical Light Types

PointSpotlightAmbientDistant

Page 30: Vertices Finalised and Lighting Begun

Point

home.elka.pw.edu.pl/.../general/General.html

Page 31: Vertices Finalised and Lighting Begun

Spot

home.elka.pw.edu.pl/.../general/General.html

Page 32: Vertices Finalised and Lighting Begun

Ambient (Area)

home.elka.pw.edu.pl/.../general/General.html

Page 33: Vertices Finalised and Lighting Begun

Distant (Sun / Moon)

home.elka.pw.edu.pl/.../general/General.html

Page 34: Vertices Finalised and Lighting Begun

Physical Light

Bidirectional Reflection Distribution Function– (BRDF)

This is based on 5 Variables:– Frequency– Source Vector (x,y)– Output Vector (x,y)

• It also requires a ‘real’ surface

Page 35: Vertices Finalised and Lighting Begun

This is a hugely expensive calculation

• Most light consists of a large frequency Range

• In games we have many fast ways of creating the common effects of light

Page 36: Vertices Finalised and Lighting Begun

Firstly we need to get rid of the ‘spectrum’

• The human Eye recognises colours through 3 types of cones– We exploit this behaviour by directly addressing

the cones

http://www.unenergy.org/index.php?p=1_180_CPV---Concentrated-PVhttp://www.atmos.ucla.edu/~fovell/AS3/theory_of_color.html

Page 37: Vertices Finalised and Lighting Begun

By exploiting this behaviour we can define the colour of light as

• Red• Green• Blue• Varying the levels of Red, Green and Blue can

make the eye ‘see’ every colour of the spectrum.

Page 38: Vertices Finalised and Lighting Begun

• The downside to this simplification is that our model of light looses the ability to separate during reflections/refractions.

• As a solution to this we can either use multiple lights, or use explicit functions to ‘create’ light separation

• For offline rendering you may even consider doing a BRDF Rendering using a more complex Lighting method

http://www.tufts.edu/as/tampl/projects/micro_rs/theory.html

Page 39: Vertices Finalised and Lighting Begun

Basic Lighting in DirectX

http://www.cs.virginia.edu/~gfx/Courses/2002/RealTime.fall.02/Cg/CG%20Effects%20Explained/Cg%20Effects%20Explained.htm

Page 40: Vertices Finalised and Lighting Begun

Specular Reflection

• Specular– The more specular a surface, the move light is

reflected very closely to the angle of reflection– A mirror is a near perfect Specular Surface

http://www.indigorenderer.com/joomla/forum/files/specular_material_test_01_168.jpg

Page 41: Vertices Finalised and Lighting Begun

Diffuse Reflection(Lambertian reflection)

http://torcode.com/http://www.glenspectra.co.uk/SiteResources/Data/Templates/1product.asp?DocID=389&v1ID=

Page 42: Vertices Finalised and Lighting Begun

Emissive

• It Glows!

http://www.gennetten-concreteartist.com/d-network.html

Page 44: Vertices Finalised and Lighting Begun

Programming Ambient Light

• Sum all of the ambient light sources in the world or partition (usually just one source)– This doesn’t require GPU balls, so we can sum on the CPU,

then set a global variable in the GPU shader• Multiply the Colour of each pixel by the ambient

intensity.Intensity = 0.0f to 1.0fAt 0.0f all pixels are 0,0,0 (Black)At 1.0f all pixels are fully lit, and will match the texture

source 1:1

Page 45: Vertices Finalised and Lighting Begun

A simple Ambient Pixel Shader

float4 Ambient;

float4 PS( float4 Pos : SV_POSITION float4 Col : COLOR) : SV_Target

{ return Col * Ambient;}

Page 46: Vertices Finalised and Lighting Begun

The End (for now)