As before, we will first create a new xNa project. For simplicity, the game framework is not used here, so you do not need to add references to it or change the inheritance structure of the game class; it is still derived from the Microsoft. xNa. Framework. Game class.
We need to add some class-level variables to the game class to manage the scenario to be rendered.
Private basiceffect _ effect;
Private vertexpositioncolor [] _ vertices = new vertexpositioncolor [4];
Next, you need to set these variables for xNa. RequiredCodeWill be added to the initialize function.
The first thing to do here is to set the projection matrix. We will discuss this in detail in the next chapter. Currently, we only focus on its main tasks and establish an abstract coordinate system. As you can see in the following code, the aspect ratio of the screen is determined by the width of the view area divided by its height, which is used to initialize the matrix. It ensures that the objects drawn on the screen are always square.
Protected override void initialize ()
{
// Calculate the screen aspect ratio
Float aspectratio = (float) graphicsdevice. viewport. width/graphicsdevice. viewport. height;
// Create a projection matrix
Matrix projection = matrix. createperspectivefieldofview (mathhelper. toradians (45), aspectratio, 0.1f, 1000.0f );
}
Note that the matrix is initialized by calling the static method of the matrix structure. As we will see later, we have multiple methods to create all types of matrices.
Next, create a view matrix. It can be compared to a scene camera, used to control the visibility of the rendered object and the scope of appearance in the screen. The following code shows how to create a view matrix in an example project.
// Calculate a view matrix (where we are looking from and)
Matrix view = matrix. createlookat (New vector3 (0, 0, 10), vector3.zero, vector3.up );
Now, we need to create an effect object to tell xNa how to render the image to the screen. All rendering requires some effect, and several effects are also provided in xNa's Windows Phone 7 implementation.
When an effect object is instantiated, a reference of a graphic device is passed in, and a series of properties are set to control its behavior. These are the status values discussed at the beginning of this chapter. The following code creates and initializes an effect object. Note that the values passed in are the projection and view matrix just created.
_ Effect = new basiceffect (graphicsdevice );
_ Effect. lightingenabled = false;
_ Effect. textureenabled = false;
_ Effect. vertexcolorenabled = true;
_ Effect. Projection = projection;
_ Effect. view = view;
_ Effect. World = matrix. identity;
Environment initialization has been completed. However, there is no painting yet. You need to pay attention to this content before painting.
As described above, xNa tends to use vertices to define objects. They are used to construct solid triangles to form the images seen on the screen.
Vertices can contain a lot of information. They always contain location information, in addition to color information, texture information, and other data that affects the drawing method. XNa provides a lot of built-in configuration information for common vertex structures. We will use a structure called vertexpositioncolor. As the name suggests, it only stores location and color information.
In our example, a simple square is drawn on the screen. To describe the square to xNa, you must create an array of vertex objects to describe the position and color of each vertex. The figure shows the vertex used to form the square. Coordinates (, 0) are in the middle of a square. A square spans two units (from-1 to 1 on each axis) on the X and Y axes ). Note that the zcoordinates of all vertices are 0, so the square is flat.
The final part of the initialization process is to provide color for each vertex. This will produce attractive effects during rendering-it is difficult to achieve this effect easily without generating textures rich in various colors when using genie. The remaining vertex initialization process and the final initialize function.
_ Vertices [0]. Color = color. Red;
_ Vertices [1]. Color = color. White;
_ Vertices [2]. Color = color. blue;
_ Vertices [3]. Color = color. Green;
Base. initialize ();