Texture2D: SetData не работает - PullRequest
       35

Texture2D: SetData не работает

0 голосов
/ 03 сентября 2011

Я хотел бы иметь своего рода пол с программно созданной текстурой. Я уже создал вершины и индексы, необходимые для работы:

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

Пол отображается, но текстура черная. В чем дело?

Спасибо за ваше время.

1 Ответ

1 голос
/ 03 сентября 2011

Я подозреваю, что проблема не в том, что вы создали текстуру неправильно.Проблема в вашем освещении;Вы позвонили EnableDefaultLighting(), но не предоставили никаких источников света, поэтому все абсолютно темно (черное).Попробуйте установить effetti.AmbientLightColor = Color.White и посмотрите, поможет ли это.

...