Proxy > Gmail Facebook Yahoo!

WPF – Cube rotation – animation




image
This program animates a cube in black background. A logo is used as texture.
Here is the XAML file:
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="pitScrnSaver 1.0" Height="300" Width="300" Loaded="Window_Loaded" Background="Black" ResizeMode="NoResize" ShowInTaskbar="False" WindowState="Maximized" WindowStyle="None" Icon="/pitScrnSaver;component/favicon.ico">
    <Grid MouseMove="Grid_MouseMove">
    <Viewport3D Margin="4,4,4,4">
 
                <ModelVisual3D>
            <ModelVisual3D.Content>
                <Model3DGroup>
                    <!-- Lights -->
                    <AmbientLight Color="Black" />
                    <DirectionalLight Color="LightGray" Direction="1,-2,-3" />
                    <DirectionalLight Color="White" Direction="-1,2,3" />
 
                    <GeometryModel3D>
                        <GeometryModel3D.Geometry>
                            <!-- Cube -->
                            <MeshGeometry3D TextureCoordinates="-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"
                    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>
                                <MaterialGroup>
                                    <DiffuseMaterial>
                                        <DiffuseMaterial.Brush>
 
                                    <ImageBrush ImageSource="C:\Users\Pits\Desktop\pitScrnSaver\pitScrnSaver\pit.png" Opacity="1"></ImageBrush>
                                </DiffuseMaterial.Brush>
 
                                    </DiffuseMaterial>
 
                                    <DiffuseMaterial>
                                        <DiffuseMaterial.Brush>
                                            <SolidColorBrush Color="LightGray" Opacity=".2"></SolidColorBrush>
                                        </DiffuseMaterial.Brush>
                                    </DiffuseMaterial>
 
                                </MaterialGroup>
 
 
 
 
                            </GeometryModel3D.Material>
 
                    </GeometryModel3D>
 
                </Model3DGroup>
            </ModelVisual3D.Content>
        </ModelVisual3D>
 
        <Viewport3D.Camera>
            <PerspectiveCamera x:Name="camera" 
          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 x:Name="axisanglerotation"
                      Axis="1 0 0" 
                      Angle="{Binding ElementName=vscroll, Path=Value}" />
                            </RotateTransform3D.Rotation>
                        </RotateTransform3D>
                    </Transform3DGroup>
                </PerspectiveCamera.Transform>
            </PerspectiveCamera>
        </Viewport3D.Camera>
    </Viewport3D>
        <TextBlock x:Name="text1" Text="" Foreground="White" FontSize="24" FontWeight="Normal" VerticalAlignment="Top" TextAlignment="Left" HorizontalAlignment="Right" Visibility="Visible"></TextBlock>
    </Grid>
</Window>
And here goes the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using System.Windows.Media.Animation;
using System.Windows.Threading;
 
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private DispatcherTimer timer;
 
        string[] str = {
                           "The Right People...",
                            "The Best Tools...",
                            "The Perfect Solution...",
                            "Since 2000",
                            "Your IT Partner for All Seasons",
                            "Visit www.pitsolutions.com",
                            "Microsoft Certified Partner",
                            ".NET Lab",
                            "PHP Factory",
                            "Java Venture",
                            "Quality Assurance",
                            "Client-Server Applications",
                            "Website and Web applications",
                            "Web and Windows services",
                            "Web and Windows Rich Client services",
                            "Porting Legacy Applications to .NET",
                            "Content Management Systems",
                            "Object Oriented Programming with PHP 5",
                            "e-commerce Applications",
                            "Customization of OpenSource Applications",
                            "J2EE applications",
                            "Flash/Flex Programming",
                            "Ruby on Rails development",
                            "Python programming",
                            "API programming (Google, iPhone, Facebook, Skype,Twitter, eBay, etc)",
                            "Delphi programming",
                            "Online tutoring (Grade 6 to College level subjects)",
                            "E-mail marketing"
                       };
 
        public Window1()
        {
            InitializeComponent();
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(12);
            timer.Tick += new EventHandler(timer_Tick);
            timer.IsEnabled = true;
        }
 
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Random r = new Random(DateTime.Now.Second);
            int a = (DateTime.Now.Second > 30) ? 1 : -1;
            DoubleAnimation tl = new DoubleAnimation(a*360, TimeSpan.FromSeconds(8));
            tl.RepeatBehavior = RepeatBehavior.Forever;
 
            axisanglerotation.Axis = new Vector3D(r.Next(2), r.Next(2), 1);
            axisanglerotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, tl);
        }
 
        private void timer_Tick(object sender, EventArgs e)
        {
            Random r = new Random();
            text1.Text = str[r.Next(str.Length)] + " ";
 
            DoubleAnimation da = new DoubleAnimation(0, TimeSpan.FromSeconds(5));
            da.From = 0;
            da.To = 1;
            da.AutoReverse = true;
            text1.BeginAnimation(TextBlock.OpacityProperty , da);
        }
 
        private void Grid_MouseMove(object sender, MouseEventArgs e)
        {
            Close();
        }
 
 
    }
}


Responses

0 Respones to "WPF – Cube rotation – animation"


Send mail to your Friends.  

Expert Feed

 
Return to top of page Copyright © 2011 | My Code Logic Designed by Suneel Kumar