Shaders and OpenFL

Hey Folks!

I decided to switch back to prototyping for a day. The siren's call of OpenGL and shader programming lured me back, despite previously crashing upon jagged shores.

In case you're wondering why I keep looking into shaders, it might help to show a few examples:

In other words, shaders can do some pretty cool visual tricks. And since most desktops and many handheld/mobile devices have GPU hardware designed to use shaders, it's pretty fast, too!

In theory, Haxe/OpenFL has a way to display these shaders. And in practice, I've got this working no problem. (They have several helpful samples and demos to get you started.)

The tricky part, however, is getting this to play nice with HaxeFlixel. Right now, HaxeFlixel will support shaders that get applied to the whole camera. So this can be useful for post-process effects (antialiasing, scaling, blur, etc.).

However, it's harder to get these shaders per-sprite. And I think that's what I'm going to need if I want to try to apply lighting/highlights to individual ship parts. (An alternative to the grid-based lighting I was trying before.)

Although, now that I write about it a bit, maybe that isn't the case. Maybe I don't need to have individual sprites with shaders if I can render a whole scene to a texture, and feed that texture to a shader that selectively processes regions of the scene? E.g. one texture shows the ship sprites' actual colors, another texture shows those sprites' normals (bumps), and a third texture highlights areas where the shader should be active vs. ignored. This way, the shader would just process lighting for pixels in one area of the mask, and ignore other areas entirely (such as the GUI).

Hmm. This might save me from a lot of matrix transformation headaches.

Or, no. No it won't. I still have the problem of a rotated ship piece having normal maps that are pointing the wrong way. The shader would need to know that these ship pieces were rotated in order to light them correctly.

Anyway, the good news is that I've got a normal map shader already running. I can move it around and see the lighting on it change accordingly. The missing parts are

1 - if that item gets rotated, the lighting doesn't behave correctly
2 - this is entirely an OpenFL GLView scene, and bypasses HaxeFlixel.

The first issue is a well-known problem, and it's just a matter of me learning how to transform my vectors appropriately in the vertex shader and pass that info to the fragment shader where lighting is figured out.

The second issue is more of an unknown. I know I can composite OpenFL (or HaxeFlixel) sprites on top of a GLView scene, but I haven't been able to have a GLView scene blend with them at all. I suspect it's doable by having the shader sample the game's frameBuffer or something, but I haven't learned enough about that to sling those around yet.

But at least I made progress today! It was damned frustrating, and my brain is starting to show its age. But I'm excited for the prospects of playing with shaders in my next game!