2012-04-03 43 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:如果這真的引起了興趣,我可以在另一個專用線程中發佈該方法。

問候函