Visual Basic Explorer
Visual Basic Explorer
 Navigation
 Home


 Coding
 Source Code

 FAQ Center

 VB Tips

 Downloads

 ToolBox

 Tutorials

 VB Games

 VB News

 VB Award

 VB Forums



 Affiliates
 Planet Source Code

 Rent a Coder

 DirectX4VB


 Misc
 Search

 Feedback

 Advertise

 About


Need to hire
a VB coder?

Please support our sponsor:

 Home 
 Site Map 
 Forums 
 News 
 Feedback 

Using Direct3D with VB

By Rod Stephens

Direct3D: Sit and Spin

This article shows how to build a Direct3D application that displays eight colored triangles forming an octahedron. It rotates the viewing position around the Y axis so the octahdron appears to rotate.

This program draws the triangles shown in Figure 1 to make an octahedron exactly as did the previous example, The Color of Triangles. The key concept it adds to the previous example is rotation of the viewing position. Use the scroll bar to change the program's speed and direction of rotation.


Figure 1. This program rotates the viewing position around the Y axis.

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 The Color of Triangles so it is not repeated here.

Routine Purpose
Camera Coordinates Explains how the program stores the viewing position coordinates
InitializeScene Defines the scene parameters (camera position, triangle material, and projection matrix)
scrSpeed This ScrollBar sets the program's speed of rotation
RenderObjects Draws the triangles in the appropriate colors
Summary Summary


Camera Coordinates

To make rotating the viewing position around the Y axis easy, the viewing position is stored in the form-level variables:

' The camera's position.
Private m_CameraR As Double
Private m_CameraTheta As Double
Private m_CameraDTheta As Double
Private m_CameraY As Double

The value m_CameraTheta measures the rotation around the Y axis in radians. The viewing position is at the point:

    X = m_CameraR * Cos(m_CameraTheta)
    Y = m_CameraY
    Z = m_CameraR * Sin(m_CameraTheta)

As m_CameraTheta changes, the point rotates around the Y axis.

The value m_CameraDTheta determines how quickly the point rotates around the Y axis. Each time the RenderObjects subroutine draws the triangles, it adds m_CameraDTheta to m_CameraTheta.


InitializeScene

This example changes the scene's viewing position each time it draws a new frame so the InitializeScene subroutine does not need to set the viewing position. Subroutine RenderObjects sets the viewing position just before it draws the triangles.

InitializeScene sets the initial position of the viewing position using this code:

    ' Initialize the viewing position variables so the point
    ' (4, 3, -20) is on the viewpoint's path.
    m_CameraR = Sqr(4 * 4 + 20 * 20)
    m_CameraTheta = 0
    m_CameraY = 3

    ' Set the rotation speed to 20 percent of maximum.
    scrSpeed.Value = 20

Back to page index


scrSpeed

The scrSpeed ScrollBar determines the program's speed of rotation. The scrSpeed_Scroll event handler calls scrSpeed_Change to set the speed. The scrSpeed_Change event handler displays the new speed in the lblSpeed label. It then sets m_CameraDTheta to the ScrollBar's value divided by 100, times PI / 20. This value is the maximum speed possible and was chosen to fit my copmuter's capabilities. You may want to adjust it for your computer.

' Call scrSpeed_Change to set the speed of rotation.
Private Sub scrSpeed_Scroll()
    scrSpeed_Change
End Sub

' Set the speed at which the camera revolves around the origin.
Private Sub scrSpeed_Change()
Dim percent As Integer

    percent = scrSpeed.Value
    lblSpeed.Caption = Format$(percent)
    m_CameraDTheta = -percent / 100 * PI / 20
End Sub

Back to page index


RenderObjects

RenderObjects draws the octahedron's triangles. It begins by clearing the viewport to erase the previous scene and calling the device's BeginScene method. It sets the triangles' initial color value clr and the color increment dclr much as the previous example did.

Next the routine calculates the viewing position's coordinates and uses the device's SetTransform method to set the viewing position.

RenderObjects then calculates the number of triangles with vertices in its vertex list and draws them, setting each to a different color exactly as the previous example did.

The routine finishes by calling the device's EndScene method.

' Draw the objects.
Private Sub RenderObjects()
Dim clr As Single
Dim dclr As Single
Dim camera_x As Double
Dim camera_z As Double
Dim matrix_camera As D3DMATRIX
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 and the color increment.
    clr = 0.9
    dclr = -0.05

    ' Set the viewing position.
    camera_x = m_CameraR * Cos(m_CameraTheta)
    camera_z = m_CameraR * Sin(m_CameraTheta)
    m_dx.ViewMatrix matrix_camera, _
        MakeVector(camera_x, m_CameraY, camera_z), _
        MakeVector(0, 0, 0), MakeVector(0, 1, 0), 0
    m_d3dDevice.SetTransform D3DTRANSFORMSTATE_VIEW, matrix_camera

    ' Revolve the viewing position around the origin.
    m_CameraTheta = m_CameraTheta + m_CameraDTheta

    ' Draw the triangles.
    num_triangles = m_NumVertices \ 3
    For i = 1 To num_triangles
        ' Set the ambient color to the next shade of yellow.
        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


Summary

This example is similar to the previous one, The Color Of Triangles. The only interesting change is to the way the program sets the viewing position. Instead of setting the position once for the scene, this example sets the viewing position just before it draws the triangles. That makes the scene appear to rotate, though actually the viewing position is rotating around the scene.

Back to page index

Back to Direct3D Introduction





Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

Quick searches: Site Search | Advanced Site Search 

Copyright 2002 by Exhedra Solutions, Inc.
By using this site you agree to its terms and conditions
VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.