Texture2D:SetData 不起作用

发布于 2024-12-02 12:02:16 字数 2125 浏览 2 评论 0原文

我想要一种带有以编程方式创建的纹理的地板。我已经创建了工作所需的顶点和索引:

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;

然后我创建了我想要显示的纹理和数据:

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);

这是我用来绘制的代码:

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);
}

显示了地板,但纹理全是黑色。怎么了?

感谢您抽出时间。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不回头走下去 2024-12-09 12:02:16

我怀疑问题不在于您错误地创建了纹理。问题在于你的照明;您已调用 EnableDefaultLighting() 但未提供任何灯光,因此一切都是完全黑暗的(黑色)。尝试设置 effetti.AmbientLightColor = Color.White 看看是否有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文