2009-10-31 96 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 
     } 

    } 
} 

希望我去過的一些幫助,至少,

問候:)