2013-02-12 89 views
1

我想畫的3軸XYZ和模型 ,看看它在哪裏的空間,以及它如何移動XNA動畫問題

當我剛畫立方體一個簡單的旋轉 一切正常

但是當我添加軸拉伸法

的rendring是不正常

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Media; 

namespace WindowsGame2 
{ 

    public class Game1 : Microsoft.Xna.Framework.Game 
    { 
    //Some declaration 
    float x = 30f; 
    float y = 0f; 
    float z = 30f; 

    //GraphicsDevice device; 
    VertexBuffer vertexBuffer; 
    VertexPositionColor[] lines; 

    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    Model model; 
    Matrix view; 
    Matrix progection; 
    float xTans = 0.0f; 
    float yTans = 0.0f; 
    float zTans = 0.0f; 
    public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 
    } 

    protected override void Initialize() 
    { 

     //device = graphics.GraphicsDevice; 
     base.Initialize(); 
     this.IsMouseVisible = true; 
    } 

    protected override void LoadContent() 
    { 
     // Create a new SpriteBatch, which can be used to draw textures. 

     spriteBatch = new SpriteBatch(GraphicsDevice); 
     model = Content.Load<Model>("cube"); 

     DrawLines(); 

     createCamera(); 

    } 

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

     //update camera 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.Azure); 

     foreach (ModelMesh mesh in model.Meshes) 
     { 
      foreach (BasicEffect fx in mesh.Effects) 
      { 
       fx.View = view; 

       //fx.World = Matrix.CreateRotationX(xTans); 
       fx.World = Matrix.CreateRotationY(yTans); 
       //fx.World = Matrix.CreateRotationZ(zTans); 

       fx.Projection = progection; 
       fx.EnableDefaultLighting(); 

      } 
      mesh.Draw(); 

     } 
     //xTans += 0.01f; 
     yTans += 0.01f; 
     //zTans += 0.01f; 

     BasicEffect effect = new BasicEffect(graphics.GraphicsDevice); 
     effect.VertexColorEnabled = true; 

     effect.View = view; 
     effect.Projection = progection; 

     effect.World = Matrix.Identity; 

     foreach (EffectPass pass in effect.CurrentTechnique.Passes) 
     { 
      // Begin Drawing 
      pass.Apply(); 
      // Set the vertex buffer to graphic device 
      graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer, 0); 
      // Draw the axes with LineList Type 
      graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
      PrimitiveType.LineList, lines, 0, 3); 
      //pass.Apply(); 
     } 
     base.Draw(gameTime); 
    } 

    protected void DrawLines() 
    { 
     lines = new VertexPositionColor[6]; 

     //Setting Size and Width and Color of the X axis 
     lines[0] = new VertexPositionColor(new Vector3(50, 0, 0), Color.Red); 
     lines[1] = new VertexPositionColor(new Vector3(-50, 0, 0), Color.Red); 

     //Setting Size and Width and Color of the Y axis 
     lines[2] = new VertexPositionColor(new Vector3(0, 50, 0), Color.Green); 
     lines[3] = new VertexPositionColor(new Vector3(0, -50, 0), Color.Green); 

     //Setting Size and Width and Color of the Z axis 
     lines[4] = new VertexPositionColor(new Vector3(0, 0, 50), Color.Blue); 
     lines[5] = new VertexPositionColor(new Vector3(0, 0, -50), Color.Blue); 

     vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, 
      VertexPositionColor.VertexDeclaration, 6, BufferUsage.None); 
     vertexBuffer.SetData<VertexPositionColor>(lines); 
    } 

    protected void createCamera() 
    { 
     view = Matrix.CreateLookAt(
      new Vector3(0f, 60.0f, 60f), 
      Vector3.Zero, 
      Vector3.Up); 
     progection = Matrix.CreatePerspectiveFieldOfView(
      MathHelper.ToRadians(8.0f), 
      graphics.GraphicsDevice.Viewport.AspectRatio, 
      10.0f, 
      1000.0f); 
    }   
} //end game class 
} //end namespace 

