PDA

View Full Version : Simple GLSL Shaders Not Working



twoski
03-10-2014, 12:23 PM
I am working on 2 very basic GLSL shaders. The first is a basic shader that has a diffuse + specular component. My grasp on the mathematics behind specular lighting is a bit shaky and my shader doesn't seem to work at all right now (for reasons i do not know).

Here's the code




//VERTEX

uniform mat4 projection, modelview, normalMat;
uniform int mode;

varying vec4 forFragColor;
const vec3 diffuseColor = vec3(0.5, 0.1, 0.2);
const vec3 specColor = vec3(0.9, 0.9, 0.9);
const float shine = 1.9;

void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
vec3 lightPos = vec3(0,0,0);

// all following gemetric computations are performed in the
// camera coordinate system (aka eye coordinates)
vec3 normal = vec3(gl_NormalMatrix * gl_Normal);
vec4 vertPos4 = gl_ModelViewMatrix * gl_Vertex;
vec3 vertPos = vec3(vertPos4);
vec3 lightDir = normalize(lightPos - vertPos);

float lambertian = max(dot(lightDir,normal), 0.0);

float specularCoefficient = 0.0;
float diffuseCoefficient = max(0.0, dot(normal, lightDir));

if(diffuseCoefficient > 0.0)
specularCoefficient = pow(max(0.0, dot(lightDir, reflect(-lightDir, normal))), shine);

vec3 specular = specularCoefficient * specColor

vec3 linearColor = lambertian * (diffuseColor + specular);

forFragColor = vec4(lambertian*diffuseColor , 1.0);
}



//FRAGMENT

precision mediump float;
varying vec4 forFragColor;

void main()
{
gl_FragColor = forFragColor;
}


I'm sure there's just some silly little mistake somewhere in there that's breaking it but I can't figure it out!

The second shader is a basic 'toon' shader.




//VERTEX

varying vec3 normal;

void main()
{
normal = gl_NormalMatrix * gl_Normal;
gl_Position = ftransform();
}

//FRAGMENT

varying vec3 normal;

void main()
{
vec4 color;
vec3 norm = normalize(normal);
vec3 lightPos = vec3(0,0,0);

vec4 vertPos4 = gl_ModelViewMatrix * gl_Vertex;
vec3 vertPos = vec3(vertPos4);

vec3 lightDir = normalize(lightPos - vertPos);

float intensity = dot(lightDir, norm);

if (intensity > 0.8)
color = vec4(1.0,0.5,0.5,1.0);
else if (intensity > 0.5)
color = vec4(0.6,0.3,0.3,1.0);
else
color = vec4(0.4,0.2,0.2,1.0);

gl_FragColor = color;

}



Both shaders are currently broken. Could anyone shed some light on why this might be?

Brokenmind
03-10-2014, 12:38 PM
This fragment

dot(lightDir, reflect(-lightDir, normal));
should calculate the specular lighting amount, right? Then you might be missing an eye direction!

For the toon shader:

vec3 lightDir = normalize(lightPos - vertPos);
float intensity = dot(lightDir, norm);

If I'm not mistaken, you calculate the light direction to the point with the normal. In this case, the direction is the wrong way around and will always result in angles >90° (dot product < 0). Maybe you could try tweaking these values ;)

€dit: I think, the second part is wrong... maybe you could add some screenshots, that will help in the analysis!

twoski
03-10-2014, 01:03 PM
For some reason my shaders aren't working at all... They just result in a white matte-style shader.

I revert to an older version and i get this:

http://i.imgur.com/NRP5SyS.png

It's missing the specular aspect though



uniform mat4 projection, modelview, normalMat;
uniform int mode;

varying vec4 forFragColor;
const vec3 diffuseColor = vec3(0.5, 0.0, 0.0);


void main(){
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
vec3 lightPos = vec3(0,0,0);

// all following gemetric computations are performed in the
// camera coordinate system (aka eye coordinates)
vec3 normal = vec3(gl_NormalMatrix * gl_Normal);
vec4 vertPos4 = gl_ModelViewMatrix * gl_Vertex;
vec3 vertPos = vec3(vertPos4);
vec3 lightDir = normalize(lightPos - vertPos);

float lambertian = max(dot(lightDir,normal), 0.0);
forFragColor = vec4(lambertian*diffuseColor , 1.0);
}

twoski
03-11-2014, 09:34 AM
I fixed my toon shader!

Apparently

vec4 vertPos4 = gl_ModelViewMatrix * gl_Vertex;
vertPos = vec3(vertPos4);

needs to be handled in the .vert file, it causes errors otherwise.

http://i.imgur.com/A900WoS.png

thokra
03-11-2014, 10:49 AM
twoski: Do you understand the differences between the vertex- and the fragment-language?