忍者ブログ

Memeplexes

プログラミング、3DCGとその他いろいろについて

かんたんXNA その8 3Dカメラのアニメーション

このページは古いです
最新版はこちら

前回は2Dでしたが今回は
3Dカメラのアニメーションを行います。

これもやはりGame.Updateメソッド内で
カメラの座標と向きを変えることによって
アニメーションを行っています。
 

 



using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


public class MyGame : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    BasicEffect effect;

    VertexPositionColor[] vertices = new VertexPositionColor[3];
    float angle = 0;

    public MyGame()
    {
        graphics = new GraphicsDeviceManager(this);

        vertices[0] = new VertexPositionColor(new Vector3(1, 1, 0), Color.Navy);
        vertices[1] = new VertexPositionColor(new Vector3(0, 0, 0), Color.White);
        vertices[2] = new VertexPositionColor(new Vector3(-1, 1, 0), Color.Red);
    }

    protected override void LoadGraphicsContent(bool loadAllContent)
    {
        if (loadAllContent)
        {
            effect = new BasicEffect(graphics.GraphicsDevice, null);
            effect.VertexColorEnabled = true;
            effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(45),
                Window.ClientBounds.Width / (float)Window.ClientBounds.Height,
                1,
                100
                );
        }
    }

    protected override void Update(GameTime gameTime)
    {
        angle += 0.01f;
    }

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

        graphics.GraphicsDevice.VertexDeclaration = new VertexDeclaration(
            graphics.GraphicsDevice,
            VertexPositionColor.VertexElements
            );

        effect.View = Matrix.CreateLookAt(
            new Vector3(3 * (float)Math.Sin(angle), 0, 3 * (float)Math.Cos(angle)),
            new Vector3(0, 0, 0),
            new Vector3(0, 1, 0)
            );

        effect.Begin();

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Begin();

            graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                PrimitiveType.TriangleList,
                vertices,
                0,  //vertexOffset
                1   //primitiveCount
                );

            pass.End();
        }

        effect.End();
    }
}


このサンプルは三角形の周りをカメラがぐるぐる回ることによってアニメーションを行っています。
(三角形が回っているように見えますが、実際に回っているのはカメラの方です。
つまり天動説ではなく地動説というわけです。)


xnaSimplestTriangleWithCameraRotating1.JPGxnaSimplestTriangleWithCameraRotating2.JPGxnaSimplestTriangleWithCameraRotating3.JPG

ただし、これ以上回転して、裏から見ると三角形は見えなくなります。
xnaSimplestTriangleWithCameraRotating4.JPG
これはXNAが、
座標が時計回りの順番になっている三角形しか描画しない
からです。
xnaCullingDescription.JPG
回転して裏から見ると反時計回りの順番になっているように見え、
描画されなくなるのです。
(もちろん、さらに回転して元の時計回りの順番に見えるようになったら
再び描画されるようになります。)
これはパフォーマンスの問題で、
これによって描画が高速化するようです。


どうしても裏から見たときも描画したいという場合は、
GraphicsDevice.RenderState.CullModeCullMode.Noneに設定すると
裏側も描画されるようになります。

        graphics.GraphicsDevice.RenderState.CullMode = CullMode.None;

 xnaSimplestTriangleWithCameraRotating5.JPG
(ちなみに、RenderState.CullModeのデフォルトは
CullMode.CullCounterClockwiseFaceで、反時計回りは描画しないという意味です。
CullModeは全部で3つあり、
1つがデフォルトのCullCounterClockwiseFace、
2つ目がここで使ったNone(「描画しないということは無い」という意味)、
3つ目はCullClockwiseFaceで、デフォルトの逆です。)

拍手[0回]

PR