LWJGL
June 19, 2013, 03:10:33 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL is now using GitHub
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: vertex shader with lighting not compiling *FIXED*  (Read 1832 times)
pfolder
Newbie
*
Posts: 7


« on: June 30, 2012, 18: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:
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
Logged
Fool Running
Nerdus Imperius
*****
Posts: 758


« Reply #1 on: July 02, 2012, 06: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).
Logged

Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option Grin
pfolder
Newbie
*
Posts: 7


« Reply #2 on: July 02, 2012, 10: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...
Logged
CodeBunny
Nerdus Imperius
*****
Posts: 561



WWW
« Reply #3 on: July 02, 2012, 11: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:
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.
Logged
pfolder
Newbie
*
Posts: 7


« Reply #4 on: July 02, 2012, 12: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?
Logged
CodeBunny
Nerdus Imperius
*****
Posts: 561



WWW
« Reply #5 on: July 02, 2012, 15:09:40 »

You do realize you aren't calling texture2D, right? Multiply your final color by the texture color.
Logged
pfolder
Newbie
*
Posts: 7


« Reply #6 on: July 02, 2012, 20: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.
Logged
CodeBunny
Nerdus Imperius
*****
Posts: 561



WWW
« Reply #7 on: July 03, 2012, 04: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?
Logged
pfolder
Newbie
*
Posts: 7


« Reply #8 on: July 03, 2012, 09: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!
Logged
CodeBunny
Nerdus Imperius
*****
Posts: 561



WWW
« Reply #9 on: July 03, 2012, 09:46:33 »

No problem.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!