WPF Cover Flow Tutorial : Part 1

15 篇文章 0 订阅
Disclaimer : if you don't know anything about WPF, you should read this excellent tutorial. This should be considered as Part 0.

Now, I will describe how to develop a Cover Flow component in WPF.
The z-axis is not visible here. Actually, it is going in your direction.

The first basic task will just display one cover in the middle of the screen.
In 3D, we usually work with triangles.
So we simply cut the square in two :

Let's start with the code. We create the 4 points :
  1. var p0 = new Point3D(-1, -1, 0);  
  2. var p1 = new Point3D(1, -1, 0);  
  3. var p2 = new Point3D(1, 1, 0);  
  4. var p3 = new Point3D(-1, 1, 0);  
Then we create a MeshGeometry3D object. This object will contain our model. In order to build a model, we need to set all the points. Then, for each triangle, we define the point indices and the normals. To calculate the normals, I use the same method as the one defined in Part 0.
  1. private Vector3D CalculateNormal(Point3D p0, Point3D p1, Point3D p2)  
  2. {  
  3. var v0 = new Vector3D(p1.X - p0.X, p1.Y - p0.Y, p1.Z - p0.Z);  
  4. var v1 = new Vector3D(p2.X - p1.X, p2.Y - p1.Y, p2.Z - p1.Z);  
  5. return Vector3D.CrossProduct(v0, v1);  
  6. }  
For the first triangle, we take the first 3 points. The normal is OK with these points (going towards us, in the positive direction of the z-axis). We need to pay attention to the second triangle. If we simply take the points in the same order (e.g. 1, 2 and 3), the normal will be inverted, in the negative direction of the z-axis. So we choose the points 0, 2 and 3.
  1. var mesh = new MeshGeometry3D();  
  2. mesh.Positions.Add(p0);  
  3. mesh.Positions.Add(p1);  
  4. mesh.Positions.Add(p2);  
  5. mesh.Positions.Add(p3);  
  6.   
  7. var normal = CalculateNormal(p0, p1, p2);  
  8. mesh.TriangleIndices.Add(0);  
  9. mesh.TriangleIndices.Add(1);  
  10. mesh.TriangleIndices.Add(2);  
  11. mesh.Normals.Add(normal);  
  12.   
  13. normal = CalculateNormal(p2, p3, p0);  
  14. mesh.TriangleIndices.Add(2);  
  15. mesh.TriangleIndices.Add(3);  
  16. mesh.TriangleIndices.Add(0);  
  17. mesh.Normals.Add(normal);  
As far as texturing is concerned, I strongly advise you to read Daniel Lehenbauer's Blog. We need to deal with 4 points, but in 2D this time :
  1. var q0 = new Point(0, 0);  
  2. var q1 = new Point(1, 0);  
  3. var q2 = new Point(1, 1);  
  4. var q3 = new Point(0, 1);  
As you've read in Daniel's post, there is a big difference between 3D and 2D texture conventions. In 3D, you have :




But 2D texturing uses :






So the coordinates associations are :
  • p0 <-> q3
  • p1 <-> q2
  • p2 <-> q1
  • p3 <-> q0
We just need to add the texture points to the mesh for each triangle point :
  1. mesh.TextureCoordinates.Add(q3);  
  2. mesh.TextureCoordinates.Add(q2);  
  3. mesh.TextureCoordinates.Add(q1);  
  4.   
  5. mesh.TextureCoordinates.Add(q0);  
  6. mesh.TextureCoordinates.Add(q1);  
  7. mesh.TextureCoordinates.Add(q2);  
The mesh will be frozen for performance reasons. In the future, we will only need to transform this mesh with rotations and translations. We will not need to move the points.
This gives us a Tesselate method that will return the mesh :
  1. private Geometry3D Tessellate()  
  2. {  
  3. var p0 = new Point3D(-1, -1, 0);  
  4. ...  
  5. var mesh = new MeshGeometry3D();  
  6. ...  
  7. mesh.Freeze();  
  8. return mesh;  
  9. }  
This method will be part of a Cover class. This class will be the main class to deal with covers. Here are two more methods that will help us to load the texture image :
  1. private ImageSource LoadImageSource(string imagePath)  
  2. {  
  3. Image thumb = Image.FromFile(imagePath);  
  4. return new BitmapImage(new Uri(imagePath, UriKind.RelativeOrAbsolute));  
  5. }  
  6. private Material LoadImage(ImageSource imSrc)  
  7. {  
  8. return new DiffuseMaterial(new ImageBrush(imSrc));  
  9. }  
We finish the class with its constructor :
  1. using ...  
  2. namespace Ded.Tutorial.Wpf.CoverFlow.Part1  
  3. {  
  4. class Cover : ModelVisual3D  
  5. {  
  6. #region Fields  
  7. private readonly Model3DGroup modelGroup;  
  8. #endregion  
  9. #region Private stuff  
  10. private Vector3D CalculateNormal(Point3D p0, Point3D p1, Point3D p2)...  
  11. private Geometry3D Tessellate()...  
  12. private ImageSource LoadImageSource(string imagePath)...  
  13. private Material LoadImage(ImageSource imSrc)...  
  14. #endregion  
  15. public Cover(string imagePath)  
  16. {  
  17.  ImageSource imSrc = LoadImageSource(imagePath);  
  18.  modelGroup = new Model3DGroup();  
  19.  modelGroup.Children.Add(new GeometryModel3D(Tessellate(), LoadImage(imSrc)));  
  20.  Content = modelGroup;  
  21. }  
  22. }  
  23. }  
Let's load this class in an empty WPF application.

We place the camera at (0, 0, 3). We add a light source so that we will not see black objects.

We also add an empty ModelVisual3D object that will contain our single cover (for now).

Here is the xaml code :
  1. <Window x:Class="Ded.Tutorial.Wpf.CoverFlow.Part1.TestWindow"  
  2.  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.  Title="WPF Coverflow" Height="320" Width="512">  
  5. <Grid>  
  6.  <Viewport3D x:Name="viewPort" Grid.Column="0" Grid.Row="0" ClipToBounds="False">  
  7.    <Viewport3D.Camera>  
  8.      <PerspectiveCamera x:Name="camera" Position="0,0,3"  
  9.        UpDirection="0,1,0" LookDirection="0,0,-1"  
  10.        FieldOfView="100" NearPlaneDistance="0.125"/>  
  11.    </Viewport3D.Camera>  
  12.    <Viewport3D.Children>  
  13.      <ModelVisual3D>  
  14.        <ModelVisual3D.Content>  
  15.          <DirectionalLight Color="White" Direction="0,0,-4" />  
  16.        </ModelVisual3D.Content>  
  17.      </ModelVisual3D>  
  18.      <ModelVisual3D x:Name="visualModel">  
  19.      </ModelVisual3D>  
  20.    </Viewport3D.Children>  
  21.  </Viewport3D>  
  22. </Grid>  
  23. </Window>  
This simple application will display :This can be prettier if we add some background :
  1. <Grid.Background>  
  2.  <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">  
  3.    <LinearGradientBrush.GradientStops>  
  4.      <GradientStop Color="Black" Offset="0"/>  
  5.      <GradientStop Color="#696988" Offset="1"/>  
  6.    </LinearGradientBrush.GradientStops>  
  7.  </LinearGradientBrush>  
  8. </Grid.Background>  

Continue with Part 2. Download source.

Edit 2011.06.07 : Only one normal is needed per triangle. Source code has not been updated.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值