Bookmark and Share Share...    Subscribe to this feed Feed   About me...


Introduction to WPF 3D

3D Graphic Basics

The basic idea of creating 3D graphics is to have a three dimensional model of an object. Because our screen is only two dimensional, we define a camera that takes a picture of the object. The picture is a projection of the object to a planar surface. This projection is rendered into an bitmap by the 3D rendering engine. The engine determines the color for every pixel by calculating the amount of light that is reflected by any light sources to the projection surface by the objects in the 3D space.
All surfaces of objects have a material and a brush. The material defines how much light is reflected for a specific angle and the brush defines the color. A brush can either be a simple color or a gradient or even an image called texture.


A world of triangles

In the world of 3D graphics, all objects are described by a set of triangles. But why triangles? The reason for this is that a triangle is the most granular geometry to describe a planar surface. The rendering engine can calculate the color of each triangle depending on its material and angle to the lights in the scene. If we would build our world on rectangles, the points don't need to be plane. The surface will be a lot more complex to calculate and render.

A surface of a 3D object is called a mesh. A mesh is defined by a number of 3D points. These points are called vertices. The vertices are joined together by a winding pattern to define the triangles. Every triangle has a front and a back side. Only the front side is rendered. The front side is defined by the winding order of the points. WPF uses a counter clockwise winding pattern. You can remember this with a simple mnemonic called the "right hand rule". This means if you close your right hand to a thumb-up gesture, your finger make a counter clockwise move while the thumb shows up. The fingers show the winding order, while the thumb indicates the upper-side of the triangle.


A right handed coordinate system

WPF uses a right-handed coordinate system. This means that if you hold the fingers of your right hand as shown on the picture, the fingers indicate the positive direction of the tree axes.


Elements of a 3D scene

  • Viewport3D
    The viewport is the control that builds the gate between the 2D and the 3D world.
  • Camera
    Every 3D scene has excactly one camera. The camera defines the Position and the LookDirection and the UpDirection of the viewer. WPF supports orthographical and perspective cameras.
  • 3D Models
    A 3D model defines an object in the scene. It has a Geometry that is the mesh and a Material that can be a diffuse, specular or emmisive material. The material itself has a brush.
  • Lights
    Without any lights you see nothing. So we need to place at least one light in our scene to illuminate our models. WPF supports different kind of lights, like:
    • AmbientLight
    • DirectionalLight
    • PointLight
    • SpotLight

Hello 3D-World - A Simple Cube

The following example shows how to create a basic cube mesh with 8 vertices that are wired up in 12 triangles that form the shape of the cube.

<Viewport3D>
   <Viewport3D.Camera>
      <PerspectiveCamera Position="-40,40,40" LookDirection="40,-40,-40 " 
                         UpDirection="0,0,1" />
   </Viewport3D.Camera>
   <ModelVisual3D>
      <ModelVisual3D.Content>
         <Model3DGroup>
            <DirectionalLight Color="White" Direction="-1,-1,-3" />
               <GeometryModel3D>
                  <GeometryModel3D.Geometry>
                     <MeshGeometry3D Positions="0,0,0 10,0,0 10,10,0 0,10,0 0,0,10 
                        10,0,10 10,10,10 0,10,10"
                        TriangleIndices="0 1 3 1 2 3  0 4 3 4 7 3  4 6 7 4 5 6 
                                         0 4 1 1 4 5  1 2 6 6 5 1  2 3 7 7 6 2"/>
                  </GeometryModel3D.Geometry>
                  <GeometryModel3D.Material>
                     <DiffuseMaterial Brush="Red"/>
                  </GeometryModel3D.Material>
               </GeometryModel3D>
            </Model3DGroup>
         </ModelVisual3D.Content>
      </ModelVisual3D>
</Viewport3D>
 



 Comments on this article

Show all comments
vishnu
Commented on 6.January 2010
Good one
Steam
Commented on 18.January 2010
I think austin is right. In both coordinate systems (left-hand and right-hand) positive X is right, positive Y is upward. The diference is Z axis. See http://msdn.microsoft.com/en-us/library/ee415205%28VS.85%29.aspx for details.
Sandeep...
Commented on 22.January 2010
Very good tutorial!

