ASP.NET
  Home arrow ASP.NET arrow Page 5 - 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 / 64
    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+ - 3-D Coordinate Systems and Projections


    (Page 5 of 14 )

    Even if you have no interest in creating 3-D games, you must understand the basic concepts of a 3-D coordinate system, because everything you do in Direct3D is defined by points and images in a 3-D world. Of course, you can ignore the z axis and pretend that you’re in a 2-D world—and you’ll see how to do this—but the z value will still be there (it will just always be a value of zero).

    When you’re dealing with three Cartesian dimensions, there are two types of coordinate systems: left-handed and right-handed. These names refer to the z-axis position relative to the x and y axis. To determine this position, point the fingers of one hand to the x-axis positive direction and move them in the counterclockwise direction to the y-axis positive position; the positive z-axis direction will be the direction to which your thumb points. Figure 3-4 illustrates this concept.


    Figure 3-4.  The Cartesian 3-D coordinate systems

    To put it a different way, imagine the origin of your coordinate system starting in the lower left at (0.0), with the y axis going up and the x axis going to the right. In a left-handed coordinate system, the z value gets bigger (the positive direction) when you go from the screen to a point away from you, the right-handed 3-D system is the opposite: The z values increase toward you from the screen.

    Direct3D uses the left-hand coordinate system, which means that positive values for z are visible, and the greater they are for a given object, the farther the object is (and, depending on the projection chosen, the smaller it appears on the screen); and negative values aren’t shown (unless you change your “camera position,” which is also possible in Direct3D).


    Who’s Left? Who’s Right?

    Although DirectX uses a left-handed coordinate system, many math books use right-handed coordinate systems that reverse the x and z axes. These differences won’t affect you as you learn DirectX, but you’ll eventually need to understand how to do mathematical calculations that will transform between different coordinate systems (in fact, a huge amount of work done in a graphics processor relates to coordinate conversions between different coordinate systems). Some modern books use geometric algebra, which discusses the mathematics in a coordinate-free context. Those authors argue that such approaches are an ideal way to learn an otherwise complex subject.


    Now that you have an understanding of 3-D coordinate systems, the next step to explore is how they present 3-D objects to your 2-D screen.

    Fortunately, all the hard mathematical work is done by DirectX, but you have to know the concept of projections and how they apply to DirectX in order to give the basic instructions about how to present the objects on screen. In a nutshell, a projection is a volume of space that represents an area that can be viewed on a screen.

    Direct3D supports two different types of projections:

    • Perspective projection: The most common type of projection, it takes into account the z distance and adjusts the objects accordingly. This projection makes objects appear smaller when far from the screen—the objects get deformed, like in the real world. For example, the borders of a straight road appear to come together in the horizon. Figure 3-5 shows a graphical representation of the perspective projection.


                Figure 3-5.  Perspective projection

    • Orthogonal projection: In this type of projection, the z component is just ignored, and the objects don’t get bigger when closer to the screen or smaller when they are farther away. This projection is mostly used for 2-D games or simpler 3-D games. Figure 3-6 presents an orthogonal projection.


    Figure 3-6.  Orthogonal projection

    When defining the projection type, you must choose the type of coordinating system and pass the parameters for the projection, according to its type. Direct3D offers six main functions (besides four others for creating custom coordinates systems) that allow you to specify the projection for your game. These functions return matrices that will be used by Direct3D to calculate the conversion from 3-D coordinates to screen coordinates.

    • Matrix.OrthoRH, Matrix.OrthoLH: Returns the matrix with the transformations that need to be applied to the object’s coordinates to define an orthogonal projection (RH stands for right-handed, LH for left-handed). Each function receives the width and the height of the viewport (usually, the screen or window size) and the range of z values that will be viewed (points before the first z value and after the last one won’t be viewed).

    • Matrix.PerspectiveRH, Matrix.PerspectiveLH: Returns the transformation matrix for perspective projection, passing the width and height of the viewport and the z distance viewed (first and last points) for right-handed and left-handed coordinate systems.

    • Matrix.PerspectiveFovRH, Matrix.PerspectiveFovLH: Returns the transformation matrix for perspective projection, passing the angle in radians of your field of view (FOV) and the z distances; for right-handed and left-handed coordinate systems.

    Figure 3-7 shows graphically the FOV angle and the z distance viewed
    (defined by view planes).


    Figure 3-7.  The field of view angle and view planes for perspective projection

    In the next section, we’ll explain the matrix concept and learn how it helps you to convert coordinates of a 3-D world to screen coordinates, allowing you to easily perform complex operations on your game objects.

    Understanding Matrices and 3-D Transformations

    Knowing how to work with transformation matrices is possibly the most important concept when dealing with Direct3D. Using matrices, you can perform rotation, scaling, or translation of any object on the 3-D world (or in the 2-D world, if you choose to ignore the z component), and these operations, correctly applied, will help you to define your projection type (as shown in the previous section) or even move the camera to see the same scene from different points.

    Let’s discuss the use of transformation matrices to do a simple translation, and then extrapolate the idea for more complex operations. Suppose that you want to move a triangle up the y axis, as shown in Figure 3-8.


    Figure 3-8.  Mounting a triangle on the y axis

    Let’s assume the triangle vertices are defined by the points shown here.

    VERTEX X Y Z
    1 50 10 0
    2 60 10 0
    3 53 25 0

    To translate 40 units over the y-axis positive direction, all you need is to sum 40 to each y position, and you have the new coordinates for the vertices, shown here:

    VERTEX X Y Z
    1 50 50 0
    2 60 50 0
    3 53 65 0

    The same results can be achieved by representing each vertex as a matrix with one row and four columns, with the vertex coordinates as the first three columns and 1 as the value in the last one, and multiplying this matrix by a special matrix constructed to produce the translation transformation to the vertex matrix.

    Figure 3-9 presents the same operation applied to the first vertex.


    Figure 3-9.  Applying a matrix multiplication to a 3-D vertex 

    To calculate the resulting matrix, you must take each value in the row of the first matrix and multiply them by each of the values in the corresponding column in the second matrix, and then perform the sum of all results. So, in the previous sample, the calculations are as follows:

      x' = (50 × 1) + (10 × 0) + (0 × 0) + (1 × 0) = 50
      y' = (50 × 0) + (10 × 1) + (0 × 0) + (1 × 40) = 50
      z' = (50 × 0) + (10 × 0) + (0 × 1) + (1 × 0) = 0

    We don’t want to get into much deeper detail here, but suffice it to say that you can perform translations by putting the desired values for translation over the x, y, and z in the last row of the transformation matrix; perform scaling by replacing the 1s on the diagonal to fractional values (to shrink) or greater values (to expand); and perform rotation around any axis using a combination of sine and cosine values in specific positions in the matrix.


    TIP  For those who want to know more about the transformation matrices, DirectX SDK help has full coverage of this topic, showing each of the matrices and explaining how to use them. You can also look in Appendix A to find more books on the mathematics of matrix transformations. We’ll cover matrix transformation in a little more depth in later chapters.

    Luckily enough, you don’t need to understand all these details to use the transformations in your program. All you need to know is the following:

    • Transformation matrices can be multiplied by each other without losing information. If you want to translate and rotate an object at the same time, you can simply multiply the translation matrix to the rotation matrix and multiply the result for your vertices, acquiring the desired result.

    • The Device object has three special properties: one is used to receive the projection matrix (which was explained in the previous section), <Device>.Transform.Projection; another to indicate the transformations desired in your 3-D world (explained here), <Device>.Transform.World; and the third to specify the camera position (explained in the next section), <Device>.Transform.View.

    • The D3DX utility library has functions to create all the transformation matrices for you, functions for matrices multiplication, and a function that returns an identity matrix (a special matrix that returns the vertices without transformations, which is used to clean the old world matrix before updating it). You’ll see these functions in the section “The Code Phase.”

    Positioning the Camera

    As an extra feature when dealing with 3-D coordinate systems, DirectX allows you to position the camera to see the same scene from different points. The camera in DirectX is referred to as the view matrix.

    You can calculate the view matrix and set it to the <Device>. Transform. View property, or you can use the helper functions Matrix.LookAtLH and Matrix.LookAtRH. These helper functions define the camera position and the direction it’s looking at by three points: the 3-D position of the camera, the 3-D position the camera is looking at, and the current “up” direction, usually the y axis.

    If you don’t define a view (camera) matrix, DirectX will provide a default one for you, but it’s an important concept to have in mind. Do you remember the first Prince of Persia game in which, at a given level, the prince drank a special potion and the screen turns upside down? Imagine creating this feature with a single line of code, rotating the view matrix by 180 degrees (multiplying it by a rotation matrix). This scenario shows the benefit of using Direct3D even for 2-D games.

    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

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT