Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF 3D
Hello everybody.
In my application I have 3D cube.
On cube sides I will place any controls.
At the borders I have a buttons for rotate this cube to +-90 degree.
But in fact I have rotate the camera, not cube. And I'd like to rotate exactly Cube.
<Window.Resources>
        <DiffuseMaterial x:Key="CubeSideMaterial" Viewport2DVisual3D.IsVisualHostMaterial="True" Brush="LimeGreen"/>
        <LinearGradientBrush x:Key="BlackBackground" EndPoint="1,0" StartPoint="0,0.3">
            <GradientStop Color="LightGray" Offset="0.8"/>
            <GradientStop Color="DarkGray" Offset="0.7"/>
            <GradientStop Color="Black" Offset="0"/>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="GreenBackground" EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="GreenYellow" Offset="0.824"/>
            <GradientStop Color="DarkGreen" Offset="0"/>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="PressedButton" EndPoint="0.5,1" StartPoint="0.5,0" MappingMode="RelativeToBoundingBox">
            <GradientStop Color="Blue" Offset="0"/>
            <GradientStop Color="White" Offset="0.816"/>
            <GradientStop Color="Violet" Offset="0.744"/>
        </LinearGradientBrush>
        <ControlTemplate x:Key="ButtonLook">
            <Grid Background="Khaki" ShowGridLines="True" >
                <Rectangle x:Name="Rect" Width="20" Height="11">
                    <Rectangle.Stroke>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="Black" Offset="1"/>
                            <GradientStop Color="Green"/>
                        </LinearGradientBrush>
                    </Rectangle.Stroke>
                    <Rectangle.Fill>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="Gray" Offset="1"/>
                            <GradientStop Color="Green"/>
                        </LinearGradientBrush>
                    </Rectangle.Fill>
                </Rectangle>
                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
            </Grid>
        </ControlTemplate>
    </Window.Resources>
    <Grid Background="Black">
        <Viewport3D x:Name="view" Margin="0,0,0,-139"  ClipToBounds="False" RenderOptions.EdgeMode="Aliased" IsHitTestVisible="True">
            <Viewport3D.Camera>
                <PerspectiveCamera x:Name="camera" FieldOfView="60" Position="0,-0.2,6" LookDirection="0.000001,-0.0005,-0.08" UpDirection="0,1,0">
                    <PerspectiveCamera.Transform>
                        <RotateTransform3D x:Name="rot" CenterY="0" CenterX="0" CenterZ="0">
                            <RotateTransform3D.Rotation>
                                <!-- rotation -->
                                <AxisAngleRotation3D x:Name="camRotation" Axis="0,1,0" Angle="0"/>
                            </RotateTransform3D.Rotation>
                        </RotateTransform3D>
                    </PerspectiveCamera.Transform>                
                </PerspectiveCamera>
            </Viewport3D.Camera>
            <ModelVisual3D x:Name="model">
                <ModelVisual3D.Content>
                    <Model3DGroup x:Name="group">
                        <AmbientLight Color="White" />
                        <DirectionalLight Color="White" Direction="-1,-1,-10"></DirectionalLight>
                    </Model3DGroup>
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
                <Viewport2DVisual3D.Geometry>
                    <MeshGeometry3D Positions="-1.77,-1,1.77 1.77,-1,1.77 1.77,1,1.77 -1.77,1,1.77"
                                    TextureCoordinates="0,1 1,1 1,0 0,0"
                                    TriangleIndices="0,2,3 0,1,2"/>
                </Viewport2DVisual3D.Geometry>
                <Grid x:Name="FrontPanel" ShowGridLines="False" IsHitTestVisible="True" Margin="0" Background="{StaticResource BlackBackground}">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition Height="15"/>
                        <RowDefinition Height="15" MaxHeight="15"/>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="20"/>
                        <ColumnDefinition />
                        <ColumnDefinition/>
                        <ColumnDefinition Width="20"/>
                    </Grid.ColumnDefinitions>
                    <Label Content="Front" Grid.Row="0" Grid.Column="2" Foreground="LightBlue"/>
                    <Button Grid.Row="1" Grid.Column="4" Grid.ColumnSpan="2" 
                            Content="+90" 
                            Foreground="Black" 
                            FlowDirection="LeftToRight" 
                            FontSize="3" 
                            VerticalContentAlignment= "Center"
                            HorizontalAlignment="Right"
                            Width="20"
                            Height="10"
                            Margin="0,2,0,2"
                            ToolTip="Типа ткни сюда и Опа!" 
                            Click="SeeOthers_Click" />
                    <!--<Image Grid.Column="4" Grid.Row="4" Grid.RowSpan="2" HorizontalAlignment="Right" Margin="2,1,2,12" Name="image1" Stretch="Fill" Width="25" Height="20" Source="monitor2.jpg"/>-->
                    <Button Grid.Column="0" 
                            Grid.Row="1" 
                            Height="10" 
                            Width="20" 
                            Content="-90" 
                            Click="SeeOthers_Click" 
                            FontSize="3" 
                            BorderThickness="0" 
                            Foreground="Black" />
                </Grid>
            </Viewport2DVisual3D>
            <!-- cube right side -->
            <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
                <Viewport2DVisual3D.Geometry>
                    <MeshGeometry3D Positions="1.77,-1,1.77 1.77,1,1.77 1.77,1,-1.77, 1.77,-1,-1.77"
                                    TextureCoordinates="0,1 0,0 1,0 1,1 "
                                    TriangleIndices="0,3,2 0,2,1"/>
                </Viewport2DVisual3D.Geometry>
                <Grid x:Name="RightPanel" IsHitTestVisible="True" Background="{StaticResource BlackBackground}">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition Height="15"/>
                        <RowDefinition Height="15" MaxHeight="15"/>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="30"/>
                        <ColumnDefinition />
                        <ColumnDefinition/>
                        <ColumnDefinition Width="30"/>
                    </Grid.ColumnDefinitions>
                    <Button Grid.Row="1" Grid.Column="4" Grid.ColumnSpan="2" 
                            Content="+90" 
                            Foreground="Black" 
                            FlowDirection="LeftToRight" 
                            FontSize="3" 
                            VerticalContentAlignment= "Center"
                            HorizontalAlignment="Right"
                            Width="20"
                            Height="10"
                            Margin="0,2,0,2"
                            ToolTip="Типа ткни сюда и Опа!" 
                            Click="SeeOthers_Click" />
                    <Label Content="Right" Grid.Row="0" Grid.Column="2" Foreground="LightBlue"/>
                    <Button Grid.Column="0" 
                            Grid.Row="1" 
                            Height="10" 
                            Width="20" 
                            Content="-90" 
                            Click="SeeOthers_Click" 
                            FontSize="3" 
                            BorderThickness="0" 
                            Foreground="Black" />
                </Grid>
            </Viewport2DVisual3D>
            <!-- cube back side -->
            <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
                <Viewport2DVisual3D.Geometry>
                    <MeshGeometry3D Positions="1.77,1,-1.77 1.77,-1,-1.77 -1.77,1,-1.77, -1.77,-1,-1.77"
                                    TextureCoordinates=" 0,0 0,1 1,0 1,1"
                                    TriangleIndices="1,3,2 1,2,0"/>
                </Viewport2DVisual3D.Geometry>
                <Grid x:Name="BackPanel"  Background="{StaticResource BlackBackground}" IsHitTestVisible="True">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition Height="15"/>
                        <RowDefinition Height="15" MaxHeight="15"/>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="30"/>
                        <ColumnDefinition />
                        <ColumnDefinition/>
                        <ColumnDefinition Width="30"/>
                    </Grid.ColumnDefinitions>
                    <Button Grid.Row="1" Grid.Column="4" Grid.ColumnSpan="2" 
                            Content="+90" 
                            Foreground="Black" 
                            FlowDirection="LeftToRight" 
                            FontSize="3" 
                            VerticalContentAlignment= "Center"
                            HorizontalAlignment="Right"
                            Width="20"
                            Height="10"
                            Margin="0,2,0,2"
                            ToolTip="Типа ткни сюда и Опа!" 
                            Click="SeeOthers_Click" />
                    <Label Content="Rear" Grid.Row="0" Grid.Column="2" Foreground="LightBlue"/>
                    <Button Grid.Column="0" 
                            Grid.Row="1" 
                            Height="10" 
                            Width="20" 
                            Content="-90" 
                            Click="SeeOthers_Click" 
                            FontSize="3" 
                            BorderThickness="0" 
                            Foreground="Black" />
                </Grid>
            </Viewport2DVisual3D>
