I'm drawing a hexagon using GraphicsDevice.DrawIndexedPrimitives and a VertexPositionTexture array. I can see the intended result flash on the screen when I first run the game, but my hexagon immediately becomes white.
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private VertexPositionTexture[] _vertexPositionTexture;
private short[] _indices;
private VertexBuffer vertexBuffer;
private IndexBuffer indexBuffer;
private BasicEffect _basicEffect;
Texture2D _texture;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
_texture = this.Content.Load<Texture2D>("resources/grad");
_vertexPositionTexture = new[]
{
new VertexPositionTexture(new Vector3(320, 300, 0), new Vector2(1, .5f)),
new VertexPositionTexture(new Vector3(310, 283, 0), new Vector2(.75f, 1)),
new VertexPositionTexture(new Vector3(290, 283, 0), new Vector2(.25f, 1)),
new VertexPositionTexture(new Vector3(280, 300, 0), new Vector2(0, .5f)),
new VertexPositionTexture(new Vector3(290, 317, 0), new Vector2(.25f, 0)),
new VertexPositionTexture(new Vector3(310, 317, 0), new Vector2(.75f, 0))
};
_indices = new short[]
{
1,0,2,
2,0,3,
3,0,4,
4,0,5
};
vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), 6, BufferUsage.WriteOnly);
vertexBuffer.SetData(_vertexPositionTexture);
indexBuffer = new IndexBuffer(GraphicsDevice, typeof(short), _indices.Length, BufferUsage.WriteOnly);
indexBuffer.SetData(_indices);
_basicEffect = new BasicEffect(GraphicsDevice)
{
World = Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1),
Texture = _texture,
TextureEnabled = true
};
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Transparent);
GraphicsDevice.SetVertexBuffer(vertexBuffer);
GraphicsDevice.Indices = indexBuffer;
EffectTechnique et = _basicEffect.Techniques[0];
EffectPassCollection epc = et.Passes;
foreach (EffectPass p in epc)
{
p.Apply();
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4);
}
_spriteBatch.Begin();
_spriteBatch.Draw(_texture, new Rectangle(new Point(350), new Point(50)), Color.White);
_spriteBatch.End();
base.Draw(gameTime);
}
}
But I can see the hexagon with the intended colors for just a moment when the game starts.
In your Draw() code, instead of
EffectTechnique et = _basicEffect.Techniques[0];
use
EffectTechnique et = _basicEffect.CurrentTechnique;
After calling p.Apply() the current technique is not on index 0 of Techniques[] anymore. Why? I'm not sure either.