drawing a line in 3d in wpf
Solution 1
You are creating a triangle where two corners are at the same point, that is a triangle with zero area so it can never be seen from any angle. WPF uses only triangles with area
To create a line you must make a rectangle the length and width of the line, and split it with a diagonal to create two narrow triangles. So you need four positions and two triangles, like "0 1 2 0 1 3". Then of course you must make sure that the orientation of this rectangle is so that it is facing the camera.
You can google or bing for helix toolbox which is an excellent library of utilities for 3D in WPF. There you might find a useful helper function.
Solution 2
Why do you want to draw just line. In 3D you usually need triangles. If you have triangle you can determine normals
. They are used to define facing of triangle which is used in lighting and texturing.
Camera settings
Typical camera Position
is somewhere in positive z coordinates (something like 0, 0, 2 or 5, 0, 20), LookDirection
vector is 0, 0, -1 and UpDirection
vector is 0, 1, 0. In this case axes should be positioned as they usually are (positive y axis goes up and positive x axis goes to the right).
If you change UpDirection
to 1, 0, 0 then positive x axis goes up not y axis.
If you change Position
to negative z coordinates (0, 0, -5) and LookDirection
to 0, 0, 1 then you look at your sceen from "behind" so positive x axis goes to the left (not to the right) but y axis still goes up.
In your settings your camera is pointing to the positive y numbers from origin and z axis goes up. X axis still goes to the right. If I add third point to your code. Point 100, 0, 100, than you can see small black triangle.
mesh.Positions.Add(startPoint);
mesh.Positions.Add(endPoint);
mesh.Positions.Add(new Point3D(100, 0, 100));
mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(1);
mesh.TriangleIndices.Add(2);
This triangle is small because of distance from camera - 100. So as @Samuels says in comment another error in settings is FarPlaneDistance="10"
.
mans
Updated on June 04, 2022Comments
-
mans almost 2 years
I am trying to draw a 3D line in wpf and I have this xaml code:
<Grid> <Viewport3D x:Name="ViewerViewport" RenderOptions.BitmapScalingMode="HighQuality" Focusable="True" Grid.RowSpan="2"> <ModelVisual3D x:Name="Model"> </ModelVisual3D> <!-- Camera --> <Viewport3D.Camera> <PerspectiveCamera x:Name="Camera" Position="0,0,0" LookDirection="0,1,0" UpDirection="0,0,1" FieldOfView="100" FarPlaneDistance="10" NearPlaneDistance="0.1"/> </Viewport3D.Camera> </Viewport3D> </Grid>
and this c# code:
public MainWindow() { InitializeComponent(); var ModelsGroup = new Model3DGroup(); ModelsGroup.Children.Add(this.AddLine(new Point3D(0, 0, 100), new Point3D(0, 100, 100),"line 1)")); ModelsGroup.Children.Add(new AmbientLight(Colors.White)); Model.Content = ModelsGroup; }
and line creation code:
private Model3D AddLine(Point3D startPoint, Point3D EndPoint, string name) { SolidColorBrush brush = new SolidColorBrush(Colors.Black); var material = new DiffuseMaterial(brush); var mesh = new MeshGeometry3D(); mesh.Positions.Add(startPoint); mesh.Positions.Add(EndPoint); mesh.TriangleIndices.Add(0); mesh.TriangleIndices.Add(1); mesh.TriangleIndices.Add(0); return new GeometryModel3D(mesh, material); }
but it doesn't show any line in output?
What is wrong with this?
I know that there are some 3d libraries that can do this easily, but I like to learn how to do it in WPF and then investigate how to do this using libraries (such as helix3d)