Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake and rotate a cube by using XAML code in Visual Basic 2008
DescriptionThis example shows how to make and rotate a cube by using XAML code in Visual Basic 2008.
KeywordsVisual Basic 2008, VB 2008, XAML, cube, rotate
CategoriesGraphics, VB.NET, WPF
 
The following shows the program's XAML code. A DockPanel contains two scroll bars and a Viewport3D. The Viewport3D contains a ModelVisual3D that holds some lights and a GeometryModel3D.

The GeometryModel3D holds a MeshGeomrtry3D. The Positions attribute gives the points used by the mesh. The TriangleIndices attribute indicates which points are used to build triangles. This data builds the cube.

The Viewport3D also contains a Camera element that defines the camera position. The projection transformation includes rotations around the Y and X axes. The rotations' angles of rotation are bound to the scroll bars so when you change the scroll bar values the projection changes and the cube rotates.

 
<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Cube" Height="300" Width="300"
    >
  <DockPanel>
    <ScrollBar Name="hscroll" 
      DockPanel.Dock="Bottom"
      Orientation="Horizontal" 
      Minimum="-180" Maximum="180" 
      LargeChange="10" SmallChange="1" Value="0" />
    <ScrollBar Name="vscroll" 
      DockPanel.Dock="Right"
      Orientation="Vertical"  
      Minimum="-180" Maximum="180" 
      LargeChange="10" SmallChange="1" Value="0" />

    <Viewport3D Margin="4,4,4,4">
        <ModelVisual3D>
          <ModelVisual3D.Content>
            <Model3DGroup>
              <!-- Lights -->
              <AmbientLight Color="Gray" />
              <DirectionalLight Color="Gray" Direction="1,-2,-3" />
              <DirectionalLight Color="Gray" Direction="-1,2,3" />

              <GeometryModel3D>
                <GeometryModel3D.Geometry>
                  <!-- Cube -->
                  <MeshGeometry3D 
                    Positions="
                      -1,-1,-1   1,-1,-1   1,-1, 1  -1,-1, 1
                      -1,-1, 1   1,-1, 1   1, 1, 1  -1, 1, 1
                       1,-1, 1   1,-1,-1   1, 1,-1   1, 1, 1
                       1, 1, 1   1, 1,-1  -1, 1,-1  -1, 1, 1
                      -1,-1, 1  -1, 1, 1  -1, 1,-1  -1,-1,-1 
                      -1,-1,-1  -1, 1,-1   1, 1,-1   1,-1,-1
                      "
                    TriangleIndices="
                       0  1  2     2  3  0
                       4  5  6     6  7  4
                       8  9 10    10 11  8
                      12 13 14    14 15 12
                      16 17 18    18 19 16
                      20 21 22    22 23 20
                      " />
                </GeometryModel3D.Geometry>

                <GeometryModel3D.Material>
                  <DiffuseMaterial Brush="Red" />
                </GeometryModel3D.Material>
              </GeometryModel3D>

            </Model3DGroup>
          </ModelVisual3D.Content>
        </ModelVisual3D>

      <Viewport3D.Camera>
        <PerspectiveCamera 
          Position = "2, 4, 6"
          LookDirection = "-1, -2, -3"
          UpDirection = "0, 1, 0"
          FieldOfView = "60">
          <PerspectiveCamera.Transform>
            <Transform3DGroup>
              <RotateTransform3D>
                <RotateTransform3D.Rotation>
                  <AxisAngleRotation3D
                      Axis="0 1 0" 
                      Angle="{Binding ElementName=hscroll, Path=Value}" />
                </RotateTransform3D.Rotation>
              </RotateTransform3D>
              <RotateTransform3D>
                <RotateTransform3D.Rotation>
                  <AxisAngleRotation3D
                      Axis="1 0 0" 
                      Angle="{Binding ElementName=vscroll, Path=Value}" />
                </RotateTransform3D.Rotation>
              </RotateTransform3D>
            </Transform3DGroup>
          </PerspectiveCamera.Transform>
        </PerspectiveCamera>
      </Viewport3D.Camera>
    </Viewport3D>
    </DockPanel>
</Window>
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated