在Windows Phone中进行3D开发之四三角形
2011-10-20 23:54
274 查看
上节内容中,我们已经有了一个三维的空间,本节中我们就来结识3D中最基本的图元——三角形
在3D开发中,三角形占有重要的地位。它是3D模型的最小基元,无论多复杂的3D模型,最终都可以表示成若干个三角形的组合。图形处理芯片也对三角形渲染进行了硬件支持。可见三角形虽然简单,但在3D开发中的重要性。下面我们就从这个最简单的三角形开始。
沿用上节我们建好的XNA项目,在VS2010中打开该项目。打开Game1.cs文件,我们来修改Game1类。
要想构建三角形,我们首先想到的是需要三个顶点的坐标。在XNA中提供了VertexPositionColor类型,用于表示一个空间中的顶点的位置和颜色信息,在此我们只用到位置信息,颜色会在后文中使用。
为Game1类添加用于保存三角形顶点信息成员变量:VertexPositionColor[] triangle;
然后在LoadContent()方法中定义三个顶点:
triangle = newVertexPositionColor[]{
new VertexPositionColor(newVector3(0, 1, 0), Color.Red),
new VertexPositionColor(newVector3(1, -1, 0), Color.Green),
new VertexPositionColor(newVector3(-1,-1, 0), Color.Blue)
};
这三个顶点坐标正好是一个三角形。然后在Draw()方法中将其绘制输出:
GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip,triangle, 0, 1);
DrawUserPrimitives()方法用于绘制图元,方法的原型如下:
public voidDrawUserPrimitives<T>(
PrimitiveTypeprimitiveType, //图元类型
T[]vertexData, //顶点数据
intvertexOffset, //读顶点数据的起始位置
intprimitiveCount //图元个数
)
其中PrimitiveType用于描述图元类型,在Windows Phone中有四个取值,含义是:
TriangleList:三角形列表
TriangleStrip:三角形带
LineList:线段列表
LineStrip:线段带
例如同样的6个顶点坐标,当使用TriangleList时得到的图形如下(注:图片来源于网络):
![](http://hi.csdn.net/attachment/201110/20/0_13191260735wzA.gif)
当使用TriangleStrip时得到的图形如下(注:图片来源于网络):
![](http://hi.csdn.net/attachment/201110/20/0_1319126110TfuO.gif)
运行程序,在模拟器中将看到如下结果:
![](http://hi.csdn.net/attachment/201110/20/0_131912613850hl.gif)
虽然很简陋,不过这确实是在3D空间中绘制的,如果有兴趣,不妨修改一下摄像机的位置或者三角形的坐标等参数,也可以绘制其他图元,体会运行结果的变化。下节中我们将进行三维物体的运动处理。
附本节Game1类的完整源码:
在3D开发中,三角形占有重要的地位。它是3D模型的最小基元,无论多复杂的3D模型,最终都可以表示成若干个三角形的组合。图形处理芯片也对三角形渲染进行了硬件支持。可见三角形虽然简单,但在3D开发中的重要性。下面我们就从这个最简单的三角形开始。
沿用上节我们建好的XNA项目,在VS2010中打开该项目。打开Game1.cs文件,我们来修改Game1类。
要想构建三角形,我们首先想到的是需要三个顶点的坐标。在XNA中提供了VertexPositionColor类型,用于表示一个空间中的顶点的位置和颜色信息,在此我们只用到位置信息,颜色会在后文中使用。
为Game1类添加用于保存三角形顶点信息成员变量:VertexPositionColor[] triangle;
然后在LoadContent()方法中定义三个顶点:
triangle = newVertexPositionColor[]{
new VertexPositionColor(newVector3(0, 1, 0), Color.Red),
new VertexPositionColor(newVector3(1, -1, 0), Color.Green),
new VertexPositionColor(newVector3(-1,-1, 0), Color.Blue)
};
这三个顶点坐标正好是一个三角形。然后在Draw()方法中将其绘制输出:
GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip,triangle, 0, 1);
DrawUserPrimitives()方法用于绘制图元,方法的原型如下:
public voidDrawUserPrimitives<T>(
PrimitiveTypeprimitiveType, //图元类型
T[]vertexData, //顶点数据
intvertexOffset, //读顶点数据的起始位置
intprimitiveCount //图元个数
)
其中PrimitiveType用于描述图元类型,在Windows Phone中有四个取值,含义是:
TriangleList:三角形列表
TriangleStrip:三角形带
LineList:线段列表
LineStrip:线段带
例如同样的6个顶点坐标,当使用TriangleList时得到的图形如下(注:图片来源于网络):
![](http://hi.csdn.net/attachment/201110/20/0_13191260735wzA.gif)
当使用TriangleStrip时得到的图形如下(注:图片来源于网络):
![](http://hi.csdn.net/attachment/201110/20/0_1319126110TfuO.gif)
运行程序,在模拟器中将看到如下结果:
![](http://hi.csdn.net/attachment/201110/20/0_131912613850hl.gif)
虽然很简陋,不过这确实是在3D空间中绘制的,如果有兴趣,不妨修改一下摄像机的位置或者三角形的坐标等参数,也可以绘制其他图元,体会运行结果的变化。下节中我们将进行三维物体的运动处理。
附本节Game1类的完整源码:
public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; Camera camera; Matrix world = Matrix.Identity; BasicEffect basicEffect; VertexPositionColor[] triangle; 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); triangle = new VertexPositionColor[]{ new VertexPositionColor(new Vector3(0, 1, 0), Color.Red), new VertexPositionColor(new Vector3(1, -1, 0), Color.Green), new VertexPositionColor(new Vector3(-1,-1, 0), Color.Blue) }; } /// <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(); GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, triangle, 0, 1); } base.Draw(gameTime); } }——欢迎转载,请注明出处 http://blog.csdn.net/caowenbin ——
相关文章推荐
- 在Windows Phone中进行3D开发之后记(附源码)
- 在Windows Phone中进行3D开发之一坐标系
- 在Windows Phone中进行3D开发之十三阳光
- 在Windows Phone中进行3D开发之六旋转
- 在Windows Phone中进行3D开发之三空间
- 在Windows Phone中进行3D开发之二摄像机
- 在Windows Phone中进行3D开发之五平移缩放
- 在Windows Phone中进行3D开发之九模型
- 在Windows Phone中进行3D开发之十二飞行
- 在Windows Phone中进行3D开发之七纹理
- 在Windows Phone中进行3D开发之八光照
- 在Windows Phone中进行3D开发之十组件
- 基于 OpenGL 进行 3D 图形开发
- 游戏开发及游戏(2D/3D)小Demo(进行中)- Android
- Windows Phone 7 3D开发中使用纹理贴图
- Balder 3D开发系列之--给自定义基本体进行贴图操作
- 【附源码】【Android 3D OpenGL】开发之二——旋转的三角形【MacroCheng原创】
- Windows Phone 7 3D开发中使用纹“.NET研究”理贴图
- Balder 3D开发系列之--给自定义基本体进行贴图操作
- 基于HTML5及WebGL开发的2D3D第一人称漫游进行碰撞检测