2009年9月22日 星期二

XNA畫線


介紹一下XNA劃線功能,vb劃線很簡單 System.DrawLine(參數自己查...)
,可是XNA卻搞一堆鬼東西,明明號稱『123,站著寫真簡單』,真是不簡單
不廢話,以下為source code 自己抄
namespace DrawLine1
{
///
/// This is the main type for your game
///

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;


VertexPositionNormalTexture[] line; // Our start and end points.
Matrix world, projection, view; // Transformation Matrices
BasicEffect basicEffect; // Standard Effect to draw with
VertexDeclaration vertexDeclaration; // Our format for the line.
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
InitializeTransforms();
InitializeBasicEffect();
base.Initialize();
}
private void InitializeTransforms()
{
world = Matrix.CreateTranslation(new Vector3(-1.5f, -0.5f, 0.0f));
view = Matrix.CreateLookAt(
new Vector3(0.0f, 0.0f, 7.0f),
new Vector3(0.0f, 0.0f, 0.0f),
Vector3.Up
);
projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45),
(float)graphics.GraphicsDevice.Viewport.Width /
(float)graphics.GraphicsDevice.Viewport.Height,
1.0f, 100.0f);
}
///
/// Setup up the required effect so we can see our scene
///

private void InitializeBasicEffect()
{
// Not part of the Effect per se, but is required for drawing
vertexDeclaration = new VertexDeclaration(
graphics.GraphicsDevice,
VertexPositionNormalTexture.VertexElements
);
basicEffect = new BasicEffect(graphics.GraphicsDevice, null);
basicEffect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);
basicEffect.World = world;
basicEffect.View = view;
basicEffect.Projection = projection;
}
///
/// 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);

// 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);
}
private void DrawLine()
{
line = new VertexPositionNormalTexture[2];
line[0] = new VertexPositionNormalTexture(new Vector3(0, 0, 0),
Vector3.Forward,
Vector2.One
);
line[1] = new VertexPositionNormalTexture(new Vector3(0, 1, 0),
Vector3.Forward,
Vector2.One
);
graphics.GraphicsDevice.DrawUserIndexedPrimitives(
PrimitiveType.LineList,
line,
0, // vertex buffer offset to add to each element of the index buffer
2, // number of vertices in pointList
new short[2] { 0, 1 }, // the index buffer
0, // first index element to read
1 // number of primitives to draw
);
}
///
/// 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.VertexDeclaration = vertexDeclaration;
// The effect is a compiled effect created and compiled elsewhere
// in the application.
basicEffect.Begin();
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Begin();
DrawLine();
pass.End();
}
basicEffect.End();
// TODO: Add your drawing code here

base.Draw(gameTime);
}
}
}

沒有留言:

張貼留言