Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
69 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Contents
 Reconfiguring our
 Direct3D application

 Basic 3D Geometry
 Vertex Buffers and
 Index Buffers

 Rendering Summary

 Source code
 Printable version
 Discuss this article

The Series
 Part 1
 Part 2
 Part 3

Rendering Summary

Over the course of this article we have covered a total of 4 different ways of rendering our cube:

'##RENDERING METHOD 1##
D3DDevice.DrawPrimitiveUP D3DPT_TRIANGLELIST, _
             12, CubeVerts(0), Len(CubeVerts(0))

'##RENDERING METHOD 2##
D3DDevice.SetStreamSource 0, vbCube, Len(CubeVerts(0))
D3DDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, 12

'##RENDERING METHOD 3##
D3DDevice.SetStreamSource 0, vbCubeIdx, Len(vList(0))
D3DDevice.SetIndices ibCube, 0
D3DDevice.DrawIndexedPrimitive D3DPT_TRIANGLELIST, 0, 36, 0, 12

'##RENDERING METHOD 4##
D3DDevice.DrawIndexedPrimitiveUP D3DPT_TRIANGLELIST, 0, 8, 12, _
        iList(0), D3DFMT_INDEX16, vList(0), Len(vList(0))

With these 4 methods, and the knowledge of how to set them up you will be perfectly capable of generating most forms of geometry in the best possible way. Experiment with different complexity models to see what advantages of space/speed you can find…

Finally, absolutely finally, I want to explain how to get data from the buffers after you have put it there. Whilst it is quite likely you’ll have the original vertex structures around to manipulate there will be times when you need to access the vertex or index data and change it. It is through this method that you can do key frame animation (I have a tutorial on my site about this - see the link in the summary). Also when using indexed rendering you need only change the vertex in the vertex buffer for it to be instantly reflected in the final rendering - all indices that point to that vertex will use the updated copy.

D3DIndexBuffer8GetData( _ 
    IBuffer As Direct3DIndexBuffer8, _ 
    Offset As Long, _ 
    Size As Long, _ 
    Flags As Long, _ 
    Data As Any) As Long
D3DVertexBuffer8GetData( _ 
    VBuffer As Direct3DVertexBuffer8, _ 
    Offset As Long, _ 
    Size As Long, _ 
    Flags As Long, _ 
    Data As Any) As Long

As you can see both functions are almost identical - the only difference being their name and the first parameter. A quick run through of the common parameters then:

Offset: offset in bytes indicating where the data should be read from

Size: The size of the destination buffer in bytes

Flags: Not relevant in 99% of cases, examine CONST_D3DLOCKFLAGS if you think you need something special

Data: This is the first element in an array that is going to store the data - it must be in the correct format, and must be the correct size.

Should there be any problem with knowing how big the vertex/index buffer is - or how many vertices/indices are stored inside then you can use the .GetDesc member of either the vertex buffer or index buffer. This will retrieve either a D3DVERTEXBUFFER_DESC or a D3DINDEXBUFFER_DESC structure, you can then use the data provided to work out the size, in the case of index buffers the following code will tell you how many indices there are in the buffer:

'D3DFMT_INDEX16 = 101
'D3DFMT_INDEX32 = 102
Dim ibDesc As D3DINDEXBUFFER_DESC
Dim IndexCount As Long 'how many indices are in the buffer
ibCube.GetDesc ibDesc
If ibDesc.Format = 101 Then
  '16 bit indices
  IndexCount = ibDesc.Size / 2
ElseIf ibDesc.Format = 102 Then
  '32 bit indices
  IndexCount = ibDesc.Size / 4
Else
  'no idea whats stored here!
End If
Debug.Print IndexCount, " indices in the buffer."

And this code will tell you how many vertices there are in a vertex buffer:

Dim vbDesc As D3DVERTEXBUFFER_DESC
Dim DummyVertex As LITVERTEX
Dim VertexCount As Long
Dim TotalDivider As Long 'the size of the vertex structure
vbCube.GetDesc vbDesc
If vbDesc.FVF = FVF_LVERTEX Then
  'The type stored is the LVertex type
  VertexCount = vbDesc.Size / Len(DummyVertex)
Else
  'it's some other type of vertex, lets find out:
  If Not (vbDesc.FVF And D3DFVF_XYZ) = 0 Then
    Debug.Print "D3DFVF_XYZ"
    TotalDivider = TotalDivider + 12
  End If

  If Not (vbDesc.FVF And D3DFVF_XYZRHW) = 0 Then
    Debug.Print "D3DFVF_XYZRHW"
    TotalDivider = TotalDivider + 16
  End If

  If Not (vbDesc.FVF And D3DFVF_NORMAL) = 0 Then
    Debug.Print "D3DFVF_NORMAL"
    TotalDivider = TotalDivider + 12
  End If

  If Not (vbDesc.FVF And D3DFVF_DIFFUSE) = 0 Then
    Debug.Print "D3DFVF_DIFFUSE"
    TotalDivider = TotalDivider + 4
  End If

  If Not (vbDesc.FVF And D3DFVF_SPECULAR) = 0 Then
    Debug.Print "D3DFVF_SPECULAR"
    TotalDivider = TotalDivider + 4
  End If

  If Not (vbDesc.FVF And D3DFVF_TEX1) = 0 Then
    Debug.Print "D3DFVF_TEX1"
    TotalDivider = TotalDivider + 8
  End If

  If Not (vbDesc.FVF And D3DFVF_TEX2) = 0 Then
    Debug.Print "D3DFVF_TEX2"
    TotalDivider = TotalDivider + 8
  End If

  VertexCount = vbDesc.Size / TotalDivider
End If
Debug.Print VertexCount, " Vertices in the buffer"

Quite lengthy you’ll see, but that’s just if the format is unknown, in which case we undo the "or-ing" to generate an FVF by "And-ing" it with the relevant components - the ones listed above will detect most common types, but if you start using more dynamic flags then you’ll need to add them to this list.

Summary

Well, I’ve finally completed this article! All 20 A4 pages of it and nearly 10,000 words of it, it even took me almost dead on 7 hours to write the whole thing… and I haven’t made a penny/dollar from it J Maybe I should write a book about this stuff…

Once you have this massive amount of information safely behind you then you should be perfectly capable of getting started with your own game, the next article will tidy up all the loose ends and introduce you to loading models and direct3D lighting, by which point you’ll be a fairly competent DirectXGraphics developer.

Please feel free to send any comments, criticisms and questions to me: Jollyjeffers@Greenonions.netscapeonline.co.uk, I always like hearing from people who’ve read my work…

Also, you can visit my main page - where a lot of this information can be found in different formats/examples, along with plenty of other articles (just over 100 in total), go along to http://www.vbexplorer.com/directx4vb/ and have a look around…

Till next time…