ASP.NET
  Home arrow ASP.NET arrow Page 12 - Managed DirectX First Steps: Direct 3D Bas...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Managed DirectX First Steps: Direct 3D Basics and DirectX vs. GDI+
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 65
    2005-01-26

    Table of Contents:
  • Managed DirectX First Steps: Direct 3D Basics and DirectX vs. GDI+
  • DirectX Overview
  • Referencing DirectX Libraries
  • Put That in Your Pipeline and Shade It
  • 3-D Coordinate Systems and Projections
  • Drawing Primitives and Texture
  • The Application Proposal
  • The Coding Phase
  • Second Step: Coding Your First Windowed Test
  • Code the Render Procedure
  • Fourth Step: Using Transparent Textures
  • Fifth Step: Changing Diffuse Colors
  • Sixth Step: Testing Matrix Transformations
  • Adding the Final Touches

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    1. Adjust the flexible vertex format structure and constant used in the vertex buffer creation to accept the color component for each vertex.

    2. Adjust the helper function CreateFlexVertex to accept the color parameter.

    3. Adjust the SquareVertices function to create the vertices using colors as defined by the numeric up-down controls.

    4. Adjust the click button procedure to create the control window and the test window, and initialize the values of the vertices colors.

    5. 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.

    More ASP.NET Articles
    More By Apress Publishing


     

    ASP.NET ARTICLES

    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...
    - Building an In-Text Advertising System Under...
    - Developing a Mini ASP.NET AJAX Server Centri...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT