CAD System Web Site - Computer Aided Design Software
i NET1000.COM
by Makoto Honda
Last Update: 2011-November-26                                    Update Info     Site Index     Licensing     Copyright Notice     Privacy Policy     Contact Us  
 

 

HOME

                                   
WPF 3D Notes
_________________

                                               Return to   C# / WPF Implementation       

  WPF 3D Features                                             

  WPF 3D Rendering        2011-March-05

I found this WPF 3D cone example. I was flabbergasted to see 3D graphic rendition by WPF. Below, I rendered a 3D image under WPF/C#. This is all I need for my G-C# project! A 3d camera, perspective projection, light, shading, model tessellation... I know WPF is using DirectX for rendering, the native mode for .NET, but does this mean I do not have to access DirectX APIs directly from my application? It seems like all I need is already encapsulated in WPF! Wow!!!!

Ref: http://msdn.microsoft.com/en-us/library/ms747437.aspx

 

 


 

   Display List --- WPF 3D vs. Open GL        2011-June-03

     OpenGL has a "display list" facility which allows geometry to be kept/retained for subsequent transformations.
     In WPF3D, everything is done via display list - by default.

     WPF 3D:  A retained mode API --- It's like everything is done via display lists.  WPF decides how and when to render.

     Open GL: An immediate mode API --- You have to explicitly draw each frame.
 

   WPF 3D Cameras / Open GL Cameras        2011-May-31

   Field of View Angle (m_viewAngle)
   /////////////////////////////////////////////////////////////////////////////

     Perspective camera --- Open GL uses "vertical" angle while WPF uses "horizontal" angle.
     Orthographic camera --- In WPF, only the "horizontal" extent (Width) is supplied. Vertical bound is computed automatically based on the window size.

     OpenGL: Perspective camera uses "vertical" angle.

     // For a perspective camera:
     gluPerspective (m_viewAngle
V, m_ratio, m_nearPerspective, m_farPerspective );

 
    // For an orthograpic camera:
     glOrtho (-m_vptX,m_vptX,-m_vptY,m_vptY,m_nearOrthographic,m_farOrthographic);

     // For both perspective and orthographic cameras:
     gluLookAt (m_eyePt.x,m_eyePt.y,m_eyePt.z,m_centerPt.x,m_centerPt.y,m_centerPt.z,m_upVec.x,m_upVec.y,m_upVec.z);


    
WPF3D: Perspective camera uses "horizontal" angle.

     // For a perspective camera:
    
PerspectiveCamera pCamera = new PerspectiveCamera();

     pCamera.FieldOfView.m_viewAngle
H;
     pCamera.NearPlaneDistance = m_nearPerspective;
     pCamera.FarPlaneDistance = m_farPerspective;

 
    // For an orthograpic camera:
    
