ASP.NET
  Home arrow ASP.NET arrow Page 10 - 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+ - Code the Render Procedure


    (Page 10 of 14 )

    With all the textures and vertices loaded, all you need now is to code the Render procedure to load one texture at a time and a finalization routine to dispose the used objects. The Render routine follows the structure of the scene starting, ending, and being presented, as shown earlier.

    private static int x = 0;
    public void Render() {
       if(device == null) {
          return;
       }
       // Clears the device with blue color.
       device.Clear(ClearFlags.Target, Color.Blue, 1.0F, 0); 
       device.BeginScene();
      
    // Show one texture a time, in order to create the
           illusion of a walking guy.
       device.SetTexture(0, textures[x]);
       x = (x == 9) ? 0 : x+1; //If x is 9, set to 0,
           otherwise increment x
       // Define which vertex buffer should be used.  
       device.SetStreamSource(0, vertBuffer, 0); 
       device.VertexFormat = customVertexFlags;
       // Draw the vertices of the vertex buffer, rendering
           them as a
       // triangle strip, using the given texture.  
       device.DrawPrimitives(PrimitiveType.TriangleStrip, 0,
           numVerts - 2);
       device.EndScene();
      
    // Using an extra try-catch will prevent any errors if
           the device was disposed.
       try {
           // Present the rendered scene.
           device.Present();
      
    }
      
    catch {
          // Normally, you would put special exception
              handling in here.
       }
    }

    Note that we don’t include any mention of back buffers or screen swapping (flipping) operations here, so why do you care about these in the Device object creation? In fact, everything is done here, but is performed in the background by the device: The back buffer is cleared using the Clear command, it’s locked for drawing using the BeginScene method, it’s unlocked after you render the scene with the EndScene function, and it’s finally flipped to the screen, and maybe discarded, using the Present method.

    The final routine just disposes of all objects created in the previous functions, and it’s called by the main program automatically whenever the WindowTest form exits or is closed.

    public void DisposeD3D() {
       for(int i = 0; i < 10; i++) {
         
    if(textures[i] != null) {
             textures[i].Dispose();
             textures[i] = null;
          }
       }
       if(vertBuffer != null) {
          vertBuffer.Dispose();
         
    vertBuffer = null;
       }
       if(device != null) {
         
    device.Dispose();
          device = null;
       }
    }

    This last function ends the sample. After coding a simple escape routine, which will end the form when the Esc key is pressed, you can run your sample and see the results, as presented in Figure 3-25.

    private void OnKeyDown(object sender, KeyEventArgs e) {
       if(e.KeyCode == Keys.Escape)
          endTest = true;
    }

     
    Figure 3-25.  Running your first DirectX program

    Third Step: Creating a Full-Screen Sample

    To make your sample run in full-screen mode, all you need to do is change the presentation parameters in the InitD3D routine. In order to have all sample code sections separated from each other, you’ll create a new button in the main window to fire the full-screen mode. Because most of the code will be the same, you can copy all the code from the windowed mode and simply apply the following updates.

    Let’s analyze the code for setting the presentation parameters, line by line.

    The initial lines are the same from the windowed mode; just gather information about the current display mode and create the presentation parameters object.

    DisplayMode DispMode =
       Manager.Adapters[Manager.Adapters.Default. Adapter]. 
          CurrentDisplayMode;
    PresentParameters presentParams = new PresentParameters();


    Following the definition, you set the parameters for creating the back buffer. In this example, you’ll be using the current format, width, and height (you must specify these three parameters); but you could be using any of the formats or resolutions shown in your Display Modes list on the main screen.

    presentParams.BackBufferFormat = DispMode.Format; presentParams.BackBufferWidth = DispMode.Width; presentParams.BackBufferHeight = DispMode.Height;

    The last line is the same as the one in the windowed mode: It sets the flipping operation to the one that has the best performance, instructing the device not to care about preserving the back buffer.

    presentParams.SwapEffect = SwapEffect.Discard;


    NOTE  Using the Discard swap effect forces the use of only one back buffer, so you don’t need to set the BackBufferCount property to 1. Another important point is that you don’t worry about setting the Windowed property to false, because running full screen is the default.

    It’s enough to make your code run in full-screen mode, but you can make a simple improvement in your SquareVertices function to create a square that covers the entire screen, stretching the walking man textures to generate a nicer effect. You can gather the screen resolution, using the same method you saw before, with a display mode object. Your final function will be as follows:

    private void SquareVertices(CustomVertex[] vertices) {
       DisplayMode mode =
         Manager.Adapters[Manager.Adapters.Default.Adapter].
               CurrentDisplayMode;
       // Create a square, composed of 2 triangles, taking all
               the screen.
       vertices[0] = CreateFlexVertex(0, 0, 0, 1, 0, 0);
       vertices[1] = CreateFlexVertex(mode.Width, 0, 0, 1, 1, 
                   0);
      
    vertices[2] = CreateFlexVertex(0, mode.Height, 0, 1, 0,
                   1);
      
    vertices[3] = CreateFlexVertex(mode.Width, mode.Height,
                   0, 1, 1, 1);
    }

    Just run the program now and press the Full Screen button in the main window to see the textures applied to the entire screen, with no visible loss in the frame rate, as presented in Figure 3-26.


    Figure 3-26.  Running your DirectX program in full-screen mode

    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

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek