<p>I'm developing a simple educational project to study OpenGL ES.
I'm try to render some simple OBJ models using Blinn-Phong.
The first version of the of my implementation calculate all the lighting equation in this way:</p>

<pre><code>    #version 300 es

struct material {
    vec4 ka;
    vec4 kd;
    vec4 ks;
    float sh;
};

struct pointLight {
    vec3 position;
    vec4 color;
};

uniform mat4 mvMatrix;
uniform mat4 mvpMatrix;
uniform mat4 normalMatrix;
uniform pointLight light;
uniform material surfaceMaterial;

layout(location = 0) in vec3 inputPosition;
layout(location = 1) in vec3 inputNormal;

out vec4 v_color;

void main() {

    //Convert vertex and normal to camera space.
    vec3 normal = vec3(normalize(normalMatrix * vec4(inputNormal, 0.0)));
    vec4 vertexPositionHomogeneous = mvMatrix * vec4(inputPosition, 1.0);
    vec3 vertexPosition = vec3(vertexPositionHomogeneous) / vertexPositionHomogeneous.w;

    //Calculate light direction and view direction.
    vec3 lightDirection = normalize(light.position - vertexPosition);
    vec3 viewPosition = vec3(0.0, 0.0, 1.0);
    vec3 viewDirection = normalize(-1.0 *  vertexPosition);

    //Cosine theta diffuse lambertian component.
    float cosTheta = max(0.0, dot(normal, normalize(lightDirection)));

    //H vector for specular component.
    vec3 h = normalize(lightDirection + viewDirection);

    vec4 ambient = surfaceMaterial.ka * light.color;
    vec4 diffuse = surfaceMaterial.kd * light.color * cosTheta;
    vec4 specular = surfaceMaterial.ks * light.color * pow(max(0.0, dot(h, normal)), surfaceMaterial.sh);

    //Output position and color for fragment shader.
    v_color = specular;
    gl_Position = mvpMatrix * vec4(inputPosition, 1.0);
}
</code></pre>

<p>This version doesn't work. This is what I obtain:</p>

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

<p>If I move the calculation to the fragment shader it works. The is the result:</p>

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

<p>This image is obtained using a fragment shader that contains the same lighting calculation that I used in the vertex shader above. So my question is: why the vertex shader only version is not working? Am I missing something about how vertex and fragment shaders works? </p>

CC BY-SA 3.0
2016-06-06T06:15:01.990Z
I can't test this until I get home so can't fix your main problem right now, but there's a fair bit of unnecessary maths in your shader :
russ
CC BY-SA 3.0
2016-06-06T06:27:01.367Z
the W divide of vertexPositionHomogenous is pointless as only the projection matrix alters W ; you're normalizing lightDirection twice, once in the declaration and once in the calculation of cosTheta ; and the halfway vector calculation is cheaper as (lightDirection + viewDirection) * 0.5 .... could you post the fragment shader you're using with this vertex shader as well? Because other than that the maths look right.
russ
<opengl><lighting><opengl-es>
OpenGL ES vertex/fragment shaders - Blinn phong
Fabrizio Duroni
# OpenGL ES vertex/fragment shaders - Blinn phong

(Asked by: username_0 on 2016-06-02)

I'm developing a simple educational project to study OpenGL ES.
I'm try to render some simple OBJ models using Blinn-Phong.
The first version of the of my implementation calculate all the lighting equation in this way:

```
    #version 300 es

struct material {
    vec4 ka;
    vec4 kd;
    vec4 ks;
    float sh;
};

struct pointLight {
    vec3 position;
    vec4 color;
};

uniform mat4 mvMatrix;
uniform mat4 mvpMatrix;
uniform mat4 normalMatrix;
uniform pointLight light;
uniform material surfaceMaterial;

layout(location = 0) in vec3 inputPosition;
layout(location = 1) in vec3 inputNormal;

out vec4 v_color;

void main() {

    //Convert vertex and normal to camera space.
    vec3 normal = vec3(normalize(normalMatrix * vec4(inputNormal, 0.0)));
    vec4 vertexPositionHomogeneous = mvMatrix * vec4(inputPosition, 1.0);
    vec3 vertexPosition = vec3(vertexPositionHomogeneous) / vertexPositionHomogeneous.w;

    //Calculate light direction and view direction.
    vec3 lightDirection = normalize(light.position - vertexPosition);
    vec3 viewPosition = vec3(0.0, 0.0, 1.0);
    vec3 viewDirection = normalize(-1.0 *  vertexPosition);

    //Cosine theta diffuse lambertian component.
    float cosTheta = max(0.0, dot(normal, normalize(lightDirection)));

    //H vector for specular component.
    vec3 h = normalize(lightDirection + viewDirection);

    vec4 ambient = surfaceMaterial.ka * light.color;
    vec4 diffuse = surfaceMaterial.kd * light.color * cosTheta;
    vec4 specular = surfaceMaterial.ks * light.color * pow(max(0.0, dot(h, normal)), surfaceMaterial.sh);

    //Output position and color for fragment shader.
    v_color = specular;
    gl_Position = mvpMatrix * vec4(inputPosition, 1.0);
}

```

This version doesn't work. This is what I obtain:

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

If I move the calculation to the fragment shader it works. The is the result:

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

This image is obtained using a fragment shader that contains the same lighting calculation that I used in the vertex shader above. So my question is: why the vertex shader only version is not working? Am I missing something about how vertex and fragment shaders works?

**Tags:** opengl, lighting, opengl-es

> **Comments:**
> **username_1**: I can't test this until I get home so can't fix your main problem right now, but there's a fair bit of unnecessary maths in your shader :  
> **username_1**: the W divide of vertexPositionHomogenous is pointless as only the projection matrix alters W ; you're normalizing lightDirection twice, once in the declaration and once in the calculation of cosTheta ; and the halfway vector calculation is cheaper as (lightDirection + viewDirection) * 0.5 .... could you post the fragment shader you're using with this vertex shader as well? Because other than that the maths look right.  

---

## 0 answers

