Using Direct3D with VBBy Rod Stephens Direct3D: Sit and Spin
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.
Camera CoordinatesTo make rotating the viewing position around the Y axis easy, the viewing position is stored in the form-level variables:
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.
InitializeSceneThis 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:
scrSpeedThe 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.
RenderObjectsRenderObjects 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.
SummaryThis 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.
|