Skip to content

Instantly share code, notes, and snippets.

@wanabe
Created June 23, 2012 13:57
Show Gist options
  • Save wanabe/2978372 to your computer and use it in GitHub Desktop.
Save wanabe/2978372 to your computer and use it in GitHub Desktop.
require 'dxruby'
hlsl = <<EOS
float g_screen_w, g_screen_h;
float g_image_w, g_image_h;
float g_parse, g_scale;
texture tex0;
sampler Samp = sampler_state
{
Texture =<tex0>;
AddressU = BORDER;
AddressV = BORDER;
};
struct VS_OUTPUT
{
float4 pos : POSITION;
float2 tex : TEXCOORD0;
float2 tmp : TEXCOORD1;
};
VS_OUTPUT VS(float4 pos: POSITION, float2 tex: TEXCOORD0)
{
VS_OUTPUT output;
int is_left = tex.x < 0.1, is_top = tex.y < 0.1;
output.pos = pos;
output.pos.x = pos.x * 2 / g_screen_w - 1; //(is_left ? -1 : 1) + 0.75;
output.pos.y = -pos.y * 2 / g_screen_h + 1; //(!is_top ? -1 : 1) + 0.5;
output.pos.w = is_top ? g_parse / g_scale: 1 / g_scale;
//output.pos.z = is_top ? 0 : 1;
output.tex = tex;
/*output.tex.x = ((is_left ? 0 : g_image_w + g_screen_w) - pos.x) / g_image_w;
output.tex.y = ((!is_top ? 0 : g_image_h + g_screen_h) - pos.y) / g_image_h;*/
output.tmp = float2(pos.x, pos.y);
return output;
}
float4 PS(VS_OUTPUT input) : COLOR0
{
float4 output;
output = tex2D( Samp, input.tex );
return output;
}
technique Tech1
{
pass P0
{
VertexShader = compile vs_2_0 VS();
PixelShader = compile ps_2_0 PS();
}
}
EOS
Window.width, Window.height = 800, 600
vars = {}
hlsl[/.*?\n\n/m].scan(/.+/) do |l|
l =~ /([^\s]+)(\d+x?\d*)?\s+(.+);/
type, name = $1.intern, $3
name.split(/,\s*/).each {|n| vars[n.intern] = type}
end
core = DXRuby::Shader::Core.new(hlsl, vars.dup)
shader = Shader.new(core, "Tech1")
shader.g_screen_w = Window.width
shader.g_screen_h = Window.height
shader.g_parse = 1
shader.g_scale = 1
image = Image.load("c.png")
#image = Image.load("1.jpg")
shader.g_image_w = image.width
shader.g_image_h = image.height
x, y = 400, 300
count = 0
Window.loop do
if Input.mouseDown?(M_LBUTTON)
x, y = Input.mousePosX, Input.mousePosY
end
if (count += 1) < 100
shader.g_scale += 0.01
else
shader.g_parse += shader.g_parse * 0.01
end
Window.draw_shader(x - image.width / 2, y - image.height / 2, image, shader)
break if Input.key_push?(K_ESCAPE)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment