9 models of 3D development in Windows Phone

Source: Internet
Author: User

In the previous article, we initially learned the basic knowledge of implementing 3D scenes under the xNa framework of Windows Phone 7, and were able to transform objects in 3D spaces, texture maps and lighting are added to show the more real world. Through these operations, we have entered the 3D world. Next, we will use real 3D object models to implement complex scenarios to form a spacecraft flying in the sky. Now, let's get started.


Any complex 3D object model is composed of several triangles, either a cube or a spaceship. The triangle used previously needs to define three vertices of the triangle. If it is a cube, each side is made up of two triangles. A total of 12 triangles are required for six faces, that is, 36 vertices are required. If it is a more complex object that requires tens of thousands or more triangles to be represented, the workload for developers to define vertices must be huge.

To solve this problem, there are usually colleagues responsible for 3D modeling in team development, use professional 3D modeling tools such as 3DS MAX and Maya of Autodesk to create complex 3D object models. X or. 3D model files in fbx format are available for development.

Next, we will use a ready-made spacecraft model file to create a complex scenario.


The first and most important thing is to render the model file. To this end, you need to add a new folder in the content project and name it models, which is used to store model files and their related textures. Copy the wedge_player1.x and wedge_p1_diff_v1.tga files to this folder, and add the wedge_player1.x file to the content project. The two files are model files, and the latter is the corresponding texture of the model. Because the texture is automatically processed when the model is loaded, the texture file does not need to be added to the content project.

After adding model resources, you can write code for rendering. XNa provides the model class for processing this 3D model. Its usage is as follows:

Model = content. Load <model> (@ "models/wedge_player1 ");

In this way, the model can be loaded in the loadcontent () method.

Next, before entering the draw () method, consider a question: what is the coordinate? Imagine how to correctly map the coordinates of a model file consisting of a large number of triangles to our own coordinate system? The image of the skeleton is used here.

There is bone data in the 3D model file, just like the human skeleton, as long as the skeleton is determined, the coordinates of the associated part attached to the skeleton are determined. If the coordinate of the skeleton is transformed, the associated part is also transformed. Therefore, before creating a model, a matrix array is required to store the skeleton coordinate information of the model.

Next, add a member variable for the class to save the skeleton coordinates:

Matrix [] modeltransforms;

After the model is loaded, construct the variable:

Modeltransforms = newmatrix [model. Bones. Count];

Next, the draw () method is used to render the output.

 

        protected override void Draw(GameTime gameTime)        {            GraphicsDevice.Clear(Color.CornflowerBlue);            model.CopyAbsoluteBoneTransformsTo(modelTransforms);            foreach (ModelMesh mesh in model.Meshes)            {                foreach (BasicEffect effect in mesh.Effects)                {                    effect.EnableDefaultLighting();                    effect.World = modelTransforms[mesh.ParentBone.Index] * scaleMatrix * translateMatrix * rotateMatrix;                    effect.View = camera.view;                    effect.Projection = camera.projection;                }                mesh.Draw();            }             base.Draw(gameTime);        }

In this Code, first use model. the copyabsolutebonetransformsto () method copies the model's skeleton data to our array, and then applies each rendering effect in each mesh area of the model, specify the world coordinates, view transformations, and projection transformations for the model to render and output the model according to our camera parameters.

Here, the following loadcontent () method is used to achieve better output.

        protected override void LoadContent()        {            rotateMatrix = Matrix.Identity;            translateMatrix = Matrix.CreateTranslation(10, 0, 0);            scaleMatrix = Matrix.CreateScale(0.01f, 0.01f, 0.01f);            camera = new Camera(this, new Vector3(0, 30, 30), Vector3.Zero, Vector3.Up, MathHelper.PiOver4, (float)GraphicsDevice.Viewport.Width / (float)GraphicsDevice.Viewport.Height, 1.0f, 50.0f);            basicEffect = new BasicEffect(GraphicsDevice);            model = Content.Load<Model>(@"Models/wedge_player1");            modelTransforms=new Matrix[model.Bones.Count];        }

That is, 10 coordinate units are moved to the positive side of the X axis, and the scale is 0.01 times.

Running result.

 

The Update () function adds the code to adjust the rotatematrix when you click the screen. Therefore, when you click the screen, you will find that the model can be correctly rotated.


How about it? This is how a complex 3D object appears. Through the above code, we can find that loading and rendering a 3D model is very easy to implement under the xNa framework. We can do our best to move all the operations on the triangle in the previous article here, there are more surprises waiting for us to create.


-- 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.