xna4.0 3D review

来源:互联网 发布:2016二级c语言考试真题 编辑:程序博客网 时间:2024/06/08 11:39
Coordinate&Matrix
Camera
How to draw things in 3D Space


CameraModel
View&ProjectiveMatrix
DrawPrimitives
VertexPositionColor (position and color)


VertexPositionColor[] verts;
VertexBuffer vertexBuffer;
BasicEffect effect;


//initialize
verts=new VertexPositionColor[3];
verts[0] = new VertexPositionColor(new Vector3(0,1,0),Color.Blue);
……
vertexBuffer=new VertexBuffer(…);
vertexBuffer.SetData(verts);
effect=new BasicEffect(GraphicsDevice);
GraphicDevice.SetVertexBuffer(vertexBuffer);


HLSL-effect
effect.World = Matrix.Identity;
effect.View = camera.view;
effect.Projection = camera.projection;
effect.VertexColorEnabled = true;


//draw for each passes
foreach(EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, verts, 0, 1);
}


VertexPositionColor
VertexPositionTexture


Matrix Multiplication
worldTranslation = Matrix.CreateTranslation(-1,0,0);
worldRotation = Matrix.CreateFromYawPitchRoll(MathHelper.PiOver4, 0, 0); //worldRotation = Matrix.CreateRotationX


(MathHelper.PiOver4);
effect.World = worldTranslation * worldRotation * Matrix.CreateScale(0.5f);


effect.Texture = texture;
effect.TextureEnabled = true;


//backfaceCulling
RasterizerState rs = new RasterState();
rs.CullMode = CullMode.None;
GraphicsDevice.RasterizerState = rs;


Texture Coordinate(u,v)
verts = new VertexPositionTexture[4];
verts[0] = new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0));


Public Model model; //XNA can load .x & .fbx
//".X&.FBX" files can each contain more than one object. These objects, called meshes, are stored within the model.


Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoulteBoneTransformsTo(transforms);
foreach(ModelMesh mesh in model.Meshes)
{
foreach(BasicEffect be in mesh.Effects)
{
be.EnableDefaultLighting();
be.View = camera.view;
be.Projection = camera.Projection;
be.World = world*mesh.ParentBone.Transform;
}
mesh.Draw();
}


//The most important thing to remember about the model-drawing code is that you have to set your BasicEffect’s Projection, View, and World properties. Once you do that, the rest of the code will handle drawing the model for you.

Since you have your GameComponent's Update method synced with your game loop, you also need to change GameComponent  to DrawableGameComponect to have the Draw method synced with your game's Draw method.


List<BasicModel> models = new List<BasicModel>();
models.Add(new BasicModel(Game.Content.Load<Model>("Models/spaceship")));


//By default, in XNAmodels contain BasicEffects. You can draw a model by looping through the meshes of the model and setting the properties of the BasicEffect class before drawing the model itself.


//Create a First-Person Camera
public Vector3 cameraPosition{get; protected set;}
Vector3 cameraDirection;
Vector3 cameraUp;


private void CreateLookAt()
{
view = Matrix.CreateLookAt(cameraPosition, cameraDirection+cameraPosition,cameraUp);
}


// Build camera view matrix
cameraPosition = pos;
cameraDirection = target - pos;
cameraDirection.Normalize();
cameraUp = up;
CreateLookAt();


if(Keyboard.GetState().IsKeyDown(Keys.D))
{
cameraPosition += cameraDirection*speed;
}

// Set mouse position and do initial get state
Mouse.SetPosition(Game.Window.ClientBounds.Width / 2,
Game.Window.ClientBounds.Height / 2);
prevMouseState = Mouse.GetState();


cameraDirection = Vector3.Transform(cameraDirection,Matrix.CreateFromAxisAngle(cameraUp, (-MathHelper.PiOver4/150)*(Mouse.GetState().X-prevMouseState.X)));
prevMouseState = Mouse.GetState();
原创粉丝点击