Normal Map Shader Lights

Started by AGP, February 08, 2012, 03:18:36 AM

Previous topic - Next topic

AGP

I used the wiki's normal map shader almost entirely. I placed it on a squad of Storm Troopers. And it works, except for the fact that its built-in light doesn't have an effect on the squad. If I fly near it, my ship has a light over it and, at given, but limited, angles I can see the models lit and all the detail of the normal map applied. What's also odd is that the light they have (I've kept their distinctive blue so I can tell it from my ship's light) bounces off my ship just fine if I fly near the squad. Only exact angles seem to be triggering the normal map shader (and object lighting).

EgonOlsen

No, of course not. If you are using shaders, you are completely on your own. You still get the lighting information though (there are some predefined uniforms for this, just look at the GLSL specs), but to actually apply them, you have to code your own lighting formula in the shader. The Robombs sources should contain a phong shader variant for one light. You might also have a look at the default shaders from jPCT-AE (inside the jar), which support up to eight gouraud shaded lights. But you can't use them directly, because GLSL for mobile devices differs from the one for standard OpenGL. The biggest difference is, that the predefined uniforms are not present on mobile.

AGP

Of course not what? That wasn't a question: only exact angles seem to be triggering the normal map shader (and object lighting). I've moved them around a thousand times to the same results.

AGP

I'm thinking it may be relative to their size (they're really small). Would that make any sense? If so, is there a setting in Config for that (I have looked but found nothing)?

EgonOlsen

I was somehow thinking that you expected the normal gl lighting to show up on the model. My bad, i was in a hurry. Have you enabled specular lighting for the squad? It makes this shader usually look better. Also play around with the invRadius setting. You might also want to try this vertex shader instead:


attribute vec4 tangent;

varying vec3 lightVec;
varying vec3 eyeVec;
varying vec2 texCoord;

void main(void)
{
gl_Position = ftransform();
texCoord = gl_MultiTexCoord0.xy;

vec3 n = normalize(gl_NormalMatrix * gl_Normal);
vec3 t = normalize(gl_NormalMatrix * vec3(tangent));
vec3 b = cross(n, t);

vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
vec3 tmpVec = gl_LightSource[0].position.xyz - vVertex;

lightVec.x = dot(tmpVec, t);
lightVec.y = dot(tmpVec, b);
lightVec.z = dot(tmpVec, n);

tmpVec = -vVertex;
eyeVec.x = dot(tmpVec, t);
eyeVec.y = dot(tmpVec, b);
eyeVec.z = dot(tmpVec, n);
}



It makes use of jPCT capability to automatically calculate the tangent vectors (the other shader in the wiki is actually faking them to a degree). Just make sure that you've assigned the shader before calling build()/compile() or call calcTangentVectors() yourself.

EgonOlsen

Quote from: AGP on February 08, 2012, 09:53:01 PM
I'm thinking it may be relative to their size (they're really small). Would that make any sense? If so, is there a setting in Config for that (I have looked but found nothing)?
No. It's just about the angle to the light. Just look at the shader...that's all there is. Whatever goes wrong has its source in this small piece of code. The rest of the pipeline doesn't do anything when using a shader.

AGP

Thanks a lot, I'll give it a try and report back.

AGP

Thank you very much, it worked. But it should be noted that it only worked when I replaced the little framework from the wiki with the GLSLShader class. Should I change the wiki (this would make for a much better example)? Then again, maybe we should just add it, rather than replace the older one.

AGP

The normals are being shown, I can see them very clearly on the model. Still, I'm getting the following messages, which eventually conclude with "shader compiled."


Fragment shader failed to compile with the following errors:
ERROR: 0:1: error(#132) Syntax error: 'fragmentshader' parse error
ERROR: error(#273) 1 compilation errors.  No code generated


Vertex shader failed to compile with the following errors:
ERROR: 0:1: error(#132) Syntax error: 'vertexshader' parse error
ERROR: error(#273) 1 compilation errors.  No code generated


[ Thu Feb 09 14:29:12 EST 2012 ] - ERROR: Vertex and Fragment shader(s) were not
successfully compiled before glLinkProgram() was called.  Link failed.

Tangent handle not found (tangents needed: false)!
Shader compiled!

EgonOlsen

You seem to pass the file names where you are actually supposed to pass the source code. You can load it via the loadText-methods in Loader or in any other way you prefer.

AGP

But does it have logic to address that? Because I am seeing the normals.

EgonOlsen

Quote from: AGP on February 09, 2012, 06:09:12 PM
But does it have logic to address that? Because I am seeing the normals.
If the shader doesn't compile, you'll simply get basic multi-texturing.

AGP


trooperSquad.setRenderHook(new GLSLShader(Loader.loadTextFile("vertexshader.glsl"), Loader.loadTextFile("fragmentshader.glsl")));


DOESN'T show the normals (but does claim to compile the shaders). Now I'm really confused.

AGP

From what I gathered, the normal map was serving as an additional texture, which is why I was seeing the normals drawn on the model). And since the light I was using (as is the wiki's--bad choice of color, by the way) was blue, I didn't think anything when the troopers appeared blue. But now that the shader compiles, it simply doesn't work.

EgonOlsen

I don't get it....what do you expect and what do you get? You should be happy that it doesn't show the normals. It's not supposed to do that. The normal map is an additional texture layer that shouldn't be seen but taken by the shader to calculate per pixel lighting. Have to tried to lower the invRadius significantly?