繪製的一個功能紅雙喜是drawLines()

方法平局()內

我有這個真正繪製軸

BasicEffect effect = new BasicEffect(graphics.GraphicsDevice); 
effect.VertexColorEnabled = true; 

effect.View = view; 
effect.Projection = progection; 

effect.World = Matrix.Identity; 

foreach (EffectPass pass in effect.CurrentTechnique.Passes) 
{ 
    // Begin Drawing 
    pass.Apply(); 
    // Set the vertex buffer to graphic device 
    graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer, 0); 
    // Draw the axes with LineList Type 
    graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
    PrimitiveType.LineList, lines, 0, 3); 
    //pass.Apply(); 
} 

我不知道爲什麼旋轉rendring的速度慢

+0

我標記爲「過於本地化」,但主要是因爲'不屬於此處'不會提供gamedev作爲選項。我意識到它可能並不適合。 – 2013-02-12 06:11:31

回答

1

有好消息,有壞消息。好消息是你的項目可以順利運行。你發佈了足夠的代碼來構建你的項目,只要構建器像我一樣有一個fbx模型。這是所需的更改。

// take this out of your Draw() method. 
BasicEffect effect = new BasicEffect(graphics.GraphicsDevice); 

// put this in your class variables 
BasicEffect effect; 

//and put this at the end of LoadContent() 
effect = new BasicEffect(graphics.GraphicsDevice); 

在帖子末尾的解釋。

壞消息是你有一大堆壞的代碼。它會編譯,並且會運行,但運行不好。我認爲你在編寫XNA項目方面有點新,這意味着你可能對遊戲程序的組件缺乏瞭解。

儘管我會試着解釋一些你錯誤做的事情,但是你的帖子對於SO來說並不是很好。這是一個簡單的不良實踐的結果,而不是技術難題。在gamedev.stackexchange.com之上,這樣的問題有時會被接受,因爲它們是如何(而不是)用特定技術編程的好例子。當我完成回答時,我將標記你的帖子在那裏移動;國防部可能會認爲這是不值得的。

不管怎麼說,這是我最大的努力:

你調用這個函數在您加載內容的方法。

protected override void LoadContent() 
    ... 
    DrawLines(); 
    ... 
} 

爲默認模板的意見告訴你,加載內容的方法是加載內容。不適用於繪圖。它只被調用一次,所以從它畫出一個單一的框架是愚蠢的。但這不完全是你用這種方法所做的。你正在聲明一些原始數據。沒關係,但最後兩行是多餘的:

protected void DrawLines() 
{ 
    lines = new VertexPositionColor[6]; 
    lines[0] = new VertexPositionColor(new Vector3(50, 0, 0), Color.Red); 
    // ... 

    vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, 
     VertexPositionColor.VertexDeclaration, 6, BufferUsage.None); 
    vertexBuffer.SetData<VertexPositionColor>(lines); 
    ... 

甚至不使用該頂點緩衝區。您使用DrawUserPrimitives<T>(),它爲您設置頂點緩衝區。這意味着你也不需要你的繪製方法中的這條線。

graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer, 0); 

您可以在開始效果通過後設置頂點數據。

這是一個壞主意。在開始工作後中斷圖形硬件。這經常成爲遊戲性能的瓶頸。這不是,在你的情況下,但仍然是一個壞主意。

您可以在每一幀創建一個新效果。

正如我上面寫的,這是你需要改變的唯一的東西。儘管它的名字,BasicEffect是一個複雜的OO容器,用於大量可配置着色器片段,以及繪製具有多個紋理和材質和效果的複雜幾何圖形所需的其他所有內容。並且您嘗試每秒分配60次這些對象中的一個。

我的建議是:閱讀更多blog posts,學習更多example code,並學會使用a memory profiler

+0

謝謝soooo seth battin :) :) – 2013-02-12 16:20:51

+0

遊戲完美運行:)謝謝你soo – 2013-02-12 16:21:20