[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; public class MyGame : Game { GraphicsDeviceManager graphics; Texture2D inputTexture; RenderTarget2D result; Effect additionCalculator; VertexDeclaration vertexDeclaration; //GPUから計算結果を受け取るバッファ float[] resultBuffer = new float[2]; VertexPositionTexture[] vertices = { new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2()), new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)), new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)), new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)), new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)), new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)) }; public MyGame() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void LoadContent() { inputTexture = new Texture2D(GraphicsDevice, 2, 1, 0, TextureUsage.None, SurfaceFormat.Single); inputTexture.SetData<float>(new float[] { 1, 1 }); result = new RenderTarget2D(GraphicsDevice, 2, 1, 0, SurfaceFormat.Single); additionCalculator = Content.Load<Effect>("AdditionCalculator"); GraphicsDevice.VertexDeclaration = new VertexDeclaration( GraphicsDevice, VertexPositionTexture.VertexElements ); vertexDeclaration = GraphicsDevice.VertexDeclaration; } protected override void UnloadContent() { inputTexture.Dispose(); result.Dispose(); vertexDeclaration.Dispose(); } protected override void Update(GameTime gameTime) { Window.Title = resultBuffer[1].ToString(); } protected override void Draw(GameTime gameTime) { GraphicsDevice.SetRenderTarget(0, result); additionCalculator.Parameters["InputTexture"].SetValue(inputTexture); additionCalculator.Parameters["InputWidth"].SetValue(2); additionCalculator.Begin(); additionCalculator.CurrentTechnique.Passes[0].Begin(); GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>( PrimitiveType.TriangleList, vertices, 0, 2 ); additionCalculator.CurrentTechnique.Passes[0].End(); additionCalculator.End(); //GetDataを呼ぶには、レンダーターゲットはいったん //デバイスから外されなければいけません。 GraphicsDevice.SetRenderTarget(0, null); result.GetTexture().GetData<float>(resultBuffer); } static void Main() { using (MyGame game = new MyGame()) { game.Run(); } } }
float InputWidth; texture InputTexture; sampler InputSampler = sampler_state { Texture = (InputTexture); }; struct VertexPositionTexture { float4 Position : POSITION; float2 TextureCoordinate : TEXCOORD; }; VertexPositionTexture VertexShaderFunction(VertexPositionTexture input) { return input; } float4 PixelShaderFunction(VertexPositionTexture input) : COLOR0 { return tex2D(InputSampler, input.TextureCoordinate + float2(-1/InputWidth, 0)) + tex2D(InputSampler, input.TextureCoordinate); } technique Technique1 { pass Pass1 { VertexShader = compile vs_1_1 VertexShaderFunction(); PixelShader = compile ps_2_0 PixelShaderFunction(); } }