[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
今まではウィンドウの画面にだけポリゴンを描画していましたが、
XNAでは実はテクスチャに対して描画することも出来ます。
これによりテクスチャを動的に作り出すことができ、
例えばゲームの中のアイテムとしてパソコンを作って、その画面を動かしたり出来るでしょう。
テクスチャに対していろいろ描画して、
それをゲームの中のパソコン画面に貼り付ければいいのです。
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
class MyGame : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
RenderTarget2D renderTarget;
BasicEffect effect;
VertexPositionColor[] vertices = {
new VertexPositionColor(new Vector3(0, 1, 0), Color.White),
new VertexPositionColor(new Vector3(1, 0, 0), Color.Blue),
new VertexPositionColor(new Vector3(-1, 0, 0),Color.Red)
};
public MyGame()
{
graphics = new GraphicsDeviceManager(this);
}
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
renderTarget = new RenderTarget2D(
graphics.GraphicsDevice,
400, 200,
0,
SurfaceFormat.Color
);
effect = new BasicEffect(graphics.GraphicsDevice, null);
effect.View = Matrix.CreateLookAt(
new Vector3(0, 0, 5),
new Vector3(),
new Vector3(0, 1, 0)
);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45),
400 / 200,
1, 100
);
effect.VertexColorEnabled = true;
}
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
renderToRenderTarget();
spriteBatch.Begin();
spriteBatch.Draw(
renderTarget.GetTexture(),
new Rectangle(0, 0, 400, 200),
Color.White
);
spriteBatch.End();
}
private void renderToRenderTarget()
{
graphics.GraphicsDevice.SetRenderTarget(0, renderTarget);
graphics.GraphicsDevice.Clear(Color.Gray);
graphics.GraphicsDevice.VertexDeclaration = new VertexDeclaration(
graphics.GraphicsDevice,
VertexPositionColor.VertexElements
);
effect.Begin();
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Begin();
graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
PrimitiveType.TriangleList,
vertices,
0,
vertices.Length / 3
);
pass.End();
}
effect.End();
graphics.GraphicsDevice.ResolveRenderTarget(0);
graphics.GraphicsDevice.SetRenderTarget(0, null);
}
}