【问题标题】:Cannot get this triangle to display无法显示此三角形
【发布时间】:2013-11-26 10:18:19
【问题描述】:

我正在关注 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);  

    }
}

}

【问题讨论】:

    标签: c# xna xna-3.0


    【解决方案1】:

    我相信左下顶点的顶点索引应该是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);
    

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

    【讨论】:

    • 宾果游戏,就是这样。这是我在不检查所有值的情况下复制和粘贴所得到的。谢谢考特尼。
    【解决方案2】:

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

    关于你的问题,我承认我的编程能力并不强,但我会扫描你的代码(这是在我自己寻找关于同一本书的问题的答案时)我立即注意到了一些差异,并认为也许你是否尝试过个性化代码?首先,你已经在 main 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
            }
    
        }
    }
    

    希望我至少能帮上一些忙,

    问候 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多