WPF 3D代码给出了空白窗口。我该如何解决?

时间:2016-12-15 09:01:41

标签: wpf xaml graphics

TLDR:你能告诉我下面的WPF 3D应用程序这个简单的“你好世界”有什么问题吗?

长版:

我正在阅读一本计算机图形学书籍,该书提供了WPF中的示例。在这个特定的例子中,它们不提供完整的源代码,而是逐个部分地引入代码。

我到目前为止唯一的区别是他们使用<Page>作为根对象,我使用<Window>

我应该在窗口看到一个黄色的三角形,但我看到一个空的Windows窗口。

我正在使用Visual Studio 2015社区版。

MainWindow.xaml的代码如下。创建WPF Windows项目后,我只更改了此文件。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <MeshGeometry3D x:Key="RSRCmeshPyramid"
                        Positions="0,75,0 -50,0,50 50,0,50"
                        TriangleIndices="0 1 2" />
        <DiffuseMaterial x:Key="RSRCmaterialFront" Brush="Yellow"/>
        <DiffuseMaterial x:Key="RSRCmaterialBack" Brush="Red"/>
    </Window.Resources>


    <Viewport3D >
        <Viewport3D.Camera>
            <PerspectiveCamera Position="57, 247, 41"
                                   LookDirection="-0.2, 0, -0.9"
                                   UpDirection="0,1,0"
                                   NearPlaneDistance="0.02"
                                   FarPlaneDistance="1000"
                                   FieldOfView="45"/>
        </Viewport3D.Camera>

        <!-- scene -->
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <Model3DGroup>
                    <AmbientLight Color="White"/>
                    <DirectionalLight Color="Green" Direction="1,-1,-0.9"/>
                    <GeometryModel3D  
                            Geometry="{StaticResource RSRCmeshPyramid}"
                            Material="{StaticResource RSRCmaterialFront}"/>
                </Model3DGroup>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>


</Window>

1 个答案:

答案 0 :(得分:1)

MeshGeometry3D靠近原点并让相机指向它,如下所示:

enter image description here

<强> XAML:

<Window x:Class="WpfApplication343.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication343"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <MeshGeometry3D x:Key="RSRCmeshPyramid"
                    Positions="0,3,0 -2,0,2 2,0,2"
                    TriangleIndices="0 1 2" />
    <DiffuseMaterial x:Key="RSRCmaterialFront" Brush="Yellow"/>
    <DiffuseMaterial x:Key="RSRCmaterialBack" Brush="Red"/>
</Window.Resources>

<Grid>


    <Viewport3D >
        <Viewport3D.Camera>
            <PerspectiveCamera Position="10,10,10"
                               LookDirection="-1,-1,-1"
                               UpDirection="0,1,0"
                               NearPlaneDistance="0.02"
                               FarPlaneDistance="1000"
                               FieldOfView="45"/>
        </Viewport3D.Camera>

        <!-- scene -->
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <Model3DGroup>
                    <AmbientLight Color="White"/>
                    <DirectionalLight Color="Green" Direction="1,-1,-0.9"/>
                    <GeometryModel3D  
                        Geometry="{StaticResource RSRCmeshPyramid}"
                        Material="{StaticResource RSRCmaterialFront}"/>
                </Model3DGroup>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>

</Grid>