Four triangles for 3D development in Windows Phone

Source: Internet
Author: User

In the previous section, we have a three-dimensional space. In this section, we will get to know the most basic 3D elements-triangles.

 

Triangles play an important role in 3D development. It is the smallest element of a 3D model. No matter how complicated a 3D model is, it can be expressed as a combination of several triangles. The graphics processing chip also provides hardware support for triangle rendering. Although the visible triangle is simple, it is important in 3D development. Let's start with the simplest triangle.

 

Follow the xNa project we created in the previous section and open it in vs2010. Open the game1.cs file and modify the game1 class.

 

To build a triangle, we first need the coordinates of three vertices. The vertexpositioncolor type is provided in xNa to indicate the positions and colors of vertices in a space. Here we only use the location information, and the color will be used later.

 

Add the member variable vertexpositioncolor [] triangle for the game1 class to save the triangle vertex information;

 

Then define three vertices in the loadcontent () method:

Triangle = newvertexpositioncolor [] {

New vertexpositioncolor (newvector3 (0, 1, 0), color. Red ),

New vertexpositioncolor (newvector3 (1,-1, 0), color. Green ),

New vertexpositioncolor (newvector3 (-1,-1, 0), color. Blue)

};

 

The coordinates of these three vertices are exactly a triangle. Then draw and output it in the draw () method:

Graphicsdevice. drawuserprimitives <vertexpositioncolor> (primitivetype. trianglestrip, triangle, 0, 1 );

 

The drawuserprimitives () method is used to draw elements. The prototype of the method is as follows:

Public voiddrawuserprimitives <t> (

Primitivetypeprimitivetype, // primitive type

T [] vertexdata, // vertex data

Intvertexoffset, // The starting position for reading vertex data

Intprimitivecount // number of elements

)

Here, primitivetype is used to describe the primitive type. There are four values in Windows Phone, meaning:

  • Trianglelist: List of triangles
  • Trianglestrip: triangle band
  • Linelist: Line Segment list
  • Linestrip: line string

 

For example, if the coordinates of the six vertices are the same, the image obtained when trianglelist is used is as follows (Note: The image comes from the network ):

 

 

When trianglestrip is used, the following figure is obtained (Note: The image is from the network ):

 

 

Run the program. The following result is displayed in the simulator:

 

 

Although it is very simple, it is indeed drawn in 3D space. If you are interested, you may wish to modify the camera position, triangle coordinates, and other parameters, or draw other elements, understand the changes in the running results. In the next section, we will perform 3D object motion processing.

 

Complete source code for the game1 class is provided in this section:

 

    public class Game1 : Microsoft.Xna.Framework.Game    {        GraphicsDeviceManager graphics;        Camera camera;        Matrix world = Matrix.Identity;        BasicEffect basicEffect;        VertexPositionColor[] triangle;        public Game1()        {            graphics = new GraphicsDeviceManager(this);            Content.RootDirectory = "Content";            // Frame rate is 30 fps by default for Windows Phone.            TargetElapsedTime = TimeSpan.FromTicks(333333);            // Extend battery life under lock.            InactiveSleepTime = TimeSpan.FromSeconds(1);            graphics.IsFullScreen = true;        }        /// <summary>        /// Allows the game to perform any initialization it needs to before starting to run.        /// This is where it can query for any required services and load any non-graphic        /// related content.  Calling base.Initialize will enumerate through any components        /// and initialize them as well.        /// </summary>        protected override void Initialize()        {            // TODO: Add your initialization logic here            base.Initialize();        }        /// <summary>        /// LoadContent will be called once per game and is the place to load        /// all of your content.        /// </summary>        protected override void LoadContent()        {            camera = new Camera(this, new Vector3(0, 0, 5), Vector3.Zero, Vector3.Up, MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 1.0f, 50.0f);            Components.Add(camera);            basicEffect = new BasicEffect(GraphicsDevice);            triangle = new VertexPositionColor[]{                new VertexPositionColor(new Vector3(0, 1, 0), Color.Red),                new VertexPositionColor(new Vector3(1, -1, 0), Color.Green),                new VertexPositionColor(new Vector3(-1,-1, 0), Color.Blue)            };        }        /// <summary>        /// UnloadContent will be called once per game and is the place to unload        /// all content.        /// </summary>        protected override void UnloadContent()        {            // TODO: Unload any non ContentManager content here        }        /// <summary>        /// Allows the game to run logic such as updating the world,        /// checking for collisions, gathering input, and playing audio.        /// </summary>        /// <param name="gameTime">Provides a snapshot of timing values.</param>        protected override void Update(GameTime gameTime)        {            // Allows the game to exit            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)                this.Exit();            // TODO: Add your update logic here            base.Update(gameTime);        }        /// <summary>        /// This is called when the game should draw itself.        /// </summary>        /// <param name="gameTime">Provides a snapshot of timing values.</param>        protected override void Draw(GameTime gameTime)        {            GraphicsDevice.Clear(Color.CornflowerBlue);            basicEffect.World = world;            basicEffect.View = camera.view;            basicEffect.Projection = camera.projection;            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)            {                pass.Apply();                GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, triangle, 0, 1);            }            base.Draw(gameTime);        }    }

-- Welcome to reprint, please indicate the source of http://blog.csdn.net/caowenbin --

Related Article

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.