I make a WPF program using 3d graphics. The point of the program is to create a cube rotating by dragging the mouse.

The model itself is ready, event handlers are also implemented. However, there is one nuisance: at large angles of rotation, cube facets disappear somewhere near the cube. It looks like this:

enter image description here

How to fix it?

XAML form code:

<Window x:Class="laba10.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:laba10" mc:Ignorable="d" Title="RotateCube" Height="350" Width="525" MouseDown="Window_MouseDown" MouseMove="Window_MouseMove" MouseUp="Window_MouseUp"> <Grid> <Viewport3D> <Viewport3D.Camera> <PerspectiveCamera Position="1.5,0,1" LookDirection="0,0,-2" FieldOfView="105" /> </Viewport3D.Camera> <ModelVisual3D> <ModelVisual3D.Content> <DirectionalLight x:Name="light" Color="#FFFFFF" Direction="-1,-1,-2" /> </ModelVisual3D.Content> <ModelVisual3D.Transform> <RotateTransform3D> <RotateTransform3D.Rotation> <AxisAngleRotation3D x:Name="rotatel" Axis="0 1 0"/> </RotateTransform3D.Rotation> </RotateTransform3D> </ModelVisual3D.Transform> </ModelVisual3D> <!--wire model--> <ModelVisual3D> <ModelVisual3D.Content> <GeometryModel3D> <GeometryModel3D.Geometry> <MeshGeometry3D Positions="-1 -1 0 1 -1 0 -1 1 0 1 1 0 -1 -1 2 1 -1 2 -1 1 2 1 1 2 -1 -0.99 0 1 -0.99 0 0.99 -1 0 0.99 1 0 -1 0.99 0 1 0.99 0 -0.99 1 0 -0.99 -1 0 -1 -0.99 2 1 -0.99 2 0.99 -1 2 0.99 1 2 -1 0.99 2 1 0.99 2 -0.99 1 2 -0.99 -1 2" TriangleIndices="0 1 8 1 9 8 10 1 11 1 3 11 2 12 13 13 3 2 0 15 2 15 14 2 0 4 8 4 16 8 4 23 6 23 22 6 2 14 22 22 6 2 4 5 16 5 17 16 18 5 19 5 7 19 6 20 21 20 21 7 9 1 17 17 1 5 11 3 19 19 3 7"/> </GeometryModel3D.Geometry> <GeometryModel3D.Material> <DiffuseMaterial> <DiffuseMaterial.Brush> <SolidColorBrush Color="Black" Opacity="1" /> </DiffuseMaterial.Brush> </DiffuseMaterial> </GeometryModel3D.Material> <GeometryModel3D.Transform> <TranslateTransform3D OffsetX="1.5" OffsetY="0." OffsetZ="-3" > </TranslateTransform3D> </GeometryModel3D.Transform> </GeometryModel3D> </ModelVisual3D.Content> <ModelVisual3D.Transform> <RotateTransform3D CenterX="1.5" CenterY="0." CenterZ="-3"> <RotateTransform3D.Rotation> <AxisAngleRotation3D x:Name="rotatew" Axis="0 1 0"/> </RotateTransform3D.Rotation> </RotateTransform3D> </ModelVisual3D.Transform> </ModelVisual3D> <!--solid model--> <ModelVisual3D> <ModelVisual3D.Content> <GeometryModel3D> <GeometryModel3D.Geometry> <MeshGeometry3D Positions="-1 -1 0 1 -1 0 -1 1 0 1 1 0 -1 -1 2 1 -1 2 -1 1 2 1 1 2" Normals="0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1" TriangleIndices="0 1 2 1 3 2 0 4 2 4 6 2 1 5 3 5 7 3 4 5 6 5 7 6 0 1 4 1 5 4 2 3 6 3 6 7" /> </GeometryModel3D.Geometry> <GeometryModel3D.Material> <DiffuseMaterial> <DiffuseMaterial.Brush> <SolidColorBrush Color="Cyan" Opacity="0.5"/> </DiffuseMaterial.Brush> </DiffuseMaterial> </GeometryModel3D.Material> <!-- Translate the plane. --> <GeometryModel3D.Transform> <TranslateTransform3D OffsetX="1.5" OffsetY="0" OffsetZ="-3" > </TranslateTransform3D> </GeometryModel3D.Transform> </GeometryModel3D> </ModelVisual3D.Content> <ModelVisual3D.Transform> <RotateTransform3D CenterX="1.5" CenterY="0." CenterZ="-3"> <RotateTransform3D.Rotation> <AxisAngleRotation3D x:Name="rotates" Axis="0 1 0"/> </RotateTransform3D.Rotation> </RotateTransform3D> </ModelVisual3D.Transform> </ModelVisual3D> </Viewport3D> </Grid> </Window> 
  • Maybe move away from the cube? Probably your camera turns when turning inside a cube. - VladD
  • @VladD, tried on different axes, the cube moves in shape, but it does not help. - Byulent
  • And if you take the camera very far? (But without fanaticism, so that the cube is not very small.) - VladD
  • @VladD Apparently, that's not the point. It does not help . - Byulent

1 answer 1

As often happens, the answer lay almost on the surface. The problem was incorrectly placed TriangleIndices . After I corrected them for the values ​​given in the manuals, everything worked as it should.

True, I still do not consider what changes need to be made for the wire model.