<!-- cube left side -->
            <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
                <Viewport2DVisual3D.Geometry>
                    <MeshGeometry3D Positions="-1.77,-1,-1.77 -1.77,1,-1.77 -1.77,1,1.77, -1.77,-1,1.77"
                                    TextureCoordinates=" 0,1 0,0 1,0 1,1"
                                    TriangleIndices="0,3,2 0,2,1"/>
                </Viewport2DVisual3D.Geometry>
                <Grid x:Name="LeftPanel"  Background="{StaticResource BlackBackground}" IsHitTestVisible="True">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition Height="15"/>
                        <RowDefinition Height="15" MaxHeight="15"/>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="30"/>
                        <ColumnDefinition />
                        <ColumnDefinition/>
                        <ColumnDefinition Width="30"/>
                    </Grid.ColumnDefinitions>
                    <Button Grid.Row="1" Grid.Column="4" Grid.ColumnSpan="2" 
                            Content="+90" 
                            Foreground="Black" 
                            FlowDirection="LeftToRight" 
                            FontSize="3" 
                            VerticalContentAlignment= "Center"
                            HorizontalAlignment="Right"
                            Width="20"
                            Height="10"
                            Margin="0,2,0,2"
                            ToolTip="Типа ткни сюда и Опа!" 
                            Click="SeeOthers_Click" />
                    <Label Content="Left" Grid.Row="0" Grid.Column="2" Foreground="LightBlue"/>
                    <Button Grid.Column="0" 
                            Grid.Row="1" 
                            Height="10" 
                            Width="20" 
                            Content="-90" 
                            Click="SeeOthers_Click" 
                            FontSize="3" 
                            BorderThickness="0" 
                            Foreground="Black" />
                </Grid>
            </Viewport2DVisual3D>
        </Viewport3D>
        <Button Height="92" HorizontalAlignment="Left" Margin="56,0,0,40" Name="button1" VerticalAlignment="Bottom" Width="186" Click="button1_Click">Button</Button>
    </Grid>
