|
|
Please support our sponsor:
Using Direct3D with VB
By Rod Stephens
Direct3D: The Color of Triangles
|
This article shows how to build a simple Direct3D application that displays eight colored triangles forming an octahedron.
|
This program draws the triangles shown in Figure 1 to make an octahedron. The key concept it adds to the previous example is different colors for the different triangles. If the program draws the triangles in the same color, you cannot tell where one ends and the next begins. Uncheck the CheckBox to see what happens.

Figure 1. Triangles in different colors.
|
Click here to download this program's Visual Basic source code. The following sections describe the program's most interesting routines. Much of the code needed to set up Direct3D is the same as in the section A First Program so it is not repeated here.
| Routine |
Purpose |
| InitializeScene |
Defines the scene parameters (camera position, triangle material, and projection matrix) |
| MakeTriangle |
Used by InitializeObjects to make a single triangle |
| InitializeObjects |
Defines the triangles that make up the octahedron |
| RenderObjects |
Draws the triangles in the appropriate colors |
| Summary |
Summary |
This example changes the scene's ambient color for each of the triangles it draws so the InitializeScene subroutine does not need to set the program's ambient color. The program sets the ambient color in subroutine RenderObjects just before it draws each triangle.
The only other change to InitializeScene is the position of the viewing position. To make the octahedron visible, the program sets this position to (4, 3, -20).
' Set the viewing position to (4, 3, -20).
m_dx.ViewMatrix matrix_camera, MakeVector(4, 3, -20), _
MakeVector(0, 0, 0), MakeVector(0, 1, 0), 0
m_d3dDevice.SetTransform D3DTRANSFORMSTATE_VIEW, matrix_camera
|
Back to page index
This routine adds one triangle to the program's lit of triangles. Other helper routines like this can add the triangles needed to build rectangles and other shapes to the program's triangle list.
' Add a triangle to the list.
Private Sub MakeTriangle( _
ByVal x1 As Single, ByVal y1 As Single, ByVal z1 As Single, _
ByVal x2 As Single, ByVal y2 As Single, ByVal z2 As Single, _
ByVal x3 As Single, ByVal y3 As Single, ByVal z3 As Single)
' Add room for the triangle's three vertices.
m_NumVertices = m_NumVertices + 3
ReDim Preserve m_Vertex(1 To m_NumVertices)
' Make the vertices.
With m_Vertex(m_NumVertices - 2)
.x = x1
.y = y1
.z = z1
End With
With m_Vertex(m_NumVertices - 1)
.x = x2
.y = y2
.z = z2
End With
With m_Vertex(m_NumVertices)
.x = x3
.y = y3
.z = z3
End With
End Sub
|
Back to page index
This routine defines the triangles used to draw the octahedron. If you make a small diagram, you can verify that these triangles list their vertices in the outward-oriented direction according to the left-hand rule.
' Initalize the objects we will display.
Private Sub InitializeObjects()
MakeTriangle 0, 10, 0, 0, 0, 10, 10, 0, 0
MakeTriangle 0, 10, 0, 10, 0, 0, 0, 0, -10
MakeTriangle 0, 10, 0, 0, 0, -10, -10, 0, 0
MakeTriangle 0, 10, 0, -10, 0, 0, 0, 0, 10
MakeTriangle 0, -10, 0, 0, 0, -10, 10, 0, 0
MakeTriangle 0, -10, 0, 10, 0, 0, 0, 0, 10
MakeTriangle 0, -10, 0, 0, 0, 10, -10, 0, 0
MakeTriangle 0, -10, 0, -10, 0, 0, 0, 0, -10
End Sub
|
Back to page index
RenderObjects draws the octahedron's triangles. It begins by clearing the viewport to erase the previous scene and calling the device's BeginScene method.
Next the routine sets the value clr. It uses this value to calculate the color for each triangle. If the Use Different Colors CheckBox is checked, the routine sets dclr to the amount by which the color should change between triangles. If the CheckBox is unchecked, it sets dclr to zero so the triangles are the same color.
The routine then calculates the number of triangles with points stored in the m_Vertex array. For each triangle, RenderObjects uses the device's SetRenderState method to set the color of the scene's ambient light. It increments the color value clr for the next triangle and uses DrawPrimitive to draw the triangle.
Finally, the routine calls the device's EndScene method.
' Draw the objects.
Private Sub RenderObjects()
Dim clr As Single
Dim dclr As Single
Dim i As Integer
Dim num_triangles As Integer
' Clear the viewport.
m_d3dDevice.Clear 1, m_ViewportRect(), D3DCLEAR_TARGET, _
m_dx.CreateColorRGB(0#, 0#, 0.5), 1, 0
' Begin the scene.
m_d3dDevice.BeginScene
' Set the color for the first triangle.
clr = 0.9
' See if we should use different colors for the different
' triangles.
If chkUseDifferentColors.Value = vbChecked Then
' Use different colors. Set the color increment.
dclr = -0.05
Else
' Use the same colors. Set the color increment to zero.
dclr = 0
End If
' Draw the triangles.
num_triangles = m_NumVertices \ 3
For i = 1 To num_triangles
' Set the ambient color to the next shade of orange.
m_d3dDevice.SetRenderState D3DRENDERSTATE_AMBIENT, _
m_dx.CreateColorRGB(clr, clr / 2, 0#)
' Increment the color for the next triangle.
clr = clr + dclr
' Draw the triangle.
m_d3dDevice.DrawPrimitive D3DPT_TRIANGLELIST, _
D3DFVF_VERTEX, m_Vertex((i - 1) * 3 + 1), 3, D3DDP_DEFAULT
Next i
' End the scene.
On Error Resume Next
m_d3dDevice.EndScene
End Sub
|
Back to page index
Most of the code used by this program in initialize Direct3D is the same as the code used in the previous example, A First Program. The only real changes are to the lighting and the objects drawn. Instead of setting the ambient light once for the scene, this program sets it just before drawing each triangle. The changes to the objects are needed because this example draws eight triangles instead of just one.
This is typical of Direct3D programs. The initialization code is practically the same in all of these programs. Only code like the lighting and vertex data that is related to the scene changes.
Back to page index
|