Managed DirectX First Steps: Direct 3D Basics and DirectX vs. GDI+ - Fifth Step: Changing Diffuse Colors
(Page 12 of 14 )
You can use the same code you created for testing DirectX in windowed mode to also do your diffuse colored light test.
Although all you need to do to test the use of diffuse light is change the flexible vertex format to support a color value per vertex, and set such values for the vertices, you’ll stick to your project and create a light control window in which you can choose the RGB components for the light color on each vertex.
The light control window, shown in Figure 3-20, is composed of four tabs, and each tab has three numeric up-down controls. You name these controls starting with Red1, Green1, Blue1 for the first vertex through to Red4, Green4, Blue4 for the fourth vertex. You’ll use the values of each control directly on the color definition for the vertices.
The steps for converting the first sample to implement light control are as follows:
- Adjust the flexible vertex format structure and constant used in the vertex buffer creation to accept the color component for each vertex.
- Adjust the helper function CreateFlexVertex to accept the color parameter.
- Adjust the SquareVertices function to create the vertices using colors as defined by the numeric up-down controls.
- Adjust the click button procedure to create the control window and the test window, and initialize the values of the vertices colors.
- Create an event procedure that will update the vertex colors when any color component for any vertex changes.
NOTE The first two steps are very connected; every time you change the structure you’ll need to change the constant and your helper function (you’ll do it again in the next test, when you’ll deal with matrices).
The new code for implementing light control is shown next:
private const VertexFormats customVertexFlags =
VertexFormats.Transformed | VertexFormats.Diffuse |
VertexFormats.Texture1;
private struct CustomVertex {
public float X;
public float Y;
public float Z;
public float rhw;
public int color;
public float tu;
public float tv;
}
private CustomVertex CreateFlexVertex(float X, float Y,
float Z,
float rhw, Color color, float tu, float
tv) {
CustomVertex custVertex = new CustomVertex();
custVertex.X = X;
custVertex.Y = Y;
custVertex.Z = Z;
custVertex.rhw = rhw;
custVertex.color = color.ToArgb();
custVertex.tu = tu;
custVertex.tv = tv;
return custVertex;
}
The SquareVertices function will be the same used in the previous samples (except for the full screen one), with the solo update in passing the color parameter for the CreateFlexVertex helper function.
To define the color, you’ll use the Color.FromARGB function you used before (when choosing a blue color for clearing the device).
private void SquareVertices(CustomVertex[] vertices) {
// Create a square, composed of 2 triangles.
vertices[0] = CreateFlexVertex(60, 60, 0, 1,
Color.FromArgb((int)Red1.Value, (int)Green1.Value, (int)
Blue1.Value), 0, 0);
vertices[1] = CreateFlexVertex(240, 60, 0, 1, Color.FromArgb((int)Red2.Value, (int)Green2.Value, (int)
Blue2.Value), 1, 0);
vertices[2] = CreateFlexVertex(60, 240, 0, 1, Color.FromArgb((int)Red3.Value, (int)Green3.Value, (int)
Blue3.Value), 0, 1);
vertices[3] = CreateFlexVertex(240, 240, 0, 1, Color.FromArgb((int)Red4.Value, (int)Green4.Value, (int)
Blue4.Value), 1, 1);
}
The test start procedure, defined in the Click button on the main form, will be very similar to the ones you saw before: It follows the same structure, but creates both test and control windows, and takes special care in initializing the values of all the numeric up-down controls to 255 to fill the vertices with white light, so the walking man image starts with no color distortion (the default value is zero, which would prevent you from seeing anything).
LightControl winLightControl = new LightControl();
using (LightTest lightTest = new LightTest()) {
winLightControl.Show();
lightTest.Show();
// Initialize Direct3D and the Device object.
if(!winLightControl.InitD3D(lightTest.Handle)) {
MessageBox.Show("Could not initialize Direct3D.");
winLightControl.Dispose();
}
else {
// Load the textures and create the vertices.
if(!winLightControl.CreateTextures()) {
MessageBox.Show("Could not initialize the textures
and vertices.");
winLightControl.DisposeD3D();
winLightControl.Dispose();
}
}
// Start with full white light in all vertices.
winLightControl.Red1.Value = 255;
winLightControl.Green1.Value = 255;
winLightControl.Blue1.Value = 255;
winLightControl.Red2.Value = 255;
winLightControl.Green2.Value = 255;
winLightControl.Blue2.Value = 255;
winLightControl.Red3.Value = 255;
winLightControl.Green3.Value = 255;
winLightControl.Blue3.Value = 255;
winLightControl.Red4.Value = 255;
winLightControl.Green4.Value = 255;
winLightControl.Blue4.Value = 255;
// Ends the test if ESC is pressed in any of the 2
windows.
while(!winLightControl.EndTest && !lightTest.EndTest)
{
winLightControl.Render();
// Frame rate calculation.
lightTest.Text = "Light Test. Frame Rate: " +
DirectXLists.CalcFrameRate().ToString();
Application.DoEvents();
}
}
The last step to make your code fully operational is including a call to update the vertex colors every time one vertex color has changed. Because the values of the controls are being read directly in the CreateVertices procedure, you can simply call this procedure on an event that handles changing in all numeric up-down controls:
private void Color_TextChanged(object sender,
System.EventArgs e) {
CreateVertices();
}
Just run your program now, and play a little with the vertex light colors. Figure 3-29 shows a sample color distorted window.

Figure 3-29. Your old friend walking man in a disco
This chapter is from Beginning .NET Game Programming in C# by David Weller et. al.(Apress, 2004, ISBN: 1590593197). Check it out at your favorite bookstore today. Buy this book now.
|
Next: Sixth Step: Testing Matrix Transformations >>
More ASP.NET Articles
More By Apress Publishing