Hello Guest

vertex shader with lighting not compiling *FIXED*

  • 9 Replies
  • 18436 Views
vertex shader with lighting not compiling *FIXED*
« on: July 01, 2012, 01:22:21 »
Hey guys,

I've been following oskar's youtube tutorials on lwjgl and they have been working great, and now I'm trying to learn shaders because I want to get into shadow mapping soon.  They have been working fine, but now my shader.vert file will not compile for me....

Code: [Select]
varying vec3 color;

void main() {
vec3 vertexPosition = (gl_ModelViewMatrix * gl_Vertex).xyz;
vec3 lightDirection = normalize(gl_LightSource[0].position.xyz - vertexPosition);
vec3 surfaceNormal = (gl_NormalMatrix * gl_Normal).xyz;
float diffuseLightIntensity = max(0.0f, dot(surfaceNormal, lightDirection);
    color.rgb = diffuseLightIntensity * gl_Color.rgb;
    color += gl_LightModel.ambient.rgb;
    vec3 reflectionDirection = normalize(reflect(-lightDirection, surfaceNormal));
    float specular = max(0, dot(surfaceNormal, reflectionDirection));
    if(diffuseLightIntensity != 0)
    {
     float fspecular = pow(specular, gl_FrontMaterial.shininess);
     color.rgb += vec3(fspecular, fspecular, fspecular);
    }
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

When I delete everything after the surfaceNormal everything works fine for me, so the problem has to be the diffuseLightIntensity, but I cannot figure out how to solve it...

Thanks for the help,
pfolder
« Last Edit: July 02, 2012, 17:59:10 by pfolder »

Re: vertex shader with lighting not compiling
« Reply #1 on: July 02, 2012, 13:20:42 »
What is the error message you are getting? It might help a lot to know that.

If you didn't know, I think you can get the shader compile log by calling glGetShaderInfoLog (or glGetInfoLogARB).
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Re: vertex shader with lighting not compiling
« Reply #2 on: July 02, 2012, 17:38:06 »
The info log says that there are three errors:

The first two are both saying that I cannot use the "max" function:
"ERROR: 0:7: 'max' : no matching overloaded function found - implicit conversion not allowed"
"ERROR: 0:11: 'max' : no matching overloaded function found - implicit conversion not allowed"

The other one says that "!=" is not an allowed for me:
"ERROR: 0:12: '!=' :  wrong operand types  no operation '!=' exists that takes a left-hand operand of type 'float' and a right operand of type 'const int' (or there is no acceptable conversion)"

EDIT: I found out it was because my 0's were not "0.0" in the coding...
« Last Edit: July 02, 2012, 17:59:42 by pfolder »

Re: vertex shader with lighting not compiling *FIXED*
« Reply #3 on: July 02, 2012, 18:05:01 »
One thing that is wrong is your use of 0.0f - there's no "f" syntax for float in GLSL. Just use 0.0.

What happens when you change it to:

Code: [Select]
varying vec3 color;

void main()
{
vec3 vertexPosition = (gl_ModelViewMatrix * gl_Vertex).xyz;
vec3 lightDirection = normalize(gl_LightSource[0].position.xyz - vertexPosition);
vec3 surfaceNormal = (gl_NormalMatrix * gl_Normal).xyz;
float diffuseLightIntensity = max(0.0, dot(surfaceNormal, lightDirection);
color.rgb = diffuseLightIntensity * gl_Color.rgb;
color += gl_LightModel.ambient.rgb;

vec3 reflectionDirection = normalize(reflect(-lightDirection, surfaceNormal));

float specular = max(0.0, dot(surfaceNormal, reflectionDirection));

if(diffuseLightIntensity != 0.0)
{
float fspecular = pow(specular, gl_FrontMaterial.shininess);
color.rgb += vec3(fspecular, fspecular, fspecular);
}

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

I'm not sure if there's implicit casting in GLSL (e.g., using max(0, 1.5) might not convert the 0 into a double). That would explain your issues.

Re: vertex shader with lighting not compiling *FIXED*
« Reply #4 on: July 02, 2012, 19:09:50 »
Yep, that is exactly what the problem was. Thank you!

The only thing I'm confused with now is that there is no color being made with it, just the lighting with gray for everything else.  I do have textures in it, but even when I'm not using textures it still does that...any idea why?

Re: vertex shader with lighting not compiling *FIXED*
« Reply #5 on: July 02, 2012, 22:09:40 »
You do realize you aren't calling texture2D, right? Multiply your final color by the texture color.

Re: vertex shader with lighting not compiling *FIXED*
« Reply #6 on: July 03, 2012, 03:27:00 »
Even when I do have no textures in it though, they completely come up weird.  I found out that doing the shaders with VBOs gives me a completely and wrong different version than the display lists do for the color.  I think it is where it is being placed, but I don't know for sure.

Re: vertex shader with lighting not compiling *FIXED*
« Reply #7 on: July 03, 2012, 11:25:04 »
I'm not expert on VBOs, but are you setting the texture coordinates correctly? I.e., is the correct vertex being associated with the appropriate texture coords?

Re: vertex shader with lighting not compiling *FIXED*
« Reply #8 on: July 03, 2012, 16:32:36 »
Everything is fixed now, I found out when I was copying and pasting I was using vertex coordinates for my normals xD.  Thanks for all of the help, everything works!

Re: vertex shader with lighting not compiling *FIXED*
« Reply #9 on: July 03, 2012, 16:46:33 »
No problem.