vertex shader with lighting not compiling *FIXED*

Started by pfolder, July 01, 2012, 01:22:21

Previous topic - Next topic

pfolder

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....

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

Fool Running

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

pfolder

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...

CodeBunny

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:

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.

pfolder

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?

CodeBunny

You do realize you aren't calling texture2D, right? Multiply your final color by the texture color.

pfolder

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.

CodeBunny

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?

pfolder

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!

CodeBunny