Author Topic: Normal Map Shader Lights  (Read 8434 times)

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Normal Map Shader Lights
« on: February 08, 2012, 03:18:36 am »
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).

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Normal Map Shader Lights
« Reply #1 on: February 08, 2012, 08:35:09 am »
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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Normal Map Shader Lights
« Reply #2 on: February 08, 2012, 04:46:27 pm »
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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Normal Map Shader Lights
« Reply #3 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)?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Normal Map Shader Lights
« Reply #4 on: February 08, 2012, 09:58:33 pm »
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:

Code: [Select]
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Normal Map Shader Lights
« Reply #5 on: February 08, 2012, 09:59:43 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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Normal Map Shader Lights
« Reply #6 on: February 08, 2012, 10:25:54 pm »
Thanks a lot, I'll give it a try and report back.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Normal Map Shader Lights
« Reply #7 on: February 09, 2012, 05:15:18 pm »
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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Normal Map Shader Lights
« Reply #8 on: February 09, 2012, 05:30:56 pm »
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!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Normal Map Shader Lights
« Reply #9 on: February 09, 2012, 06:02:29 pm »
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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Normal Map Shader Lights
« Reply #10 on: February 09, 2012, 06:09:12 pm »
But does it have logic to address that? Because I am seeing the normals.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Normal Map Shader Lights
« Reply #11 on: February 09, 2012, 06:12:11 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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Normal Map Shader Lights
« Reply #12 on: February 09, 2012, 06:16:30 pm »
Code: [Select]
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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Normal Map Shader Lights
« Reply #13 on: February 09, 2012, 08:17:41 pm »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Normal Map Shader Lights
« Reply #14 on: February 09, 2012, 09:32:40 pm »
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?