xna4.0 3D review

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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值