Problem adding Viewport2DVisual3D from Code

Posted by Jeff on Stack Overflow See other posts from Stack Overflow or by Jeff
Published on 2010-03-06T00:57:03Z Indexed on 2010/03/08 21:21 UTC
Read the original article Hit count: 283

Filed under:
|

I'm trying to add a Viewport2DVisual3D to a Viewport3D in code, but the visual isn't showing up. Any help understanding why not would be appreciated. The following is the code for the main window.

Is it sufficient to just add the Viewport2DVisual3D to the children of the Viewport3D in order for it to be rendered?

public partial class Window1 : System.Windows.Window
{

    public Window1()
    {
        InitializeComponent();

        this.Loaded += new RoutedEventHandler(temp);
    }


    public void temp(object sender, RoutedEventArgs e)
    {
        Viewport2DVisual3D test = new Viewport2DVisual3D();
        MeshGeometry3D testGeometry = new MeshGeometry3D();

        Vector3D CameraLookDirection = Main_Target_CameraOR20.LookDirection;

        // Calculate the Positions based on the Camera 
        Point3DCollection myPoint3DCollection = new Point3DCollection();
        myPoint3DCollection.Add(new Point3D(-1, 1, 0));
        myPoint3DCollection.Add(new Point3D(-1, -1, 0));
        myPoint3DCollection.Add(new Point3D(1, -1, 0));
        myPoint3DCollection.Add(new Point3D(1, 1, 0));
        testGeometry.Positions = myPoint3DCollection;

        PointCollection myPointCollection = new PointCollection();
        myPointCollection.Add(new Point(0, 0));
        myPointCollection.Add(new Point(0, 1));
        myPointCollection.Add(new Point(1, 1));
        myPointCollection.Add(new Point(1, 0));
        testGeometry.TextureCoordinates = myPointCollection;

        Int32Collection triangleIndicesCollection = new Int32Collection();
        triangleIndicesCollection.Add(0);
        triangleIndicesCollection.Add(1);
        triangleIndicesCollection.Add(2);
        triangleIndicesCollection.Add(2);
        triangleIndicesCollection.Add(3);
        triangleIndicesCollection.Add(0);
        testGeometry.TriangleIndices = triangleIndicesCollection;


        DiffuseMaterial myDiffuseMaterial = new DiffuseMaterial(Brushes.White);
        Viewport2DVisual3D.SetIsVisualHostMaterial(myDiffuseMaterial, true);

        Transform3DGroup myTransform3DGroup = new Transform3DGroup();
        ScaleTransform3D myScaleTransform3D = new ScaleTransform3D();
        myScaleTransform3D.ScaleX = 2;
        myScaleTransform3D.ScaleY = 2;
        myScaleTransform3D.ScaleZ = 2;

        TranslateTransform3D myTranslateTransform3D = new TranslateTransform3D();

        myTranslateTransform3D.OffsetX = -27;
        myTranslateTransform3D.OffsetY = 13;
        myTranslateTransform3D.OffsetZ = 6;


        RotateTransform3D rotateTransform = new RotateTransform3D()
        {
            Rotation = new AxisAngleRotation3D
            {
                Angle = -50,
                Axis = new Vector3D(0, 1, 0)
            }
        };

        myTransform3DGroup.Children.Add(myTranslateTransform3D);
        myTransform3DGroup.Children.Add(myScaleTransform3D);
        myTransform3DGroup.Children.Add(rotateTransform);
        test.Transform = myTransform3DGroup;

        Button myButton = new Button();
        myButton.Content = "Test Button";

        test.Material = myDiffuseMaterial;
        test.Geometry = testGeometry;
        test.Visual = myButton;

        ZAM3DViewport3D.Children.Add(test);
    }
}

© Stack Overflow or respective owner

Related posts about wpf

Related posts about viewport2dvisual3d

  • Problem adding Viewport2DVisual3D from Code

    as seen on Stack Overflow - Search for 'Stack Overflow'
    I'm trying to add a Viewport2DVisual3D to a Viewport3D in code, but the visual isn't showing up. Any help understanding why not would be appreciated. The following is the code for the main window. Is it sufficient to just add the Viewport2DVisual3D to the children of the Viewport3D in order for… >>> More

  • WPF - Render text in Viewport3D

    as seen on Stack Overflow - Search for 'Stack Overflow'
    I want to present up to 300 strings (just a few words) in a Viewport3D - fast! I want to render them on different Z positions and zoom in and out fluently. The ways I have found so far to render text in a Viewport3D: Put a TextBlock in a Viewport2DVisual3D. This guy's PlanarText class. The same… >>> More

  • WPF Memory Leak

    as seen on Stack Overflow - Search for 'Stack Overflow'
    I have an WPF form that I myself did not create, so I am not very good at WPF. It is leaking badly though, up to 400 MB and closing the form does not help. The problem lies in my application loading all the pictures at once. I would like to only load the ones visible at the moment. It is about 300… >>> More