使用BasicEffect

原文地址http://msdn.microsoft.com/zh-cn/library/bb203926(v=xnagamestudio.10).aspx

这个案例展示了如何创建并初始化一个实例BasicEffect,初始化一个顶点缓冲,可以提供BasicEffect、应用效果和呈现几何。

注意     这里描述的步骤适用于创建的效果BasicEffect类。 相比之下,Effect类 提供了用户希望编写的自定义效果,有关更详细的信息,请参考 如何:创建和应用的自定制效果

使用BasicEffect

1、使用BasicEffect类需要一组世界、视图和投影矩阵,一个顶点缓冲, 一个顶点声明和一个BasicEffect类的实例。在游戏开始的时候声明这些变量。

Matrix worldMatrix;

Matrix viewMatrix;

Matrix projectionMatrix;

VertexPositionNormalTexture[] cubeVertices;

VertexDeclaration basicEffectVertexDeclaration;

VertexBuffer vertexBuffer;

BasicEffect basicEffect;

 

2、初始化世界、视图和投影矩阵。在这个示例中,创建一个世界矩阵 沿x轴和y轴几何旋转22.5度。视图矩阵是一个矩阵的摄像机,看位置从(0,0,5)指向原点。投影矩阵是一个透视投影矩阵基于视野,指定一个45度的视野,一个宽高比等于客户的窗口,和一组远近的飞机。

float tilt = MathHelper.ToRadians( 22.5f ); // 22.5 degree angle

// Use the world matrix to tilt the cube along x and y axes.

worldMatrix = Matrix.CreateRotationX( tilt ) * Matrix.CreateRotationY( tilt );

viewMatrix = Matrix.CreateLookAt( new Vector3( 0, 0, 5 ), Vector3.Zero, Vector3.Up );

projectionMatrix = Matrix.CreatePerspectiveFieldOfView(

MathHelper.ToRadians( 45 ), // 45 degree angle

(float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height, 1.0f, 100.0f );

 

3、初始化BasicEffect 所需要的值

basicEffect = new BasicEffect( graphics.GraphicsDevice, null );
basicEffect.Alpha = 1.0f;
basicEffect.DiffuseColor = new Vector3( 1.0f, 0.0f, 1.0f ); basicEffect.SpecularColor = new Vector3( 0.25f, 0.25f, 0.25f );
basicEffect.SpecularPower = 5.0f;
basicEffect.AmbientLightColor = new Vector3( 0.75f, 0.75f, 0.75f );

basicEffect.DirectionalLight0.Enabled = true;
basicEffect.DirectionalLight0.DiffuseColor = Vector3.One;
basicEffect.DirectionalLight0.Direction = Vector3.Normalize( new Vector3( 1.0f, -1.0f, -1.0f ) );
basicEffect.DirectionalLight0.SpecularColor = Vector3.One;

basicEffect.DirectionalLight1.Enabled = true;
basicEffect.DirectionalLight1.DiffuseColor = new Vector3( 0.5f, 0.5f, 0.5f );
basicEffect.DirectionalLight1.Direction = Vector3.Normalize( new Vector3( -1.0f, -1.0f, 1.0f ) );
basicEffect.DirectionalLight1.SpecularColor = new Vector3( 0.5f, 0.5f, 0.5f );

basicEffect.LightingEnabled = true;

basicEffect.World = worldMatrix;
basicEffect.View = viewMatrix;
basicEffect.Projection = projectionMatrix;

 

4、 BasicEffect可以使用顶点类型,选择适当的选项你将使效果。

  • 如果照明被启用,顶点必须有一个正常的。
  • 如果顶点颜色被启用,顶点必须有颜色。
  • 如果变形被启用,顶点必须有一个纹理坐标。

下面的代码显示了一个立方体的顶点上创建的类型VertexPositionNormalTexture

cubeVertices = new VertexPositionNormalTexture[36];

Vector3 topLeftFront = new Vector3( -1.0f, 1.0f, 1.0f );
Vector3 bottomLeftFront = new Vector3( -1.0f, -1.0f, 1.0f );
Vector3 topRightFront = new Vector3( 1.0f, 1.0f, 1.0f );
Vector3 bottomRightFront = new Vector3( 1.0f, -1.0f, 1.0f );
Vector3 topLeftBack = new Vector3( -1.0f, 1.0f, -1.0f );
Vector3 topRightBack = new Vector3( 1.0f, 1.0f, -1.0f );
Vector3 bottomLeftBack = new Vector3( -1.0f, -1.0f, -1.0f ); Vector3 bottomRightBack = new Vector3( 1.0f, -1.0f, -1.0f );

Vector2 textureTopLeft = new Vector2( 0.0f, 0.0f );
Vector2 textureTopRight = new Vector2( 1.0f, 0.0f );
Vector2 textureBottomLeft = new Vector2( 0.0f, 1.0f );
Vector2 textureBottomRight = new Vector2( 1.0f, 1.0f );

Vector3 frontNormal = new Vector3( 0.0f, 0.0f, 1.0f );
Vector3 backNormal = new Vector3( 0.0f, 0.0f, -1.0f );
Vector3 topNormal = new Vector3( 0.0f, 1.0f, 0.0f );
Vector3 bottomNormal = new Vector3( 0.0f, -1.0f, 0.0f );
Vector3 leftNormal = new Vector3( -1.0f, 0.0f, 0.0f );
Vector3 rightNormal = new Vector3( 1.0f, 0.0f, 0.0f );

// Front face.
cubeVertices[0] =
    new VertexPositionNormalTexture(
    topLeftFront, frontNormal, textureTopLeft );
cubeVertices[1] =
    new VertexPositionNormalTexture(
    bottomLeftFront, frontNormal, textureBottomLeft );
cubeVertices[2] =
    new VertexPositionNormalTexture(
    topRightFront, frontNormal, textureTopRight );
cubeVertices[3] =
    new VertexPositionNormalTexture(
    bottomLeftFront, frontNormal, textureBottomLeft );
cubeVertices[4] =
    new VertexPositionNormalTexture(
    bottomRightFront, frontNormal, textureBottomRight );
cubeVertices[5] =
    new VertexPositionNormalTexture(
    topRightFront, frontNormal, textureTopRight );

// Back face.
cubeVertices[6] =
    new VertexPositionNormalTexture(
    topLeftBack, backNormal, textureTopRight );
cubeVertices[7] =
    new VertexPositionNormalTexture(
    topRightBack, backNormal, textureTopLeft );
cubeVertices[8] =
    new VertexPositionNormalTexture(
    bottomLeftBack, backNormal, textureBottomRight );
cubeVertices[9] =
    new VertexPositionNormalTexture(
    bottomLeftBack, backNormal, textureBottomRight );
cubeVertices[10] =
    new VertexPositionNormalTexture(
    topRightBack, backNormal, textureTopLeft );
cubeVertices[11] =
    new VertexPositionNormalTexture(
    bottomRightBack, backNormal, textureBottomLeft );

// Top face.
cubeVertices[12] =
    new VertexPositionNormalTexture(
    topLeftFront, topNormal, textureBottomLeft );
cubeVertices[13] =
    new VertexPositionNormalTexture(
    topRightBack, topNormal, textureTopRight );
cubeVertices[14] =
    new VertexPositionNormalTexture(
    topLeftBack, topNormal, textureTopLeft );
cubeVertices[15] =
    new VertexPositionNormalTexture(
    topLeftFront, topNormal, textureBottomLeft );
cubeVertices[16] =
    new VertexPositionNormalTexture(
    topRightFront, topNormal, textureBottomRight );
cubeVertices[17] =
    new VertexPositionNormalTexture(
    topRightBack, topNormal, textureTopRight );

// Bottom face.
cubeVertices[18] =
    new VertexPositionNormalTexture(
    bottomLeftFront, bottomNormal, textureTopLeft );
cubeVertices[19] =
    new VertexPositionNormalTexture(
    bottomLeftBack, bottomNormal, textureBottomLeft );
cubeVertices[20] =
    new VertexPositionNormalTexture( bottomRightBack, bottomNormal, textureBottomRight ); cubeVertices[21] = new VertexPositionNormalTexture(
    bottomLeftFront, bottomNormal, textureTopLeft );
cubeVertices[22] =
    new VertexPositionNormalTexture(
    bottomRightBack, bottomNormal, textureBottomRight );
cubeVertices[23] =
    new VertexPositionNormalTexture(
    bottomRightFront, bottomNormal, textureTopRight );

// Left face.
cubeVertices[24] =
    new VertexPositionNormalTexture(
    topLeftFront, leftNormal, textureTopRight );
cubeVertices[25] =
    new VertexPositionNormalTexture(
    bottomLeftBack, leftNormal, textureBottomLeft );
cubeVertices[26] =
    new VertexPositionNormalTexture(
    bottomLeftFront, leftNormal, textureBottomRight );
cubeVertices[27] =
    new VertexPositionNormalTexture(
    topLeftBack, leftNormal, textureTopLeft );
cubeVertices[28] =
    new VertexPositionNormalTexture(
    bottomLeftBack, leftNormal, textureBottomLeft );
cubeVertices[29] =
    new VertexPositionNormalTexture(
    topLeftFront, leftNormal, textureTopRight );

// Right face.
cubeVertices[30] =
    new VertexPositionNormalTexture(
    topRightFront, rightNormal, textureTopLeft );
cubeVertices[31] =
    new VertexPositionNormalTexture(
    bottomRightFront, rightNormal, textureBottomLeft );
cubeVertices[32] =
    new VertexPositionNormalTexture(
    bottomRightBack, rightNormal, textureBottomRight );
cubeVertices[33] =
    new VertexPositionNormalTexture(
    topRightBack, rightNormal, textureTopRight );
cubeVertices[34] =
    new VertexPositionNormalTexture(
    topRightFront, rightNormal, textureTopLeft );
cubeVertices[35] =
    new VertexPositionNormalTexture(
    bottomRightBack, rightNormal, textureBottomRight );

vertexBuffer = new VertexBuffer(
    graphics.GraphicsDevice,
    VertexPositionNormalTexture.SizeInBytes * cubeVertices.Length,
    ResourceUsage.None,
    ResourceManagementMode.Automatic
);

vertexBuffer.SetData<VertexPositionNormalTexture>( cubeVertices );

 5、创建一个顶点声明类型VertexPositionNormalTexture.  This vertex declaration will be used in the render loop in the next step

basicEffectVertexDeclaration = new VertexDeclaration(
    graphics.GraphicsDevice, VertexPositionNormalTexture.VertexElements );

6、Call the Effect.Begin  to begin applying the BasicEffect. Draw the desired geometry between calls to EffectPass.Begin and EffectPass.End. Call Effect.End to stop application of the technique.

graphics.GraphicsDevice.VertexDeclaration =
    basicEffectVertexDeclaration;

graphics.GraphicsDevice.Vertices[0].SetSource( vertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes );

// This code would go between a device
// BeginScene-EndScene block.
basicEffect.Begin();
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
    pass.Begin();

    graphics.GraphicsDevice.DrawPrimitives(
        PrimitiveType.TriangleList,
        0,
        12
    );

    pass.End();
}
basicEffect.End();

完整的例子

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    ContentManager content;

    Matrix worldMatrix;
    Matrix viewMatrix;
    Matrix projectionMatrix;
    VertexPositionNormalTexture[] cubeVertices;
    VertexDeclaration basicEffectVertexDeclaration;
    VertexBuffer vertexBuffer;
    BasicEffect basicEffect;

    public Game1()
    {
        graphics = new GraphicsDeviceManager( this );
        content = new ContentManager( Services );
    }

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

    protected override void LoadGraphicsContent( bool loadAllContent )
    {
        InitializeTransform();
        if (loadAllContent)
        {
            InitializeEffect();
            InitializeCube();
        }
    }

    private void InitializeTransform()
    {
        float tilt = MathHelper.ToRadians( 22.5f );  // 22.5 degree angle
        // Use the world matrix to tilt the cube along x and y axes.
        worldMatrix = Matrix.CreateRotationX( tilt ) *
            Matrix.CreateRotationY( tilt );

        viewMatrix = Matrix.CreateLookAt( new Vector3( 0, 0, 5 ), Vector3.Zero,
            Vector3.Up );

        projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
            MathHelper.ToRadians( 45 ),  // 45 degree angle
            (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height,
            1.0f, 100.0f );

    }

    private void InitializeEffect()
    {
        basicEffectVertexDeclaration = new VertexDeclaration(
            graphics.GraphicsDevice, VertexPositionNormalTexture.VertexElements );


        basicEffect = new BasicEffect( graphics.GraphicsDevice, null );
        basicEffect.Alpha = 1.0f;
        basicEffect.DiffuseColor = new Vector3( 1.0f, 0.0f, 1.0f );
        basicEffect.SpecularColor = new Vector3( 0.25f, 0.25f, 0.25f );
        basicEffect.SpecularPower = 5.0f;
        basicEffect.AmbientLightColor = new Vector3( 0.75f, 0.75f, 0.75f );

        basicEffect.DirectionalLight0.Enabled = true;
        basicEffect.DirectionalLight0.DiffuseColor = Vector3.One;
        basicEffect.DirectionalLight0.Direction = Vector3.Normalize( new Vector3( 1.0f, -1.0f, -1.0f ) );
        basicEffect.DirectionalLight0.SpecularColor = Vector3.One;

        basicEffect.DirectionalLight1.Enabled = true;
        basicEffect.DirectionalLight1.DiffuseColor = new Vector3( 0.5f, 0.5f, 0.5f );
        basicEffect.DirectionalLight1.Direction = Vector3.Normalize( new Vector3( -1.0f, -1.0f, 1.0f ) );
        basicEffect.DirectionalLight1.SpecularColor = new Vector3( 0.5f, 0.5f, 0.5f );

        basicEffect.LightingEnabled = true;

        basicEffect.World = worldMatrix;
        basicEffect.View = viewMatrix;
        basicEffect.Projection = projectionMatrix;


    }

    private void InitializeCube()
    {

        cubeVertices = new VertexPositionNormalTexture[36];

        Vector3 topLeftFront = new Vector3( -1.0f, 1.0f, 1.0f );
        Vector3 bottomLeftFront = new Vector3( -1.0f, -1.0f, 1.0f );
        Vector3 topRightFront = new Vector3( 1.0f, 1.0f, 1.0f );
        Vector3 bottomRightFront = new Vector3( 1.0f, -1.0f, 1.0f );
        Vector3 topLeftBack = new Vector3( -1.0f, 1.0f, -1.0f );
        Vector3 topRightBack = new Vector3( 1.0f, 1.0f, -1.0f );
        Vector3 bottomLeftBack = new Vector3( -1.0f, -1.0f, -1.0f );
        Vector3 bottomRightBack = new Vector3( 1.0f, -1.0f, -1.0f );

        Vector2 textureTopLeft = new Vector2( 0.0f, 0.0f );
        Vector2 textureTopRight = new Vector2( 1.0f, 0.0f );
        Vector2 textureBottomLeft = new Vector2( 0.0f, 1.0f );
        Vector2 textureBottomRight = new Vector2( 1.0f, 1.0f );

        Vector3 frontNormal = new Vector3( 0.0f, 0.0f, 1.0f );
        Vector3 backNormal = new Vector3( 0.0f, 0.0f, -1.0f );
        Vector3 topNormal = new Vector3( 0.0f, 1.0f, 0.0f );
        Vector3 bottomNormal = new Vector3( 0.0f, -1.0f, 0.0f );
        Vector3 leftNormal = new Vector3( -1.0f, 0.0f, 0.0f );
        Vector3 rightNormal = new Vector3( 1.0f, 0.0f, 0.0f );

        // Front face.
        cubeVertices[0] =
            new VertexPositionNormalTexture(
            topLeftFront, frontNormal, textureTopLeft );
        cubeVertices[1] =
            new VertexPositionNormalTexture(
            bottomLeftFront, frontNormal, textureBottomLeft );
        cubeVertices[2] =
            new VertexPositionNormalTexture(
            topRightFront, frontNormal, textureTopRight );
        cubeVertices[3] =
            new VertexPositionNormalTexture(
            bottomLeftFront, frontNormal, textureBottomLeft );
        cubeVertices[4] =
            new VertexPositionNormalTexture(
            bottomRightFront, frontNormal, textureBottomRight );
        cubeVertices[5] =
            new VertexPositionNormalTexture(
            topRightFront, frontNormal, textureTopRight );

        // Back face.
        cubeVertices[6] =
            new VertexPositionNormalTexture(
            topLeftBack, backNormal, textureTopRight );
        cubeVertices[7] =
            new VertexPositionNormalTexture(
            topRightBack, backNormal, textureTopLeft );
        cubeVertices[8] =
            new VertexPositionNormalTexture(
            bottomLeftBack, backNormal, textureBottomRight );
        cubeVertices[9] =
            new VertexPositionNormalTexture(
            bottomLeftBack, backNormal, textureBottomRight );
        cubeVertices[10] =
            new VertexPositionNormalTexture(
            topRightBack, backNormal, textureTopLeft );
        cubeVertices[11] =
            new VertexPositionNormalTexture(
            bottomRightBack, backNormal, textureBottomLeft );

        // Top face.
        cubeVertices[12] =
            new VertexPositionNormalTexture(
            topLeftFront, topNormal, textureBottomLeft );
        cubeVertices[13] =
            new VertexPositionNormalTexture(
            topRightBack, topNormal, textureTopRight );
        cubeVertices[14] =
            new VertexPositionNormalTexture(
            topLeftBack, topNormal, textureTopLeft );
        cubeVertices[15] =
            new VertexPositionNormalTexture(
            topLeftFront, topNormal, textureBottomLeft );
        cubeVertices[16] =
            new VertexPositionNormalTexture(
            topRightFront, topNormal, textureBottomRight );
        cubeVertices[17] =
            new VertexPositionNormalTexture(
            topRightBack, topNormal, textureTopRight );

        // Bottom face.
        cubeVertices[18] =
            new VertexPositionNormalTexture(
            bottomLeftFront, bottomNormal, textureTopLeft );
        cubeVertices[19] =
            new VertexPositionNormalTexture(
            bottomLeftBack, bottomNormal, textureBottomLeft );
        cubeVertices[20] =
            new VertexPositionNormalTexture(
            bottomRightBack, bottomNormal, textureBottomRight );
        cubeVertices[21] =
            new VertexPositionNormalTexture(
            bottomLeftFront, bottomNormal, textureTopLeft );
        cubeVertices[22] =
            new VertexPositionNormalTexture(
            bottomRightBack, bottomNormal, textureBottomRight );
        cubeVertices[23] =
            new VertexPositionNormalTexture(
            bottomRightFront, bottomNormal, textureTopRight );

        // Left face.
        cubeVertices[24] =
            new VertexPositionNormalTexture(
            topLeftFront, leftNormal, textureTopRight );
        cubeVertices[25] =
            new VertexPositionNormalTexture(
            bottomLeftBack, leftNormal, textureBottomLeft );
        cubeVertices[26] =
            new VertexPositionNormalTexture(
            bottomLeftFront, leftNormal, textureBottomRight );
        cubeVertices[27] =
            new VertexPositionNormalTexture(
            topLeftBack, leftNormal, textureTopLeft );
        cubeVertices[28] =
            new VertexPositionNormalTexture(
            bottomLeftBack, leftNormal, textureBottomLeft );
        cubeVertices[29] =
            new VertexPositionNormalTexture(
            topLeftFront, leftNormal, textureTopRight );

        // Right face.
        cubeVertices[30] =
            new VertexPositionNormalTexture(
            topRightFront, rightNormal, textureTopLeft );
        cubeVertices[31] =
            new VertexPositionNormalTexture(
            bottomRightFront, rightNormal, textureBottomLeft );
        cubeVertices[32] =
            new VertexPositionNormalTexture(
            bottomRightBack, rightNormal, textureBottomRight );
        cubeVertices[33] =
            new VertexPositionNormalTexture(
            topRightBack, rightNormal, textureTopRight );
        cubeVertices[34] =
            new VertexPositionNormalTexture(
            topRightFront, rightNormal, textureTopLeft );
        cubeVertices[35] =
            new VertexPositionNormalTexture(
            bottomRightBack, rightNormal, textureBottomRight );

        vertexBuffer = new VertexBuffer(
            graphics.GraphicsDevice,
            VertexPositionNormalTexture.SizeInBytes * cubeVertices.Length,
            ResourceUsage.None,
            ResourceManagementMode.Automatic
        );

        vertexBuffer.SetData<VertexPositionNormalTexture>( cubeVertices );

    }

    protected override void UnloadGraphicsContent( bool unloadAllContent )
    {
        if (unloadAllContent == true)
        {
            content.Unload();
        }
    }

    protected override void Update( GameTime gameTime )
    {
        // Allows the default game to exit on Xbox 360 and Windows.
        if (GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        base.Update( gameTime );
    }


    protected override void Draw( GameTime gameTime )
    {

        graphics.GraphicsDevice.Clear( Color.CornflowerBlue );

        graphics.GraphicsDevice.RenderState.CullMode =
        CullMode.CullClockwiseFace;

        graphics.GraphicsDevice.VertexDeclaration =
            basicEffectVertexDeclaration;

        graphics.GraphicsDevice.Vertices[0].SetSource( vertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes );

        // This code would go between a device
        // BeginScene-EndScene block.
        basicEffect.Begin();
        foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
        {
            pass.Begin();

            graphics.GraphicsDevice.DrawPrimitives(
                PrimitiveType.TriangleList,
                0,
                12
            );

            pass.End();
        }
        basicEffect.End();

        base.Draw( gameTime );
    }
}
posted on 2013-04-11 16:48  竟飞工作室  阅读(292)  评论(0编辑  收藏  举报