XNA-3D-plot cubes

Source: Internet
Author: User
I. Highlights

DrawCubeThe method of drawing a body (or other 3D image) is similar to that of drawing a triangle. The outline of a 3D image consists of a series of triangles. to reduce data redundancy, use

Graphicsdevice. drawindexedprimitives () method, instead of the graphicsdevice. drawuserprimitives () method used to draw a triangle ().

II. Implementation Code

1. Add member variables for the game class:

 
1: vertexpositioncolor [] vertexlist;
 
2: vertexbuffer;
 
3:Ushort[] Indexlist;
4: indexbuffer;

2. Define the coordinates of the six vertices of the cube in the loadcontent () method.

 
1: vertexlist =NewVertexpositioncolor []
 
2 :{
 
3:NewVertexpositioncolor (NewVector3 (1f, 1f,-1f), color. Red ),
4:NewVertexpositioncolor (NewVector3 (1f,-1f,-1f), color. Red ),
 
5:NewVertexpositioncolor (NewVector3 (-1f,-1f,-1f), color. Red ),
 
6:NewVertexpositioncolor (NewVector3 (-1f, 1f,-1f), color. Red ),
7:NewVertexpositioncolor (NewVector3 (1f, 1f, 1f), color. Red ),
 
8:NewVertexpositioncolor (NewVector3 (1f,-1f, 1f), color. Red ),
 
9:NewVertexpositioncolor (NewVector3 (-1f,-1f, 1f), color. Red ),
10:NewVertexpositioncolor (NewVector3 (-1f, 1f, 1f), color. Red)
 
11 :};
 
12: vertexbuffer =NewVertexbuffer (
 
13: graphicsdevice,
14:Typeof(Vertexpositioncolor ),
 
15: vertexlist. length,
 
16: bufferusage. None );
 
17: vertexbuffer. setdata <vertexpositioncolor> (vertexlist );

3. Define the vertex index array:

1: indexlist = new ushort []
 
2 :{
 
3,
 
4: 7, 4, 5, 7, 5, 6,
 
5,
6,
 
7,
 
8: 2, 1, 5, 2, 5, 6
 
9 :};
 
10: indexbuffer = new indexbuffer (
11: graphicsdevice,
 
12: typeof (ushort ),
 
13: indexlist. length,
 
14: bufferusage. None );
 
15: indexbuffer. setdata <ushort> (indexlist );

4. Add the drawing code in the draw () method:

1: graphicsdevice. setvertexbuffer (vertexbuffer );
 
2: graphicsdevice. Indices = indexbuffer;
 
3:
 
4:IntPrimitivecount = indexlist. Length/3;
5:Foreach(VAR passInBasiceffect. currenttechnique. Passes)
 
6 :{
 
7: Pass. Apply ();
 
8: graphicsdevice. drawindexedprimitives (
9: primitivetype. trianglelist,
 
10: 0,
 
11: 0,
 
12: vertexlist. length,
 
13: 0,
14: primitivecount );
 
15 :}

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.