And this is C# code
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.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using Microsoft.Win32;
namespace WpfApplication4
{
        public partial class Window1 : Window
    {
        System.Windows.Threading.DispatcherTimer clock = new System.Windows.Threading.DispatcherTimer();
        System.Windows.Threading.DispatcherTimer Zoomclock = new System.Windows.Threading.DispatcherTimer();
        public Window1()
        {
            InitializeComponent();
            //rotation clock
            clock.Tick += new EventHandler(clock_Tick);
            clock.Interval = new TimeSpan(0, 0, 0, 0, 20);
            Zoomclock.Tick += new EventHandler(Zoomclock_Tick);
            Zoomclock.Interval = new TimeSpan(0, 0, 0, 0, 15);
        }
        //this is event for zooming
        void Global_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            camera.Position = new Point3D(
            camera.Position.X,
            camera.Position.Y,
            camera.Position.Z - e.Delta / 250D);
                 
        }
        //rotation angle - for rotation on Y-axis
        double rotAngle = 90;
        double CamRot=0;
        bool flagfirst = true;
        void clock_Tick(object sender, EventArgs e)
        {
            if (flagfirst==true) rotAngle = rotAngle + CamRot;
            if (rotAngle > 0) 
            { 
                camRotation.Angle += 5;
                if (camRotation.Angle >= rotAngle)
                {
                    clock.Stop();
                    CamRot = camRotation.Angle;
                    flagfirst = true;
                    camera.FieldOfView = 50;
                }
            }
            else 
            { 
                camRotation.Angle -= 5; 
                if (camRotation.Angle <= rotAngle) 
                { 
                    clock.Stop(); 
                    CamRot = camRotation.Angle;
                    flagfirst = true;
                    camera.FieldOfView = 50;
                }
            }
            flagfirst = false;
        }
        double CamDist = 60;
        void Zoomclock_Tick(object sender, EventArgs e)
        {
            camera.FieldOfView += 5;
            if (camera.FieldOfView >= 100) Zoomclock.Stop();
        }
 
