2009-10-31 90 views
0

我正在关注Chad Carter的XNA 3.0 Game Studio Unleashed Book。无法让这个三角形显示

我在第4章,下面的列表应该在游戏窗口上呈现一个纹理三角形,但是对于我的生活,我无法弄清楚为什么它不是。我只是得到纯矢车菊蓝色屏幕。

我使用Visual Studio 2008与XNA 3.1

那些谁拥有这本书,我到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); 

    } 
} 

}

回答

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

里面说到我的另一种可能性是设置的cullmode以及定义顶点的顺序。你可能正在看什么被视为三角形的后面,因此没有渲染。

+0

宾果,就是这样。这是我得到的复制和粘贴而不检查所有的值。感谢考特尼。 – Spidey 2009-10-31 15:04:22

1

即时使用相同的软件,并遵循同一本书。自我教导如何最终制作2D游戏。

关于你的问题,我没有强大的编程,我承认,但通过你的代码扫描(这是同时寻求一个关于同一本书的问题的答案本人)我立即注意到一些差异,并认为也许你试过个性化代码?首先,您已经在void initialize()中运行了初始化方法,除非我错过了本书,并且由于我的版本正在工作,所以它呈现的三角形只是在创建正方形时遇到问题,特别是以下码;这本书的内容

  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 
     } 

    } 
} 

希望我去过的一些帮助,至少,

问候:)