Vertex Buffers, Index Buffers - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Vertex Buffers, Index Buffers

Description:

The color and specular component of the first vertex in the triangle are used to ... The color and specular components of the face are determined by a linear ... – PowerPoint PPT presentation

Number of Views:80
Avg rating:3.0/5.0
Slides: 23
Provided by: bdug1
Category:

less

Transcript and Presenter's Notes

Title: Vertex Buffers, Index Buffers


1
Vertex Buffers, Index Buffers Textures
  • Bryan Duggan
  • Hugh McAtamney

2
To draw a 3D object
  • We store the vertices of the 3D object in a
    vertex buffer.
  • The vertices are the corners of the triangles
    that make up the object

3
Vertex Buffers
  • A chunk of contiguous memory that contains vertex
    data
  • A place to hold the vertices we want to draw.
  • Can be placed in VRAM for faster rendering
  • Represented by a IDirect3DVertexBuffer9 object

4
You decide the format of the vertex
  • Depending on whether you want colour, texturing
    etc.
  • E.g.
  • struct Vertex
  • Vertex()
  • Vertex(float x, float y, float z)
  • _x x _y y _z z
  • float _x, _y, _z
  • static const DWORD FVF
  • const DWORD VertexFVF D3DFVF_XYZ

5
Vertices with colours
  • struct ColorVertex
  • ColorVertex()
  • ColorVertex(float x, float y, float z, D3DCOLOR
    c)
  • _x x _y y _z z _color c
  • float _x, _y, _z
  • D3DCOLOR _color
  • static const DWORD FVF
  • const DWORD ColorVertexFVF D3DFVF_XYZ
    D3DFVF_DIFFUSE

6
Vertices with textures
  • struct TexelVertex
  • TexelVertex()
  • TexelVertex(float x, float y, float z, float u,
    float v)
  • _x x _y y _z z
  • _u u _v v
  • float _x, _y, _z
  • float _u, _v
  • static const DWORD FVF
  • const DWORD TexelVertexFVF D3DFVF_XYZ
    D3DFVF_TEX1

7
To create one
  • HRESULT CreateVertexBuffer(      
  • UINT Length,     DWORD Usage,DWORD FVF,
    D3DPOOL Pool,     IDirect3DVertexBuffer9 ppVer
    texBuffer, HANDLE pSharedHandle
  • )
  • Length Number of bytes (number_of_vertices
    sizeof(Vertex)
  • Usage Well usually use D3DUSAGE_WRITEONLY
  • FVF Flexible vertex format The static member
    of the struct
  • Pool - D3DPOOL_MANAGED Let DirectX manage the
    pool
  • pSharedHandle Usually 0

8
Example
  • device-gtCreateVertexBuffer(
  • 36 sizeof(TexelVertex), // size in bytes
  • D3DUSAGE_WRITEONLY, // flags
  • TexelVertexFVF, // vertex format
  • D3DPOOL_MANAGED, // managed memory pool
  • _landscapeVertices,// return create vertex
    buffer
  • 0
  • )

9
To write to the buffer
  • Obtain a pointer to the contents by
  • Vertex vertices
  • IDirect3DVertexBuffer9Lock(
  • UINT Offset,
  • UNIT size,
  • BYTE vertices,
  • DWORD flags
  • )
  • Offset Offset in bytes from the start of the
    buffer
  • Size Number of bytes to lock
  • vertices Pointer to the start of the locked
    data. We can write to this pointer after we lock
    the buffer
  • Flags Usually 0.
  • E.g.
  • landscapeVertices-gtLock(0, 0, (void)vertices,
    0)
  • vertices0 Vertex(1,2,3)
  • vertices1 Vertex(1,2,3)

10
To draw the buffer
  • Set the vertex stream source
  • device-gtSetStreamSource(0, _landscapeVertices,
    0, sizeof(Vertex))
  • Set the format of the stream
  • device-gtSetFVF(VertexFVF)
  • Perform any transformations necessary
  • D3DXMATRIX landscapeScale
  • D3DXMatrixScaling(landscapeScale, 1,1,1)
  • device-gtSetTransform(D3DTS_WORLD,
    landscapeScale)
  • Draw the vertices
  • device-gtDrawPrimitive(D3DPT_TRIANGLELIST, 0, 2)

11
Duplicated vertices
  • Most 3D shapes contain duplicated vertices. For
    example

This vertex is shared by 4 triangles
12
As a solution we can use
  • Index buffers
  • Create the vertex buffer, with just one instance
    of each vertex
  • Create an index buffer with multiple pointers
    into to the vertex buffer

13
Or triangle strips
  • device-gtDrawPrimitive(D3DPT_TRIANGLESTRIP
  • Renders the vertices as a triangle strip. The
    backface-culling flag is automatically flipped on
    even-numbered triangles.

14
Or triangle fans
  • device-gtDrawPrimitive(D3DPT_TRIANGLEFAN
  • Renders the vertices as a triangle fan

15
Rendering mode
  • We can configure the rendering mode with
  • device-gtSetRenderState(D3DRS_FILLMODE, value)
  • Where value is
  • D3DFILL_POINT
  • Fill points.
  • D3DFILL_WIREFRAME
  • Fill wireframes.
  • D3DFILL_SOLID
  • Fill solids.

16
Shading mode
  • We can configure the shading mode with
  • device-gtSetRenderState(D3DRS_SHADEMODE, value)
  • Where value is
  • D3DSHADE_FLAT
  • Flat shading mode. The color and specular
    component of the first vertex in the triangle are
    used to determine the color and specular
    component of the face. These colors remain
    constant across the triangle that is, they are
    not interpolated. The specular alpha is
    interpolated.
  • D3DSHADE_GOURAUD
  • Gouraud shading mode. The color and specular
    components of the face are determined by a linear
    interpolation between all three of the triangle's
    vertices.

17
Flat shading
18
Gourad Shading
19
Texturing
  • A texture is a bitmap that gets painted onto a 3D
    polygon
  • To load a texture, use
  • IDirect3DTexture9 _boxTexture
  • D3DXCreateTextureFromFile(device, "rocks.bmp",
    _boxTexture)

20
Texels
  • A texel is like a pixel, but is a point on a
    texture
  • The origin for texels, is the top left
  • The bounds of a texture are 1,1

u-axis
(0, 0)
v-axis
(1, 1)
21
To apply a texture to a polygon
  • Define the texel that applies to a vertex

    -Vertices- -Texel- vertices24
    TexelVertex(-1, 1, -1, 0, 1)
  • Set the vertex format when you create the buffer
  • device-gtCreateVertexBuffer(
  • 30 sizeof(TexelVertex),
  • D3DUSAGE_WRITEONLY,
  • TexelVertexFVF, // vertex format
  • D3DPOOL_MANAGED, _boxVertices,
  • 0)
  • Set the vertex before drawing
  • device-gtSetFVF(TexelVertexFVF)
  • Set the texture before drawing
  • device-gtSetTexture(0,_boxTexture)

22
Outside the vertex range
  • Set the texels beyond 0, 1
  • Set the SamplerState to be
  • wrap
  • border colour
  • clamp
  • mirror
  • E.g.
  • _device-gtSetSamplerState(D3DSAMP_ADDRESSU,
    D3DTADRESS_WRAP)
  • _device-gtSetSamplerState(D3DSAMP_ADDRESSV,
    D3DTADRESS_WRAP)
Write a Comment
User Comments (0)
About PowerShow.com