Grab and Go CodingPosted: April 18, 2012
Now that doing shaders is relatively easy, how about some shaders!
Often times I find myself casting about the internet to find good techniques for achieving certain things. There is one great site, Geeks3D, which has a ton of useful stuff on doing graphics coding. I found one little gem, that showed some CrossHatch shading, so I figured I’d try it out in HeadsUp and see how hard it was to code up.
The above video is a screen capture of the program in action. Basically, apply this cross hatch shader to whatever is being displayed. There are various parameters you can set to tweak things.
I setup the shader like this:
-- load and set the mandelbrot shader gpuprog = GLSLProgram(fragtext, vertext) gpuprog:Use(); gpuprog.sceneTex = 0; -- 0 gpuprog.vx_offset = 0.5; gpuprog.hatch_y_offset = 5; -- 5.0 gpuprog.lum_threshold_1 = 1; -- 1.0 gpuprog.lum_threshold_2 = 0.7; -- 0.7 gpuprog.lum_threshold_3 = 0.5; -- 0.5 gpuprog.lum_threshold_4 = 0.3; -- 0.3
Then, when I implement keydown() to look like this:
function keydown(key, x, y) local offset = gpuprog.vx_offset; local offsetfactor = 0.01; if key == VK_LEFT then offset = offset - offsetfactor; if offset < 0 then offset = 0 end end if key == VK_RIGHT then offset = offset + offsetfactor; if offset > 1 then offset = 1 end end gpuprog.vx_offset = offset; end
Basically, if I press the left arrow key, I’ll decrease the offset by some amount, and increase the offset if the right arrow key is pressed.
Oh yes, and that keydown will receive raw virtual key codes from the keyboard. It’s different from keychar(), which receives translated characters.
But, you can see how easy it is to deal with the shader. Not a single OpenGL API call in there at all. Just pure Lua code, clean and simple. This took about 5 minutes to throw together. Recording the screen, and making this post probably took longer.
Being able to quickly experiment with shaders is a great thing. One of things I’ve been wanting to try is visualing things like changes to a source control system as ripples on a pond. Well, with shaders, I can probably do that fairly easily, so it will be fun.
If I had the same easy of programming with OpenCL, then that really would be something wouldn’t it? Imagine, being able to combine OpenCL, OpenGL, Lua, all in one tidy little package, without having to flip back and forth between different API styles and coding systems. I think the prospects are very interesting.