OrthographicCamera oCamera = new OrthographicCamera();

     oCamera.Width = m_orthoSize * 2;
 // Only horizontal extent specified
     oCamera.NearPlaneDistance = m_nearOrthographic;
     oCamera.FarPlaneDistance = m_farOrthographic;

 
    // For both perspective and orthographic cameras:
     camera.Position = new Point3D (m_eyePt.x, m_eyePt.y, m_eyePt.z);
     camera.LookDirection = new Vector3D (m_centerPt.x - m_eyePt.x, m_centerPt.y - m_eyePt.y, m_centerPt.z - m_eyePt.z);
     camera.UpDirection = new Vector3D (m_upVec.x, m_upVec.y, m_upVec.z);
 

    Window size change event...
    /////////////////////////////////////////////////////////////////////////////

     MFC' OnSize() ---> WPF's UserConrol_SizeChanged()

     CView::SetVptPixelSizeXY ()
     {
          m_pixSizeX = this.myViewport.ActualWidth; 
// border -7 & -7
          m_pixSizeY = this.myViewport.ActualHeight;
 // title bar -31 & bottom border -7
     }

    Cursor position...
    /////////////////////////////////////////////////////////////////////////////

     CView::Point GetCursorPosition (sender, e)
     {
          Point p = e.GetPosition (this.myViewport);
          return p;
     }
 

  WPF 3D - XAML STRUCTURE         2011-May-18    

   Viewport3D - Camera - Light - Geometry - Material
   /////////////////////////////////////////////////////////////////////////////

     <!-- Viewport3D is the rendering surface. -->
     <Viewport3D Name="myViewport" ...

     <!-- ====== ADD CAMERA under Viewport3D.Camera ================= -->
     <Viewport3D.Camera>
          <PerspectiveCamera x:Name="myCamera" Position="0,0,-4" LookDirection="0,0,1" UpDirection="1,0,0"
                                          FieldOfView="15" NearPlaneDistance="1" FarPlaneDistance="20" />
     </Viewport3D.Camera>

     <!-- ====== ADD GEOMETRY under Viewport3D.Children ================= -->
     <Viewport3D.Children>
          <!-- ============== Geometry (1) ==Axes lines=============== -->
          <local:ScreenSpaceLines3D Color="Red" Thickness="4.0" Points="0,0,0, 2,0,0" />
          <local:ScreenSpaceLines3D Color="Green" Thickness="4.0" Points="0,0,0, 0,2,0" />
          <local:ScreenSpaceLines3D Color="Blue" Thickness="4.0" Points="0,0,0, 0,0,2" />
          <!-- ============== Geometry (2) ========================= -->
          <ModelVisual3D>
          <ModelVisual3D.Content>
               <Model3DGroup>
               <Model3DGroup.Children>
                    <!-- ============== Light 1 ================= -->
                    <DirectionalLight Color="#FFFFFFFF" Direction="3,-4,5"/>
                    <AmbientLight x:Name="AmbientLight" Color="Black" />
                    <!-- ============ GeometryModel3D : Cone 1 ================= -->
                    <GeometryModel3D>
                         <GeometryModel3D.Geometry>
                              <MeshGeometry3D x:Name="myCone" Positions="0.293893 -0.5 0.404509 0.4755....
                                                                                        Normals="0.7236065,0.4472139,0.52573......
                                                                                        TriangleIndices="0 1 2 3 4 5 6 7 8 9 10 11.....
                         </GeometryModel3D.Geometry>
                         <GeometryModel3D.Material>
                              <DiffuseMaterial>
                              <DiffuseMaterial.Brush>
                                   <SolidColorBrush x:Name="ConeBrush" Color="Yellow" Opacity="1.0"/>
                              </DiffuseMaterial.Brush>
                              </DiffuseMaterial>
                         </GeometryModel3D.Material>
                    </GeometryModel3D>
                    <!-- ==============  GeometryModel3D : Box - Z face ================= -->
                    <GeometryModel3D>
                         <GeometryModel3D.Geometry>
                              <MeshGeometry3D Positions="0,0,0   1,0,0   0,1,0   1,0,0   1,1,0   0,1,0" />
                         </GeometryModel3D.Geometry>
                         <GeometryModel3D.Material>
                              <DiffuseMaterial>
                              <DiffuseMaterial.Brush>
                                   <SolidColorBrush Color="Yellow" Opacity="0.3"/>
                              </DiffuseMaterial.Brush>
                              </DiffuseMaterial>
                         </GeometryModel3D.Material>
                    </GeometryModel3D>
                    <!-- ==============  GeometryModel3D : Box - X face ================= -->
                    <GeometryModel3D>
                         <GeometryModel3D.Geometry>
                              <MeshGeometry3D Positions="1,0,0 1,0,-1 1,1,0 1,0,-1 1,1,-1 1,1,0" />
                         </GeometryModel3D.Geometry>
                         <GeometryModel3D.Material>
                              <DiffuseMaterial>
                              <DiffuseMaterial.Brush>
                                   <SolidColorBrush Color="Red" Opacity="0.3"/>
                              </DiffuseMaterial.Brush>
                              </DiffuseMaterial>
                         </GeometryModel3D.Material>
                    </GeometryModel3D>
                    <!-- ============== Box - Z face ================= -->

               </Model3DGroup.Children>
               </Model3DGroup>

          </ModelVisual3D.Content>
          </ModelVisual3D>

     </Viewport3D.Children>
     </Viewport3D>

     </Grid>
     </UserControl>
 

   XAML   verses   C# Code-behind        2011-May-19

   XAML
   /////////////////////////////////////////////////////////////////////////////

     <Viewport3D Name="myViewport"
     <Viewport3D.Children>
     <ScreenSpaceLines3D Color="Red" Thickness="2" Points="0,1,0,   1,0,1" />

  
