After the first two sections of study, we have already met the conditions for creating 3D space. I believe many people are eager to try it. Next, let's start our 3D development journey in Windows Phone.
Open Visual Studio 2010 (what? No development environment for Windows Phone? Alas, please handle it yourself. If you have any questions, ask Baidu.) create a new project (file-> New-> project ), in the displayed dialog box, select Windows Phone game in xNa game studio 4.0 and name it "hello" in name. Click "OK" to confirm. Shows the interface.
In the Solution Explorer window, right-click the project name and execute the add-> Class Menu item, as shown in.
In the displayed dialog box, press "select", name the class camera, and click "add.
Add two attributes to the camera class:
Public matrix view {Get; protected set ;}
Public matrix projection {Get; protectedset ;}
Modify the constructor of the camera class:
Public camera (game, vector3 POs, vector3 target, vector3 up, float fieldofview, float aspectratio, floatnearplanedistance, float farplanedistance)
: Base (game)
{
View = matrix. createlookat (Pos, target, up );
Projection = matrix. createperspectivefieldofview (fieldofview, aspectratio, nearplanedistance, farplanedistance );
}
For the complete code of this class, see article "3D Development 2 camera in Windows Phone.
Open the game1 class and add the following member variables:
Camera camera;
Matrix world = matrix. identity;
Basiceffect;
Add the code in the game1 () constructor:
Graphics. isfullscreen = true;
Add the code in the loaccontent () method:
Camera = new camera (this, newvector3 (0, 0, 5), vector3.zero, vector3.up, mathhelper. piover4, graphicsdevice. viewport. aspectratio, 1.0f, 50366f );
Components. Add (CAMERA );
Basiceffect = newbasiceffect (graphicsdevice );
In this Code, mathhelper is a mathematical helper class. Its piover4 attribute is π/4, that is, the 45 degree angle. Viewport. aspectratio is the aspect ratio.
Add code to the draw () method:
Basiceffect. World = World;
Basiceffect. view = camera. view;
Basiceffect. Projection = camera. projection;
Foreach (effectpass Pass pass inbasiceffect. currenttechnique. Passes)
{
Pass. Apply ();
}
The role of the basiceffect class is to control the rendering effect, which is essential for 3D development. It is usually used in the draw () method to change the behavior of the Renderer. Later we will use it to control many special effects such as color and light. In the previous sections, the three attributes of basiceffect are used, namely, world, view, and projection in the code, representing the World matrix, camera matrix, and projection matrix respectively. Here we want to draw an object at the coordinate origin, so world sets the unit matrix, that is, mathix. Identity.
Basiceffect contains some technique, and each technique is composed of several technical tpass. We need to apply requests on each technical tpass before drawing on the technical tpass. This apply () method is called just like spritebatch. Begin () when we call 2D. It is a method that must be called before the start of painting. Therefore, a foreach loop is required when basiceffect is used.
The complete game1 class code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace Hello
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
Camera camera;
Matrix world = Matrix.Identity;
BasicEffect basicEffect;
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);
}
/// <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();
}
base.Draw(gameTime);
}
}
}
Run the program and open the simulator. The running result is shown in.
No doubt, although there is nothing on the screen, but in fact the screen is already a three-dimensional space, we have a camera at (0, 0, 5), that is, outside the screen, the camera points to the origin point and uses a 45-degree angle of view and screen aspect ratio for imaging. The near plane is 1, and the far plane is 50. In the next section, we will add an object to it.
-- Welcome to reprint, please indicate the source of http://blog.csdn.net/caowenbin --