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

       上节内容中,我们已经有了一个三维的空间,本节中我们就来结识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.csdn.net/caowenbin ——

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

文斌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值