Wpf 3D入门指南(Windows Presentation Foundation (WPF) 3D Tutorial)(二)

来源:互联网 发布:手机频点软件 编辑:程序博客网 时间:2024/05/29 10:28

Getting Started With the Code

I'll assume you have a basic understanding of WPF and how to create a basic WPF user interface with XAML. So with that, let's jump in and create a new WPF application in VS.Net 2005. Add the following XAML to the app to create a simple layout with a panel for buttons and a Viewport3D for displaying our 3D stuff:

设你有些wpf基础及知道怎样用xaml创建用户界面,这样,让我们创建一个新的WPF程序通过vs 2005.net,添加下面的XAML到程序中做个简单的布局,以便更好的显示3D图形。

 

<Grid>
 <DockPanel 
  Width="Auto" 
  VerticalAlignment="Stretch" 
  Height="Auto" 
  HorizontalAlignment="Stretch" 
  Grid.ColumnSpan="1" 
  Grid.Column="0" 
  Grid.Row="0" 
  Margin="0,0,0,0" 
  Grid.RowSpan="1">
  <StackPanel>
   <StackPanel.Background>
    <LinearGradientBrush>
      <GradientStop Color="White" Offset="0"/>
      <GradientStop Color="DarkKhaki" Offset=".3"/>
      <GradientStop Color="DarkKhaki" Offset=".7"/>
      <GradientStop Color="White" Offset="1"/>
    </LinearGradientBrush>
   </StackPanel.Background>
   <StackPanel Margin="10">
    <Button 
     Name="simpleButton" 
     Click="simpleButtonClick">Simple</Button>
   </StackPanel>
  </StackPanel>
  <Viewport3D Name="mainViewport" ClipToBounds="True">
   <Viewport3D.Camera>
    <PerspectiveCamera 
        FarPlaneDistance="100"
        LookDirection="-11,-10,-9"
        UpDirection="0,1,0"
        NearPlaneDistance="1" 
        Position="11,10,9" 
        FieldOfView="70" />
    </Viewport3D.Camera>
    <ModelVisual3D>
     <ModelVisual3D.Content>
         <DirectionalLight 
          Color="White" 
          Direction="-2,-3,-1" />
        </ModelVisual3D.Content>
   </ModelVisual3D>
  </Viewport3D>
 </DockPanel>
</Grid>

Basically, all of the 3D stuff in WPF happens in Viewport3D controls. That's where we'll be adding our 3D models once we start writing some code. Notice that a PerspectiveCamera has been added to the Viewport3D. The camera is used to allow us to "view" what's in the model from the user interface. Note that the camera is looking at the point {0,0,0} in the model.

基本上,wpf中,所有的三维物体都在viewport3d. 一旦我们开始写一些代码那(viewport3d)正是我们加入3D模型的地方.注意perspectivecamera已添加入viewport3d . 照相机是用来让我们从用户界面角度来"浏览"什么在模型里.注意,照相机望着点模型中的( 0,0,0 ).

 

The model also contains a DirectionalLight light source so that we can view stuff in the model.

这个模型也包含directionallight光源,使我们可以浏览模型中的物体.

I encourage you to change the camera's LookDirection and Position while you go through the examples. The screenshots in this tutorial do not necessarily use the LookDirection and Position values in the XAML above.

我鼓励你们改变照相机的LookDirectionPosition,同时翻遍例子. 本教程中截图不一定使用LookDirectionPosition在以上xaml.

The XAML above has one Button named simpleButton, so we need to hook up its click event to a method named simpleButtonClick in the window's code-behind file.

xaml以上有一个Button命名为simplebutton,因此,我们必须追随其click事件,并在窗口的代码后面的文件中给其处理方法命名simplebuttonclick.

private void simpleButtonClick(object sender, RoutedEventArgs e)
{
}

Creating a Simple Mesh

创建一个简单的网格

When you click the simpleButton button, you're going to create a very simple mesh and add the model to the Viewport3D. But before you do that, you'll need to know a little bit about the types of 3D classes and structures you'll be working with:

当你点击simpleButton按钮,你要去创建一个非常简单的网格并向Viewport3D添加模形. 但在你做这些之前,您需要了解一下3D类的类型及其结构:

  • GeometryModel3D - a model described by a Mesh (MeshGeometry3D) and a Material (DiffuseMaterial).
  • MeshGeometry3D - a mesh. Has a Positions collection, a TriangleIndeces collection, and a Normals collection.
  • Point3D - the most basic unit of mesh construction. Used to define positions and vectors.
  • Vector3D - used in the calculation of normals.
  • DiffuseMaterial - gives a model color and texture.
  • DirectionalLight - provides light so that objects in the Viewport3D can be seen. Without it, nothing shows up.

Full documentation of all of the System.Windows.Media.Media3D classes can be found at msdn.microsoft.com

·       GeometryModel3D一个通过网格Mesh (MeshGeometry3D)和材料Material (DiffuseMaterial) 描述的模型. Geometry(几何)

·       MeshGeometry3D - a mesh.MeshGeometry3D –网格. 它Has a Positions collection, a TriangleIndeces collection, and a Normals collection.有一个Positions集合, TriangleIndeces集合,以及一个Normals(法线)的收集.

·       Point3D - the most basic unit of mesh construction.Point3D -网结构最基本单元. Used to define positions and vectors.用来确定 positions位置和vectors向量.

·       Vector3D - used in the calculation of normals.Vector3D -用于法线的计算.

·       DiffuseMaterial - gives a model color and texture.DiffuseMaterial -提供一个模型颜色和材质.

·       DirectionalLight - provides light so that objects in the Viewport3D can be seen.DirectionalLight -提供灯光,使在Viewport3D物体能够被看到. Without it, nothing shows up.没有它,什么也显示不出来.

Full documentation of all of the System.Windows.Media.Media3D classes can be found at msdn.microsoft.com关于system.windows.media.media3d类的全部文件,可以在msdn.microsoft.com 找到。

 
原创粉丝点击