This applicationfor this tutorial is here. In this tutorial we try to apply a texture (image file) to some drawing pimitives.
Texture2d texture;
//...
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
texture = Content.Load("charlize_sml");
// TODO: use this.Content to load your game content here
}
VertexPositionTexture[] vertices = new VertexPositionTexture[4];
VertexDeclaration vertexDeclaration;
\\...
protected override void Initialize()
{
// TODO: Add your initialization logic here
vertexDeclaration =
new VertexDeclaration(graphics.GraphicsDevice,
VertexPositionTexture.VertexElements);
// create our quad
vertices[0].Position = new Vector3(0, 100, 0);
vertices[0].TextureCoordinate = new Vector2(0, 1);
vertices[1].Position = new Vector3(0, 0, 0);
vertices[1].TextureCoordinate = new Vector2(0, 0);
vertices[2].Position = new Vector3(100, 100, 0);
vertices[2].TextureCoordinate = new Vector2(1, 1);
vertices[3].Position = new Vector3(100, 0, 0);
vertices[3].TextureCoordinate = new Vector2(1, 0);
base.Initialize();
}
basicEffect.TextureEnabled = true;
basicEffect.Texture = texture;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace TexturedQuad
{
///
/// This is the main type for your game
///
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
VertexPositionTexture[] vertices = new VertexPositionTexture[4];
VertexDeclaration vertexDeclaration;
BasicEffect basicEffect;
Texture2D texture;
float angle;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
///
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
///
protected override void Initialize()
{
// TODO: Add your initialization logic here
basicEffect = new BasicEffect(graphics.GraphicsDevice, null);
basicEffect.View = Matrix.CreateLookAt(new Vector3(0, 0, 500), new Vector3(0, 0, 0), Vector3.Up);
basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView((float)Math.PI / 4.0f,
(float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height,
0.1f,
100000);
// create a vertex declaration, which tells the graphics card what kind of
// data to expect during a draw call. We're drawing using
// VertexPositionColors, so we'll use those vertex elements.
vertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionTexture.VertexElements);
// create our quad
//give each vertex texture coordinates
vertices[0].Position = new Vector3(0, 100, 0);
vertices[0].TextureCoordinate = new Vector2(0, 1);
vertices[1].Position = new Vector3(0, 0, 0);
vertices[1].TextureCoordinate = new Vector2(0, 0);
vertices[2].Position = new Vector3(100, 100, 0);
vertices[2].TextureCoordinate = new Vector2(1, 1);
vertices[3].Position = new Vector3(100, 0, 0);
vertices[3].TextureCoordinate = new Vector2(1, 0);
base.Initialize();
}
///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
texture = Content.Load("charlize_sml");
// TODO: use this.Content to load your game content here
}
///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///
/// Provides a snapshot of timing values.
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
///
/// This is called when the game should draw itself.
///
/// Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
graphics.GraphicsDevice.RenderState.CullMode = CullMode.None;
// TODO: Add your drawing code here
graphics.GraphicsDevice.VertexDeclaration = vertexDeclaration;
angle += 0.01f;
basicEffect.TextureEnabled = true;
basicEffect.Texture = texture;
basicEffect.World = Matrix.CreateRotationZ(angle) * Matrix.CreateRotationY(angle * 2);
basicEffect.Begin();
basicEffect.CurrentTechnique.Passes[0].Begin();
graphics.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, vertices, 0, 2);
// tell basic effect that we're done.
basicEffect.CurrentTechnique.Passes[0].End();
basicEffect.End();
base.Draw(gameTime);
}
}
}