Click here to Skip to main content
15,891,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all
I am newbie with WPF
I am trying to draw triangle but it not rendering
what is going wrong with my code?

XML
<Viewport3D>
        <Viewport3D.Camera>
            <PerspectiveCamera Position="0,0,100" LookDirection="0,0,-100"
                         UpDirection="0,1,0" />
        </Viewport3D.Camera>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <Model3DGroup>
                    <DirectionalLight Color="White" Direction="-1,-1,-3" />
                    <GeometryModel3D>
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D Positions="10,0,0 -10,0,0 0,10,0"/>
                        </GeometryModel3D.Geometry>                        
                        <GeometryModel3D.Material>
                            <DiffuseMaterial Brush="Red"/>
                        </GeometryModel3D.Material>
                    </GeometryModel3D>
                </Model3DGroup>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>
Posted

1 solution

The problem is that every triangle has 2 sides, front and back, and WPF only renders the front side by default (I'm not sure if it's possible to enable backface rendering, but I believe it is).

The right-hand rule is applied to determine the front and back side of the triangle (which means that the order in which the vertices of a triangle are declared does matter!). In your case the back face of the triangle is facing to the camera, so it does not get rendered.

One soluton is to switch the order of vertices in your code:
XAML
<MeshGeometry3D Positions="-10,0,0 10,0,0 0,10,0"/>


The other way to do it is to specify the order of vertices explicitly:
XAML
<MeshGeometry3D Positions="10,0,0 -10,0,0 0,10,0" TriangleIndices="0 2 1"/>


That's it... Good luck with your project!
 
Share this answer
 
Comments
Khaniya 13-Oct-10 1:07am    
thanks for your response.
that means we have to assign point anticlockwise, right?
Kubajzz 13-Oct-10 13:28pm    
Yes, when looking at the front side of the triangle the points must be assigned in counter-clockwise order.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900