Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

WPF Wonders: 3D Drawing : Page 4

WPF makes drawing three-dimensional scenes quick and easy.


advertisement

Materials

I said earlier that a surface's color depends on the angle at which light strikes it. The color also depends on the material out of which it is made. WPF provides three kinds of materials: diffuse, specular, and emissive.

  • Diffuse materials absorb light and then radiate some of the light's energy in various wavelengths, depending on the material's color. Basically this is what ordinary objects do. If you shine a white light on a green tennis ball, the ball radiates green light.
  • Specular materials are shiny. They reflect light brightly near the mirror angle their surface makes with the light source. (If the object were perfectly reflective, then the mirror angle is where you would see the reflection of the light source.) The result is an extra bright spot. For example, if you shine a white light on a polished red apple, you'll see a bright white highlight near the mirror angle.
  • Emissive materials glow. Note that an emissive material in WPF doesn't act as a light source for other objects. It makes its own object glow but does not shine on other objects. Giving an object an emissive material makes it brighter. Normally, you can achieve a similar effect by using brighter lights, but using a material lets you make one object brighter without affecting others as brighter lights would.
  • Normally, diffuse materials contribute the most to an object's color, and often you can get away with using only diffuse materials. To add highlights or adjust brightness, however, you can combine more than one type of material in the same object.

     
    Figure 6. Marvelous Materials: The diffuse, specular, and emissive materials are combined to create the bright green ball with a highlight on the right.



    The first three spheres in Figure 6 are drawn with diffuse, specular, and emissive materials, while the rightmost sphere combines all three materials to produce a bright green ball with a highlight.

    There are two main ways to use a material in XAML code. First, you can define a material right within the 3D object that uses it. The following XAML code defines a GeometryModel3D object that creates a rectangle. The object's Material attribute element defines the yellow diffuse material that the square uses. (Don't worry about the rest of the code right now. I'll get to it later.)

    <GeometryModel3D> <GeometryModel3D.Material> <MaterialGroup> <DiffuseMaterial Brush="Yellow"/> </MaterialGroup> </GeometryModel3D.Material> <GeometryModel3D.Geometry> <MeshGeometry3D Positions="1,-1,1 1,-1,-1 1,1,-1 1,1,1" TriangleIndices="0,1,2 0,2,3" /> </GeometryModel3D.Geometry> </GeometryModel3D>

    The second approach is to define the material in a Resources section and later refer to it as a static resource. For example, the following code fragment defines a diffuse material named matRed in the Window's Resources section. Later a GeometryModel3D object defines an object (a rectangle) that uses this material as a StaticResource. (Don't worry about the missing code for now.)

    <Window ...> <Window.Resources> <MaterialGroup x:Key="matRed"> <DiffuseMaterial Brush="Red"/> </MaterialGroup> ... <Window.Resources> ... <GeometryModel3D Material="{StaticResource matRed}"> <GeometryModel3D.Geometry> <MeshGeometry3D Positions="1,-1,1 1,-1,-1 1,1,-1 1,1,1" TriangleIndices="0,1,2 0,2,3" /> </GeometryModel3D.Geometry> </GeometryModel3D> ... </Window>

    This approach is useful if you want to make many objects that use the same material. For example, if you want to make a cube consisting of six sides that all use the same material, you can define the material as a resource once and refer to it six times. Later, if you decide to change the cube from red to blue, you need to change only the material's definition.

    The spheres in Figure 6 are actually created by code sitting behind the XAML file. Each sphere contains 3,420 triangles so, while you could build them all by hand in the XAML file, it's a lot easier to generate them from code using a couple of for loops.

    The following code shows the Window Loaded event handler used by program Materials.

    // Build the model. private void Window_Loaded(object sender, RoutedEventArgs e) { MakeSingleMeshSphere(Sphere00, new DiffuseMaterial(Brushes.Green), 1, 20, 30); MakeSingleMeshSphere(Sphere01, new SpecularMaterial(Brushes.Green, 50), 1, 20, 30); MakeSingleMeshSphere(Sphere02, new EmissiveMaterial(Brushes.DarkGreen), 1, 20, 30); MaterialGroup combined_material = new MaterialGroup(); combined_material.Children.Add( new DiffuseMaterial(Brushes.Green)); combined_material.Children.Add( new SpecularMaterial(Brushes.Green, 50)); combined_material.Children.Add( new EmissiveMaterial(Brushes.DarkGreen)); MakeSingleMeshSphere(Sphere03, combined_material, 1, 20, 30); }

    Subroutine MakeSingleMeshSphere builds a Mesh3DGeometry object holding a sphere. It works much as you'd expect, using for loops to create the points and triangles that make up the sphere.

    MakeSingleMeshSphere's second parameter specifies the material that the sphere should use. You can see how the preceding code passes in new DiffuseMaterial, SpecularMaterial, and EmissiveMaterial objects when it builds the first three spheres.

    The final sphere uses a material that combines the three other kinds of materials. The code creates a new MaterialGroup object and adds the other kinds of materials to its Children collection. The code then passes the MaterialGroup object to MakeSingleMeshSphere.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap