2011-11-30 16:03:51 +00:00
#region Using Statements
using System ;
using System.Collections.Generic ;
using System.Linq ;
2011-12-27 08:19:27 +00:00
using ANX.Framework ;
using ANX.Framework.Content ;
using ANX.Framework.GamerServices ;
using ANX.Framework.Graphics ;
using ANX.Framework.Input ;
using ANX.Framework.Media ;
2011-11-30 16:03:51 +00:00
#endregion // Using Statements
2012-08-09 09:45:04 +00:00
// This file is part of the ANX.Framework created by the
// "ANX.Framework developer group" and released under the Ms-PL license.
// For details see: http://anxframework.codeplex.com/license
2012-01-30 16:17:27 +00:00
2011-11-30 16:03:51 +00:00
namespace Primitives
{
2011-12-27 08:19:27 +00:00
public class Game1 : ANX . Framework . Game
2011-11-30 16:03:51 +00:00
{
GraphicsDeviceManager graphics ;
SpriteBatch spriteBatch ;
SpriteFont font ;
Texture2D bgTexture ;
2011-12-05 14:44:39 +00:00
BasicEffect basicEffect ;
2011-12-16 09:21:39 +00:00
Effect hardwareInstanceEffect ;
2011-12-05 14:44:39 +00:00
Matrix viewMatrix ;
2011-12-27 08:19:27 +00:00
Matrix instancedViewMatrix ;
2011-12-05 14:44:39 +00:00
Matrix projectionMatrix ;
2011-12-16 09:21:39 +00:00
Matrix instancedProjectionMatrix ;
2011-12-05 14:44:39 +00:00
Matrix worldMatrix ;
2011-12-16 09:21:39 +00:00
float rotation = 0.0f ;
DynamicVertexBuffer instanceVertexBuffer ;
2011-12-05 14:44:39 +00:00
VertexBuffer cubeNoIndicesBuffer ;
2011-12-16 09:21:39 +00:00
VertexBuffer cubeVertexBuffer ;
IndexBuffer cubeIndexBuffer ;
2011-12-05 14:44:39 +00:00
2012-09-20 10:23:03 +00:00
bool [ ] enabled = new bool [ ] { true , true , true , true , true } ;
KeyboardState lastKeyState ;
2011-12-05 14:44:39 +00:00
#region Corners of cube
static Vector3 topLeftFront = new Vector3 ( - 1.0f , 1.0f , 1.0f ) ;
static Vector3 bottomLeftFront = new Vector3 ( - 1.0f , - 1.0f , 1.0f ) ;
static Vector3 topRightFront = new Vector3 ( 1.0f , 1.0f , 1.0f ) ;
static Vector3 bottomRightFront = new Vector3 ( 1.0f , - 1.0f , 1.0f ) ;
static Vector3 topLeftBack = new Vector3 ( - 1.0f , 1.0f , - 1.0f ) ;
static Vector3 topRightBack = new Vector3 ( 1.0f , 1.0f , - 1.0f ) ;
static Vector3 bottomLeftBack = new Vector3 ( - 1.0f , - 1.0f , - 1.0f ) ;
static Vector3 bottomRightBack = new Vector3 ( 1.0f , - 1.0f , - 1.0f ) ;
#endregion
2011-12-27 06:03:48 +00:00
Matrix [ ] instanceTransformMatrices = new Matrix [ ] { Matrix . CreateTranslation ( - 10.0f , 0.0f , 0.0f ) ,
Matrix . CreateTranslation ( - 5.0f , 0.0f , 0.0f ) ,
Matrix . CreateTranslation ( 0.0f , 0.0f , 0.0f ) ,
2011-12-16 09:21:39 +00:00
Matrix . CreateTranslation ( 5.0f , 0.0f , 0.0f ) ,
Matrix . CreateTranslation ( 10.0f , 0.0f , 0.0f ) ,
} ;
VertexDeclaration instanceDecl = new VertexDeclaration
(
new VertexElement ( 0 , VertexElementFormat . Vector4 , VertexElementUsage . BlendWeight , 0 ) ,
new VertexElement ( 16 , VertexElementFormat . Vector4 , VertexElementUsage . BlendWeight , 1 ) ,
new VertexElement ( 32 , VertexElementFormat . Vector4 , VertexElementUsage . BlendWeight , 2 ) ,
new VertexElement ( 48 , VertexElementFormat . Vector4 , VertexElementUsage . BlendWeight , 3 )
) ;
VertexPositionColor [ ] cubeVertices = new VertexPositionColor [ ] { new VertexPositionColor ( topLeftFront , Color . White ) ,
new VertexPositionColor ( bottomLeftFront , Color . White ) ,
new VertexPositionColor ( topRightFront , Color . White ) ,
new VertexPositionColor ( bottomRightFront , Color . White ) ,
new VertexPositionColor ( topLeftBack , Color . White ) ,
new VertexPositionColor ( topRightBack , Color . White ) ,
new VertexPositionColor ( bottomLeftBack , Color . White ) ,
new VertexPositionColor ( bottomRightBack , Color . White )
} ;
short [ ] cubeIndices = new short [ ] { 0 , 3 , 1 ,
0 , 2 , 3 ,
4 , 6 , 7 ,
4 , 7 , 5 ,
0 , 4 , 2 ,
2 , 4 , 5 ,
1 , 3 , 6 ,
3 , 7 , 6 ,
0 , 1 , 6 ,
6 , 4 , 0 ,
2 , 7 , 3 ,
7 , 2 , 5 } ;
VertexPositionColor [ ] cubeNoIndices = new VertexPositionColor [ ] { new VertexPositionColor ( topLeftFront , Color . White ) , // 0
new VertexPositionColor ( bottomRightFront , Color . White ) , // 3
new VertexPositionColor ( bottomLeftFront , Color . White ) , // 1
new VertexPositionColor ( topLeftFront , Color . White ) , // 0
new VertexPositionColor ( topRightFront , Color . White ) , // 2
new VertexPositionColor ( bottomRightFront , Color . White ) , // 3
new VertexPositionColor ( topLeftBack , Color . White ) , // 4
new VertexPositionColor ( bottomLeftBack , Color . White ) , // 6
new VertexPositionColor ( bottomRightBack , Color . White ) , // 7
new VertexPositionColor ( topLeftBack , Color . White ) , // 4
new VertexPositionColor ( bottomRightBack , Color . White ) , // 7
new VertexPositionColor ( topRightBack , Color . White ) , // 5
new VertexPositionColor ( topLeftFront , Color . White ) , // 0
new VertexPositionColor ( topLeftBack , Color . White ) , // 4
new VertexPositionColor ( topRightFront , Color . White ) , // 2
new VertexPositionColor ( topRightFront , Color . White ) , // 2
new VertexPositionColor ( topLeftBack , Color . White ) , // 4
new VertexPositionColor ( topRightBack , Color . White ) , // 5
new VertexPositionColor ( bottomLeftFront , Color . White ) , // 1
new VertexPositionColor ( bottomRightFront , Color . White ) , // 3
new VertexPositionColor ( bottomLeftBack , Color . White ) , // 6
new VertexPositionColor ( bottomRightFront , Color . White ) , // 3
new VertexPositionColor ( bottomRightBack , Color . White ) , // 7
new VertexPositionColor ( bottomLeftBack , Color . White ) , // 6
new VertexPositionColor ( topLeftFront , Color . White ) , // 0
new VertexPositionColor ( bottomLeftFront , Color . White ) , // 1
new VertexPositionColor ( bottomLeftBack , Color . White ) , // 6
new VertexPositionColor ( bottomLeftBack , Color . White ) , // 6
new VertexPositionColor ( topLeftBack , Color . White ) , // 4
new VertexPositionColor ( topLeftFront , Color . White ) , // 0
new VertexPositionColor ( topRightFront , Color . White ) , // 2
new VertexPositionColor ( bottomRightBack , Color . White ) , // 7
new VertexPositionColor ( bottomRightFront , Color . White ) , // 3
new VertexPositionColor ( bottomRightBack , Color . White ) , // 7
new VertexPositionColor ( topRightFront , Color . White ) , // 2
new VertexPositionColor ( topRightBack , Color . White ) , // 5
2011-12-05 14:44:39 +00:00
} ;
2011-11-30 16:03:51 +00:00
public Game1 ( )
{
graphics = new GraphicsDeviceManager ( this ) ;
Content . RootDirectory = "SampleContent" ;
2011-12-01 11:20:49 +00:00
graphics . PreparingDeviceSettings + = new EventHandler < PreparingDeviceSettingsEventArgs > ( graphics_PreparingDeviceSettings ) ;
2011-11-30 16:03:51 +00:00
}
2011-12-01 11:20:49 +00:00
void graphics_PreparingDeviceSettings ( object sender , PreparingDeviceSettingsEventArgs e )
2011-11-30 16:03:51 +00:00
{
2011-12-01 11:20:49 +00:00
e . GraphicsDeviceInformation . PresentationParameters . BackBufferWidth = 600 ;
e . GraphicsDeviceInformation . PresentationParameters . BackBufferHeight = 600 ;
}
2011-11-30 16:03:51 +00:00
2011-12-01 11:20:49 +00:00
protected override void Initialize ( )
{
2011-11-30 16:03:51 +00:00
base . Initialize ( ) ;
2012-09-20 10:23:03 +00:00
lastKeyState = Keyboard . GetState ( ) ;
2011-11-30 16:03:51 +00:00
}
protected override void LoadContent ( )
{
spriteBatch = new SpriteBatch ( GraphicsDevice ) ;
2011-12-05 14:44:39 +00:00
this . basicEffect = new BasicEffect ( GraphicsDevice ) ;
2011-12-16 09:21:39 +00:00
this . hardwareInstanceEffect = Content . Load < Effect > ( @"Effects/HardwareInstancing" ) ;
2011-12-05 14:44:39 +00:00
this . worldMatrix = Matrix . Identity ;
2011-12-16 09:21:39 +00:00
this . projectionMatrix = Matrix . CreatePerspectiveFieldOfView ( MathHelper . PiOver4 , 300f / 200f , 0.1f , 50.0f ) ;
this . instancedProjectionMatrix = Matrix . CreatePerspectiveFieldOfView ( MathHelper . PiOver4 , 600f / 200f , 0.1f , 50.0f ) ;
2011-12-05 14:44:39 +00:00
this . viewMatrix = Matrix . CreateLookAt ( new Vector3 ( 5 , 5 , 5 ) , new Vector3 ( 0 , 0 , 0 ) , Vector3 . Up ) ;
2011-12-27 08:19:27 +00:00
this . instancedViewMatrix = Matrix . CreateLookAt ( new Vector3 ( 0.0f , 5.0f , 10.0f ) , new Vector3 ( 0 , 0 , 0 ) , Vector3 . Up ) ;
2011-12-05 14:44:39 +00:00
2011-11-30 16:03:51 +00:00
this . font = Content . Load < SpriteFont > ( @"Fonts/Debug" ) ;
this . bgTexture = new Texture2D ( GraphicsDevice , 1 , 1 ) ;
this . bgTexture . SetData < Color > ( new Color [ ] { Color . White } ) ;
2011-12-05 14:44:39 +00:00
//
// create a VertexBuffer for a cube without indices
//
this . cubeNoIndicesBuffer = new VertexBuffer ( GraphicsDevice , typeof ( VertexPositionColor ) , cubeNoIndices . Length , BufferUsage . None ) ;
this . cubeNoIndicesBuffer . SetData < VertexPositionColor > ( cubeNoIndices ) ;
2011-12-16 09:21:39 +00:00
//
// create a Vertex- and IndexBuffer for a cube with indexed primitives
//
this . cubeVertexBuffer = new VertexBuffer ( GraphicsDevice , typeof ( VertexPositionColor ) , cubeVertices . Length , BufferUsage . None ) ;
this . cubeVertexBuffer . SetData < VertexPositionColor > ( cubeVertices ) ;
this . cubeIndexBuffer = new IndexBuffer ( GraphicsDevice , typeof ( short ) , this . cubeIndices . Length , BufferUsage . None ) ;
this . cubeIndexBuffer . SetData < short > ( this . cubeIndices ) ;
//
// create a VertexBuffer for the transformation matrices used by DrawInstancedPrimitives
//
2011-12-27 06:03:48 +00:00
this . instanceVertexBuffer = new DynamicVertexBuffer ( GraphicsDevice , instanceDecl , 5 , BufferUsage . WriteOnly ) ;
2011-11-30 16:03:51 +00:00
}
protected override void UnloadContent ( )
{
}
protected override void Update ( GameTime gameTime )
{
if ( GamePad . GetState ( PlayerIndex . One ) . Buttons . Back = = ButtonState . Pressed | |
Keyboard . GetState ( ) . IsKeyDown ( Keys . Escape ) )
this . Exit ( ) ;
2011-12-16 09:21:39 +00:00
rotation + = 1.0f * ( float ) gameTime . ElapsedGameTime . TotalSeconds ;
this . worldMatrix = Matrix . CreateRotationY ( rotation ) ;
2011-11-30 16:03:51 +00:00
2012-09-20 10:23:03 +00:00
KeyboardState keyState = Keyboard . GetState ( ) ;
if ( keyState . IsKeyDown ( Keys . D1 ) & & ! lastKeyState . IsKeyDown ( Keys . D1 ) )
enabled [ 0 ] = ! enabled [ 0 ] ;
if ( keyState . IsKeyDown ( Keys . D2 ) & & ! lastKeyState . IsKeyDown ( Keys . D2 ) )
enabled [ 1 ] = ! enabled [ 1 ] ;
if ( keyState . IsKeyDown ( Keys . D3 ) & & ! lastKeyState . IsKeyDown ( Keys . D3 ) )
enabled [ 2 ] = ! enabled [ 2 ] ;
if ( keyState . IsKeyDown ( Keys . D4 ) & & ! lastKeyState . IsKeyDown ( Keys . D4 ) )
enabled [ 3 ] = ! enabled [ 3 ] ;
if ( keyState . IsKeyDown ( Keys . D5 ) & & ! lastKeyState . IsKeyDown ( Keys . D5 ) )
enabled [ 4 ] = ! enabled [ 4 ] ;
lastKeyState = keyState ;
2011-11-30 16:03:51 +00:00
base . Update ( gameTime ) ;
}
protected override void Draw ( GameTime gameTime )
{
GraphicsDevice . Clear ( Color . CornflowerBlue ) ;
2011-12-16 09:21:39 +00:00
this . GraphicsDevice . Viewport = new Viewport ( 0 , 0 , 600 , 600 ) ;
2011-11-30 16:03:51 +00:00
spriteBatch . Begin ( ) ;
spriteBatch . Draw ( bgTexture , new Rectangle ( 0 , 0 , 600 , 200 ) , Color . Blue ) ;
spriteBatch . Draw ( bgTexture , new Rectangle ( 0 , 200 , 300 , 200 ) , Color . Red ) ;
spriteBatch . Draw ( bgTexture , new Rectangle ( 300 , 200 , 300 , 200 ) , Color . Orange ) ;
spriteBatch . Draw ( bgTexture , new Rectangle ( 0 , 400 , 300 , 200 ) , Color . Green ) ;
spriteBatch . Draw ( bgTexture , new Rectangle ( 300 , 400 , 300 , 200 ) , Color . LightSeaGreen ) ;
2012-09-20 10:23:03 +00:00
DrawShadowText ( spriteBatch , this . font , "DrawInstancedPrimitives\n (press 1 to " + ( enabled [ 0 ] ? "disable" : "enable" ) + ")" , new Vector2 ( 10 , 10 ) , Color . White , Color . Black ) ;
DrawShadowText ( spriteBatch , this . font , "DrawPrimitives\n (press 2 to " + ( enabled [ 1 ] ? "disable" : "enable" ) + ")" , new Vector2 ( 10 , 210 ) , Color . White , Color . Black ) ;
DrawShadowText ( spriteBatch , this . font , "DrawIndexedPrimitives\n (press 3 to " + ( enabled [ 2 ] ? "disable" : "enable" ) + ")" , new Vector2 ( 310 , 210 ) , Color . White , Color . Black ) ;
DrawShadowText ( spriteBatch , this . font , "DrawUserPrimitives\n (press 4 to " + ( enabled [ 3 ] ? "disable" : "enable" ) + ")" , new Vector2 ( 10 , 410 ) , Color . White , Color . Black ) ;
DrawShadowText ( spriteBatch , this . font , "DrawUserIndexedPrimitives\n (press 5 to " + ( enabled [ 4 ] ? "disable" : "enable" ) + ")" , new Vector2 ( 310 , 410 ) , Color . White , Color . Black ) ;
2011-11-30 16:03:51 +00:00
spriteBatch . End ( ) ;
2011-12-16 09:21:39 +00:00
this . GraphicsDevice . RasterizerState = new RasterizerState ( ) { CullMode = CullMode . CullCounterClockwiseFace , FillMode = FillMode . WireFrame } ;
2011-12-05 14:44:39 +00:00
2011-12-16 09:21:39 +00:00
this . basicEffect . VertexColorEnabled = true ;
2011-12-05 14:44:39 +00:00
this . basicEffect . View = this . viewMatrix ;
2011-12-16 09:21:39 +00:00
this . basicEffect . World = this . worldMatrix ;
2011-12-05 14:44:39 +00:00
this . basicEffect . Projection = this . projectionMatrix ;
this . basicEffect . CurrentTechnique . Passes [ 0 ] . Apply ( ) ;
2011-12-16 09:21:39 +00:00
#region DrawPrimitives
2012-09-20 10:23:03 +00:00
if ( enabled [ 1 ] )
{
GraphicsDevice . Viewport = new Viewport ( 0 , 200 , 300 , 200 ) ;
2011-12-05 14:44:39 +00:00
2012-09-20 10:23:03 +00:00
GraphicsDevice . SetVertexBuffer ( this . cubeNoIndicesBuffer ) ;
GraphicsDevice . DrawPrimitives ( PrimitiveType . TriangleList , 0 , cubeNoIndices . Length / 3 ) ;
}
2011-12-16 09:21:39 +00:00
#endregion // DrawPrimitives
#region DrawIndexedPrimitives
2012-09-20 10:23:03 +00:00
if ( enabled [ 2 ] )
{
GraphicsDevice . Viewport = new Viewport ( 300 , 200 , 300 , 200 ) ;
GraphicsDevice . SetVertexBuffer ( this . cubeVertexBuffer ) ;
GraphicsDevice . Indices = this . cubeIndexBuffer ;
GraphicsDevice . DrawIndexedPrimitives ( PrimitiveType . TriangleList , 0 , 0 , cubeVertices . Length , 0 , cubeNoIndices . Length / 3 ) ;
}
2011-12-16 09:21:39 +00:00
#endregion
#region DrawUserPrimitives
2012-09-20 10:23:03 +00:00
if ( enabled [ 3 ] )
{
GraphicsDevice . Viewport = new Viewport ( 0 , 400 , 300 , 200 ) ;
2011-12-16 09:21:39 +00:00
2012-09-20 10:23:03 +00:00
GraphicsDevice . DrawUserPrimitives < VertexPositionColor > ( PrimitiveType . TriangleList , this . cubeNoIndices , 0 , this . cubeNoIndices . Length / 3 ) ;
}
2011-12-16 09:21:39 +00:00
#endregion
#region DrawUserIndexedPrimitives
2012-09-20 10:23:03 +00:00
if ( enabled [ 4 ] )
{
GraphicsDevice . Viewport = new Viewport ( 300 , 400 , 300 , 200 ) ;
2011-12-16 09:21:39 +00:00
2012-09-20 10:23:03 +00:00
GraphicsDevice . DrawUserIndexedPrimitives < VertexPositionColor > ( PrimitiveType . TriangleList , this . cubeVertices , 0 , this . cubeVertices . Length , this . cubeIndices , 0 , this . cubeIndices . Length / 3 ) ;
}
2011-12-16 09:21:39 +00:00
#endregion
#region DrawInstancedPrimitives
2012-09-20 10:23:03 +00:00
if ( enabled [ 0 ] )
{
GraphicsDevice . Viewport = new Viewport ( 0 , 0 , 600 , 200 ) ;
this . hardwareInstanceEffect . Parameters [ "View" ] . SetValue ( this . instancedViewMatrix ) ;
this . hardwareInstanceEffect . Parameters [ "Projection" ] . SetValue ( this . instancedProjectionMatrix ) ;
this . hardwareInstanceEffect . Parameters [ "World" ] . SetValue ( this . worldMatrix ) ;
this . hardwareInstanceEffect . CurrentTechnique . Passes [ 0 ] . Apply ( ) ;
instanceVertexBuffer . SetData < Matrix > ( this . instanceTransformMatrices , 0 , this . instanceTransformMatrices . Length , SetDataOptions . Discard ) ;
GraphicsDevice . SetVertexBuffers ( cubeVertexBuffer , new VertexBufferBinding ( instanceVertexBuffer , 0 , 1 ) ) ;
GraphicsDevice . Indices = this . cubeIndexBuffer ;
GraphicsDevice . DrawInstancedPrimitives ( PrimitiveType . TriangleList , 0 , 0 , this . cubeVertices . Length , 0 , this . cubeIndices . Length / 3 , 5 ) ;
}
2011-12-16 09:21:39 +00:00
#endregion
2011-11-30 16:03:51 +00:00
base . Draw ( gameTime ) ;
2011-12-16 09:21:39 +00:00
2011-11-30 16:03:51 +00:00
}
private void DrawShadowText ( SpriteBatch spriteBatch , SpriteFont font , String text , Vector2 position , Color foreground , Color shadow )
{
2012-09-20 10:23:03 +00:00
spriteBatch . DrawString ( font , text , position + new Vector2 ( 2 , 2 ) , shadow , 0f , Vector2 . Zero , 1.0f , SpriteEffects . None , 1.0f ) ;
spriteBatch . DrawString ( font , text , position , foreground , 0f , Vector2 . Zero , 1.0f , SpriteEffects . None , 0.0f ) ;
2011-11-30 16:03:51 +00:00
}
}
}