So what's different now than when we were first using lights? The only major difference (other than the fact that we are using a mesh) is the lack of color in our vertex data. This is the cause of the light "failing" now. In order for Direct3D to correctly calculate the color of a particular point on a 3D object, it must not only know the color of the light, but how that object will reflect the color of that light. In the real world if you shine a red light on a light blue surface, that surface will appear a soft purple color. You need to describe how our "surface" (our cube) reflects light. In Direct3D, materials describe this property. You can specify how the object will reflect ambient and diffuse lighting, what the specular highlights (discussed later) will look like, and whether the object appears to emit light at all. Add the following code to your DrawBox call (before the DrawSubset call). Material boxMaterial = new Material(); boxMaterial.Ambient = Color.White; boxMaterial.Diffuse = Color.White; device.Material = boxMaterial; Here we create a new material, and set its ambient and diffuse color values to white. Using white means we will reflect all ambient and diffuse lighting that hits these objects. We then use the Material property on the device so that Direct3D knows which material to use while rendering its data. Running the application now shows us the red cubes spinning around that we expected to see before. Modifying the ambient light color will change the color of every cube in the scene. Modifying the ambient color component of the material will change how the light is reflected off of the object. Changing the material to a color with absolutely no red in it (such as green) will cause the object to once again render black. Changing the material to a color with some red (for example, gray) will cause the object to appear a darker gray. There are actually quite a few stock objects you can use when using mesh files. Use any of the following methods for creating these meshes (each of the stock mesh functions requires the device as the first parameter): mesh = Mesh.Box(device, 2.0f, 2.0f, 2.0f);
Uses a left-handed coordinate system to create a box.
mesh = Mesh.Cylinder(device, 2.0f, 2.0f, 2.0f, 36, 36); Uses a left-handed coordinate system to create a cylinder.
mesh = Mesh.Polygon(device, 2.0f, 8); Uses a left-handed coordinate system to create a polygon.
Uses a left-handed coordinate system to create a sphere.
mesh = Mesh.Torus(device, 0.5f, 2.0f, 36, 18); Uses a left-handed coordinate system to create a torus.
mesh = Mesh.Teapot(device); Uses a left-handed coordinate system to create a teapot (yes a teapot). See Figure 5.3. Figure 5.3. The intrinsic teapot mesh.Each of these methods also has a second overload that can return adjacency information. Adjacency information is returned as three integers per face that specify the three neighbors of each face in the mesh. |