在Windows Phone中进行3D开发之四三角形

本文介绍如何使用XNA在三维空间中绘制三角形,包括定义顶点、设置颜色和渲染图元的过程。通过实例演示了在模拟器中绘制一个由三个顶点组成的三角形,并解释了不同图元类型的渲染效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上节内容中,我们已经有了一个三维的空间,本节中我们就来结识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时得到的图形如下(注:图片来源于网络):

当使用TriangleStrip时得到的图形如下(注:图片来源于网络):

运行程序,在模拟器中将看到如下结果:

虽然很简陋,不过这确实是在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.youkuaiyun.com/caowenbin ——

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值