        //this would need to be global key but i didn't have time to finish this
        //it have problem when some control get focus cube stop to rotate
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            double angle = 5;
            if (e.Key == Key.Left)
                if (camRotation.Angle == 0) camRotation.Angle = 360 - angle;
                else camRotation.Angle -= angle;
            else if (e.Key == Key.Right) camRotation.Angle += angle;
        }
 
        //navigation button which goes to users panel
        private void SeeOthers_Click(object sender, RoutedEventArgs e)
        {
            Zoomclock.Start();
            Rotar(sender, e);
        }
        double CurrentAngle = 0;
        private void Rotar(object sender, RoutedEventArgs e)
        {
            if (Convert.ToString(((System.Windows.Controls.ContentControl)(e.OriginalSource)).Content) == "+90")
            {
                rotAngle = CurrentAngle + 90;
                clock.Start();
            }
            if (Convert.ToString(((System.Windows.Controls.ContentControl)(e.OriginalSource)).Content) == "-90")
            {
                rotAngle = CurrentAngle - 90;
                clock.Start();
            }
            CurrentAngle = rotAngle;
            if (CurrentAngle >= 360) { CurrentAngle = 0; }
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        private void label3_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Transform3D My3DTrans = new RotateTransform3D();
            //My3DTrans.
        }
    }
}
 
Please help me, and one more.
Who know please advice - Why I have a troubles with rotation forward and backward?
 
Thanks.
Posted 31-Mar-11 22:30pm
R2d1311
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

What I think you should do is wrap all your cube sides in a new ModelVisual3D like this:
 
<ModelVisual3D x:Name="cube">
    <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
Your cube stuff goes here...
    </Viewport2DVisual3D>
    <!-- cube right side -->
    <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
...and here...
    </Viewport2DVisual3D>
    <!-- cube back side -->
    <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
...and here...
    </Viewport2DVisual3D>
    <!-- cube left side -->
    <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
...and also here!
    </Viewport2DVisual3D>
</ModelVisual3D>
 
Notice that I've left all your cube stuff intact and just wrapped them inside a ModelVisual3D, and by naming that model you can rotate all of them together using an animation of a rotation transform:
 
        private Rotation3D rotation;
        private RotateTransform3D transform;
        public Window1()
        {
            InitializeComponent();
            rotation = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0);
            transform = new RotateTransform3D(rotation);
            cube.Transform = transform;
        }
        private void SeeOthers_Click(object sender, RoutedEventArgs e)
        {
            DoubleAnimation animation = new DoubleAnimation(90, new Duration(TimeSpan.FromSeconds(1)));
            rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation);
        }
 
In this example I'm just rotating it 90 degrees but I think you can figure out how to call that with other angles as well Smile | :)
 
Hope this helps,
Fredrik Bornander
  Permalink  
Comments
Tarun.K.S at 1-Apr-11 4:49am
   
Perfect!
Fredrik Bornander at 1-Apr-11 5:02am
   
Sweet, I'm glad it worked. Please mark the answer as accepted so we can close the question.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

To rotate a cube (in my example just called 'cube'), but it could be any 3D object, it is simple to rotate using the centre point:
 

//this defines the centre point - half-way thru the object in all 3 axis
Point3D centerPoint = New Point3D(2.5, 2.5, 2.5);  
 
//the angle I want to rotate by. Not specified yet which axis
int angleRot = 45;
 
//Here, in the Vector3D, specify with axis (x, y, z) to apply the angle
RotateTransForm myRotateTransform = New RotateTransform3D(New AxisAngleRotation3D(New Vector3D(0, 1, 0), angleRot), centerPoint);
 
//apply the transform
cube.Transform = myRotateTransform;
 
Just apply a new rotateTransform when you want to change angle
  Permalink  
Comments
CHill60 at 26-Feb-14 11:20am
   
Question was answered nearly 3 years ago

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 240
2 DamithSL 200
3 Raul Iloc 185
4 Richard MacCutchan 175
0 OriginalGriff 5,130
1 DamithSL 4,197
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 26 Feb 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100