Wpf 3D入门指南(Windows Presentation Foundation (WPF) 3D Tutorial)(二)
2007-05-25 23:23
776 查看
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 aViewport3Dfor 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
Viewport3Dcontrols. That's where we'll be adding our 3D models once we start writing some code. Notice that a
PerspectiveCamerahas 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
DirectionalLightlight source so that we can view stuff in the model.
这个模型也包含directionallight光源,使我们可以浏览模型中的物体.
I encourage you to change the camera's
LookDirectionand
Positionwhile you go through the examples. The screenshots in this tutorial do not necessarily use the
LookDirectionand
Positionvalues in the XAML above.
我鼓励你们改变照相机的
LookDirection和
Position,同时翻遍例子. 本教程中截图不一定使用
LookDirection和
Position在以上xaml中.
The XAML above has one
Buttonnamed
simpleButton, so we need to hook up its click event to a method named
simpleButtonClickin 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 thesimpleButtonbutton, 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
Positionscollection, a
TriangleIndecescollection, and a
Normalscollection.
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
Viewport3Dcan 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 找到。
相关文章推荐
- Wpf 3D入门指南(Windows Presentation Foundation (WPF) 3D Tutorial)(三)
- Wpf 3D入门指南(Windows Presentation Foundation (WPF) 3D Tutorial)(一)
- Windows Presentation Foundation (WPF) 3D 教程(三) ----从下面的代码开始
- Windows Presentation Foundation (WPF) 3D 教程(一)---声明
- Windows Presentation Foundation (WPF) 3D 教程(二)----3D模型基本知识
- 在Windows Presentation Foundation(WPF)中增加Month Calendar月历控件
- WPF(Windows Presentation Foundation)图形系统
- WPF(Windows Presentation Foundation)图形系统
- WPF----Windows Presentation Foundation
- WPF-Windows Presentation Foundation
- WPF(Windows Presentation Foundation)Overview
- WPF(Windows Presentation Foundation)
- WPF(Windows Presentation Foundation) 线程处理模型
- Visual C# 2008+SQL Server 2005 数据库与网络开发-- 14.1 Windows Presentation Foundation(WPF)
- Professional WPF Programming: .NET Development with the Windows Presentation Foundation
- Foundations of WPF: An Introduction to Windows Presentation Foundation
- Windows Presentation Foundation(WPF)中的数据绑定(控件与控件值的绑定)
- Windows Presentation Foundation(WPF)介绍
- Windows Presentation Foundation(wpf) FAQ
- WPF(Windows Presentation Foundation)