Click here to Skip to main content
15,896,915 members
Articles / Desktop Programming / WPF

D3dScenePresenter - How to present and manipulate a 3D scene using MDX

Rate me:
Please Sign up or sign in to vote.
4.57/5 (14 votes)
15 Feb 2013CPOL14 min read 45.4K   1.6K   25  
This article shows how we can present a 3D scene and, perform common operations (zoom, rotate, move, zoom to specific region, adjust the camera to view the whole of the scene, and pick a 3D shape on a specific region on the rendered surface) on it, using Managed DirectX.
<Window x:Class="MdxScene.Example.SceneManipulationExampleWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:MdxSceneControls="clr-namespace:MdxScene.Controls;assembly=MdxScene"
        Title="Scene manipulation example" Height="650" Width="850" >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Border BorderBrush="DarkGreen" BorderThickness="2" Margin="10">
            <MdxSceneControls:D3dScenePresenter Name="mdxSceneHost"
                                                D3dSurfaceWidth="2000"
                                                D3dSurfaceHeight="2000"
                                                D3dSurfaceMouseWheel="mdxSceneHost_D3dSurfaceMouseWheel"
                                                IsMouseOverShapeTestEnabled="True"
                                                MouseOverShapeChanged="mdxSceneHost_MouseOverShapeChanged" />
        </Border>

        <StackPanel Grid.Column="1" HorizontalAlignment="Center"
                    Margin="10">
            <TextBlock Text="Camera Actions"
                       HorizontalAlignment="Center"
                       Margin="10"
                       Foreground="DarkGreen"
                       FontSize="22" />
            <TextBlock Text="Camera Zoom:" 
                       HorizontalAlignment="Left"
                       Foreground="DarkBlue" />
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="Horizontal Zoom: "
                           HorizontalAlignment="Left"
                           VerticalAlignment="Center" />
                <RepeatButton Content="-" Grid.Column="1" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.HorizontalZoomCommand}"
                    CommandParameter="1.1"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <RepeatButton Content="+" Grid.Column="2" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.HorizontalZoomCommand}"
                    CommandParameter="0.9"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <TextBlock Text="Vertical Zoom: " Grid.Row="1"
                           HorizontalAlignment="Left"
                           VerticalAlignment="Center" />
                <RepeatButton Content="-" Grid.Column="1" Grid.Row="1" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.VerticalZoomCommand}"
                    CommandParameter="1.1"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <RepeatButton Content="+" Grid.Column="2" Grid.Row="1" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.VerticalZoomCommand}"
                    CommandParameter="0.9"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <TextBlock Text="Uniform Zoom: " Grid.Row="2"
                           HorizontalAlignment="Left"
                           VerticalAlignment="Center" />
                <RepeatButton Content="-" Grid.Column="1" Grid.Row="2" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.UniformZoomCommand}"
                    CommandParameter="1.1"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <RepeatButton Content="+" Grid.Column="2" Grid.Row="2" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.UniformZoomCommand}"
                    CommandParameter="0.9"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>

            </Grid>
            <Border BorderBrush="DarkGreen" BorderThickness="1" Margin="0,5" Height="2" />
            <TextBlock Text="Camera Move:"
                       HorizontalAlignment="Left"
                       Foreground="DarkBlue" />
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="Horizontal Move: "
                           HorizontalAlignment="Left"
                           VerticalAlignment="Center"/>
                <RepeatButton Content="-" Grid.Column="1" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.HorizontalMoveCommand}"
                    CommandParameter="-0.1"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <RepeatButton Content="+" Grid.Column="2" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.HorizontalMoveCommand}"
                    CommandParameter="0.1"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <TextBlock Text="Vertical Move: " Grid.Row="1"
                           HorizontalAlignment="Left"
                           VerticalAlignment="Center"/>
                <RepeatButton Content="-" Grid.Column="1" Grid.Row="1" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.VerticalMoveCommand}"
                    CommandParameter="-0.1"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <RepeatButton Content="+" Grid.Column="2" Grid.Row="1" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.VerticalMoveCommand}"
                    CommandParameter="0.1"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <TextBlock Text="Look Direction Move: " Grid.Row="2"
                           HorizontalAlignment="Left"
                           VerticalAlignment="Center"/>
                <RepeatButton Content="-" Grid.Column="1" Grid.Row="2" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.MoveAtLookDirectionCommand}"
                    CommandParameter="-0.1"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <RepeatButton Content="+" Grid.Column="2" Grid.Row="2" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.MoveAtLookDirectionCommand}"
                    CommandParameter="0.1"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>

            </Grid>

            <Border BorderBrush="DarkGreen" BorderThickness="1" Margin="0,5" Height="2" />
            <TextBlock Text="Camera Rotation:"
                       HorizontalAlignment="Left"
                       Foreground="DarkBlue" />
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="X Axis Rotation: "
                           HorizontalAlignment="Left"
                           VerticalAlignment="Center"/>
                <RepeatButton Content="-" Grid.Column="1" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.RotateCameraAroundXAxisCommand}"
                    CommandParameter="-0.02"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <RepeatButton Content="+" Grid.Column="2" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.RotateCameraAroundXAxisCommand}"
                    CommandParameter="0.02"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <TextBlock Text="Y Axis Rotation: " Grid.Row="1"
                           HorizontalAlignment="Left"
                           VerticalAlignment="Center"/>
                <RepeatButton Content="-" Grid.Column="1" Grid.Row="1" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.RotateCameraAroundYAxisCommand}"
                    CommandParameter="-0.02"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <RepeatButton Content="+" Grid.Column="2" Grid.Row="1" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.RotateCameraAroundYAxisCommand}"
                    CommandParameter="0.02"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <TextBlock Text="Z Axis Rotation: " Grid.Row="2"
                           HorizontalAlignment="Left"
                           VerticalAlignment="Center"/>
                <RepeatButton Content="-" Grid.Column="1" Grid.Row="2" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.RotateCameraAroundZAxisCommand}"
                    CommandParameter="-0.02"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <RepeatButton Content="+" Grid.Column="2" Grid.Row="2" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.RotateCameraAroundZAxisCommand}"
                    CommandParameter="0.02"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>

            </Grid>
            <Border BorderBrush="DarkGreen" BorderThickness="1" Margin="0,5" Height="2" />
            <TextBlock Text="Target Rotation:"
                       HorizontalAlignment="Left"
                       Foreground="DarkBlue" />
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="X Axis Rotation: "
                           HorizontalAlignment="Left"
                           VerticalAlignment="Center"/>
                <RepeatButton Content="-" Grid.Column="1" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.RotateTargetAroundXAxisCommand}"
                    CommandParameter="-0.02"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <RepeatButton Content="+" Grid.Column="2" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.RotateTargetAroundXAxisCommand}"
                    CommandParameter="0.02"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <TextBlock Text="Y Axis Rotation: " Grid.Row="1"
                           HorizontalAlignment="Left"
                           VerticalAlignment="Center"/>
                <RepeatButton Content="-" Grid.Column="1" Grid.Row="1" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.RotateTargetAroundYAxisCommand}"
                    CommandParameter="-0.02"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <RepeatButton Content="+" Grid.Column="2" Grid.Row="1" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.RotateTargetAroundYAxisCommand}"
                    CommandParameter="0.02"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <TextBlock Text="Z Axis Rotation: " Grid.Row="2"
                           HorizontalAlignment="Left"
                           VerticalAlignment="Center"/>
                <RepeatButton Content="-" Grid.Column="1" Grid.Row="2" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.RotateTargetAroundZAxisCommand}"
                    CommandParameter="-0.02"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>
                <RepeatButton Content="+" Grid.Column="2" Grid.Row="2" Margin="5"
                    Command="{x:Static MdxSceneControls:D3dScenePresenter.RotateTargetAroundZAxisCommand}"
                    CommandParameter="0.02"
                    CommandTarget="{Binding ElementName=mdxSceneHost}"/>

            </Grid>
            <Border BorderBrush="DarkGreen" BorderThickness="1" Margin="0,5" Height="2" />
            <Button Content="Adjust view"
                Margin="5"
                Command="{x:Static MdxSceneControls:D3dScenePresenter.AdjustCameraViewCommand}"
                CommandTarget="{Binding ElementName=mdxSceneHost}"/>
        </StackPanel>
    </Grid>

</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions