Finally! Shader's Working.

Hey Folks! Hope everyone had a good weekend. I kept meaning to sit down and work on an RPG design presentation for a possible developer's talk, but I barely would get into it when baby stuff came up. Such is parent life :)

Conversely, I did manage to finally sort out a working lighting shader in OpenFL using OpenGL Views. Almost another day passed with little success, and lots of my wrestling with transforms. Every time I'd suspect I found an error in the shader, it'd still turn out wrong. And all of the debugging tricks I could think of (rendering colors based on direction, distance, etc.) weren't yielding any answers.

But in the afternoon, I finally broke-down and decided to try rewriting the shader in world space to start debugging it. (This tutorial, for those interested.)

Thinking the removal of view-space would uncomplicate things a tad, I fired it up...and it worked?

I was dumbfounded. I still am, a bit. I mean, it seemed like everything I was doing before was by the book. As far as I can tell, I must've been doing something before that was world-space instead of view-space, and now that the shader was setup for world-space, things worked. And in all likelihood, it was the setup of one of my transform matrices in the app, or the TBN matrix in the vertex shader.

Whatever the case, here's what I've been striving for:

IMAGE( This whole thing screams Disney's "Aladdin" all of the sudden.

Hopefully, the improvements over last week's screenshot are obvious. We now have lighting which correctly highlights each surface from an appropriate direction, and it works regardless of the plane's orientation or position.

And, as a bonus, you can see a few additional lighting components at work. For one thing, the light has a color (yellowish orange), and it casts a warm color contrasting with a cooler, ambient blue-violet that lights everywhere else. There's also a falloff component which fades light intensity based on distance from the light, which helps illuminate areas directly under the light (which were dark without it).

This means I can have separate control over ambient lighting and point-source lighting, each with their own colors and intensities. Theoretically, additional lights are possible, too.

Now, this isn't the end goal. It's a huge step forward, and the backbone of the proposed lighting system. However, there is at least one additional piece missing: composing multiple ship parts of various orientations into a scene with lighting.

For that to work, I can see two possible approaches. The first is to simply render each ship part as planar geometry with this shader attached, and appropriate textures, and let the GPU do all the work. This would basically be a 3D engine rendered from a 2D, top-down perspective, and is maybe overkill.

The second approach would be to render all the ship parts onto a scene with their relative positions and orientations, and then do one giant lighting calculation on the whole screen. For this to work, I'd have to render the color information to one screen-sized texture, render the normal map info to another texture, then combine the two in a shader with lighting info. Also, that normal map texture would need each sprite's normals adjusted for orientation, which means transforming their normal map colors based on orientation. This, too, is pretty complex.

Before I can do that, however, I think I need to figure out how OpenGL handles multiple render targets, so I can have one for each scene texture (diffuse color, normal map, and any others I might need, like depth or occlusion info).

More on that tomorrow. For now, my brain needs a rest!