Click here to Skip to main content
15,886,761 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.1K   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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MdxScene.Cameras;
using MdxScene.Controls;
using MdxScene.Lights;
using MdxScene.Shapes;
using MdxWpfInteroperability;
using Microsoft.DirectX.Direct3D;

namespace MdxScene.Example
{
    /// <summary>
    /// Interaction logic for SceneManipulationExampleWindow.xaml
    /// </summary>
    public partial class SceneManipulationExampleWindow : Window
    {
        private D3dScene _scene;

        private MarkBox _pickMarkBox;

        public SceneManipulationExampleWindow()
        {
            InitializeComponent();

            InitScene();
        }

        private void InitScene()
        {
            _scene = new D3dScene
                         {
                             ClearColor = System.Drawing.Color.Gray
                         };

            // Set the light.
            _scene.Lights.Add(new D3dDirectionalLight
                                  {
                                      XDirection = 1,
                                      YDirection = -1,
                                      ZDirection = -1,
                                      Diffuse = System.Drawing.Color.DimGray
                                  });

            _scene.Lights.Add(new D3dDirectionalLight
            {
                XDirection = -1,
                YDirection = 1,
                ZDirection = 1,
                Diffuse = System.Drawing.Color.Gray
            });

            // Add shapes.
            _scene.Shapes.Add(new Pyramid
                                  {
                                      DefaultMaterial = new Material {Diffuse = System.Drawing.Color.Red},
                                      ScalingX = 200,
                                      ScalingY = 200,
                                      ScalingZ = 200
                                  });

            _scene.Shapes.Add(new Star
                                  {
                                      DefaultMaterial = new Material {Diffuse = System.Drawing.Color.Yellow},
                                      ScalingX = 300,
                                      ScalingY = 300,
                                      ScalingZ = 300,
                                      TranslationX = 600
                                  });

            _scene.Shapes.Add(new House
                                  {
                                      BodyMaterial = new Material {Diffuse = System.Drawing.Color.Green},
                                      RoofMaterial = new Material {Diffuse = System.Drawing.Color.Red},
                                      ScalingX = 150,
                                      ScalingZ = 150,
                                      ScalingY = 300,
                                      TranslationX = -600
                                  });

            // Add the mouse-over mark.
            _pickMarkBox = new MarkBox();
            _scene.Shapes.Add(_pickMarkBox);

            mdxSceneHost.Scene = _scene;
        }

        private void mdxSceneHost_D3dSurfaceMouseWheel(object sender, D3dSurfaceMouseWheelEventArgs e)
        {
            float relativeZoomDelta = e.MouseWheelEventArgs.Delta/1000f;
            float scalingFactor = 1 - relativeZoomDelta;
            D3dScenePresenter.UniformZoomCommand.Execute(scalingFactor, mdxSceneHost);
        }

        private void mdxSceneHost_MouseOverShapeChanged(object sender, MouseOverShapeChangedRoutedEventArgs e)
        {
            D3dShape pickedShape = e.NewShape;

            mdxSceneHost.PerformSceneAction(() => _pickMarkBox.MarkedShape = pickedShape);
        }
    }

}

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