3D development in Windows Phone on November 11

Source: Internet
Author: User

The ship cannot leave the sky. Although we have already built the ship and tested it successfully in the previous section, we still haven't given the ship a larger background to soar. In one scenario, only the subject is not enough, and rendering of the surrounding environment is required. If we can add the sky, clouds, and clouds to the sky, and the Green Mountains and mountains, we must be closer to reality.

In this scenario, we need a sky environment as the background without considering the terrain and landform features. Therefore, a typical practice is to implement the sky box ). The sky box is a large cube, which is attached with continuous sky and earth textures to the six sides of the cube. The ship is placed inside the cube, so that it can be viewed from the perspective of the spacecraft, the surroundings are surrounded by the sky and the earth.

Next, we will construct a sky box like this.


Define a skybox class first and inherit from drawablegamecompenent.

To use textures and light, add a member variable box for the class to define the vertices of the cube:

Vertexpositionnormaltexture [] box;

In initialize (), the origin is centered and the edge length is two coordinate units to construct the box data. Although there are 12 triangles, these triangles use 8 vertices of the cube. Therefore, you can define the coordinates of the vertex first.

            Vector3 topLeftFront=new Vector3(-1,1,1);            Vector3 topRightFront = new Vector3(1, 1, 1);            Vector3 bottomLeftFront = new Vector3(-1, -1, 1);            Vector3 bottomRightFront = new Vector3(1, -1, 1);            Vector3 topLeftBack = new Vector3(-1, 1, -1);            Vector3 topRightBack = new Vector3(1, 1, -1);            Vector3 bottomLeftBack = new Vector3(-1, -1, -1);            Vector3 bottomRightBack = new Vector3(1, -1, -1);

In this code, we can see from the variable name that these eight coordinates are the vertices of the two sides of the cube's Z axis and the Y axis's Y axis and the Y axis's negative direction. Variables suffixed with front represent the front of the positive Z axis and the back variable represents the back of the Negative Z axis. As shown in.

 

Then, the triangle of each surface of the cube is constructed using the eight coordinates, and each surface is spliced by two triangles.

            box = new VertexPositionNormalTexture[]{                new VertexPositionNormalTexture(topLeftFront,new Vector3(0,0,-1),new Vector2(0.33f,0.25f)),  //front                new VertexPositionNormalTexture(topRightFront,new Vector3(0,0,-1),new Vector2(0.66f,0.25f)),                new VertexPositionNormalTexture(bottomLeftFront,new Vector3(0,0,-1),new Vector2(0.33f,0.5f)),                new VertexPositionNormalTexture(bottomLeftFront,new Vector3(0,0,-1),new Vector2(0.33f,0.5f)),                new VertexPositionNormalTexture(topRightFront,new Vector3(0,0,-1),new Vector2(0.66f,0.25f)),                new VertexPositionNormalTexture(bottomRightFront,new Vector3(0,0,-1),new Vector2(0.66f,0.5f)),                new VertexPositionNormalTexture(bottomRightFront,new Vector3(0,0,-1),new Vector2(0.66f,0.5f)),  //bottom                new VertexPositionNormalTexture(bottomRightBack,new Vector3(0,0,-1),new Vector2(0.66f,0.75f)),                new VertexPositionNormalTexture(bottomLeftBack,new Vector3(0,0,-1),new Vector2(0.33f,0.75f)),                new VertexPositionNormalTexture(bottomLeftBack,new Vector3(0,0,-1),new Vector2(0.33f,0.75f)),                new VertexPositionNormalTexture(bottomLeftFront,new Vector3(0,0,-1),new Vector2(0.33f,0.5f)),                new VertexPositionNormalTexture(bottomRightFront,new Vector3(0,0,-1),new Vector2(0.66f,0.5f)),        //..........

In the above Code, the Paster coordinates of each vertex are determined by reference to the Paster file shown in figure (the image comes from the network, the resolution is low, and is only used for illustration.

 

If the texture is folded to form a cube, the UV coordinates of the triangles on each surface are determined based on the figure.

After the coordinates are ready, draw them in the draw () method.

            basicEffect.TextureEnabled = true;            basicEffect.Texture = texture;            basicEffect.World = worldMatrix;            basicEffect.View = viewMatrix;            basicEffect.Projection = projectionMatrix;            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)            {                pass.Apply();                Game.GraphicsDevice.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, box, 0, box.Length/3);            }            base.Draw(gameTime);

In this Code, texture is the instance variable of texture2d, and the texture file is loaded from the context in the initialize () method.

Basiceffect is an instance variable of basiceffect, used to set rendering effect options.

Worldmatrix, viewmatrix, and projectionmatrix are three transformation matrices. They are used to determine the transformation coordinates like the encapsulation of the ship class in the previous section. The definitions are as follows:

        public Matrix worldMatrix {set;get;}        public Matrix viewMatrix { set; get; }        public Matrix projectionMatrix { set; get; }

Here, the skybox class is encapsulated. We will test it in mainscene to see if it meets our expectations.

Add the skybox object to mainscene and add it to components.

            skyBox = new SkyBox(this);            Components.Add(skyBox);

Create a camera located at the origin and pointing to the negative direction of the Z axis, and set the view matrix and projection matrix of skybox as the camera parameters:

            camera = new Camera(this, new Vector3(100,100,100), new Vector3(0, 0, -1), Vector3.Up, MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 0.1f, 500);            skyBox.projectionMatrix = camera.projection;            skyBox.viewMatrix = Matrix.CreateWorld(new Vector3(0,0,0),new Vector3(0,0,-1),Vector3.Up);

Finally, in order to see the effect, add the following code to update:

        protected override void Update(GameTime gameTime)        {            // Allows the game to exit            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)                this.Exit();            TouchPanel.EnabledGestures = GestureType.Tap;            if (TouchPanel.IsGestureAvailable)            {                GestureSample gestureSample = TouchPanel.ReadGesture();                if (gestureSample.GestureType == GestureType.Tap)                {                    skyBox.worldMatrix *= Matrix.CreateRotationY(MathHelper.ToRadians(5)); ;                }            }            base.Update(gameTime);        }

Run the program and click on the screen. The effect is shown in (because the texture file comes from the network and the resolution is low, it is blurred. It is used only for illustration ). It's a vast world!


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