2012-04-03 42 views
2

我想绘制和显示矩形立方体的边或边,但仍然没有成功使用WPF。它位于用户控件中,我想在Windows窗体(c#)应用程序中显示。正如我所看到的,WPF中的3D对象是用三角形制作的,所以我尝试使用矩形绘制线条,其中矩形的宽度很小(足以识别为立方体的边或侧),但不显示正确。这里是我使用的代码:试图在WPF中绘制矩形立方体的边缘,但不成功

<UserControl x:Class="Spatial_and_Temporal_Research.BoundingBoxes" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" DataContext="{Binding}" Height="509" Width="739" FontFamily="Times New Roman"> 
     <Grid> 

      <!-- Place a Label control at the top of the view. --> 
      <Label 
        HorizontalAlignment="Center" 
        TextBlock.TextAlignment="Center" 
        FontSize="20" 
        Foreground="Red" 
        Content="Model: Cone"/> 

      <!-- Viewport3D is the rendering surface. --> 
      <Viewport3D Name="myViewport" > 

       <!-- Add a camera. --> 
       <Viewport3D.Camera> 
        <PerspectiveCamera 
          FarPlaneDistance="20" 
          LookDirection="-6, -5, -4" 
          UpDirection="0,1,0" 
          NearPlaneDistance="1" 
          Position="6 5 4" 
          FieldOfView="45" /> 

       </Viewport3D.Camera> 

       <!-- Add models. --> 
       <Viewport3D.Children> 

        <ModelVisual3D> 
         <ModelVisual3D.Content> 

          <Model3DGroup > 
           <Model3DGroup.Children> 

            <!-- Lights, MeshGeometry3D and DiffuseMaterial objects are added to the ModelVisual3D. --> 
            <AmbientLight Color="White" /> 

            <!-- Define a red cone. --> 
            <GeometryModel3D> 

             <GeometryModel3D.Geometry> 
              <MeshGeometry3D 
     Positions="-0.55 -0.5 -0.5 -0.45 -0.5 -0.5 -0.55 -0.5 0.5 -0.45 -0.5 0.5 -0.55 0.5 -0.5 -0.45 0.5 -0.5 -0.55 0.5 0.5 -0.45 0.5 0.5 0.45 -0.5 -0.5 0.55 -0.5 -0.5 0.45 -0.5 0.5 0.55 -0.5 0.5 -0.5 -0.55 -0.5 -0.5 -0.45 -0.5 0.5 -0.55 -0.5 0.5 -0.45 -0.5 -0.5 -0.55 0.5 -0.5 -0.45 0.5 0.5 -0.55 0.5 0.5 -0.45 0.5 0.45 0.5 -0.5 0.55 0.5 -0.5 0.45 0.5 0.5 0.55 0.5 0.5 -0.5 0.45 -0.5 -0.5 0.55 -0.5 0.5 0.45 -0.5 0.5 0.55 -0.5 -0.5 0.45 0.5 -0.5 0.55 0.5 0.5 0.45 0.5 0.5 0.55 0.5" 

     TriangleIndices="0 2 3 3 1 0 4 6 7 7 5 4 1 0 4 4 5 1 3 2 6 6 7 3 8 10 11 11 9 8 13 12 14 14 15 13 17 16 18 18 19 17 20 22 23 23 21 20 17 16 20 20 21 17 19 18 22 22 23 19 25 24 26 26 27 25 29 28 30 30 31 29"          
      > 

              </MeshGeometry3D> 
             </GeometryModel3D.Geometry> 

             <GeometryModel3D.Material> 
              <DiffuseMaterial> 
               <DiffuseMaterial.Brush> 
                <SolidColorBrush 
           Color="Red" 
           Opacity="1.0"/> 
               </DiffuseMaterial.Brush> 
              </DiffuseMaterial> 
             </GeometryModel3D.Material>        

            </GeometryModel3D> 

           </Model3DGroup.Children> 
          </Model3DGroup> 

         </ModelVisual3D.Content> 

        </ModelVisual3D> 

       </Viewport3D.Children> 

      </Viewport3D> 
     </Grid> 

</UserControl> 

我想着尝试其他图形库,如opengl或gdi +。如果您知道如何在wpf中绘制3D线,那么请举例说明。任何帮助将不胜感激。提前致谢。

+0

矩形立方体?你的意思是直角棱镜吗? – bouvierr 2012-04-03 22:14:26

+0

你想绘制一个对象的线框? – ChrisF 2012-04-03 22:15:03

+0

我编辑了帖子。只有我想看到的物体的边缘或边缘。我不想绘制完整的对象或可见的面,只有对象的边框或边/边。 – mj1261829 2012-04-03 22:25:29

回答

0

无论您绘制什么,它都需要镶嵌细分,因为WPF基本上绘制了三角形。

因此,要将一条线模型化为一组镶嵌曲面,首先将该线看作为实体,或更具体地说,具有小横截面的长矩形棱镜。如果你用这种方式来思考这条线,那么你有一个六面实体(包括线的两个“端盖”,如果你甚至想要它们的话),你可以定义合适的三角形,而WPF会渲染它。

肯定有点麻烦,但绝对可行。

如果在WPF中有更好的方法来做到这一点,我很乐意听到它。

祝你好运!

0

如果要渲染3D对象的网格,可以使用Helixtoolkit的一些重新添加。但包含在这个添加中的“linevisual3D”相当蹩脚...... 我鼓励你不要三维棱镜(就像SlimsGhost所说的),而是作为二维矩形,然后通过backmaterial属性使其可见。 我编写这个和它的做工非常精细

我想添加的图像(比螺旋Linevisual3D更好),但我不能,因为我没有我的“10点名誉” ^^:

PS:如果这真的引起了兴趣,我可以在另一个专用线程中发布该方法。

问候函