DirectX 8 - Tutorials - Direct3D 8 - Drawing Triangle (All Vertex Formats)

Introduction to Drawing Triangle
Actual Work
In the DXLOOP
Prepare Triangles SUB and Dim
Did you do everything?

Introduction to Drawing Triangle

Before we start, please make sure you have done the DirectX 8 Intro and Initialization and then Direct3D Intro and Initialization.

If you are planning on using untranformed wether lit or not, you need to learn how to Initialize Matrecies. Yes, this present tutorial works with all the FVFs, but you need to

Actual Work

By now, you'll have a BAS with a few functions and a form.

In the form(which we are using) or in the BAS file (if you wish, but you might have to change things):
Dim stopLoop As Boolean
Dim V(0 To 2) As ?????????? 'Leave it this way, until I explain!
Private Sub Form_Load()
    
Set DX = New DirectX8 'Already here!'
    D3DInit(Me.hWnd)
'Windowed
    PrepareTriangles
'Explained later in this tutorial
    'UnREM the PrepareMatricies if you are using Untransformed
    ' FVF. You will need to go to the Initialization of
    ' Matricies to fill in the PrepareMatricies SUB.
    'PrepareMatrices
    'Loop over and over again until stopLoop = True
    ' Yes, we can include the Do/Loop in the Sub, but
    ' for simplicity again.
    Do While stopLoop = False
        DXLOOP
    
Loop
End Sub
Private Sub DXLOOP()
    'We are now going to explain what is needed here
End Sub

Now, where you see the Private Sub DXLOOP, this sub could be put in the BAS file. Also, we could put everything in the BAS file. But, I find this way much easier to explain. If you do not like the way it is, change it yourself please.

In the DXLOOP

New method here! It's the Clear!

Clear (ClearRectCount As Long, clearD3DRect As Any, flags As CONST_D3DCLEARFLAGS, color As Long, z As Single, stencil As Long)

ClearRectCount - Keep this 0. Very complicated stuff if you change this number
clearD3DRect - Keep this ByVal 0. This ties with the previous argument (0).
flags - This is to specify to clear the target. We could change this to

D3DCLEAR_TARGET Clear the geometry/vertecies/triangles whatever.
D3DCLEAR_DEPTH Clear the depth buffer (z-buffer/w-buffer). Explained later.
D3DCLEAR_STENCIL Clear the stencil buffer. Explained later.

color - The colour of which will replace the target. If you do not specify D3DCLEAR_TARGET then the color argument won't matter.
z - The depth of which we will clear the depth buffer. It is a number from 0.00000000 to 1.00000000. If you do not specify D3DCLEAR_DEPTH, then the z argument won't matter.
stencil - From DirectX8 SDK for Visual Basic : "Integer value to store in each stencil-buffer entry. This parameter can be in the range from 0 through 2n–1, where n is the bit depth of the stencil buffer."

This is the line we will be using, since we do not have a depth or stencil buffer.
By the way, the D3DColorARGB is to make a colour. The following line will use A(255), R(0), G(0) and B(255). Which means that the background of our application will be blue.
Also, for your information, if you do not NEED to clear, don't clear! It will slow down your program a lot.

    D3DDEV.Clear 0, ByVal 0, D3DCLEAR_TARGET, D3DColorARGB(255, 0, 0, 255), 0, 0

Now I will explain how Direct3D applications are most likely written, just to brush things up.

We will concentrate more on the circles and square at the moment.

So we have already done our clear, now let's put our BeginScene and EndScene.

    D3DDEV.Clear 0, ByVal 0, D3DCLEAR_TARGET, D3DColorARGB(255, 0, 0, 255), 0, 0
    D3DDEV.BeginScene
        D3DDEV.DrawPrimitiveUP D3DPT_LINELIST, 1, V(0), Len(V(0))
    D3DDEV.EndScene

DrawPrimitive (PrimitiveType As CONST_D3DPRIMITIVETYPE, PrimitiveCount As Long, VertexStreamZeroDataArray As Any, VertexStreamZeroStride As Long)

PrimitiveType - The type of primitive, explained before in the Direct3D 8 -> Intro -> Primitive.
PrimitiveCount - The number of triangles. We will explain later on how to mathimcatically calculate the number of triangles in comparison to the primitive type.'
VertexStreamZeroDataArray - In short, it means the first element in the vertex array: arrayname(0) )
VertexStreamZeroStride - The size of the vertex in bytes. To make things easier, simply do this: Len(arrayname(0))

Prepare Triangles SUB and Dim

Now, we need to replace that ????? at the "Dim V(0 To 2) As" line. To replace the ????, we need to reference to the FVF we decided to use.

If you chose ... then use the following ....

Unlit and Untransformed Dim V(0 To 2) As D3DVERTEX '(U)
Untransformed and Lit Dim V(0 To 2) As D3DLVERTEX '(L)
Transformed and Lit Dim V(0 To 2) As D3DTLVERTEX '(T)

Now the SUB!

Put this in the FORM. (or put in the BAS, then change the Private to Public)
Please unrem the lines that have (L) if you are using Lit FVF wether transformed or not.
Please unrem the lines that have (T) if you are using Transformed FVF wether lit or not.
Private Sub PrepareTriangles()
    
With V(0)
        .X = -0.5
        .Y = -0.5
        .Z = 1
        '.color = &HFFFF0000
'Red (L)
        '.rhw = 1
'(T)
    
End With
    
With V(1)
        .X = -0
        .Y = 0.5
        .Z = 1
        '.color = &HFF00FF00
'Green (L)
        '.rhw = 1
'(T)
    
End With
    
With V(2)
        .X = 0.5
        .Y = -0.5
        .Z = 1
        '.color = &HFFFF00FF
'Red+Blue (L)
        '.rhw = 1
'(T)
    
End With
End Sub

Did you do everything?

Did you replace the ????? at the top?
Did you unREM the lines in the PrepareTriangles() sub in conjunction of which vertex format you where using? (the above step!)
If you were using unlit (transformed or not), then please go to the Matrix tutorial and fill in the SUB and unREM that line.