using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Game1
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Effect effect;
Matrix world;
Matrix view;
Matrix projection;
IndexBuffer indexBuffer;
VertexBuffer vertexBuffer;
VertexPositionColorTexture[] vertices;
int[] indices;
Texture2D texture;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
effect = Content.Load<Effect>("File");
texture = Content.Load<Texture2D>("texture");
vertexBuffer = new VertexBuffer(GraphicsDevice, VertexPositionColorTexture.VertexDeclaration, 4, BufferUsage.WriteOnly);
indexBuffer = new IndexBuffer(GraphicsDevice, IndexElementSize.ThirtyTwoBits, 6, BufferUsage.WriteOnly);
vertices = new VertexPositionColorTexture[4];
indices = new int[6];
vertices[0] = new VertexPositionColorTexture(new Vector3(-5, 5, -1.2f), Color.Red, new Vector2(0, 0));
vertices[1] = new VertexPositionColorTexture(new Vector3(5, 5, -1.2f), Color.Green, new Vector2(1, 0));
vertices[2] = new VertexPositionColorTexture(new Vector3(5, -5, -1.2f),Color.Green, new Vector2(1, 1));
vertices[3] = new VertexPositionColorTexture(new Vector3(-5, -5, -1.2f), Color.Red, new Vector2(0, 1));
indices[0] = 0;
indices[1] = 3;
indices[2] = 2;
indices[3] = 2;
indices[4] = 1;
indices[5] = 0;
vertexBuffer.SetData(vertices);
indexBuffer.SetData(indices);
world = Matrix.Identity;
view = Matrix.CreateLookAt(Vector3.Zero, Vector3.Forward, Vector3.Up);
projection = Matrix.CreatePerspective(10.0f * GraphicsDevice.Viewport.AspectRatio, 10.0f, 1.0f, 1000.0f);
}
protected override void UnloadContent()
{
}
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.CornflowerBlue);
GraphicsDevice.RasterizerState = RasterizerState.CullNone;
GraphicsDevice.Indices = indexBuffer;
GraphicsDevice.SetVertexBuffer(vertexBuffer);
effect.Parameters["World"].SetValue(world);
effect.Parameters["View"].SetValue(view);
effect.Parameters["Projection"].SetValue(projection);
effect.Parameters["Texture"].SetValue(texture);
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indices.Length / 3);
}
base.Draw(gameTime);
}
}
}
#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1
#endif
float4x4 World;
float4x4 View;
float4x4 Projection;
texture Texture;
sampler TextureSampler = sampler_state
{
texture = <Texture>;
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
AddressU = mirror;
AddressV = mirror;
};
struct VertexToPixel
{
float4 Position : POSITION;
float4 Color : COLOR0;
float2 TexCoords : TEXCOORD0;
};
struct PixelToFrame
{
float4 Color : COLOR0;
};
VertexToPixel SimplestVertexShader(float4 inPos : POSITION, float4 color : COLOR, float2 inTexCoords : TEXCOORD0)
{
VertexToPixel Output = (VertexToPixel) 0;
float4 worldPosition = mul(inPos, World);
float4 viewPosition = mul(worldPosition, View);
Output.Position = mul(viewPosition, Projection);
Output.Color = color;
Output.TexCoords = inTexCoords;
return Output;
}
PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)
{
PixelToFrame Output = (PixelToFrame) 0;
Output.Color = PSIn.Color * tex2D(TextureSampler, PSIn.TexCoords);
return Output;
}
technique Default
{
pass Pass0
{
VertexShader = compile VS_SHADERMODEL SimplestVertexShader();
PixelShader = compile PS_SHADERMODEL OurFirstPixelShader();
}
}