RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


WPF Wonders: 3D Drawing : Page 3

WPF makes drawing three-dimensional scenes quick and easy.



Even if you've defined 3D objects and defined a camera, you still won't see anything, because the lights are turned off. You need to add lights to a scene to make anything visible. WPF provides four kinds of lights: ambient, directional, point, and spot.

  • Ambient light hits every surface in the scene equally. This is the kind of light that lets you see under your desk (assuming it's daylight and the curtains are open) even though you probably don't have a light under there.
  • Directional light hits every surface in a scene as if it is coming from a specific direction. For example, a directional light might point down along the Y axis or it might point left along the X axis.
  • A point light sends light radiating out from a particular spot, as if you had a tiny light bulb there.
  • A spot light sends light out in a cone pointed in a particular direction. Surfaces in the center of the cone receive more light than those near the edges, and surfaces that are completely outside the cone get no light at all.
    Figure 4. Bright Lights: Different lights result in different effects.

    The direction at which light strikes a surface is important because it helps determine the surface's color. When light falls squarely on a surface, that surface receives a lot of light. If the light hits the surface at an oblique angle, the surface receives less light. Note that a point light source will cast light that makes different angles with different surfaces depending on their orientations to the source.

    Figure 4 shows the four different types of lights shining on a green square.

    Each square in Figure 4 is actually made up of a lot of little triangles. That's particularly important for the point and spot lights, because it lets each triangle have a different color depending on that particular triangle's relationship to the light. If each square consisted of only two big triangles, each triangle would make a single angle with the light source and therefore would have a single color, which wouldn't show the changing color effects produced by the lights.

    Ambient and directional lights are more efficient than point and spot lights. If you have a very complex scene, you may need to stick with ambient and directional lights to maximize performance.

    Figure 5. See the Light: All three cubes are identical, but changing the lighting changes your perception of the cubes. Sometimes you need to use many lights to make a scene look right.

    Some scenes use different materials to color different shapes. For example, if you give each side of a cube a different color, it's easy to tell where one side ends and the next begins. However, if all the sides have the same color, then you need to light the scene carefully; otherwise, the sides will all blend together (see Figure 5).

    The first cube in Figure 5 uses ambient light only, so all the cube's sides have the same color—and they blend together. The following code shows how this scene creates its light.

    <AmbientLight Color="LightGray"/>

    The second cube in Figure 5 uses ambient light and one directional light. The directional light strikes the three visible faces at slightly different angles, so they appear as different colors. Unfortunately the light doesn't strike the cube's hidden faces so those faces all have the same color. You can download the code and try it yourself. If you use the program's scroll bars to peek behind and below the cube, you'll see that those faces blend together. The following code shows how this scene creates its lights.

    <AmbientLight Color="Gray"/>
    <DirectionalLight Color="Gray" Direction="-1,-3,-2"/>

    The final cube uses the same lights as the second cube but adds an additional directional light that shines on two of the cube's hidden faces. The new light doesn't shine on the cube's bottom, so that face is the darkest. If you rotate the scene, you'll see that every face has a slightly different color than its neighbors. The following code shows how this scene creates its lights.

    <AmbientLight Color="Gray"/>
    <DirectionalLight Color="Gray" Direction="-1,-3,-2"/>
    <DirectionalLight Color="Gray" Direction="1,-2,3"/>

    Note that lighting is cumulative. Lots of gray lights produce as much light as fewer light gray or white lights.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date