技術をかじる猫

適当に気になった技術や言語、思ったこと考えた事など。

3Dレンダリング基礎

書いてみたコードぺたり。

三角形2枚をカメラ移動で眺めるサンプル。

namespace XNASample003
{
    /// <summary>
    /// 基底 Game クラスから派生した、ゲームのメイン クラスです。
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        BasicEffect effect;
        VertexPositionColor[] data;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            this.data = new VertexPositionColor[] {
                new VertexPositionColor(new Vector3(1, 1, 1),Color.Blue),
                new VertexPositionColor(new Vector3(2, 0, 1),Color.Blue),
                new VertexPositionColor(new Vector3(0, 0, 1),Color.Blue),
                new VertexPositionColor(new Vector3(0, 0.5F, 2),Color.Red),
                new VertexPositionColor(new Vector3(1, -0.5F, 2),Color.Red),
                new VertexPositionColor(new Vector3(-1, -0.5F, 2),Color.Red)
            };
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // 新規の SpriteBatch を作成します。これはテクスチャーの描画に使用できます。
            spriteBatch = new SpriteBatch(GraphicsDevice);

            this.effect = new BasicEffect(this.GraphicsDevice);
            this.effect.VertexColorEnabled = true;

            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            this.effect.Dispose();
        }

        protected override void Update(GameTime gameTime)
        {
            // ゲームの終了条件をチェックします。
            GamePadState state = GamePad.GetState(PlayerIndex.One);
            if (state.Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: ここにゲームのアップデート ロジックを追加します。
            Vector3 position = new Vector3(
                5 * state.ThumbSticks.Left.X,
                5 * state.ThumbSticks.Left.Y,
                5);
            Vector3 target = new Vector3(
                state.ThumbSticks.Right.X,
                state.ThumbSticks.Right.Y, 0);
            Vector3 upVector = new Vector3(0, 1, 0);

            effect.View = Matrix.CreateLookAt(position, target, upVector);
            effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(45),
                this.GraphicsDevice.DisplayMode.AspectRatio,
                1,
                10);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: ここに描画コードを追加します。
            foreach (var pass in this.effect.CurrentTechnique.Passes) {
                pass.Apply();
                this.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                    PrimitiveType.TriangleList, this.data, 0, 2);
            }

            base.Draw(gameTime);
        }
    }
}

Projectionが視野角、表示範囲の指定。
View がカメラ座標と、表示の向き。
VertexColorEnabled は半ばおまじない。3D頂点の色をポリゴンに加味するかというはなし。
他に World とか言うものがあって、3D 空間全体をずらすとかそういうもの。だから、World 座標をX軸に -0.2F ずらすと、カメラが正面、0 座標参照の時に、すべてのポリゴンが若干左にずれて見える。
てーか普通カメラ動かすから出番ない、、、、、筈だよね?