Click here to Skip to main content
15,884,388 members
Articles / Desktop Programming / WPF

D3dHost - MDX and WPF interoperability

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
13 Nov 2012CPOL5 min read 34K   2.6K   20  
This article shows how we can render an interoperable MDX (Managed DirectX) scene, inside a WPF window.
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 MdxWpfInteroperability;

namespace MdxWpfInteroperability.Example
{
    /// <summary>
    /// Interaction logic for InteroperabilityExample.xaml
    /// </summary>
    public partial class InteroperabilityExample : Window
    {
        private double surfaceWidth = 2000;
        private double surfaceHeight = 1500;

        public InteroperabilityExample()
        {
            InitializeComponent();

            mdxHost.D3dSurfaceWidth = surfaceWidth;
            mdxHost.D3dSurfaceHeight = surfaceHeight;
            mdxHost.Width = surfaceWidth * zoomSlider.Value;
            mdxHost.Height = surfaceHeight * zoomSlider.Value;
        }

        private void RenderScene()
        {
            float circleCenterX = 200;
            float circleCenterY = 300;
            float circleRadius = 50;

            // Clear the surface.
            mdxHost.D3dDevice.Clear(Microsoft.DirectX.Direct3D.ClearFlags.Target, ColorToInt(Colors.DarkGray), 1.0f, 0);

            // Draw a circle.
            Render2dCircle(circleCenterX, circleCenterY, circleRadius, Colors.Red, mdxHost.D3dDevice); // Stroke
            Render2dCircle(circleCenterX, circleCenterY, circleRadius - 3, Colors.DarkRed, mdxHost.D3dDevice); // Fill
            Render2dCircle(circleCenterX, circleCenterY, 5, Colors.Salmon, mdxHost.D3dDevice); // Center indication

            // Draw a text that presents the circle's center position.
            Render2dText(string.Format("Circle center: ({0},{1})", circleCenterX, circleCenterY),
                (int)(circleCenterX - circleRadius), (int)(circleCenterY + circleRadius + 10),
                36f, Colors.White, mdxHost.D3dDevice);

            // Present the scene on the D3dHost control.
            mdxHost.InvalidateD3dRegion();
        }        

        public void Render2dCircle(float centerX, float centerY, float radius, Color color,
            Microsoft.DirectX.Direct3D.Device device)
        {
            int convertedColor = ColorToInt(color);

            int numOfPoints = (int)radius;
            if (numOfPoints < 10)
            {
                numOfPoints = 10;
            }

            Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored[] vertices = 
                new Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored[numOfPoints + 2];
            vertices[0].Position = new Microsoft.DirectX.Vector4(centerX, centerY, 0, 1.0f);
            vertices[0].Color = convertedColor;

            double partAngle = Math.PI * 2 / numOfPoints;

            for (int vertexInx = 0; vertexInx <= numOfPoints; vertexInx++)
            {
                double currAngle = vertexInx * partAngle;
                float currX = (float)(centerX + radius * Math.Cos(currAngle));
                float currY = (float)(centerY + radius * Math.Sin(currAngle));

                vertices[vertexInx + 1].Position = 
                    new Microsoft.DirectX.Vector4(currX, currY, 0, 1.0f);
                vertices[vertexInx + 1].Color = convertedColor;
            }

            device.BeginScene();
            device.VertexFormat = Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored.Format;
            device.DrawUserPrimitives(Microsoft.DirectX.Direct3D.PrimitiveType.TriangleFan, numOfPoints, vertices);
            device.EndScene();
        }

        public void Render2dText(string text, int x, int y, float fontSize, Color color, Microsoft.DirectX.Direct3D.Device device)
        {
            System.Drawing.Font systemfont = new System.Drawing.Font("Arial", fontSize, System.Drawing.FontStyle.Regular);
            Microsoft.DirectX.Direct3D.Font d3dFont = new Microsoft.DirectX.Direct3D.Font(mdxHost.D3dDevice, systemfont);

            device.BeginScene();

            d3dFont.DrawText(null, text, new System.Drawing.Point(x, y),
                System.Drawing.Color.FromArgb(ColorToInt(color)));

            device.EndScene();

            d3dFont.Dispose();
        }

        private int ColorToInt(Color color)
        {
            return (int)color.A << 24 | (int)color.R << 16 | (int)color.G << 8 | (int)color.B;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            RenderScene();
        }

        private void zoomSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (!IsLoaded)
            {
                return;
            }

            mdxHost.Width = surfaceWidth * zoomSlider.Value;
            mdxHost.Height = surfaceHeight * zoomSlider.Value;
        }

        private void mdxHost_D3dSurfaceMouseLeave(object sender, D3dSurfaceMouseEventArgs e)
        {
            surfaceMousePosition.Text = "Out of surface";
        }

        private void mdxHost_D3dSurfaceMouseMove(object sender, D3dSurfaceMouseEventArgs e)
        {
            surfaceMousePosition.Text = string.Format("({0},{1})",
                e.D3dSurfaceMousePosition.X, e.D3dSurfaceMousePosition.Y);
        }

    }
}

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