2010-12-15 22 views
0

我正在試圖弄清楚XNA中的3D渲染是如何工作的,它似乎與DirectX非常相似,這使得它非常簡單。XNA/WP7在3D上繪製2D爲我創建了一個奇妙的3D渲染

但是有一種我得到的行爲,我沒有想到用sprite批量繪圖時。

當我只畫了原始的,一個3D立方體,無任何精靈我得到這樣的結果:http://i.stack.imgur.com/DQAHg.png

但是,當我試圖把它畫在2D精靈的頂部以下(從SpriteBatch繪製)時尚:

 SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); 
     base.Draw(gameTime); 
     SpriteBatch.End(); 

     shape.Draw(gameTime); 

這是結果我得到:http://imgur.com/7QZlj

這顯然是錯誤的。我的第一個想法是它使用正射投影渲染(出於某種原因),但我不確定。

這是我用來繪製立方體的代碼,它幾乎是1:1從XNA元樣品(我還沒有開始對遊戲中的適當的三維框架工作),所以它可能是凌亂

  GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; 

      // Create camera matrices, making the object spin. 
      float time = (float)gameTime.TotalGameTime.TotalSeconds; 

      Vector3 cameraPosition = new Vector3(0, 0, 2.5f); 

      float aspect = GraphicsDevice.Viewport.AspectRatio; 

      float yaw = 3.05f; 
      float pitch = 5.34f; 
      float roll = 8.396f; 

      Matrix world = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll); 
      Matrix view = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up); 
      Matrix projection = Matrix.CreatePerspectiveFieldOfView(1, aspect, 1, 10); 

      // currentPrimitive.Draw(world, view, projection, Color.Red); 

      // Reset the fill mode renderstate. 
      GraphicsDevice.RasterizerState = RasterizerState.CullClockwise; 


      Color color = Color.Red; 

      // Set BasicEffect parameters. 
      basicEffect.World = world; 
      basicEffect.View = view; 
      basicEffect.Projection = projection; 
      basicEffect.DiffuseColor = color.ToVector3(); 
      basicEffect.Alpha = 1.0f;// color.A/255.0f; 

      GraphicsDevice device = basicEffect.GraphicsDevice; 
      device.DepthStencilState = DepthStencilState.Default; 


      GraphicsDevice.RasterizerState = RasterizerState.CullClockwise; 

      GraphicsDevice graphicsDevice = basicEffect.GraphicsDevice; 

      // Set our vertex declaration, vertex buffer, and index buffer. 
      graphicsDevice.SetVertexBuffer(vertexBuffer); 

      graphicsDevice.Indices = indexBuffer; 


      foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes) 
      { 
       effectPass.Apply(); 

       int primitiveCount = indices.Count/3; 

       graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 
                vertices.Count, 0, primitiveCount); 

      } 

我也應該澄清,這不是spritebatch.begin/spritebatch.end的結果,如果我剛剛開始/結束並且沒有繪製任何東西,最終結果是好的,但如果我繪製字體或精靈,它會打破立方體。

任何人有任何想法?

回答

1

我的建議是在您執行SpriteBatch.End()方法調用後檢查GraphicsDevice設置。當你使用SpriteBatch它確實會改變GraphicsDevice中的一些渲染狀態。可能值得在SpriteBatch.Begin()之前存儲GraphicsDevice的值,然後在SpriteBatch.End()方法調用之後重置它們。

+0

這解決了這個問題,它改變了淘汰的順序。 – tweetypi 2010-12-15 09:20:28

+1

我有一個類似的問題(http://project-vanquish.co.cc),造成了很多挫折。 – 2010-12-15 11:10:42