Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Helix toolkit Rotate 3D Model

I'm new to WPF and I'm trying to make a program that displays a 3d model (that is saved on my computer) and rotate it based on button clicks. I would like to have three buttons to rotate the object about the x, y, and z axes. I have code that will display the model but I am unsure how to rotate it using button clicks. Here is what I have so far;

C#

public MainWindow()
{
    InitializeComponent();
    ModelVisual3D device3D = new ModelVisual3D();
    device3D.Content = Display3d(MODEL_PATH);

    // Add to view port
    viewPort3d.Children.Add(device3D);
}

private Model3D Display3d(string model)
{
    Model3D device = null;
    try
    {
        //Adding a gesture here
        viewPort3d.RotateGesture = new MouseGesture(MouseAction.LeftClick);

        //Import 3D model file
        ModelImporter import = new ModelImporter();

        //Load the 3D model file
        device = import.Load(model);
    }
    catch (Exception e)
    {
        // Handle exception in case can not find the 3D model file
        MessageBox.Show("Exception Error : " + e.StackTrace);
    }
    return device;
}
private void buttonX_Click(object sender, RoutedEventArgs e)
{
    //not sure what to put in here
}

XAML

<Grid Margin="0,0,6,94" RenderTransformOrigin="0.5,0.5">
    <Grid.RenderTransform>
        <TransformGroup>
            <ScaleTransform/>
            <SkewTransform/>
            <RotateTransform Angle="-0.275"/>
            <TranslateTransform/>
        </TransformGroup>
    </Grid.RenderTransform>
    <helix:HelixViewport3D x:Name="viewPort3d" ZoomExtentsWhenLoaded="true" Margin="0,0,10,64" >
        <!-- Remember to add light to the scene -->
        <helix:DefaultLights/>
        <ModelVisual3D x:Name="Models"/>
    </helix:HelixViewport3D>
    <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Margin="417,219,0,0" Stroke="Black" VerticalAlignment="Top" Width="100" RenderTransformOrigin="0.146,-0.196"/>
    <Button x:Name="buttonX" Content="ButtonX" HorizontalAlignment="Left" Height="30" Margin="216,356,0,-60" VerticalAlignment="Top" Width="104" Click="buttonX_Click"/>
</Grid>

I'm currently using the Helix tool-kit but if there is an easier way, please let me know.

like image 623
user3471066 Avatar asked Mar 13 '23 16:03

user3471066


1 Answers

From the wording of your question I'll assume you want to rotate the model and not the camera, in which case save device3D somewhere and do this:

    private void buttonX_Click(object sender, RoutedEventArgs e)
    {
        var axis = new Vector3D(0, 0, 1);
        var angle = 10;

        var matrix = device3D.Transform.Value;
        matrix.Rotate(new Quaternion(axis, angle));

        device3D.Transform = new MatrixTransform3D(matrix);
    }
like image 66
Mark Feldman Avatar answered Mar 17 '23 12:03

Mark Feldman