XAML

Code




WPF Tutorial Contents

APL  Home Page



XAML


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <DockPanel>
    <Viewbox>
      <Canvas Width="321" Height="201">

        <!-- The Viewport3D provides a rendering surface for 3-D visual content. -->
        <Viewport3D ClipToBounds="True" Width="150" Height="150" Canvas.Left="0" Canvas.Top="10">

          <!-- Defines the camera used to view the 3D object. -->
          <Viewport3D.Camera>
            <PerspectiveCamera Position="0,0,2" LookDirection="0,0,-1" FieldOfView="60" />
          </Viewport3D.Camera>

          <!-- The ModelVisual3D children contain the 3D models -->
          <Viewport3D.Children>

            <!-- This ModelVisual3D defines the light cast in the scene. Without light, the 3D
                 object cannot be seen. Also, the direction of the lights affect shadowing. If desired,
                 you can create multiple lights with different colors that shine from different directions. -->
            <ModelVisual3D>
              <ModelVisual3D.Content>
                <DirectionalLight Color="#FFFFFF" Direction="-0.612372,-0.5,-0.612372" />
              </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D>
              <ModelVisual3D.Content>
                <GeometryModel3D>

                  <!-- The geometry specifes the shape of the 3D plane. In this sample, a flat sheet is created. -->
                  <GeometryModel3D.Geometry>
                    <MeshGeometry3D
                     TriangleIndices="0,1,2 3,4,5 "
                     Normals="0,0,1 0,0,1 0,0,1 0,0,1 0,0,1 0,0,1 "
                     TextureCoordinates="0,0 1,0 1,1 1,1 0,1 0,0 "
                     Positions="-0.5,-0.5,0.5 0.5,-0.5,0.5 0.5,0.5,0.5 0.5,0.5,0.5 -0.5,0.5,0.5 -0.5,-0.5,0.5 " />
                  </GeometryModel3D.Geometry>

                  <!-- The material specifies the material applied to the 3D object. In this sample a linear gradient
                       covers the surface of the 3D object.-->
                  <GeometryModel3D.Material>
                    <MaterialGroup>
                      <DiffuseMaterial>
                        <DiffuseMaterial.Brush>
                          <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                            <LinearGradientBrush.GradientStops>
                              <GradientStop Color="Yellow" Offset="0" />
                              <GradientStop Color="Red" Offset="0.25" />
                              <GradientStop Color="Blue" Offset="0.75" />
                              <GradientStop Color="LimeGreen" Offset="1" />
                            </LinearGradientBrush.GradientStops>
                          </LinearGradientBrush>
                        </DiffuseMaterial.Brush>
                      </DiffuseMaterial>
                    </MaterialGroup>
                  </GeometryModel3D.Material>

                  <!-- Apply a transform to the object. In this sample, a rotation transform is applied, rendering the
                       3D object rotated. -->
                  <GeometryModel3D.Transform>
                    <RotateTransform3D>
                      <RotateTransform3D.Rotation>
                        <AxisAngleRotation3D Axis="0,3,0" Angle="40" />
                      </RotateTransform3D.Rotation>
                    </RotateTransform3D>
                  </GeometryModel3D.Transform>
                </GeometryModel3D>
              </ModelVisual3D.Content>
            </ModelVisual3D>
          </Viewport3D.Children>

        </Viewport3D>
      </Canvas>
    </Viewbox>
  </DockPanel>
</Window>

Code


Microsoft's page shows VB and C# code which are claimed to display the equivalent - I haven't verified this but I have transliterated to arrive at some APL which renders similarly (there's a scaling issue that I haven't yet figured out).  For brevity I've omitted the comments, for ease of comparison I've done a line-by-line transliteration rather than attempt to make the code have more of an APL flavour.

First, some XAML to make a simple Window to draw into...

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Width="200" Height="200">
</Window>


And the bit you've been waiting for...

 Draw3D w;⎕IO;⎕ML;⎕USING;myViewport3D;myModel3DGroup;myGeometryModel;myModelVisual3D;myPCamera;myDirectionalLight;myMeshGeometry3D;myNormalCollection;myPositionCollection;myTextureCoordinatesCollection;myTriangleIndicesCollection;myHorizontalGradient;myMaterial;myRotateTransform3D;myAxisAngleRotation3d;form;xaml
