您的位置:首页 > 其它

在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时得到的图形如下(注:图片来源于网络):




当使用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 ——
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: