XNA-3D-draw triangles

Source: Internet
Author: User
1. Draw triangles in 3D space

1. Create an xNa windows game project and add member variables:

 
1: vertexpositioncolor [] vertices;
 
2: vertexbuffer;
 
3:
 
4: basiceffect;
 
5: Matrix world;
6: matrix view;
 
7: matrix projection;

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; 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.