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.
Vertex Shader:
#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;
}
Fragement Shader:
#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;
}
