简体   繁体   中英

Trying to draw the edges of a rectangular cube in WPF but not successful

I am trying to draw and display only the edges or sides of rectangular cube but still no success using WPF. It is inside a user control that I want to display in a windows form (c#) application. As I have seen, 3D objects in WPF are made using triangles so i tried to draw the lines using rectangles where the the width of the rectangle is small (enough to be recognized as an edge or side of the cube) but it is not displaying correctly. Here's the code I'm using:

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

I'm thinking of trying other graphics libraries like opengl or gdi+. If you know how to draw a 3D line in wpf then please demonstrate with an example.

Whatever you draw, it needs to be tessellated, since WPF essentially draws triangles.

So to model a line as a set of tessellated surfaces, first think of the line as a solid , or more specifically, a long rectangular prism with a small cross-section. If you think of the line this way, you have a six-sided solid (including the two "end caps" of the line, if you even want them), and you can define the appropriate triangles and WPF will render it just fine.

A little cumbersome for sure, but definitely doable.

If there is a better way out there to do this in WPF, I'd love to hear about it.

If you want to render the mesh of your 3D object, some recommend the Helixtoolkit add on. But the "linevisual3D" contained in this add on is pretty lame...

I encourage you to think not on 3D prisms (like SlimsGhost said) but as 2D rectangles and then using backmaterial properties to make it visible both ways. I've coded this and it work very fine (better than Helix Linevisual3D)

PS: if that really raises interest I can post the method in another dedicated thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM