2009-06-03 18 views
0

我使用Windows.Media.Media3D工具包将DirectX应用程序移植到WPF中。 除了即使已经获得所有可能的数据,模型呈现完整之外,哪一个工作正常,并且都是正常工作的。MeshGeometry3D中的顶点平滑(WPF)

我怀疑这是因为DirectX优化,并以某种方式平滑或帮助渲染额外的内容取决于顶点和指示。

所以我想知道是否有一个选项可以改变WPF渲染3D模型的方式?要表达我的意思有点困难,但我希望它能以某种方式理解。

这里的重要代码:

private void DrawModel() 
{ 
    SwordModel swordModel; 

    string filename = "PG_S8_02"; 

    using(FileStream stream = File.OpenRead(string.Format("Media/Models/{0}.gb", filename))) 
    using(BinaryReader reader = new BinaryReader(stream)) 
    { 
     swordModel = new SwordModel(reader);   
    } 

    var group = new Model3DGroup(); 

    foreach(var modelMesh in swordModel.Meshes) 
    { 
     var mesh3D = new MeshGeometry3D(); 

     // Indices 
     foreach(var index in modelMesh.Indices) 
     { 
      mesh3D.TriangleIndices.Add(index); 
     } 

     // Vertices 
     foreach(var vertex in modelMesh.Vertices) 
     { 
      mesh3D.Positions.Add(vertex); 
     } 

     // Normals 
     foreach(var normal in modelMesh.Normals) 
     { 
      mesh3D.Normals.Add(normal); 
     } 

     // Texture Coordinates 
     foreach(var vertex in modelMesh.Vertices) 
     { 
      mesh3D.TextureCoordinates.Add(new Point(vertex.X, vertex.Y)); 
     } 

     // Material 
     var material = new DiffuseMaterial(); 
     var texture = new BitmapImage(new Uri(
      string.Format("Media/Textures/{0}.png", filename), 
      UriKind.RelativeOrAbsolute 
     )); 
     material.Brush = new ImageBrush(texture);        

     // Add to View 
     group.Children.Add(
      new GeometryModel3D(mesh3D, material) 
     ); 
    }  

    mainViewport.Children.Add(
     new ModelVisual3D() { Content = group } 
    );   
} 

和XAML:

<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> 

回答

2

的唯一的事情,但没有看到网格看起来像我无法确定的。因此,我的意思是顶点法线与面向法线的平行线赋予网格多面的外观。如果法线平滑,那么整个形状看起来很平滑。事情是这样的:

平面渲染:

http://www.forman.free-online.co.uk/images/flatshading.png

光滑着色:

http://www.forman.free-online.co.uk/images/smoothshading.png

通过链接替换

图像来限制我的带宽使用

+0

它看起来像面子的“片面性”是问题(请参阅Ian Hopkinson的回答),虽然DirectX(我认为)也是如此,所以我不能立即想到转换会出错的原因。 – ChrisF 2009-06-03 09:08:33

0

是否与片面性问题?默认情况下,我认为WPF呈现方面 - 你不能从错误的一面看到它们。

同样有灯光,你可以得到奇怪的效果,如果你的模型没有从各个方向照射 - 尝试把在我能想到的是,法线不进行平滑处理中AmbientLight