【问题标题】:Drawing a textured quad using XNA使用 XNA 绘制带纹理的四边形
【发布时间】:2009-07-11 01:26:12
【问题描述】:

我正在尝试使用位于here 的示例来渲染带纹理的四边形。

我可以成功渲染四边形,但纹理信息似乎丢失了。不过,四边形采用底层纹理的颜色。

我检查了明显的问题(“渲染四边形的 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();
}

【问题讨论】:

    标签: c# xna


    【解决方案1】:

    据我所知,这应该可行。我能想象的唯一一件事,不在此代码中,是纹理的加载在某处出错。我也无法完全想象你的意思是四边形具有纹理的底层颜色?你有截图给我们吗?

    另外,如果确实出现了某些东西,例如纹理的非常扭曲的版本,other 的渲染可能会影响四边形的渲染。例如,如果您在图形设备上有另一个顶点声明时绘制四边形,或者如果之前渲染的东西设置了一些特殊的渲染状态,或者如果您在其他东西的绘图代码中绘制四边形。尝试将此代码隔离到一个新的项目或其他东西中,或者禁用其他所有内容的渲染。

    【讨论】:

    • 前一件事确实设置了一个奇异的渲染状态。上面的代码或多或少是正确的,但我没有提到我将 BasicEffect 与 SpriteBatch 结合使用,SpriteBatch 会改变渲染状态。我错误地认为 SpriteBatch 没有副作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    相关资源
    最近更新 更多