开发者

Texture2D: SetData doesn't work

I'd like to have a sort of floor with a programmaically created texture on it. I already created the vertices and the indexes needed for the work:

开发者_开发技巧
private VertexPositionNormalTexture[] verticiBase;
private short[] indici;

...

verticiBase = new VertexPositionNormalTexture[4];
verticiBase[0] = new VertexPositionNormalTexture(new Vector3(0.0f, 0.0f, 0.0f), Vector3.Up, new Vector2(0, 0));
verticiBase[1] = new VertexPositionNormalTexture(new Vector3(dimensioneVera.X, 0.0f, 0.0f), Vector3.Up, new Vector2(1, 0));
verticiBase[2] = new VertexPositionNormalTexture(new Vector3(0.0f, 0.0f, dimensioneVera.Y), Vector3.Up, new Vector2(0, 1));
verticiBase[3] = new VertexPositionNormalTexture(new Vector3(dimensioneVera.X, 0.0f, dimensioneVera.Y), Vector3.Up, new Vector2(1, 1));

graphics.GraphicsDevice.VertexDeclaration = new 
    VertexDeclaration(graphics.GraphicsDevice,
    VertexPositionNormalTexture.VertexElements);

indici = new short[6];
indici[0] = 0;
indici[1] = 1;
indici[2] = 2;
indici[3] = 1;
indici[4] = 3;
indici[5] = 2;

Then I created the texture and the data which I'd like to show:

private Texture2D texture;
private Color[] textureData;

...

texture = new Texture2D(Game.GraphicsDevice, (int)dimensioneVera.X, (int)dimensioneVera.Y);
textureData = new Color[(int)dimensioneVera.X * (int)dimensioneVera.Y];

for (int x = 0; x < textureData.Length; x++)
    textureData[x] = Color.Red;

texture.SetData(textureData);

And this is the code I used to draw:

private BasicEffect effetti;

...

effetti = new BasicEffect(graphics.GraphicsDevice, null);

...

public override void Draw(GameTime gameTime)
{
    effetti.World = Matrix.Identity;
    effetti.View = camera.view;
    effetti.Projection = camera.projection;

    effetti.TextureEnabled = true;
    effetti.Texture = texture;

    effetti.Begin();
    effetti.EnableDefaultLighting();

    effetti.CurrentTechnique.Passes[0].Begin();
    graphics.GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList,
        verticiBase, 0, verticiBase.Length, indici, 0, indici.Length / 3);

    effetti.CurrentTechnique.Passes[0].End();

    effetti.End();

    base.Draw(gameTime);
}

The floor is displayed but the texture is all black. What's wrong?

Thanks for your time.


I suspect the problem is not that you've created the texture incorrectly. The problem is your lighting; you've called EnableDefaultLighting() but not provided any lights, so everything is completely dark (black). Try setting effetti.AmbientLightColor = Color.White and see if that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