Created
November 13, 2013 10:35
-
-
Save daramkun/7446951 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.ComponentModel; | |
| using System.Data; | |
| using System.Drawing; | |
| using System.Linq; | |
| using System.Reflection; | |
| using System.Runtime.InteropServices; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using System.Windows.Forms; | |
| using SharpDX; | |
| using SharpDX.Direct3D9; | |
| namespace Dx9Test | |
| { | |
| public partial class Form1 : Form | |
| { | |
| Direct3D d3d; | |
| Device d3dDevice; | |
| Sprite d3dSprite; | |
| VertexBuffer vertexBuffer; | |
| VertexDeclaration vertexDeclaration; | |
| VertexFormat fvf; | |
| IndexBuffer indexBuffer; | |
| Texture texture; | |
| VertexShader vertexShader; | |
| PixelShader pixelShader; | |
| struct VertexStruct | |
| { | |
| public Vector2 position; | |
| public Vector4 diffuse; | |
| public Vector2 texcoord; | |
| } | |
| public Form1 () | |
| { | |
| InitializeComponent (); | |
| } | |
| protected override void OnLoad ( EventArgs e ) | |
| { | |
| ClientSize = new Size ( 800, 600 ); | |
| StartPosition = FormStartPosition.CenterScreen; | |
| d3d = new Direct3D (); | |
| d3dDevice = new Device ( d3d, 0, DeviceType.Hardware, | |
| Handle, CreateFlags.HardwareVertexProcessing, | |
| new PresentParameters () | |
| { | |
| BackBufferWidth = 800, | |
| BackBufferHeight = 600, | |
| BackBufferFormat = Format.A8R8G8B8, | |
| BackBufferCount = 1, | |
| Windowed = true, | |
| SwapEffect = SwapEffect.Discard, | |
| PresentationInterval = PresentInterval.Immediate, | |
| DeviceWindowHandle = Handle, | |
| AutoDepthStencilFormat = Format.D24S8, | |
| EnableAutoDepthStencil = true, | |
| } ); | |
| d3dSprite = new Sprite ( d3dDevice ); | |
| Bitmap image = new Bitmap ( Assembly.GetEntryAssembly (). | |
| GetManifestResourceStream ( "Dx9Test.test4.png" ) ); | |
| texture = new Texture ( d3dDevice, image.Width, image.Height, | |
| 1, Usage.None, Format.A8R8G8B8, Pool.Managed ); | |
| var rect = texture.LockRectangle ( 0, new SharpDX.Rectangle ( 0, 0, | |
| image.Width, image.Height ), LockFlags.None ); | |
| SharpDX.DataStream dataStream = new SharpDX.DataStream ( rect.DataPointer, Width * Height * 4, false, true ); | |
| SharpDX.Color [] colours = new SharpDX.Color [ image.Width * image.Height ]; | |
| int index = 0; | |
| for ( int i = 0; i < image.Height; i++ ) | |
| for ( int j = 0; j < image.Width; j++ ) | |
| colours [ index++ ] = new SharpDX.Color ( image.GetPixel ( j, i ).ToArgb () ); | |
| dataStream.WriteRange<SharpDX.Color> ( colours, 0, image.Width * image.Height ); | |
| texture.UnlockRectangle ( 0 ); | |
| fvf = VertexFormat.Position | VertexFormat.Diffuse | VertexFormat.Texture1; | |
| vertexBuffer = new VertexBuffer ( d3dDevice, | |
| Marshal.SizeOf ( typeof ( VertexStruct ) ) * 4, | |
| Usage.None, fvf, Pool.Managed ); | |
| vertexBuffer.Lock ( 0, 0, LockFlags.None ).WriteRange<VertexStruct> ( new VertexStruct [] | |
| { | |
| new VertexStruct () | |
| { | |
| position = new Vector2 ( 0, 0 ), | |
| diffuse = new Vector4 ( 1, 1, 1, 1 ), | |
| texcoord = new Vector2 ( 0, 0 ), | |
| }, | |
| new VertexStruct () | |
| { | |
| position = new Vector2 ( image.Width, 0 ), | |
| diffuse = new Vector4 ( 1, 1, 1, 1 ), | |
| texcoord = new Vector2 ( 1, 0 ), | |
| }, | |
| new VertexStruct () | |
| { | |
| position = new Vector2 ( 0, image.Height ), | |
| diffuse = new Vector4 ( 1, 1, 1, 1 ), | |
| texcoord = new Vector2 ( 0, 1 ), | |
| }, | |
| new VertexStruct () | |
| { | |
| position = new Vector2 (image.Width, image.Height), | |
| diffuse = new Vector4 ( 1, 1, 1, 1 ), | |
| texcoord = new Vector2 ( 1, 1 ), | |
| } | |
| } ); | |
| vertexBuffer.Unlock (); | |
| indexBuffer = new IndexBuffer ( d3dDevice, | |
| sizeof ( int ) * 6, Usage.None, Pool.Managed, false ); | |
| indexBuffer.Lock ( 0, 0, LockFlags.None ).WriteRange<int> ( new int [] | |
| { | |
| 0, 1, 2, | |
| 1, 3, 2 | |
| } ); | |
| indexBuffer.Unlock (); | |
| vertexShader = new VertexShader ( d3dDevice, ShaderBytecode.Compile ( @" | |
| float4x4 projectionMatrix; | |
| float4x4 worldMatrix; | |
| struct VS_INPUT | |
| { | |
| float2 i_position : POSITION; | |
| float4 i_overlay : COLOR; | |
| float2 i_texture : TEXCOORD0; | |
| }; | |
| struct PS_INPUT | |
| { | |
| float4 o_position : POSITION; | |
| float4 o_overlay : COLOR; | |
| float2 o_texture : TEXCOORD0; | |
| }; | |
| PS_INPUT vs_main(VS_INPUT input) | |
| { | |
| PS_INPUT output; | |
| output.o_position = float4(input.i_position, 1, 1); | |
| output.o_position = mul(output.o_position, worldMatrix); | |
| output.o_position = mul(output.o_position, projectionMatrix); | |
| output.o_overlay = input.i_overlay; | |
| output.o_texture = input.i_texture; | |
| return output; | |
| } | |
| ", "vs_main", "vs_2_0", ShaderFlags.None ) ); | |
| pixelShader = new PixelShader ( d3dDevice, ShaderBytecode.Compile ( @" | |
| sampler2D texture0; | |
| struct PS_INPUT | |
| { | |
| float4 o_position : POSITION; | |
| float4 o_overlay : COLOR; | |
| float2 o_texture : TEXCOORD0; | |
| }; | |
| float4 ps_main(PS_INPUT input) : COLOR | |
| { | |
| return tex2D ( texture0, input.o_texture ) * input.o_overlay; | |
| } | |
| ", "ps_main", "ps_2_0", ShaderFlags.None ) ); | |
| vertexDeclaration = new VertexDeclaration ( d3dDevice, new VertexElement [] | |
| { | |
| new VertexElement(0, 0, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.Position, 0), | |
| new VertexElement(0, 8, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Color, 0), | |
| new VertexElement(0, 24, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0), | |
| VertexElement.VertexDeclarationEnd, | |
| } ); | |
| d3dDevice.SetRenderState ( RenderState.CullMode, ( int ) Cull.None ); | |
| base.OnLoad ( e ); | |
| } | |
| protected override void OnFormClosed ( FormClosedEventArgs e ) | |
| { | |
| texture.Dispose (); | |
| pixelShader.Dispose (); | |
| vertexShader.Dispose (); | |
| vertexDeclaration.Dispose (); | |
| indexBuffer.Dispose (); | |
| vertexBuffer.Dispose (); | |
| d3dSprite.Dispose (); | |
| d3dDevice.Dispose (); | |
| d3d.Dispose (); | |
| base.OnFormClosed ( e ); | |
| } | |
| private void timer1_Tick ( object sender, EventArgs e ) | |
| { | |
| d3dDevice.Clear ( ClearFlags.All, new SharpDX.ColorBGRA ( 0.0f, 0, 0, 1 ), 1, 0 ); | |
| d3dDevice.BeginScene (); | |
| /* | |
| d3dSprite.Begin ( SpriteFlags.AlphaBlend ); | |
| d3dSprite.Draw ( texture, new ColorBGRA ( 1, 1, 1, 1.0f ) ); | |
| d3dSprite.End (); | |
| */ | |
| //d3dDevice.VertexFormat = fvf; | |
| d3dDevice.VertexDeclaration = vertexDeclaration; | |
| d3dDevice.SetStreamSource ( 0, vertexBuffer, 0, Marshal.SizeOf ( typeof ( VertexStruct ) ) ); | |
| d3dDevice.Indices = indexBuffer; | |
| EffectHandle projectionMatrix = vertexShader.Function.ConstantTable.GetConstantByName ( null, "projectionMatrix" ); | |
| EffectHandle worldMatrix = vertexShader.Function.ConstantTable.GetConstantByName ( null, "worldMatrix" ); | |
| vertexShader.Function.ConstantTable.SetValue ( d3dDevice, projectionMatrix, Matrix.OrthoOffCenterLH ( 0, 800, 600, 0, 0.001f, 1000.0f ) ); | |
| vertexShader.Function.ConstantTable.SetValue ( d3dDevice, worldMatrix, Matrix.Identity * Matrix.RotationZ ( 0.1f ) ); | |
| EffectHandle texture0 = pixelShader.Function.ConstantTable.GetConstantByName ( null, "texture0" ); | |
| int samplerIndex = pixelShader.Function.ConstantTable.GetSamplerIndex ( texture0 ); | |
| d3dDevice.SetTexture ( samplerIndex, texture ); | |
| d3dDevice.VertexShader = vertexShader; | |
| d3dDevice.PixelShader = pixelShader; | |
| d3dDevice.DrawIndexedPrimitive ( PrimitiveType.TriangleList, | |
| 0, 0, 4, 0, 2 ); | |
| d3dDevice.EndScene (); | |
| d3dDevice.Present (); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ProjectLiqueur에 적용하려고 작성해본 코드인데 여기선 잘 되는게 왜 Liqueur에선 안 되는걸까