2. initialize the triangle vertex coordinates and the world, view, and projection matrices in the initialize () method:
1: vertices =NewVertexpositioncolor []
2 :{
3:NewVertexpositioncolor (NewVector3 (0, 0), color. Red ),
4:NewVertexpositioncolor (NewVector3 (1,-1, 0), color. Green ),
5:NewVertexpositioncolor (NewVector3 (-1,-1, 0), color. Blue)
6 :};
7: vertexbuffer =NewVertexbuffer (graphicsdevice,
8:Typeof(Vertexpositioncolor ),
9: vertices. length,
10: bufferusage. None );
11: vertexbuffer. setdata <vertexpositioncolor> (vertices );
12: graphicsdevice. setvertexbuffer (vertexbuffer );
13:
14: World = matrix. identity;
15: view = matrix. createlookat (NewVector3 (0, 0, 5), vector3.zero, vector3.up );
16: Projection = matrix. createperspectivefieldofview (mathhelper. piover4,
17: graphicsdevice. viewport. aspectratio,
18: 1f,
19: 100f );
20:
21: basiceffect =NewBasiceffect (graphicsdevice );
22: basiceffect. vertexcolorenabled =True;
3. Draw a triangle in the draw () method:
1: basiceffect. World = World;
2: basiceffect. view = view;
3: basiceffect. Projection = projection;
4:
5: foreach (VAR passInBasiceffect. currenttechnique. Passes)
6 :{
7: Pass. Apply ();
8: graphicsdevice. drawuserprimitives <vertexpositioncolor> (
9: primitivetype. trianglelist,
10: vertices,
11: 0,
12: 1 );
13 :}
4. Running result:
Ii. Coordinate Transformation
In the update () method, respond to the keyboard button event and perform Coordinate Transformation:
1: keyboardstate = keyboard. getstate ();
2:If(Keyboardstate. iskeydown (Keys. R ))
3 :{
4: matrix rototez = matrix. createrotationz (mathhelper. piover4 );
5: World * = rototez;
6 :}
7:Else If(Keyboardstate. iskeydown (Keys. Left ))
8 :{
9: matrix translateleft = matrix. createtranslation (NewVector3 (-0.25f, 0, 0 ));
10: World * = translateleft;
11 :}
12:Else If(Keyboardstate. iskeydown (Keys. Right ))
13 :{
14: matrix translateright = matrix. createtranslation (NewVector3 (0.25f, 0, 0 ));
15: World * = translateright;
16 :}
3. Tips
To set the initial window size, you can modify the properties in the game constructor:
1: graphics. preferredbackbufferwidth = 400;
2: graphics. preferredbackbufferheight = 300;