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

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

<pre><code>float LdotN = max(0, dot(normalizedPos,transformedNormal));
</code></pre>

<p>I'm not sure about the uniform <code>eye_Position</code>, 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: </p>

<pre><code>vec3 direction = -normalize(vertexCamera);
</code></pre>

<p>The complete fragment shader should be:</p>

<pre><code>#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;
}
</code></pre>

CC BY-SA 3.0
2016-01-12T16:36:27.080Z
2016-01-12T16:36:27.080Z
MadEqua
<p>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.</p>

<p><a href="https://i.sstatic.net/eKOTe.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/eKOTe.jpg" alt="enter image description here"></a></p>

<p>Vertex Shader: </p>

<pre><code>#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;
}
</code></pre>

<p>Fragement Shader:</p>

<pre><code>#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;
}
</code></pre>

CC BY-SA 3.0
2016-01-11T22:11:03.257Z
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
CC BY-SA 3.0
2016-01-11T22:13:12.750Z
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
CC BY-SA 3.0
2016-01-12T01:16:59.820Z
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
<opengl><shader><glsl><fragment-shader>
Shader won't work
eneko
# Shader won't work

(Asked by: username_0 on 2016-01-11)

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.

[![enter image description here](https://i.sstatic.net/eKOTe.jpg)](https://i.sstatic.net/eKOTe.jpg)

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;
}

```

**Tags:** opengl, shader, glsl, fragment-shader

> **Comments:**
> **username_1**: 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.  
> **username_0**: 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.  
> **username_2**: 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.  

---

## 1 answers

### Answer by username_3 on 2016-01-12. Score: 2 | Accepted answer

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;
}

```


---