⍝ Simple 3D drawing from Microsoft example
 ⎕IO ⎕ML←0 3
 ⎕USING←,⊂'System,System.dll'
 ⎕USING,←⊂'System.Windows,c:\windows\microsoft.net\framework64\v4.0.30319\wpf\WindowsBase.dll'
 ⎕USING,←⊂'System.Windows.Controls,c:\windows\microsoft.net\framework64\v4.0.30319\wpf\PresentationFramework.dll'
 ⎕USING,←⊂'System.Windows.Media,c:\windows\microsoft.net\framework64\v4.0.30319\wpf\PresentationCore.dll'
 ⎕USING,←⊂'System.Windows.Media.Media3D,c:\windows\microsoft.net\framework64\v4.0.30319\wpf\PresentationCore.dll'
 myViewport3D←⎕NEW Viewport3D
 myViewport3D.Width←150
 myViewport3D.Height←150
 myViewport3D.ClipToBounds←1
 myModel3DGroup←⎕NEW Model3DGroup
 myGeometryModel←⎕NEW GeometryModel3D
 myModelVisual3D←⎕NEW ModelVisual3D
 myPCamera←⎕NEW PerspectiveCamera
 myPCamera.Position←⎕NEW Point3D(0 0 2)
 myPCamera.LookDirection←⎕NEW Vector3D(0 0 ¯1)
 myPCamera.FieldOfView←60
 myViewport3D.Camera←myPCamera
 myDirectionalLight←⎕NEW DirectionalLight
 myDirectionalLight.Color←Colors.White
 myDirectionalLight.Direction←⎕NEW Vector3D(¯0.61 ¯0.5 ¯0.61)
 myModel3DGroup.Children.Add(myDirectionalLight)
 myMeshGeometry3D←⎕NEW MeshGeometry3D
 myNormalCollection←⎕NEW Vector3DCollection
 myNormalCollection.Add(⎕NEW Vector3D(0 0 1))
 myNormalCollection.Add(⎕NEW Vector3D(0 0 1))
 myNormalCollection.Add(⎕NEW Vector3D(0 0 1))
 myNormalCollection.Add(⎕NEW Vector3D(0 0 1))
 myNormalCollection.Add(⎕NEW Vector3D(0 0 1))
 myNormalCollection.Add(⎕NEW Vector3D(0 0 1))
 myMeshGeometry3D.Normals←myNormalCollection
 myPositionCollection←⎕NEW Point3DCollection
 myPositionCollection.Add(⎕NEW Point3D(¯0.5 ¯0.5 0.5))
 myPositionCollection.Add(⎕NEW Point3D(0.5 ¯0.5 0.5))
 myPositionCollection.Add(⎕NEW Point3D(0.5 0.5 0.5))
 myPositionCollection.Add(⎕NEW Point3D(0.5 0.5 0.5))
 myPositionCollection.Add(⎕NEW Point3D(¯0.5 0.5 0.5))
 myPositionCollection.Add(⎕NEW Point3D(¯0.5 ¯0.5 0.5))
 myMeshGeometry3D.Positions←myPositionCollection
 myTextureCoordinatesCollection←⎕NEW PointCollection
 myTextureCoordinatesCollection.Add(⎕NEW Point(0 0))
 myTextureCoordinatesCollection.Add(⎕NEW Point(1 0))
 myTextureCoordinatesCollection.Add(⎕NEW Point(1 1))
 myTextureCoordinatesCollection.Add(⎕NEW Point(1 1))
 myTextureCoordinatesCollection.Add(⎕NEW Point(0 1))
 myTextureCoordinatesCollection.Add(⎕NEW Point(0 0))
 myMeshGeometry3D.TextureCoordinates←myTextureCoordinatesCollection
 myTriangleIndicesCollection←⎕NEW Int32Collection
 myTriangleIndicesCollection.Add 0
 myTriangleIndicesCollection.Add 1
 myTriangleIndicesCollection.Add 2
 myTriangleIndicesCollection.Add 3
 myTriangleIndicesCollection.Add 4
 myTriangleIndicesCollection.Add 5
 myMeshGeometry3D.TriangleIndices←myTriangleIndicesCollection
 myGeometryModel.Geometry←myMeshGeometry3D
 myHorizontalGradient←⎕NEW LinearGradientBrush
 myHorizontalGradient.StartPoint←⎕NEW Point(0 0.5)
 myHorizontalGradient.EndPoint←⎕NEW Point(1 0.5)
 myHorizontalGradient.GradientStops.Add(⎕NEW GradientStop(Colors.Yellow 0))
 myHorizontalGradient.GradientStops.Add(⎕NEW GradientStop(Colors.Red 0.25))
 myHorizontalGradient.GradientStops.Add(⎕NEW GradientStop(Colors.Blue 0.75))
 myHorizontalGradient.GradientStops.Add(⎕NEW GradientStop(Colors.LimeGreen 1))
 myMaterial←⎕NEW DiffuseMaterial(myHorizontalGradient)
 myGeometryModel.Material←myMaterial
 myRotateTransform3D←⎕NEW RotateTransform3D
 myAxisAngleRotation3d←⎕NEW AxisAngleRotation3D
 myAxisAngleRotation3d.Axis←⎕NEW Vector3D(0 3 0)
 myAxisAngleRotation3d.Angle←40
 myRotateTransform3D.Rotation←myAxisAngleRotation3d
 myGeometryModel.Transform←myRotateTransform3D
 myModel3DGroup.Children.Add(myGeometryModel)
 myModelVisual3D.Content←myModel3DGroup
 myViewport3D.Children.Add(myModelVisual3D)
 xaml←#.UTIL.FILE.ReadUnicodeFile w
 form←#.UTIL.WPF.LoadXaml xaml''
 form.Content←myViewport3D
 {}form.ShowDialog


Microsoft's example suggests that I ought to have been able to set a Canvas property, but that led to an invisible result, hence drawing straight onto the Window - I've also set some Viewport3D properties which seemed to be in the XAML and missing from the Microsoft code.