# Shader won't work

I have managed to implement a garoud shader with specular lighting efects in Processing 3.0 . Now I am trying with a fragment Phong shader but cannot make it work. I can´t find where is the error. It should just implent the phong illumination model, with specular and diffuse components.

#define PROCESSING_LIGHT_SHADER

uniform mat4 modelview;
uniform mat4 transform;
uniform mat3 normalMatrix;

attribute vec4 vertex;
attribute vec4 color;
attribute vec3 normal;

varying vec4 vertColor;

varying vec3 transformedNormal;
varying vec3 vertexCamera;

void main() {
gl_Position = transform * vertex;

vertexCamera = vec3(modelview * vertex);
transformedNormal = normalize(normalMatrix * normal);
vertColor = color;
}


#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

uniform vec3 lightPosition;
uniform vec3 eye_Position;

varying vec4 vertColor;
varying vec3 lightDir;

varying vec3 transformedNormal; //world pos
varying vec3 vertexCamera; // world normal

void main() {

vec3 normalizedPos =  normalize(lightPosition.xyz -vertexCamera);
vec3 direction = normalize(eye_Position - vertexCamera);

float intensity = 0.0;
float specular = 0.0;

float LdotN = max(0, dot(normalizedPos,direction));
float diffuse = 1 * LdotN;

vec3 R = -normalize(reflect(normalizedPos,transformedNormal));
specular = pow( max(0, dot( R, direction)), 16);

intensity += (diffuse + specular);

gl_FragColor = vec4(intensity, intensity, intensity, 1) * vertColor;
}

• So what's the problem, specifically? "I cannot make it work" doesn't give us much to go on. Are you getting compiler errors? Post them. Bad output? Show us a screenshot. – Nathan Reed Jan 11 '16 at 22:11
• I have just started with shaders. Sorry about that, I am a little bit desperate right now... It compiles just fine. But won't affect the lighting in any way. The objects just stays there unaffected by the shader, same as it it was not even loaded. – eneko Jan 11 '16 at 22:13
• as a test, you might make the sphere have a color based on some value that you are using to calculate your lighting. This is a really primitive way of getting an idea of the value of your variables. Doing this, you might notice that something is constant across the sphere which shouldn't be, or a similar problem, which will then help point you in the direction of what is going wrong specifically. – Alan Wolfe Jan 12 '16 at 1:16

If you are using Processing the variable lightPosition must be declared as a vec4, otherwise it won't be passed in, and will be always 0.

Then your diffuse computation is not correct, the dot product we want it's between the normal and the light direction:

float LdotN = max(0, dot(normalizedPos,transformedNormal));


I'm not sure about the uniform eye_Position, I believe it's always 0 as nobody is setting it. Anyway, it is not needed. Since the fragment position it's already on camera space, the camera direction is simply:

vec3 direction = -normalize(vertexCamera);


The complete fragment shader should be:

#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

uniform vec4 lightPosition;

varying vec4 vertColor;
varying vec3 transformedNormal;
varying vec3 vertexCamera;

void main() {

vec3 normalizedPos =  normalize(lightPosition.xyz -vertexCamera);
vec3 direction = -normalize(vertexCamera);

float intensity = 0.0;
float specular = 0.0;

float LdotN = max(0, dot(normalizedPos,transformedNormal));
float diffuse = 1 * LdotN;

vec3 R = -normalize(reflect(normalizedPos,transformedNormal));
specular = pow( max(0, dot( R, direction)), 16);

intensity += (diffuse + specular);

gl_FragColor = vec4(intensity, intensity, intensity, 1) * vertColor;
}