DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

25
DirectX Objects Finalised Paul Taylor 2010

Transcript of DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

Page 1: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

DirectX Objects Finalised

Paul Taylor 2010

Page 2: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

Packing Your Objects

• http://www.fastmovevanlines.com/images/packing_box.jpg

Page 3: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

DirectX Buffers

• A buffer contains Elements• Elements are basic structures such as– Colours– Integer– Float Values

• An Element can contain a maximum of four components e.g.– Colour B8 G8 R8 A8– Position X Y Z– Texture Coordinates– 4x Floats

Page 4: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

DirectX Buffers

Three types of bufferVertexIndexConstant

Page 5: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

Vertex Buffers

• Contain per-vertex data• X Elements per Vertex

• Offset• Base Vertex Location

Vertex 0 Vertex 1

Position Colour TexCoords Position Colour TexCoords

Page 6: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

Index Buffers

• A String of 16 or 32 bit Indices• Each Index points to a location of an element in

an existing vertex buffer

• Offset (2)• Start Index Location (1)• Index Count (2)

Not used

Not used

Not used

0 1 2

Vertex 0 Vertex 1

Position Colour TexCoords Position Colour TexCoords

Page 7: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

Constant Buffers

• New to Dx10• A SINGLE element buffer, no multi-element

abilities like vertex buffers• Supply the Shaders with an array of constant

values• Each stage can support 15x 4096 component

constant buffers

Page 8: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

The DirectX 10 Pipelinehttp://msdn.microsoft.com/en-us/library/ee415715%28VS.85%29.aspx

A Constant Buffer can be used to capture the results of the Stream Output Stage

Page 9: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

A Basic Packing ExampleSimpleVertex vertices[] ={ D3DXVECTOR3( 0.0f, 0.5f, 0.5f ), D3DXVECTOR3( 0.5f, -0.5f, 0.5f ), D3DXVECTOR3( -0.5f, -0.5f, 0.5f ), };D3D10_BUFFER_DESC bd; bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof( SimpleVertex ) * 3; bd.BindFlags = D3D10_BIND_VERTEX_BUFFER

D3D10_SUBRESOURCE_DATA InitData; InitData.pSysMem = vertices;

CreateBuffer( &bd, &InitData, &g_pVertexBuffer );

UINT stride = sizeof( SimpleVertex ); UINT offset = 0;IASetVertexBuffers( 0, 1, &g_pVertexBuffer, &stride, &offset );Draw( 3, 0 );

http://obamarama.org/wp-content/uploads/2007/04/heres-the-beef.jpg

Page 10: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

Buffers can save time!

Translate()Draw(Beef);

Translate(…)Draw(Beef);

Translate(…)Draw(Beef);

http://obamarama.org/wp-content/uploads/2007/04/heres-the-beef.jpghttp://barfblog.foodsafety.ksu.edu/HappyCow.jpg

Page 11: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

Index Buffers are great for reuse!

Page 12: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

Warning!!!You can’t always share your Normals!

http://www.songho.ca/opengl/gl_vertexarray.html

Page 13: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

Partitioning your worldhttp://www.collinslefebvrestoneberger.com/artists/Jens/ClearCut.jpg

Page 14: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

Binary Spacial Partitioning (BSP)

• In today's gaming world, there are two important reasons for dividing the world– Rendering without the Z-Buffer – Rendering Translucent (Alpha Blend) Polygons

Page 15: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

Why?

• Modern games have so many polygons that stretching the z buffer to cover the entire object range would result in many visual glitches, as many polygons start falling into the same depth levels of the buffer.

• Translucent Polygons are created using a blend function which requires back-to-front rendering (Painters algorithm)

Page 16: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

How?

• There are many differing ways and methods to create maps of polygons, especially static collections

• One of the most common, and a very efficient way is utilising BSP trees.– The first use of a BSP tree was Doom in 1993• It was only used in a 2D implementation!

Page 17: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

A BSP is a specialised implementation of a Binary Data Tree

• Binary Trees are used because:• the ability to be efficiently traversed in both

directions• The ability to add data randomly during the

building of the tree• The speed in which a random location can be

found

Page 19: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

The Principal to creating a BSP Tree

• Each polygon (3D) / line (2D) creates a dividing line / plane in the tree where every other Polygon/line is either in front or behind.

Page 20: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

Cutting your Polygons

• The offending Polygon / Line must be cut into two parts

• It then becomes two separate polygons / lines

• One infront of the division, • the other behind

Page 21: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

Reordering the split

• If we split using the red/green line first, our tree would require no splitting

Page 22: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

Problems Creating BSP Lists

http://www.devmaster.net/articles/bsp-trees/http://beehivehairdresser.com/wp-content/uploads/2007/10/yin_yang.jpg

Minimising Polygon Cuts

Balancing the Tree

Page 23: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

A short 2D Example

http://web.cs.wpi.edu/~matt/courses/cs563/talks/bsp/bsp.html

Page 24: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

Creating a BSP Tree for Lines (Doom Style)

http://pauillac.inria.fr/~levy//bsp/

Page 25: DirectX Objects Finalised Paul Taylor 2010. Packing Your Objects .

References

• http://msdn.microsoft.com/en-us/library/bb205133%28VS.85%29.aspx#Buffer_Resources