IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

[C#][WPF] Rotation ModelVisual3D


Sujet :

Windows Presentation Foundation

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mai 2012
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Mai 2012
    Messages : 4
    Points : 5
    Points
    5
    Par défaut [C#][WPF] Rotation ModelVisual3D
    Bonjour à tous,
    J'ai un petit soucis concernant la rotation de mon ModelVisual3D...

    Je travaille sur une application qui doit permettre de faire tourner dans tous les sens un objet 3D avec des gestures de la main, avec le sdk Kinect.
    En gros, l'objet doit effectuer des rotations suivant les gestures que j'ai déjà développées .

    Le problème est que lorsque j'applique une rotation1 d'axe (1,0,0) par exemple, et qu'ensuite je lui applique une rotation2 d'axe différent de la rotation1 précédente, celui-ci reprend sa position d'avant la rotation1 puis effectue la rotation2... et du coup je ne peux pas vraiment bouger mon objet comme je le souhaite, ça fait des sortes de sursauts....

    Après des recherches, j'ai trouvé qu'il s'agit très certainement du point d'origine de l'objet (ou de l'axe ?) (point qui constitue le centre pour mes transformations) qui n'est pas modifié au fur et à mesure de mes rotations.

    Ma question est donc triple :

    Mon problème est-il bien dû au point d'origine qui ne change pas ?
    Si oui, comment puis-je le changer dynamiquement après chaque rotation ?

    Si non, quelqu'un a-il une idée d'où peut provenir mon souci ?


    Merci d'avance

  2. #2
    Expert confirmé Avatar de DonQuiche
    Inscrit en
    Septembre 2010
    Messages
    2 741
    Détails du profil
    Informations forums :
    Inscription : Septembre 2010
    Messages : 2 741
    Points : 5 493
    Points
    5 493
    Par défaut
    Bonjour.

    Je ne me rappelle même plus à quoi ressemble la manipulation d'objets 3D sous WPF mais comme partout ce doit être en assignant une matrice 4x4. Ma question est donc : la transformation que tu appliques est-elle bien une multiplication de la matrice décrivant la rotation d'axe 1 avec celle de la rotation d'axe 2 ?

    Sinon, un petit bout de ton code me permettra sans doute de comprendre ce qui cloche.

  3. #3
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 442
    Points
    4 442
    Par défaut
    bonjour Lordonly.
    Comme on ne sait pas exactement quel objet tu es entrain de faire pivoter,et le code employe (xaml,cs) ,difficile de repondre ....

    Mais en matiere de mouvements d'objets solides ,on peut obtenir des mouvements singulierement compliques si on combine des transformations affines de WPF.....
    Il faut faire attention à l'ordre dans lequel on fait des transformations..
    Pour les Rotations et les Scales le "centre" est necessaire en wpf et s'il n'est pas precise WPF prend le centre de l'objet(centre de "gravite" geometrique)........
    Il faut faire attention au TimeLine ou StoryBoard de WPF ,car il agit sur la succession dans le temps des transformations(appliquer une transformation apres une autre)....

    Bref voici 3 exemples :
    code behind de l'objet(un cube cree en code dote d'un centre):
    Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
     
    using System;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Media3D;
     
    namespace WpfCube
    {
        public class CubeGeometry
        {
            // Define private fields:
            private double length = 1.0;
            private double width = 1.0;
            private double height = 1.0;
            private Point3D center = new Point3D();
            // Define public properties:
            public double Length
            {
            get { return length; }
            set { length = value; }
            }
            public double Width
            {
            get { return width; }
            set { width = value; }
            }
            public double Height
            {
            get { return height; }
            set { height = value; }
            }
            public Point3D Center
            {
            get { return center; }
            set { center = value; }
            }
            // Get-only property generates MeshGeometry3D object:
            public MeshGeometry3D Mesh3D
            {
            get { return GetMesh3D(); }
            }
            private MeshGeometry3D GetMesh3D()
            {
                MeshGeometry3D mesh = new MeshGeometry3D();
                Point3D[] pts = new Point3D[8];
                double hl = 0.5 * Length;
                double hw = 0.5 * Width;
                double hh = 0.5 * Height;
     
                pts[0] = new Point3D(hl, hh, hw);
                pts[1] = new Point3D(hl, hh, -hw);
                pts[2] = new Point3D(-hl, hh, -hw);
                pts[3] = new Point3D(-hl, hh, hw);
                pts[4] = new Point3D(-hl, -hh, hw);
                pts[5] = new Point3D(-hl, -hh, -hw);
                pts[6] = new Point3D(hl, -hh, -hw);
                pts[7] = new Point3D(hl, -hh, hw);
            for (int i = 0; i < 8; i++)
            {
                pts[i] += (Vector3D)Center;
            }
            // Top surface (0-3):
            for (int i = 0; i < 4; i++)
            {
                mesh.Positions.Add(pts[i]);
            }
            mesh.TriangleIndices.Add(0);
            mesh.TriangleIndices.Add(1);
            mesh.TriangleIndices.Add(2);
            mesh.TriangleIndices.Add(2);
            mesh.TriangleIndices.Add(3);
            mesh.TriangleIndices.Add(0);
            //Bottom surface (4-7):
            for (int i = 4; i < 8; i++)
            {
                mesh.Positions.Add(pts[i]);
            }
            mesh.TriangleIndices.Add(4);
            mesh.TriangleIndices.Add(5);
            mesh.TriangleIndices.Add(6);
            mesh.TriangleIndices.Add(6);
            mesh.TriangleIndices.Add(7);
            mesh.TriangleIndices.Add(4);
            // Front surface (8-11):
            mesh.Positions.Add(pts[0]);
            mesh.Positions.Add(pts[3]);
            mesh.Positions.Add(pts[4]);
            mesh.Positions.Add(pts[7]);
            mesh.TriangleIndices.Add(8);
            mesh.TriangleIndices.Add(9);
            mesh.TriangleIndices.Add(10);
            mesh.TriangleIndices.Add(10);
            mesh.TriangleIndices.Add(11);
            mesh.TriangleIndices.Add(8);
            // Back surface (12-15):
            mesh.Positions.Add(pts[1]);
            mesh.Positions.Add(pts[2]);
            mesh.Positions.Add(pts[5]);
            mesh.Positions.Add(pts[6]);
            mesh.TriangleIndices.Add(12);
            mesh.TriangleIndices.Add(15);
            mesh.TriangleIndices.Add(14);
            mesh.TriangleIndices.Add(14);
            mesh.TriangleIndices.Add(13);
            mesh.TriangleIndices.Add(12);
            // Left surface (16-19):
            mesh.Positions.Add(pts[2]);
            mesh.Positions.Add(pts[3]);
            mesh.Positions.Add(pts[4]);
            mesh.Positions.Add(pts[5]);
            mesh.TriangleIndices.Add(16);
            mesh.TriangleIndices.Add(19);
            mesh.TriangleIndices.Add(18);
            mesh.TriangleIndices.Add(18);
            mesh.TriangleIndices.Add(17);
            mesh.TriangleIndices.Add(16);
            // Right surface (20-23):
            mesh.Positions.Add(pts[0]);
            mesh.Positions.Add(pts[1]);
            mesh.Positions.Add(pts[6]);
            mesh.Positions.Add(pts[7]);
            mesh.TriangleIndices.Add(20);
            mesh.TriangleIndices.Add(23);
            mesh.TriangleIndices.Add(22);
            mesh.TriangleIndices.Add(22);
            mesh.TriangleIndices.Add(21);
            mesh.TriangleIndices.Add(20);
            mesh.Freeze();
            return mesh;
            }
        }
    }
    le 1er fait tourner 2 exemplaires d'un meme cube autour :
    -de l'axe Y
    - d'un centre commun (centre de gravite du cube parent 0,0,0) dans un storyboard....
    code xaml form1

    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
     
    <Window x:Class="WpfCube.WindowRotationAxeCommun"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfCube"
            Title="Rotation sur Axe Commun" Height="300" Width="300">
        <Window.Resources>
            <local:CubeGeometry 
                x:Key="cube"
                Center="0,0,0"
                Length="0.5"
                Width="0.5"
                Height="1"/>
        </Window.Resources>
        <Viewport3D>
            <ContainerUIElement3D>
                <ModelUIElement3D>
                    <Model3DGroup>
                        <!-- Create first cube: -->
                        <GeometryModel3D 
                            Geometry="{Binding Source=
                            {StaticResource cube},Path=Mesh3D}">
                            <!-- Set material: -->
                            <GeometryModel3D.Material>
                                <DiffuseMaterial
                                    Brush="LightBlue"/>
                            </GeometryModel3D.Material>
                            <!-- Set rotation: -->
                            <GeometryModel3D.Transform>
                                <RotateTransform3D>
                                    <RotateTransform3D.Rotation>
                                        <AxisAngleRotation3D
                                            x:Name="rotate1"
                                            Axis="0,1,0"/>
                                    </RotateTransform3D.Rotation>
                                </RotateTransform3D>
                            </GeometryModel3D.Transform>
                        </GeometryModel3D>
                        <!-- Create another cube: -->
                        <GeometryModel3D 
                            Geometry="{Binding Source={StaticResource cube},
                            Path=Mesh3D}">
                            <!-- Set material: -->
                            <GeometryModel3D.Material>
                                <DiffuseMaterial
                                    Brush="Goldenrod"/>
                            </GeometryModel3D.Material>
                            <!-- Set rotation ce cube est decale de -1 pour etre visible : -->
                            <GeometryModel3D.Transform>
                                <Transform3DGroup>
                                    <TranslateTransform3D
                                        OffsetZ="-1"/>
                                    <RotateTransform3D>
                                        <RotateTransform3D.Rotation>
                                            <AxisAngleRotation3D
                                                x:Name="rotate2"
                                                Axis="0,1,0"/>
                                        </RotateTransform3D.Rotation>
                                    </RotateTransform3D>
                                </Transform3DGroup>
                            </GeometryModel3D.Transform>
                        </GeometryModel3D>
                        <!-- Set light source: -->
                        <DirectionalLight 
                            Color="Gray"
                            Direction="-1,-2,-1.5" />
                        <AmbientLight Color="Gray"/>
                    </Model3DGroup>
                </ModelUIElement3D>
            </ContainerUIElement3D>
            <!-- Set camera: -->
            <Viewport3D.Camera>
                <PerspectiveCamera 
                    Position="2,2,2"
                    LookDirection="-2,-2,-2"
                    UpDirection="0,1,0"/>
            </Viewport3D.Camera>
        </Viewport3D>
        <Window.Triggers>
            <EventTrigger 
                RoutedEvent="Window.Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation
                            Storyboard.TargetName="rotate1"
                            Storyboard.TargetProperty="Angle"
                            From="0" To="360" Duration="0:0:5"
                            RepeatBehavior="Forever"/>
                        <DoubleAnimation
                            Storyboard.TargetName="rotate2"
                            Storyboard.TargetProperty="Angle"
                            From="0" To="360" Duration="0:0:5"
                            RepeatBehavior="Forever"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Window.Triggers>
    </Window>

    le 2eme fait tourner les 2 memes exemplaires du meme cube chacun autour :
    -de l'axe Y
    -de son centre propre....
    code xaml form2

    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
     
    <Window x:Class="WpfCube.WindowRotationAxeObjet"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfCube"
            Title="Rotation sur Axe Objet" Height="300" Width="300">
        <Window.Resources>
            <local:CubeGeometry 
                x:Key="cube"
                Center="0,0,0"
                Length="0.5"
                Width="0.5"
                Height="1"/>
        </Window.Resources>
        <Viewport3D>
            <ContainerUIElement3D>
                <ModelUIElement3D>
                    <Model3DGroup>
                        <!-- Create first blue cube: -->
                        <GeometryModel3D 
                            Geometry="{Binding Source=
                            {StaticResource cube},Path=Mesh3D}">
                            <!-- Set material: -->
                            <GeometryModel3D.Material>
                                <DiffuseMaterial
                                    Brush="LightBlue"/>
                            </GeometryModel3D.Material>
                            <!-- Set rotation: -->
                            <GeometryModel3D.Transform>
                                <RotateTransform3D 
                                    CenterX="0"
                                    CenterY="0"
                                    CenterZ="0">
                                    <RotateTransform3D.Rotation>
                                        <AxisAngleRotation3D
                                            x:Name="rotate1"
                                            Axis="0,1,0"/>
                                    </RotateTransform3D.Rotation>
                                </RotateTransform3D>
                            </GeometryModel3D.Transform>
                        </GeometryModel3D>
                        <!-- Create another golden cube: -->
                        <GeometryModel3D 
                            Geometry="{Binding Source={StaticResource cube},
                            Path=Mesh3D}">
                            <!-- Set material: -->
                            <GeometryModel3D.Material>
                                <DiffuseMaterial
                                    Brush="Goldenrod"/>
                            </GeometryModel3D.Material>
                            <!-- Set rotation ce cube est decale de -1 pour etre visible : -->
                            <GeometryModel3D.Transform>
                                <Transform3DGroup>
                                    <TranslateTransform3D
                                        OffsetZ="-1"/>
                                    <RotateTransform3D
                                          CenterX="0"
                                          CenterY="0"
                                          CenterZ="-1">
                                        <RotateTransform3D.Rotation>
                                            <AxisAngleRotation3D
                                                x:Name="rotate2"
                                                Axis="0,1,0"/>
                                        </RotateTransform3D.Rotation>
                                    </RotateTransform3D>
                                </Transform3DGroup>
                            </GeometryModel3D.Transform>
                        </GeometryModel3D>
                        <!-- Set light source: -->
                        <DirectionalLight 
                            Color="Gray"
                            Direction="-1,-2,-1.5" />
                        <AmbientLight Color="Gray"/>
                    </Model3DGroup>
                </ModelUIElement3D>
            </ContainerUIElement3D>
            <!-- Set camera: -->
            <Viewport3D.Camera>
                <PerspectiveCamera 
                    Position="2,2,2"
                    LookDirection="-2,-2,-2"
                    UpDirection="0,1,0"/>
            </Viewport3D.Camera>
        </Viewport3D>
        <Window.Triggers>
            <EventTrigger 
                RoutedEvent="Window.Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation
                            Storyboard.TargetName="rotate1"
                            Storyboard.TargetProperty="Angle"
                            From="0" To="360" Duration="0:0:5"
                            RepeatBehavior="Forever"/>
                        <DoubleAnimation
                            Storyboard.TargetName="rotate2"
                            Storyboard.TargetProperty="Angle"
                            From="0" To="360" Duration="0:0:5"
                            RepeatBehavior="Forever"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Window.Triggers>
    </Window>

    le 3eme plus compliques fait
    1/tourner un exemplaire du 1er cube autour
    -de l'axe Y
    -de son centre (centre de gravite 0,0,0)
    -demarre à la fin de l'event Completed du storyboard une 2eme rotation sur
    -l'axe Y
    -mais sur centre decale à (-1,-1,-1)
    -dans un autre storyboard....
    1/tourner un exemplaire du 2eme cube(2 rotations combines) autour :
    -de l'axe Y
    -de son centre (centre de gravite 0,0,-1)
    -l'axe Y
    -mais sur le centre (0,0,0)
    code xaml form3
    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
     
     
    <Window x:Class="WpfCube.WindowRotationCombine"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfCube"
            Title="WindowRotationCombine" Height="300" Width="300">
        <Window.Resources>
            <local:CubeGeometry 
                x:Key="cube"
                Center="0,0,0"
                Length="0.5"
                Width="0.5"
                Height="1"/>
        </Window.Resources>
        <Viewport3D>
            <ContainerUIElement3D >
                <ModelUIElement3D >
                    <Model3DGroup>
                        <!-- Create first blue cube: -->
                        <!-- execute 2 rotations suivant le meme  axe mais successives(dans temps) et ayant des 
                        centres differents: -->
                        <GeometryModel3D 
                            Geometry="{Binding Source=
                            {StaticResource cube},Path=Mesh3D}">
                            <!-- Set material: -->
                            <GeometryModel3D.Material>
                                <DiffuseMaterial
                                    Brush="LightBlue"/>
                            </GeometryModel3D.Material>
                            <!-- Rotation1 axe y autour du centre du cube: -->
                            <GeometryModel3D.Transform>
                                <Transform3DGroup>
                                    <RotateTransform3D   
                                                x:Name="Rotation1"
                                                CenterX="0"
                                                CenterY="0"
                                                CenterZ="0">
                                        <RotateTransform3D.Rotation>
                                            <AxisAngleRotation3D
                                                x:Name="rotate1"
                                                Axis="0,1,0"/>
                                        </RotateTransform3D.Rotation>
                                    </RotateTransform3D>
                                    <!-- Rotation2 axe y autour du point (-1,-1,-1): -->
                                    <!--dans le code mais temporellement demarree apres rotation 1-->
                                    <RotateTransform3D 
                                               x:Name="Rotation1bis" 
                                               >
                                        <RotateTransform3D.Rotation>
                                            <AxisAngleRotation3D
                                                x:Name="rotate1bis"
                                                Axis="0,1,0"/>
                                    </RotateTransform3D.Rotation>
                                    </RotateTransform3D>
                                </Transform3DGroup>
                            </GeometryModel3D.Transform>
                        </GeometryModel3D>
                        <!-- Create another golden cube: -->
                        <!-- execute 2 rotations combinees (produit) suivant le meme  axe et ayant des 
                        centres differents: -->
                        <GeometryModel3D 
                            Geometry="{Binding Source={StaticResource cube},
                            Path=Mesh3D}">
                            <!-- Set material: -->
                            <GeometryModel3D.Material>
                                <DiffuseMaterial
                                    Brush="Goldenrod"/>
                            </GeometryModel3D.Material>
                            <!-- cube est decale de -1 pour etre visible : -->
                            <GeometryModel3D.Transform>
                                <Transform3DGroup>
                                    <TranslateTransform3D
                                        OffsetZ="-1"/>
                                    <!-- Rotation1 axe y autour du centre  -->
                                    <!-- son centre a ete translate en (0,0,-1) suite à TranslateTransform -->
                                   <!--tourne sur lui-meme--> 
                                    <RotateTransform3D
                                           CenterX="0"
                                           CenterY="0"
                                           CenterZ="-1">
                                        <RotateTransform3D.Rotation>
                                            <AxisAngleRotation3D
                                                x:Name="rotate2"
                                                Axis="0,1,0"/>
                                        </RotateTransform3D.Rotation>
                                    </RotateTransform3D>
                                    <!-- Rotation2 axe y autour de centre initial(0,0,0): -->
                                    <!--  rotation sur  l'orbite d'axe y -->
                                    <RotateTransform3D
                                           CenterX="0"
                                           CenterY="0"
                                           CenterZ="0">
                                        <RotateTransform3D.Rotation>
                                            <AxisAngleRotation3D
                                                x:Name="rotate2bis"
                                                Axis="0,1,0"/>
                                        </RotateTransform3D.Rotation>
                                    </RotateTransform3D>
                                </Transform3DGroup>
                            </GeometryModel3D.Transform>
                        </GeometryModel3D>
                        <!-- Set light source: -->
                        <DirectionalLight 
                            Color="Gray"
                            Direction="-1,-2,-1.5" />
                        <AmbientLight Color="Gray"/>
                    </Model3DGroup>
                </ModelUIElement3D>
            </ContainerUIElement3D>
            <!-- Set camera: -->
            <Viewport3D.Camera>
                <PerspectiveCamera 
                    Position="2,2,2"
                    LookDirection="-2,-2,-2"
                    UpDirection="0,1,0"/>
            </Viewport3D.Camera>
        </Viewport3D>
        <Window.Triggers>
            <EventTrigger 
                RoutedEvent="Window.Loaded">
                <!--demarre la sb "sb_Rotate1bis" a la fin de "sb_Rotate1"--> 
                <BeginStoryboard>
                    <Storyboard  x:Name="sb_Rotate1"
                                 Completed="sb_Rotate1_Completed">
                        <DoubleAnimation
                            Storyboard.TargetName="rotate1"
                            Storyboard.TargetProperty="Angle"
                            From="0" To="360" Duration="0:0:5"
                            FillBehavior="HoldEnd"
                            SpeedRatio="1"
                            RepeatBehavior="0:0:5" />
                    </Storyboard>
                </BeginStoryboard>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation
                            Storyboard.TargetName="rotate2"
                            Storyboard.TargetProperty="Angle"
                            From="0" To="360" Duration="0:0:5"
                            RepeatBehavior="Forever"/>
                        <DoubleAnimation
                            Storyboard.TargetName="rotate2bis"
                            Storyboard.TargetProperty="Angle"
                            From="0" To="360" Duration="0:0:5"
                            RepeatBehavior="Forever"/>
                    </Storyboard>
                </BeginStoryboard>
                <BeginStoryboard>
                    <Storyboard  x:Name="sb_Rotate1bis">
                        <DoubleAnimation
                            Storyboard.TargetName="rotate1bis"
                            Storyboard.TargetProperty="Angle"
                            From="0" To="360" Duration="0:0:5"
                            FillBehavior="HoldEnd"
                            SpeedRatio="2"
                            RepeatBehavior="Forever"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Window.Triggers>
    </Window>
    code behind cs form3
    Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
     
    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;
     
    namespace WpfCube
    {
        /// <summary>
        /// Logique d'interaction pour WindowRotationCombine.xaml
        /// </summary>
        public partial class WindowRotationCombine : Window
        {
            public WindowRotationCombine()
            {
                InitializeComponent();
            }
     
     
     
     
     
            private void sb_Rotate1_Completed(object sender, EventArgs e)
            {
                this.Rotation1bis.CenterX = -1.0;
                this.Rotation1bis.CenterY = -1.0;
                this.Rotation1bis.CenterZ = -1.0;
                this.sb_Rotate1bis.Begin( );
            }
        }
    }

    Ce code xaml montre que une rotation necessite non seulement un axe mais egalement un centre.....
    De plus sequencer les rotations succesives necessite d'agir :
    - sur le storyboard et utiliser l'event Completed pour demarrer par code un 2eme pivotement ....
    bon code............

  4. #4
    Futur Membre du Club
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mai 2012
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Mai 2012
    Messages : 4
    Points : 5
    Points
    5
    Par défaut
    Bonsoir à tous et merci de vos réponses.

    Pour ce qui est du code, je suis en train de faire des tests pour savoir ce qu'il cloche. Le voici donc simplifié. J'ai donc 3 boutons qui font faire une rotation différente à mon objet 3D :
    - Un sur l'axe (0,1,0) avec un angle positif,
    - Un sur le même axe mais avec un angle négatif (ça tourne dans l'autre sens en fait)
    - Un dernier sur l'axe (1,1,0).

    Si j'appuie 2 fois sur le même bouton, mon objet tourne bien toujours dans le même sens. Si j'appuie genre 10 fois sur le bouton 1, puis sur le bouton 2, mon objet repart bien dans l'autre sens depuis sa position résultant des 10 rotations précédentes. Par contre, si j'appuie sur le bouton 3, mon objet fait bien la rotation, mais depuis sa position de départ -> c'est donc je que je voudrais éviter. Je souhaite que mon objet fasse sa rotation d'axe (1,1,0) mais en partant de la position à laquelle il se retrouve après plusieurs rotations d'axes différents...

    Bref, xaml :
    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    <Window x:Class="objet_3D.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" WindowState="Maximized" Name="mainWindow" Background="Black">
        <Grid>
            <Canvas>
                <Canvas>
     
                    <Button Content="rot.droite" Name="rotationDroite" Width="50" Height="50" Click="rotationDroite_Click" />
                    <Button Content="rot.gauche" Name="rotationGauche" Width="50" Height="50" Margin="50" Click="rotationGauche_Click" />
                    <Button Content="rot.basgauche" Name="rotationBasGauche" Width="50" Height="50" Margin="100" Click="rotationBasGauche_Click" />
                    <Button Content="infos" Name="infos" Width="50" Height="50" Margin="150,0,0,0" Click="infos_Click" />
                    <Viewbox x:Name="viewbox" ClipToBounds="true">
                        <Viewport3D x:Name="viewport" ClipToBounds="true" Width="600" Height="800">
                            <Viewport3D.Resources>
                                <ResourceDictionary>
                                    <MaterialGroup x:Key="compteurMR1">
                                        <DiffuseMaterial>
                                            <DiffuseMaterial.Brush>
                                                <ImageBrush ImageSource="Compteur1UVMaterialMR1.png" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 1 1" AlignmentX="Left" AlignmentY="Top" Opacity="1.000000" />
                                            </DiffuseMaterial.Brush>
                                        </DiffuseMaterial>
                                        <SpecularMaterial SpecularPower="85.3333">
                                            <SpecularMaterial.Brush>
                                                <SolidColorBrush Color="#FFFFFF" Opacity="1.000000" />
                                            </SpecularMaterial.Brush>
                                        </SpecularMaterial>
                                    </MaterialGroup>
                                    <Transform3DGroup x:Key="SceneTR7">
                                        <TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0" />
                                        <ScaleTransform3D ScaleX="1" ScaleY="1" ScaleZ="1" />
                                        <RotateTransform3D>
                                            <RotateTransform3D.Rotation>
                                                <AxisAngleRotation3D Angle="0" Axis="0 1 0" />
                                            </RotateTransform3D.Rotation>
                                        </RotateTransform3D>
                                        <TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0" />
                                    </Transform3DGroup>
                                    <Transform3DGroup x:Key="CubeOR9TR8">
                                        <TranslateTransform3D OffsetX="0.0453084" OffsetY="-0.0790308" OffsetZ="-0.0187794" />
                                        <ScaleTransform3D ScaleX="1" ScaleY="1" ScaleZ="1" />
                                        <RotateTransform3D>
                                            <RotateTransform3D.Rotation>
                                                <AxisAngleRotation3D Angle="179.2757609" Axis="0.007474061364 -0.9998974132 -0.01221889765" />
                                            </RotateTransform3D.Rotation>
                                        </RotateTransform3D>
                                        <TranslateTransform3D OffsetX="-0.0453084" OffsetY="0.0790308" OffsetZ="0.0187794" />
                                    </Transform3DGroup>
                                    <MeshGeometry3D x:Key="CubeOR9GR10" ... " />
                                </ResourceDictionary>
                            </Viewport3D.Resources>
     
                            <Viewport3D.Camera>
                                <PerspectiveCamera x:Name="Camera" FarPlaneDistance="3000" LookDirection="0,0,-1" UpDirection="0,1,0" NearPlaneDistance="4" Position="0,0,12" FieldOfView="39.5978" />
                            </Viewport3D.Camera>
     
                            <ModelVisual3D x:Name="Compteur">
                                <ModelVisual3D.Content>
                                    <Model3DGroup x:Name="Scene" Transform="{DynamicResource SceneTR7}">
                                        <!-- Scene (XAML Path = ) -->
                                        <AmbientLight Color="#646464" />
                                        <DirectionalLight Color="#FFFFFF" Direction="-0.612372,-0.5,-0.612372" />
                                        <DirectionalLight Color="#FFFFFF" Direction="0.612372,-0.5,-0.612372" />
                                        <Model3DGroup x:Name="CubeOR9" Transform="{DynamicResource CubeOR9TR8}">
                                            <!-- Cube (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3]) -->
                                            <GeometryModel3D x:Name="CubeOR9GR10" Geometry="{DynamicResource CubeOR9GR10}" Material="{DynamicResource compteurMR1}" BackMaterial="{DynamicResource compteurMR1}" />
                                        </Model3DGroup>
                                    </Model3DGroup>
                                </ModelVisual3D.Content>
                            </ModelVisual3D>
                        </Viewport3D>
                    </Viewbox>
                </Canvas>
            </Canvas>
        </Grid>
    </Window>

    et .cs:

    Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;
    using System.Windows.Media.Media3D;
    using System.Windows.Threading;
     
    namespace objet_3D
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            private double angle = 5;
            private AxisAngleRotation3D rotation;
            private DoubleAnimation animation;
            private RotateTransform3D rotateTransform3D;
     
     
            public MainWindow()
            {
                InitializeComponent();
     
                rotation = new AxisAngleRotation3D();
                animation = new DoubleAnimation();
                transform3DGroup = new Transform3DGroup();
                rotateTransform3D = new RotateTransform3D();
     
     
            }
     
     
            private void rotationDroite_Click(object sender, RoutedEventArgs e)
            {
                animation.From = angle;
                animation.To = angle += 5;
                animation.Duration = TimeSpan.FromSeconds(0.2);
                rotation.Axis = new Vector3D(0, 1, 0);
                rotation.Angle = angle;
                Compteur.Transform = new RotateTransform3D(rotation);
                rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation);
            }
     
            private void rotationGauche_Click(object sender, RoutedEventArgs e)
            {
                animation.From = angle;
                animation.To = angle -= 5;
                animation.Duration = TimeSpan.FromSeconds(0.2);
                rotation.Axis = new Vector3D(0, 1, 0);
                rotation.Angle = angle;
                Compteur.Transform = new RotateTransform3D(rotation);
                rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation);
            }
     
            private void rotationBasGauche_Click(object sender, RoutedEventArgs e)
            {
                animation.From = angle;
                animation.To = angle += 5;
                animation.Duration = TimeSpan.FromSeconds(0.2);
                rotation.Axis = new Vector3D(1, 1, 0);
                rotation.Angle = angle;
                Compteur.Transform = new RotateTransform3D(rotation);
                rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation);
            }

    Voilà.
    Pour ta remarque MABROUKI :

    une rotation necessite non seulement un axe mais egalement un centre.....
    Me serais-ce possible de changer dynamiquement le centre de chaque rotation selon le mouvement qu'aurai fait mon objet auparavant ?

    Et pour te répondre DonQuiche, il me semble que tu parles des Quaternions non ? Du coup en effet, la rotation (n+1) doit être une multiplication de la matrice de la rotation (n) je suppose. Dois-je m'y prendre manuellement alors ? C'est à dire récupérer "les résultats de la rotation 1" puis "multiplier ça par la rotation 2" ?

  5. #5
    Expert confirmé Avatar de DonQuiche
    Inscrit en
    Septembre 2010
    Messages
    2 741
    Détails du profil
    Informations forums :
    Inscription : Septembre 2010
    Messages : 2 741
    Points : 5 493
    Points
    5 493
    Par défaut
    Bonjour.

    Oui, en effet, je parlais de quaternions mais en fait en XAML ceux-ci sont implicitement générés par les transformations et les compositions sont faîtes par les Transform3DGroup : tous ces objets vont se charger de créer des quaternions, de les multiplier comme il convient puis de les passer à la carte graphique. Désolé pour la petite digression : comme expliqué plus tôt je ne me rappelais même plus à quoi ça ressemblait.

    Au vu de ton code la problème est évident cela dit : ta transformation est réinitialisée parce que tu assignes une nouvelle transformation, tout simplement. Ton objet lui-même n'a aucune position ou alignement intrinsèques autres que ceux par défaut, si bien qu'il ne garde pas le souvenir des précédentes transformations. Tu dois donc lui spécifier une transformation qui décrira l'ensemble des opérations nécessaires pour passer de la position et de l'alignement par défauts à ceux que tu désires.

    La solution est la suivante : tu dois lors de l'initialisation affecter à ton objet un Transform3DGroup qui contiendra deux rotations, une pour chaque axe. Puis, lors des clics, tu animeras l'angle de l'une ou l'autre des deux rotations mais sans assigner de nouvelle transformation.

  6. #6
    Futur Membre du Club
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mai 2012
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Mai 2012
    Messages : 4
    Points : 5
    Points
    5
    Par défaut
    Bonjour,

    J'ai fait ce que tu m'a conseillé DonQuiche, et ça ne fonctionne pas, j'ai toujours mon objet qui se remet à sa place lorsque je lui applique une rotation différente de celle appliquée précédemment.
    Pour la rotation d'axe (0,1,0), l'objet pivote toujours bien comme je le souhaite.
    J'ai sûrement fait quelque chose qui ne va pas :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;
    using System.Windows.Media.Media3D;
    using System.Windows.Threading;
     
    namespace Compteur_3D
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            private double angleX = 5, angleY = 5;
            private AxisAngleRotation3D rotationX, rotationY;
            private DoubleAnimation animation;
            private Transform3DGroup transform3DGroup;
            private RotateTransform3D rotateTransform3DX, rotateTransform3DY;
     
     
            public MainWindow()
            {
                InitializeComponent();
     
                rotationX = new AxisAngleRotation3D
                               {
                                   Axis = new Vector3D(0,1,0)
                               };
     
                rotationY = new AxisAngleRotation3D
                               {
                                   Axis = new Vector3D(1, 1, 0)
                               };
     
                animation = new DoubleAnimation();
     
                rotateTransform3DX = new RotateTransform3D(rotationX);
                rotateTransform3DY = new RotateTransform3D(rotationY);
     
                transform3DGroup = new Transform3DGroup();
                transform3DGroup.Children.Add(rotateTransform3DX);
                transform3DGroup.Children.Add(rotateTransform3DY);
     
                objet3d.Transform = transform3DGroup;
     
     
     
            }
     
     
            private void rotationDroite_Click(object sender, RoutedEventArgs e)
            {
                animation.From = angleX;
                animation.To = angleX += 5;
                animation.Duration = TimeSpan.FromSeconds(0.2);
                objet3d.Transform = rotateTransform3DX;
                rotationX.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation);
            }
     
            private void rotationGauche_Click(object sender, RoutedEventArgs e)
            {
                animation.From = angleX;
                animation.To = angleX -= 5;
                animation.Duration = TimeSpan.FromSeconds(0.2);
                objet3d.Transform = rotateTransform3DX;
                rotationX.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation);
            }
     
            private void rotationBasGauche_Click(object sender, RoutedEventArgs e)
            {
                animation.From = angleY;
                animation.To = angleY += 5;
                animation.Duration = TimeSpan.FromSeconds(0.2);
                Compteur.Transform = rotateTransform3DY;
                rotationY.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation);
            }
        }
    }
    Il y a aussi le fait que j'ai deux valeurs d'angle, une pour l'axe (0,1,0) et une pour l'axe (1,1,0). Au départ je n'en avais qu'une, et ça posait problème car lorsque j'appliquais une rotation d'axe (1,1,0) après avoir fait pivoter plusieurs fois mon objet, la rotation s'effectuait directement avec la valeur utilisée dans les rotations précédentes...

  7. #7
    Expert confirmé Avatar de DonQuiche
    Inscrit en
    Septembre 2010
    Messages
    2 741
    Détails du profil
    Informations forums :
    Inscription : Septembre 2010
    Messages : 2 741
    Points : 5 493
    Points
    5 493
    Par défaut
    Dans ton code tu assignes à chaque fois comme transformation soit rotateTransform3DX, soit rotateTransform3DY, voilà pourquoi ça ne fonctionne pas. Vire ces assignations de tes trois handlers : la transformation doit toujours être le groupe tout entier, comme tu l'as défini dans ton constructeur. Une fois ces trois lignes supprimées, tout devrait fonctionner correctement.

  8. #8
    Futur Membre du Club
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mai 2012
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Mai 2012
    Messages : 4
    Points : 5
    Points
    5
    Par défaut
    Parfait, ça fonctionne !
    Merci bien

    Pour info, voilà le code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;
    using System.Windows.Media.Media3D;
    using System.Windows.Threading;
     
    namespace objet3d
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            private double angleX = 5, angleY = 5;
            private AxisAngleRotation3D rotationX, rotationY;
            private DoubleAnimation animation;
            private Transform3DGroup transform3DGroup;
            private RotateTransform3D rotateTransform3DX, rotateTransform3DY;
     
     
            public MainWindow()
            {
                InitializeComponent();
     
                rotationX = new AxisAngleRotation3D
                               {
                                   Axis = new Vector3D(0,1,0)
                               };
     
                rotationY = new AxisAngleRotation3D
                               {
                                   Axis = new Vector3D(1, 1, 0)
                               };
     
                animation = new DoubleAnimation();
     
                rotateTransform3DX = new RotateTransform3D(rotationX);
                rotateTransform3DY = new RotateTransform3D(rotationY);
     
                transform3DGroup = new Transform3DGroup();
                transform3DGroup.Children.Add(rotateTransform3DX);
                transform3DGroup.Children.Add(rotateTransform3DY);
     
                objet3d.Transform = transform3DGroup;
     
     
     
            }
     
     
            private void rotationDroite_Click(object sender, RoutedEventArgs e)
            {
                animation.From = angleX;
                animation.To = angleX += 5;
                animation.Duration = TimeSpan.FromSeconds(0.2);;
                rotationX.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation);
            }
     
            private void rotationGauche_Click(object sender, RoutedEventArgs e)
            {
                animation.From = angleX;
                animation.To = angleX -= 5;
                animation.Duration = TimeSpan.FromSeconds(0.2);
                rotationX.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation);
            }
     
            private void rotationBasGauche_Click(object sender, RoutedEventArgs e)
            {
                animation.From = angleY;
                animation.To = angleY += 5;
                animation.Duration = TimeSpan.FromSeconds(0.2);
                rotationY.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation);
            }
        }
    }

  9. #9
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 442
    Points
    4 442
    Par défaut
    bonjour Lordonly
    Don Quiche m'as precede.Effectivement tu dois passer par un GroupTransform pour :
    - composer tes "transformations"(multiplication des matrices)..
    - faut faire à attention à l'ordre d'ajout au groupe(multiplication des matrices n'est pas commutative)...
    -tu as 2 props à animer :axeX & axeY de l'objet.....
    -utilise HandoffBehavior.Compose pour la fluidite car avec 2 ou plusieurs animations successives ,la suivante peut ecraser la precedente........

    Enfin ton code modifie avec le GroupTransform...
    Code-Behind:
    Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
     
    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 System.Windows.Media.Animation;
     
    namespace WpfCubeLord
    {
        /// <summary>
        /// Logique d'interaction pour MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
                private double angleX = 5;
                private double angleY = 5;
                //2 props distinctes à animer au lieu d'une.L'axe Y  & l'axe X de l'objet..... 
                private AxisAngleRotation3D AxisRotationX;
                private AxisAngleRotation3D AxisRotationY; 
                private DoubleAnimation animation;
     
                private Transform3DGroup transform3DGroup;
     
                public MainWindow()
                {
                    InitializeComponent();
     
                    AxisRotationY = new AxisAngleRotation3D();
                    AxisRotationX = new AxisAngleRotation3D();
     
                    animation = new DoubleAnimation();
                    transform3DGroup = new Transform3DGroup();
                    //le groupe est necessaire car il y a 2 props....
                    //le groupe permet de les ordonner....
                    //BAS en premier
                    transform3DGroup.Children.Add(new RotateTransform3D(AxisRotationX));
     
                    //GAUCHE en second
                    transform3DGroup.Children.Add(new RotateTransform3D(AxisRotationY));
     
                    //le GroupTransform est applique à l'objet..
                    Compteur.Transform = transform3DGroup;
     
     
     
     
     
                }
     
            //NB:pour le centre tu peux ecrire
            //transform3DGroup.Children.Add(new RotateTransform3D(AxisRotationX,new Point3D(-1,-1,-1)));
     
            private void rotationDroite_Click(object sender, RoutedEventArgs e)
            {
                this.tbDroite.Text = angleY.ToString(); 
     
     
                animation.From = angleY;
                animation.To = angleY += 5;
                animation.Duration = TimeSpan.FromSeconds(0.2);
                AxisRotationY.Axis = new Vector3D(0, 1, 0);
                AxisRotationY.Angle = angleY;
     
                AxisRotationY.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation, HandoffBehavior.Compose);
            }
     
            private void rotationGauche_Click(object sender, RoutedEventArgs e)
            {
                this.tbGauche.Text = angleY.ToString(); 
     
                animation.From = angleY;
                animation.To = angleY -= 5;
                animation.Duration = TimeSpan.FromSeconds(0.2);
                AxisRotationY.Axis = new Vector3D(0, 1, 0);
                AxisRotationY.Angle = angleY;
     
                AxisRotationY.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation);
     
            }
     
     
            private void rotationBasGauche_Click(object sender, RoutedEventArgs e)
            {
     
                // BAS : angleX (applique à prop AxisRotationX)..... 
                this.tbBGAngleX.Text = angleX.ToString(); 
                animation.Duration = TimeSpan.FromSeconds(0.2);
                animation.From = angleX;
                animation.To = angleX -= 5;
                AxisRotationX.Axis = new Vector3D(1, 0, 0);
                AxisRotationX.Angle = angleY;
                AxisRotationX.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation,  HandoffBehavior.Compose);
     
                //GAUCHE :angleY (applique à prop AxisRotationY)..... 
                this.tbBGAngleY.Text = angleY.ToString(); 
                animation.Duration = TimeSpan.FromSeconds(0.2);
                animation.From = angleY;
                animation.To = angleY -= 5;
                AxisRotationY.Axis = new Vector3D(0, 1, 0);
                AxisRotationY.Angle = angleY;
                AxisRotationY.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation, HandoffBehavior.Compose);
     
            }
     
            private void infos_Click(object sender, RoutedEventArgs e)
            {
     
            }
     
     
        }
    }
    Code-xaml du winform:
    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
     
    <Window x:Class="WpfCubeLord.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfCubeLord"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Canvas>
                <Button 
                    Content="rot.droite" 
                    Name="rotationDroite" 
                    Click="rotationDroite_Click" />
                <Label x:Name="lblDroite" Content="droite" Margin="150,0,0,20"/>
                <TextBlock x:Name="tbDroite" Background="BlanchedAlmond" Margin="250,0,0,20"/>
                <Button 
                    Content="rot.gauche" 
                    Name="rotationGauche" 
                    Margin="30" 
                    Click="rotationGauche_Click" />
                <Label x:Name="lblGauche" Content="gauche" Margin="150,30,0,20"/>
                <TextBlock x:Name="tbGauche" Background="BlanchedAlmond" Margin="250,30,0,20"/>
                <Button
                    Content="rot.basgauche"
                    Name="rotationBasGauche" 
                    Margin="60" 
                    Click="rotationBasGauche_Click" />
                <Label x:Name="lblBasGaucheX" Content="BasX" Margin="150,60,0,20"/>
                <TextBlock x:Name="tbBGAngleX" Background="BlanchedAlmond" Margin="250,60,0,20"/>
                <Label x:Name="lblBasGaucheY" Content="GaucheY" Margin="150,90,0,20"/>
                <TextBlock x:Name="tbBGAngleY" Background="BlanchedAlmond" Margin="250,90,0,20"/>
                <Button 
                    Content="infos" 
                    Name="infos" 
                    Margin="60,0,0,0" Click="infos_Click" />
                <Viewbox 
                    x:Name="viewbox"
                    ClipToBounds="true">
                    <Viewport3D 
                        x:Name="viewport" 
                        ClipToBounds="true" 
                        Width="600" 
                        Height="800">
                        <Viewport3D.Resources>
                            <ResourceDictionary>
                                <MaterialGroup x:Key="compteurMR1">
                                    <DiffuseMaterial
                                            Brush="LightBlue">
                                    </DiffuseMaterial>
                                </MaterialGroup>
                                <Transform3DGroup 
                                    x:Key="SceneTR7">
                                    <TranslateTransform3D
                                        OffsetX="0" OffsetY="0" OffsetZ="0" />
                                    <ScaleTransform3D 
                                        ScaleX="1" ScaleY="1" ScaleZ="1" />
                                    <RotateTransform3D>
                                        <RotateTransform3D.Rotation>
                                            <AxisAngleRotation3D Angle="0" Axis="0 1 0" />
                                        </RotateTransform3D.Rotation>
                                    </RotateTransform3D>
                                    <TranslateTransform3D 
                                        OffsetX="0" OffsetY="0" OffsetZ="0" />
                                </Transform3DGroup>
                                <Transform3DGroup
                                    x:Key="CubeOR9TR8">
                                    <TranslateTransform3D 
                                        OffsetX="0.0453084" 
                                        OffsetY="-0.0790308" 
                                        OffsetZ="-0.0187794" />
                                    <ScaleTransform3D 
                                        ScaleX="1" ScaleY="1" ScaleZ="1" />
                                    <RotateTransform3D>
                                        <RotateTransform3D.Rotation>
                                            <AxisAngleRotation3D 
                                                Angle="179.2757609" Axis="0.007474061364 -0.9998974132 -0.01221889765" />
                                        </RotateTransform3D.Rotation>
                                    </RotateTransform3D>
                                    <TranslateTransform3D
                                        OffsetX="-0.0453084" OffsetY="0.0790308" OffsetZ="0.0187794" />
                                </Transform3DGroup>
                            <local:CubeGeometry 
                                x:Key="CubeOR9GR10"
                                Center="0,0,0"
                                Length="1.0"
                                Width="1.0"
                                Height="1.5"/> 
                        </ResourceDictionary>
                    </Viewport3D.Resources>
                    <Viewport3D.Camera>
                        <PerspectiveCamera
                            x:Name="Camera"
                            Position="2,2,2"
                            LookDirection="-2,-2,-2"
                            UpDirection="0,1,0"
                            FieldOfView="60.000"
                                />
                    </Viewport3D.Camera>
                        <ModelVisual3D x:Name="Compteur">
                            <ModelVisual3D.Content>
                                <Model3DGroup
                                    x:Name="Scene" Transform="{DynamicResource SceneTR7}">
                                    <!-- Scene (XAML Path = ) -->
                                    <!--<AmbientLight Color="LightGray" />-->
                                    <!--<DirectionalLight 
                                        Color="Gray"
                                        Direction="-1,-2,-1.5" />-->
                                    <SpotLight  Color="Blue" Position="1,1,8"  Direction="-2,-2,-2" InnerConeAngle="90" OuterConeAngle="270"/>
                                    <SpotLight Color="Yellow" Position="4,20,4" Direction="-1.0,-1.0,-1.0" InnerConeAngle="90.0" OuterConeAngle="180"/>
                                    <SpotLight Color="Red" Position="-1,-1,-8" Direction="2.0,2.0,2.0" InnerConeAngle="90.0" OuterConeAngle="270"/>
                                    <Model3DGroup 
                                        x:Name="CubeOR9" 
                                        Transform="{DynamicResource CubeOR9TR8}">
                                    <!-- Cube (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3]) -->
                                        <GeometryModel3D 
                                            x:Name="CubeOR9GR10" 
                                            Geometry="{Binding Source={StaticResource  CubeOR9GR10},Path=Mesh3D }">
                                            <GeometryModel3D.Material>
                                                <DiffuseMaterial Brush="LightBlue"/>
                                            </GeometryModel3D.Material>
                                            <GeometryModel3D.BackMaterial>
                                                <DiffuseMaterial Brush="Blue"/> 
                                            </GeometryModel3D.BackMaterial>
                                        </GeometryModel3D>
                                    </Model3DGroup>
                                </Model3DGroup>
                            </ModelVisual3D.Content>
                        </ModelVisual3D>
                    </Viewport3D>
                </Viewbox>
            </Canvas>
        </Grid>
    </Window>
    Pour specifier le centre de rotation la surcharge du new RotateTransform
    Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    public RotateTransform3D(
    	Rotation3D rotation,
    	Point3D center
    )
     
     
    new RotateTransform3D(AxisRotationX,new Point3D(-1,-1,-1)))
    bon code................

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Galerie d'image en rotation avec wpf
    Par Invité dans le forum Windows Presentation Foundation
    Réponses: 2
    Dernier message: 08/02/2014, 11h48
  2. Rotation de Bitmap -> ScanLine
    Par jujuesteban dans le forum Langage
    Réponses: 7
    Dernier message: 03/07/2003, 15h11
  3. Rotation d'un bouton ?
    Par ken_survivant dans le forum Composants
    Réponses: 3
    Dernier message: 01/04/2003, 18h16
  4. matrice et rotation
    Par charly dans le forum Algorithmes et structures de données
    Réponses: 4
    Dernier message: 07/12/2002, 17h59
  5. algo : rotation d'objet 3d
    Par numeror dans le forum Algorithmes et structures de données
    Réponses: 4
    Dernier message: 19/08/2002, 22h58

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo