0

我正在关注 Chad Carter 的 XNA 3.0 Game Studio Unleashed Book。

我在第 4 章,下面的清单应该在游戏窗口上渲染一个带纹理的三角形,但对于我来说,我无法弄清楚为什么它不是。我只是得到普通的矢车菊蓝屏。

我正在使用带有 XNA 3.1 的 Visual Studio 2008

那些有这本书的人,我已经到了第 66 页的顶部,在那里我将纹理应用于三角形。

非常感谢任何帮助。

namespace XNADemo
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    private Matrix projection;
    private Matrix view;
    private Matrix world;

    private Vector3 cameraPosition = new Vector3(0.0f, 0.0f, 3.0f);
    private Vector3 cameraTarget = Vector3.Zero;
    private Vector3 cameraUpVector = Vector3.Up;

    private VertexPositionNormalTexture[] vertices;
    private Texture2D texture;
    private BasicEffect effect;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    /// <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
        InitializeCamera();
        InitializeVertices();            
        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()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        texture = Content.Load<Texture2D>("texture");
        effect = new BasicEffect(graphics.GraphicsDevice, null);



    }

    /// <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);
        world = Matrix.Identity;

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


        effect.Projection = projection;
        effect.View = view;

        effect.EnableDefaultLighting();


        effect.World = world;
        effect.TextureEnabled = true;
        effect.Texture = texture;
        effect.Begin();

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Begin();
            graphics.GraphicsDevice.DrawUserPrimitives
                (PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3);
            pass.End();
        }

        effect.End();

        base.Draw(gameTime);
    }


    private void InitializeCamera()
    {
        float aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width /
                            (float)graphics.GraphicsDevice.Viewport.Height;
        Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 0.0001f, 1000.0f, out projection);

        Matrix.CreateLookAt(ref cameraPosition, ref cameraTarget, ref cameraUpVector, out view);


    }

    private void InitializeVertices()
    {
        Vector3 position;
        Vector2 textureCoordinates;

        vertices = new VertexPositionNormalTexture[3];

        //top left
        position = new Vector3(-1, 1, 0);
        textureCoordinates = new Vector2(0, 0);
        vertices[0] = new VertexPositionNormalTexture(position, Vector3.Forward, textureCoordinates);

        //bottom right
        position = new Vector3(1, -1, 0);
        textureCoordinates = new Vector2(1, 1);
        vertices[1] = new VertexPositionNormalTexture(position, Vector3.Forward, textureCoordinates);

        //bottom left
        position = new Vector3(-1, -1, 0);
        textureCoordinates = new Vector2(0, 1);
        vertices[1] = new VertexPositionNormalTexture(position, Vector3.Forward, textureCoordinates);  

    }
}

}

4

2 回答 2

3

我相信左下顶点的顶点索引应该是2而不是1

    //bottom right
    position = new Vector3(1, -1, 0);
    textureCoordinates = new Vector2(1, 1);
    vertices[1] = new VertexPositionNormalTexture(position,
    Vector3.Forward, textureCoordinates);

    //bottom left
    position = new Vector3(-1, -1, 0);
    textureCoordinates = new Vector2(0, 1);
    //vertices[1] = new VertexPositionNormalTexture(position,
    vertices[2] = new VertexPositionNormalTexture(position,
    Vector3.Forward, textureCoordinates);

我想到的另一种可能性是设置的剔除模式和定义顶点的顺序。您可能正在查看被视为三角形背面的部分,因此未渲染。

于 2009-10-31T14:36:43.287 回答
1

我现在使用相同的软件并遵循同一本书。最终教自己如何制作 2D 游戏。

关于您的问题,我承认我的编程能力并不强,但是扫描您的代码(这是在自己寻找有关同一本书的问题的答案时)我立即注意到了一些差异,并认为您可能尝试过个性化编码 ?首先,您已经在void initialize() 中运行了您的 inizialize 方法,除非我错过了这本书,并且由于我的版本正在运行,因此它渲染了三角形,我只是在创建正方形时遇到了问题,特别是以下代码;

            graphics.GraphicsDevice.DrawUserPrimitives(
            PrimitiveType.TriangleList, vertices, 0,
            vertices.Length, indices, 0 , indices.Length / 3);