C# Code-Behind
   /////////////////////////////////////////////////////////////////////////////

    ScreenSpaceLines3D line = new ScreenSpaceLines3D();
    Point3D p1 = new Point3D(0, 0, 1);
    Point3D p2 = new Point3D(0, 1, 0);
    Color c = Colors.Red;
    int wid = 3;
    line.Thickness = wid;
    line.Color = c;
    line.Points.Add(p1);
    line.Points.Add(p2);
    pV.myViewport.Children.Add(line);

 

   WPF 3D Lighting        2011-Nov-11

 public void Setup3Light_WPF()
{
     //---------------------------------------------
    
// Ambient light creates flat image, but a low-intensity ambient creates
     // nice diffused effect. (Use Color="#FF333333" or #555555 or lower)
     //----------------------------------------------

     AmbientLight lightAmb = new AmbientLight();
     lightAmb.Color = Color.FromRgb(10, 10, 10); // Colors.DarkGray;
     ModelVisual3D mod1 = new ModelVisual3D();
     mod1.Content = lightAmb;
 
    //----------------------------------------------
     this.myViewport.Children.Add(mod1);
    
//----------------------------------------------
     // Point light radiates light uniformly in all directions from a point.
     // Light attenuates with distance.
     //---------------------------------------------

     PointLight lightPoint = new PointLight();
     lightPoint.Color = Color.FromRgb(30, 30, 30);
     lightPoint.Position = new Point3D(40, 0, -5);
     ModelVisual3D mod2 = new ModelVisual3D();
     mod2.Content = lightPoint;
 
   //----------------------------------------------
     this.myViewport.Children.Add(mod2);
   
 //----------------------------------------------
     // Spot light emits a cone of light from a point.
     // Light attenuates with distance.
     //----------------------------------------------

     SpotLight lightSpot = new SpotLight();
     lightSpot.Color = Color.FromRgb(30, 30, 30);
     lightSpot.Position = new Point3D(-10, -1, -1);
     lightSpot.Direction = new Vector3D(1, -1, -1);
     lightSpot.InnerConeAngle = 45;
     lightSpot.OuterConeAngle = 45;
     ModelVisual3D mod3 = new ModelVisual3D();
     mod3.Content = lightSpot;
 
    //----------------------------------------------
     this.myViewport.Children.Add(mod3);
 
   //----------------------------------------------
     // Directional light casts parallel light from infinity (like the sun)
     //----------------------------------------------

     byte VAL = 100;
     DirectionalLight lightDir1 = new DirectionalLight();
     lightDir1.Color = Color.FromRgb(VAL, VAL, VAL); // Colors.White;
     lightDir1.Direction = new Vector3D(1, 0, 5);
     ModelVisual3D mod4 = new ModelVisual3D();
     mod4.Content = lightDir1;
 
   //----------------------------------------------
     this.myViewport.Children.Add(mod4);
 
    //----------------------------------------------
     DirectionalLight lightDir2 = new DirectionalLight();
     lightDir2.Color = Color.FromRgb(VAL, VAL, VAL); // Colors.White;
     lightDir2.Direction = new Vector3D(0, 5, -1);
     ModelVisual3D mod5 = new ModelVisual3D();
     mod5.Content = lightDir2;
 
   //----------------------------------------------
     this.myViewport.Children.Add(mod5);
 
    //----------------------------------------------
}
 

   References and Namespaces Via "using" Directives        2011-Nov-26

   To use WPF 3D classes (like Color,,,,) do the following:

   1) From the solution explorer, right-click References folder of the Project containing the file (xxx.cs) you are modifying.
       Select "Add Reference...". Under ".NET" tab, select "WindowsBase", "PresentationCore", and "PresentationFramework" and OK.

   2) Add the following namespaces via "using" directives to the file where these WPF 3D classes are to be used.
       using System.Windows;
       using System.Windows.Controls;
       using System.Windows.Documents;
       using System.Windows.Data;
       using System.Windows.Input;
       using System.Windows.Navigation;
       using System.Windows.Shapes;
       using System.Windows.Markup;
       using System.Windows.Media;
       using System.Windows.Media.Media3D; // Point3D, Colors
       using System.Windows.Media.Imaging;
 

 

potLight lightSpot = new SpotLight();
     lightSpot.Color = Color.FromRgb(30, 30, 30);
     lightSpot.Position =

   xxx        2011-Nov-11

          

 

CAD System Web Site - Computer Aided Design Software:  Copyright 2010-2013 Makoto Honda. All Rights Reserved.  

 

   

Copyright 2010-2013 Makoto Honda. All Rights Reserved.                                       www.iNET1000.com