Click here to Skip to main content
15,878,748 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 45K   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 Microsoft.DirectX;

namespace MdxScene.Cameras
{
    public class D3dPerspectiveOffCenterCamera : D3dPerspectiveCameraBase
    {
        public D3dPerspectiveOffCenterCamera()
        {
            Left = -0.5f;
            Right = 0.5f;
            Bottom = 0.5f;
            Top = -0.5f;
        }

        #region D3dPerspectiveCameraBase implementation
        public override Matrix GetProjectionMatrix()
        {
            return CameraCoordinateSystem.LeftHanded == CoordinateSystem
                       ? Matrix.PerspectiveOffCenterLH(Left, Right, Bottom, Top, ZNearPlane, ZFarPlane)
                       : Matrix.PerspectiveOffCenterRH(Left, Right, Bottom, Top, ZNearPlane, ZFarPlane);
        }

        public override void Zoom(float scalingFactorX, float scalingFactorY)
        {
            float centerX = (Left + Right) / 2;
            float centerY = (Bottom + Top) / 2;

            float halfWidth = ((Right - Left) / 2) * scalingFactorX;
            float halfHeight = ((Top - Bottom) / 2) * scalingFactorY;

            Left = centerX - halfWidth;
            Right = centerX + halfWidth;
            Bottom = centerY - halfHeight;
            Top = centerY + halfHeight;
        }

        public override void GetFov(out float fovX, out float fovY)
        {
            float width = Right - Left;
            float height = Top - Bottom;

            fovX = (float)Math.Atan(width / (2 * ZNearPlane)) * 2;
            fovY = (float)Math.Atan(height / (2 * ZNearPlane)) * 2;
        }
        #endregion

        #region properties

        #region Left
        private float _left;
        public float Left
        {
            get { return _left; }
            set
            {
                lock (this)
                {
                    _left = value;
                    _isProjectionMatrixValid = false;
                }
            }
        }
        #endregion

        #region Right
        private float _right;
        public float Right
        {
            get { return _right; }
            set
            {
                lock (this)
                {
                    _right = value;
                    _isProjectionMatrixValid = false;
                }
            }
        }
        #endregion

        #region Bottom
        private float _bottom;
        public float Bottom
        {
            get { return _bottom; }
            set
            {
                lock (this)
                {
                    _bottom = value;
                    _isProjectionMatrixValid = false;
                }
            }
        }
        #endregion

        #region Top
        private float _top;
        public float Top
        {
            get { return _top; }
            set
            {
                lock (this)
                {
                    _top = value;
                    _isProjectionMatrixValid = false;
                }
            }
        }
        #endregion

        #endregion
    }
}

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