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")));
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();
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.
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();
- xna4.0 3D review
- XNA4.0 2D游戏学习提问
- vs2013安装xna4.0模板
- A review of 3D/2D registration methods for image-guided interventions(1)
- A review of 3D/2D registration methods for image-guided interventions(2)
- XNA3.1升级XNA4.0所需要做的改动
- gma950 调试 xna4.0代码 编译正常 运行出错
- XNA4.0 在游戏窗口上显示中文
- XNA4.0学习笔记1:XNA解析及精灵动画
- XNA4.0学习笔记2:控制精灵和碰撞检测
- XNA 3.1 转换到 XNA4.0 的备忘录
- XNA 3.1 转换到 XNA4.0 的备忘录
- XNA4.0 RPG游戏开发教程(一)
- XNA4.0 RPG游戏开发教程(二)
- XNA4.0 RPG游戏开发教程(三)
- XNA4.0 RPG游戏开发教程(四)
- XNA4.0 RPG游戏开发教程(五)
- XNA4.0 RPG游戏开发教程(六)
- 归并排序算法
- 如何查看进程的实际的内存占用
- Java基本语法--变量
- hive ,hbase 整合错误日志记录
- C++之RAII惯用法
- xna4.0 3D review
- [基础] UDP数据包大小一般为多少?什么是碎片包?
- apache nginx 共存 详细配置
- JS 让网页中Flash Player的宽度自适应网页大小
- C和指针学习
- android开发工程师_专业技能_知识架构
- oracle 复制表与复制表结构
- 【JAVA】Java中抽象类和接口的区别
- 使用VS2010调试技巧让C指针无处遁形