Sandeep
http://sandeep-aparajit.blogspot.com
Deepak
Commented on 3.February 2010
Thanks For the Tutorial..
Kevin
Commented on 9.February 2010
Uh, this is pretty useless. I think that you just copied the article here: http://www.kindohm.com/technical/wpf3dtutorial.htm which explains exactly what you did in better detail and goes a little more in depth... noone wins when you plagiarize.
Qyuan
Commented on 10.March 2010
Good tut, nice work! Thanks alot.
Arturiux
Commented on 22.March 2010
Yo soy aprendiz en WPF pero tengo un truco para crear un modelo tridimensional que sea algo complicado y con muy buena presentación, Primero crea el modelo en SolidWorks, Guardalo como .XAML, Después utilice Expression Blender y ahi lo agregue como recurso .XAML en la opción agregar elemento existente. El archivo generado ya cuenta con viewport, Camara, Lights y los 3dmodels para cada parte del ensamble de solid.
csmac
Commented on 7.April 2010
Very nice tutorial of the basics of 3D in wpf. Very helpful. Keep up the good work.
mohamed...
Commented on 1.May 2010
a very good tutorial, i need more aboute 3D tutorials
Norris
Commented on 7.May 2010
Thanks for this tutorial ! I learn WPF since ... 2 hours and I have understand all lol.I use much the 3D as a computer graphics "expert" (MAXON C4D) and I don't understand Why Microsoft have choosed to use the Right Hand Coordinate system.is this only to induce us in error?
Raja
Commented on 7.May 2010
Good article for WPF 3D begineers
Ammar marzouk
Commented on 13.May 2010
how i can build a good applicatio with wpf?
Nobody
Commented on 14.June 2010
Dude, such a fake site. You're just copy & pasting the first page of any WPF for dummies tutorial and selling it as your own. Shame on you. Bet you got even hired due to this site by some brain-dead company which thought you're a WPF expert... lol.. the world is full of strange people indeed.
Parimal Pimple
Commented on 14.June 2010
Great site. Helped me a lot learning WPF Basics.
khagesh
Commented on 17.June 2010
vry good..
Tyson
Commented on 17.June 2010
vry good.. U Rock Dude..Who!!
ar
Commented on 18.June 2010
good job
subhkriti
Commented on 21.June 2010
fabulous article, such a nice concept seems directly delivered from physics..but my expectation is more than this.
arjun
Commented on 25.June 2010
fabulous article, such a nice concept seems directly delivered from physics..but my expectation is more than this.but I don't know how to use
Adam
Commented on 4.July 2010
I have the following XAML code.
P0 (0, -3.0), P1 (5, -3.0), P2 (5, -3.5), P3 (0, -3.5).

"<Viewport3D Height="750" Width="800">
<Viewport3D.Camera>
<PerspectiveCamera Position="10,-1,10" LookDirection="-10,1,-10" UpDirection="0,1,0" />
</Viewport3D.Camera>
<ModelVisual3D>
<ModelVisual3D.Content>
<Model3DGroup>
<AmbientLight Color="Bisque"/>
<DirectionalLight Color="Red" Direction="-3,-5,-2"/>
<GeometryModel3D>
<GeometryModel3D.Geometry>
<MeshGeometry3D Positions="0,-3,0 5,-3,0 5,-3,5 0,-3,5" TriangleIndices="0 2 1 0 3 2 "/>
</GeometryModel3D.Geometry>
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Brown"/>
</GeometryModel3D.Material>
</GeometryModel3D>
</Model3DGroup>
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D>"

I want to draw the line p0-> P1, P1-> P2, P2-> 3 ... . We have different colors. I draw the square by p0, p1, p2, p3 with two three price p0, P1, P2 and P1, P2, P3. I would like this triangle we have two different colors. So please just help me XAML code.
humanblade
Commented on 11.July 2010
i've been following examples on this site, most have been good. this one slit my wife's throat and proceeded to violate the gash with it's digital member. where do i submit bug?
Techie_Devel...
Commented on 20.July 2010
Very Nice article!!
Giles
Commented on 6.August 2010
It doesn't matter which way you hold your hand, and whether any axis ends up being up/down/in/out or what ever. The right hand concept is that going from pointer finger to middle figer to thumb represent x,y and z, and the image in this article conforms to this. The differnce lies in that in the the right hand system positive rotations occur anti-clockwise about an axis, for example, to rotate the X axis towards the Y, you would rotate about Z in a anti-clockwise direction with respect to the Z axis. In the left hand system a clockwise rotation about the Z axis would be required to rotate the X axis towards the Y.
Christian Moser
Commented on 23.August 2010
Its funny that you guys find my articles so helpful considering I just copy and pasted random shit from other random sites that I found. Its all good though because those google ads are making me cash!!!
Christian Moser
Commented on 23.August 2010
Its funny that you guys find my articles so helpful considering I just copy and pasted random shit from other random sites that I found. Its all good though because those google ads are making me cash!!!

Name
E-Mail (optional)
Comment
About Christian Moser