这就是本书内容的 C/P,我的问题是没有接受 7 个参数的重载方法,所以我正在寻找解决方法并更好地理解 DrawUserPrimitives 方法,任何帮助将不胜感激。

回到你的问题,我已经在 LoadContent() 下运行了这些函数,这可能会给你带来问题,我不确定只是因为我还在学习自己,下面是我的代码,你可以用它来与你自己的比较如果您愿意,我建议您开始一个新项目并 C/P 代码,以便您拥有一个工作版本并知道它正在工作,从中进行您想要的所有个性化/定制。这就是我的朋友总是对我说的,“让它工作,然后改进它”。

namespace XNA_Demo
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        private Matrix projection;
        private Matrix view;
        private Matrix world;

        private Vector3 cameraPosition = new Vector3(0.0f, 0.0f, 3.0f);
        private Vector3 cameraTarget = Vector3.Zero;
        private Vector3 cameraUpVector = Vector3.Up;
        private VertexPositionNormalTexture[] vertices;

        private Texture2D texture;
        private short[] indices;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <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

            float aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width /
                (float)graphics.GraphicsDevice.Viewport.Height;

            Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio,
                0.0001f, 1000.0f, out projection);

            Matrix.CreateLookAt(ref cameraPosition, ref cameraTarget,
            ref cameraUpVector, out view);

            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()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here

            InitializeVertices();
            InitializeIndices();

            texture = Content.Load<Texture2D>("MGS4vet");
        }

        /// <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);

            // TODO: Add your drawing code here

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

            BasicEffect effect = new BasicEffect(graphics.GraphicsDevice, null);

            world = Matrix.Identity;

            effect.World = world;
            effect.Projection = projection;
            effect.View = view;
            effect.EnableDefaultLighting();

            effect.TextureEnabled = true;
            effect.Texture = texture;

            effect.Begin();
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();
                graphics.GraphicsDevice.DrawUserPrimitives(
                PrimitiveType.TriangleList, vertices, 0,
                vertices.Length / 3);
                pass.End();
            }
            effect.End();

            base.Draw(gameTime);


        }

        private void InitializeVertices()
        {
            Vector3 position;
            Vector2 textureCoordinates;
            vertices = new VertexPositionNormalTexture[4];

            //top left
            position = new Vector3(-1, 1, 0);
            textureCoordinates = new Vector2(0, 0);
            vertices[0] = new VertexPositionNormalTexture(position, Vector3.Forward,
            textureCoordinates);
            //bottom right
            position = new Vector3(1, -1, 0);
            textureCoordinates = new Vector2(1, 1);
            vertices[1] = new VertexPositionNormalTexture(position, Vector3.Forward,
            textureCoordinates);
            //bottom left
            position = new Vector3(-1, -1, 0);
            textureCoordinates = new Vector2(0, 1);
            vertices[2] = new VertexPositionNormalTexture(position, Vector3.Forward,
            textureCoordinates);

            //top right
            position = new Vector3(1, 1, 0);
            textureCoordinates = new Vector2(1, 0);
            vertices[3] = new VertexPositionNormalTexture(position, Vector3.Forward,
            textureCoordinates);

        }

        private void InitializeIndices()
        {
            //6 vertices make up 2 triangles which make up our rectangle
            indices = new short[6];
            //triangle 1 (bottom portion)
            indices[0] = 0; // top left
            indices[1] = 1; // bottom right
            indices[2] = 2; // bottom left
            //triangle 2 (top portion)
            indices[3] = 0; // top left
            indices[4] = 3; // top right
            indices[5] = 1; // bottom right
        }

    }
}

希望至少对我有所帮助

问候 :)

于 2012-03-22T00:42:36.063 回答