2009-07-11 128 views
3

我正在嘗試使用位於here的示例渲染質感四邊形。使用XNA繪製質感四邊形

我可以成功渲染四邊形,但紋理信息似乎已丟失。儘管如此,四邊形還是會獲得底層紋理的顏色。

我已經檢查了顯而易見的問題(「BasicEffect呈現四邊形是否將TextureEnabled屬性設置爲true?」),並且無法立即看到問題。下面

代碼:

public class Quad 
{ 
    public VertexPositionNormalTexture[] Vertices; 
    public Vector3 Origin; 
    public Vector3 Up; 
    public Vector3 Normal; 
    public Vector3 Left; 
    public Vector3 UpperLeft; 
    public Vector3 UpperRight; 
    public Vector3 LowerLeft; 
    public Vector3 LowerRight; 
    public int[] Indexes; 


    public Quad(Vector3 origin, Vector3 normal, Vector3 up, 
     float width, float height) 
    { 
     this.Vertices = new VertexPositionNormalTexture[4]; 
     this.Indexes = new int[6]; 
     this.Origin = origin; 
     this.Normal = normal; 
     this.Up = up; 

     // Calculate the quad corners 
     this.Left = Vector3.Cross(normal, this.Up); 
     Vector3 uppercenter = (this.Up * height/2) + origin; 
     this.UpperLeft = uppercenter + (this.Left * width/2); 
     this.UpperRight = uppercenter - (this.Left * width/2); 
     this.LowerLeft = this.UpperLeft - (this.Up * height); 
     this.LowerRight = this.UpperRight - (this.Up * height); 

     this.FillVertices(); 
    } 

    private void FillVertices() 
    { 
     Vector2 textureUpperLeft = new Vector2(0.0f, 0.0f); 
     Vector2 textureUpperRight = new Vector2(1.0f, 0.0f); 
     Vector2 textureLowerLeft = new Vector2(0.0f, 1.0f); 
     Vector2 textureLowerRight = new Vector2(1.0f, 1.0f); 

     for (int i = 0; i < this.Vertices.Length; i++) 
     { 
      this.Vertices[i].Normal = this.Normal; 
     } 

     this.Vertices[0].Position = this.LowerLeft; 
     this.Vertices[0].TextureCoordinate = textureLowerLeft; 
     this.Vertices[1].Position = this.UpperLeft; 
     this.Vertices[1].TextureCoordinate = textureUpperLeft; 
     this.Vertices[2].Position = this.LowerRight; 
     this.Vertices[2].TextureCoordinate = textureLowerRight; 
     this.Vertices[3].Position = this.UpperRight; 
     this.Vertices[3].TextureCoordinate = textureUpperRight; 

     this.Indexes[0] = 0; 
     this.Indexes[1] = 1; 
     this.Indexes[2] = 2; 
     this.Indexes[3] = 2; 
     this.Indexes[4] = 1; 
     this.Indexes[5] = 3; 
    } 
} 

this.quadEffect = new BasicEffect(this.GraphicsDevice, null); 
this.quadEffect.AmbientLightColor = new Vector3(0.8f, 0.8f, 0.8f); 
this.quadEffect.LightingEnabled = true; 
this.quadEffect.World = Matrix.Identity; 
this.quadEffect.View = this.View; 
this.quadEffect.Projection = this.Projection; 
this.quadEffect.TextureEnabled = true; 
this.quadEffect.Texture = someTexture; 

this.quad = new Quad(Vector3.Zero, Vector3.UnitZ, Vector3.Up, 2, 2); 

this.quadVertexDecl = new VertexDeclaration(this.GraphicsDevice, VertexPositionNormalTexture.VertexElements); 

public override void Draw(GameTime gameTime) 
{ 
    this.GraphicsDevice.Textures[0] = this.SpriteDictionary["B1S1I800"]; 
    this.GraphicsDevice.VertexDeclaration = quadVertexDecl; 
    quadEffect.Begin(); 

    foreach (EffectPass pass in quadEffect.CurrentTechnique.Passes) 
    { 
     pass.Begin(); 
     GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(
      PrimitiveType.TriangleList, 
      beamQuad.Vertices, 0, 4, 
      beamQuad.Indexes, 0, 2); 

     pass.End(); 
    } 

    quadEffect.End(); 
} 

回答

1

從我所看到的,這應該工作。我可以想象的唯一的事情是,這個代碼中沒有的地方是,紋理的加載出錯了。我也無法完全想象你的意思是四邊形是否具有紋理的底色?你有我們的截圖嗎?另外,如果出現某種情況,例如紋理的扭曲版本,則可能會渲染其他材質對四邊形的渲染有影響。例如,如果您繪製四邊形,而圖形設備上有另一個頂點聲明,或者前面的事情設置了一些異國情調的渲染狀態,或者您正在其他繪圖代碼中繪製四邊形。嘗試將此代碼隔離到新項目中,或禁用其他所有內容的渲染。

+0

以前的事情確實確定了異國情調的渲染狀態。上面的代碼或多或少是正確的,但我忽略了提到我正在將BasicEffect與SpriteBatch結合使用,這會改變渲染狀態。我錯誤地認爲SpriteBatch是無副作用的。 – 2010-10-06 18:34:47