Developing Programming Assignments on the XBOX 360 Console

Section I: Introduction and Overview
c. Drawing with XNA

back to workshop main page


References:

Goals:

Notice:


Compile and Run: Download the source code, compile and run and you will see this output:
Notice the triangle is located at the center of application window. In this tutorial, we will examine the changes that allow the drawing of this triangle.


Summary of changes to support drawing: We have added code to the Game1.cs file of the simple default project at 3 separate places:
  1. Instance variables: for graphics drawing support: EffectPool and BasicEffect.
  2. Initialize():  allocate and initialize the instance variables.
  3. Draw():  use the drawing support to draw a single triangle.

Here are the details, each of the following changes are enclosed by regions with label: __CODE_ADDED__:

  1. Declare new instance variables (in the beginning of the file) for drawing support:
    #region __CODE_ADDED__
        EffectPool effectPool;     // for transformation and shading
        BasicEffect basicEffect;   // traditional pipeline and fixed 3-stage transform
    #endregion

  2. Initialize graphics and application state (in Initialize() function):
    #region __CODE_ADDED__ // Initialize the graphics rendering state
        graphics.GraphicsDevice.VertexDeclaration = new VertexDeclaratione(...);
            
        // Create default shading procedure

        effectPool = new EffectPool();
        basicEffect = new BasicEffect(graphics.GraphicsDevice, effectPool);
        basicEffect.VertexColorEnabled = true;
    #endregion

  3. Draw a triangle (in Draw() function):
    #region __CODE_ADDED__
        // Begin drawing ...
        ...
        // Triangle vertex position based on center at (0,0)
        Vector3 p = new Vector3(0, 0, 0);
        VertexPositionColor[] vertices = new VertexPositionColor[3];
        for (int i=0; i<3; i++)
                vertices[i].Position = p;   // Center of triangle
        ...    // color of the triangle vertices
        vertices[0].Position.X -= 0.1f;     // left vertex of triangle 
        vertices[1].Position.X += 0.1f;     // right vertex of triangle
        vertices[2].Position.Y += 0.2f;     // top vertex of triangle
        // draw the triangle
        graphics...DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 1);
        ...
    #endregion
    Notice that:

This document and the related materials are developed with support from Microsoft Research Computer Gaming Initiative under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871.


Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
ksung@u.washington.edu