2011-04-30 21 views
1

image of the problem混亂的三角形使用VertexPositionColorTexture與BasicEffect

當我用微軟的BasicEffect教程here和代碼示例這裏:go.microsoft.com/fwlink/?LinkId=198921並得到了一切工作正常。接下來,我改變了一切使用vertexPositionNormalTexture,添加了一些小方法來幫助紋理,並能夠渲染紋理立方體。我也讓立方體旋轉了一下。接下來,我想嘗試使用vertexPositionNormalTexture。不幸的是,我得到這個圖像,而不是一個立方體。這是我的一些代碼,其中包含重大修改。

Draw方法

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

      RasterizerState rasterizerState1 = new RasterizerState(); 
      //backface culling 
      rasterizerState1.CullMode = CullMode.None; 
      //turn off texture blurring 
      graphics.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp; 


      graphics.GraphicsDevice.RasterizerState = rasterizerState1; 
      foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) 
      { 
       pass.Apply(); 
       graphics.GraphicsDevice.DrawPrimitives(
        PrimitiveType.TriangleList, 
        0, 
        12 
       ); 
      } 

      base.Draw(gameTime); 
     } 

,設置了頂點

private void InitializeCube() 
    { 

     Vector3 topLeftFront = new Vector3(-1.0f, 1.0f, 1.0f); 
     Vector3 bottomLeftFront = new Vector3(-1.0f, -1.0f, 1.0f); 
     Vector3 topRightFront = new Vector3(1.0f, 1.0f, 1.0f); 
     Vector3 bottomRightFront = new Vector3(1.0f, -1.0f, 1.0f); 
     Vector3 topLeftBack = new Vector3(-1.0f, 1.0f, -1.0f); 
     Vector3 topRightBack = new Vector3(1.0f, 1.0f, -1.0f); 
     Vector3 bottomLeftBack = new Vector3(-1.0f, -1.0f, -1.0f); 
     Vector3 bottomRightBack = new Vector3(1.0f, -1.0f, -1.0f); 

     Vector2 textureTopLeft = new Vector2(0.0f, 0.0f); 
     Vector2 textureTopRight = new Vector2(.25f, 0.0f); 
     Vector2 textureBottomLeft = new Vector2(0.0f, .25f); 
     Vector2 textureBottomRight = new Vector2(.25f, .25f); 

     Color frontColor = new Color(255, 255, 255); 
     Color backColor = new Color(255, 0, 0); 
     Color topColor = new Color(0, 255, 0); 
     Color bottomColor = new Color(0, 0, 255); 
     Color leftColor = new Color(0, 255, 255); 
     Color rightColor = new Color(0, 0, 0); 

     // Front face. 
     cubeVertices[0] = 
      new VertexPositionColorTexture(
      topLeftFront, frontColor, GetTexPos(2)); 
     cubeVertices[1] = 
      new VertexPositionColorTexture(
      bottomLeftFront, frontColor, GetTexPos(2) + textureBottomLeft); 
     cubeVertices[2] = 
      new VertexPositionColorTexture(
      topRightFront, frontColor, GetTexPos(2) + textureTopRight); 
     cubeVertices[3] = 
      new VertexPositionColorTexture(
      bottomLeftFront, frontColor, GetTexPos(2) + textureBottomLeft); 
     cubeVertices[4] = 
      new VertexPositionColorTexture(
      bottomRightFront, frontColor, GetTexPos(2) + textureBottomRight); 
     cubeVertices[5] = 
      new VertexPositionColorTexture(
      topRightFront, frontColor, GetTexPos(2) + textureTopRight); 

初始化basicEffect

private void InitializeEffect() 
    { 
     basicEffect = new BasicEffect(graphics.GraphicsDevice); 


     basicEffect.World = worldMatrix; 
     basicEffect.View = viewMatrix; 
     basicEffect.Projection = projectionMatrix; 

     //basicEffect.EnableDefaultLighting 
    } 

LoadContent

protected override void LoadContent() 
    { 
     canyonTexture = Content.Load<Texture2D>("CanyonTexture"); 
     textureSheetWidth = canyonTexture.Width/16; 
     InitializeTransform(); 
     InitializeEffect(); 
     basicEffect.TextureEnabled = true; 
     basicEffect.VertexColorEnabled = true; 
     basicEffect.Texture = canyonTexture; 

     InitializeCube(); 

    } 
方法的一部分210

建立VertexBuffer

private void CreateVertexBuffer() 
    { 
     vertexDeclaration = new VertexDeclaration(new VertexElement[] 
      { 
       new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0), 
       new VertexElement(12, VertexElementFormat.Color, VertexElementUsage.Color, 0), 
       new VertexElement(24, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0) 
      }); 

     vertexBuffer = new VertexBuffer(
      graphics.GraphicsDevice, 
      vertexDeclaration, 
      number_of_vertices, 
      BufferUsage.None 
      ); 


     cubeVertices = new VertexPositionColorTexture[number_of_vertices]; 
     InitializeCube(); 


     vertexBuffer.SetData<VertexPositionColorTexture>(cubeVertices); 

     graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer); 
    } 


    protected override void Initialize() 
    { 
     // TODO: Add your initialization logic here 
     CreateVertexBuffer(); 

     base.Initialize(); 
    } 
+0

嗯這個問題會更好http://gamedev.stackexchange.com/ imho。 – 2011-04-30 21:18:34

+0

謝謝,如果我沒有得到解決方案,我可能會在一天左右發佈它。 – Mark 2011-04-30 21:20:55

+0

是的,如果你沒有得到答案真的值得一看,我總是會在那裏得到我的XNA問題的答案。 – 2011-04-30 21:22:53

回答

1

基本上你的頂點聲明是錯誤的。

A Color只有四個字節寬。因此,後面的紋理座標元素的偏移應該是16,而不是24.

但是,您甚至不需要在XNA 4.0中爲此創建頂點聲明。只需將VertexPositionColorTexture.VertexDeclarationtypeof(VertexPositionColorTexture)傳遞給您的VertexBuffer的構造函數即可。

有一個blog post here解釋了這一切如何工作。

+0

謝謝,它現在完美!我懷疑頂點聲明有什麼問題,但我無法弄清楚什麼。看起來每當我遇到問題時總會有一些內置的XNA功能讓所有的事情都變得更容易。問題實際上意識到它在那裏! – Mark 2011-05-01 01:55:43

+0

@Mark不客氣:)不要忘記接受答案:) – 2011-05-01 03:16:50

+0

@Mark它不會贊成它太... – 2014-01-16 23:27:56