Jump to content
  • Advertisement
Sign in to follow this  

OpenGL ES Phone Lighting?

Recommended Posts

Any sage advice for me available? I am trying to get a phone shader up and working, although I am running into some sad blocks in OpenGL ES 2.0.


I have used this article as a starting point:



Do I just need MORE polygons, or am I fundamentally misunderstanding something? Please help me, very sorry.


Vertex Shader:

uniform mat4 ProjectionMatrix;

uniform mat4 ModelViewMatrix;

uniform mat4 NormalMatrix;


attribute vec3 Positions;


attribute vec3 Normals;


attribute vec3 TextureCoords;

varying vec2 TextureCoordsOut;


varying vec3 N;

varying vec3 v;


void main(void) {

    v = vec3(NormalMatrix * vec4(Positions[0], Positions[1], Positions[2], 1.0));


    N = normalize(vec3(NormalMatrix * vec4(Normals[0], Normals[1], Normals[2], 1.0)));


    gl_Position = ProjectionMatrix * ModelViewMatrix * vec4(Positions[0], Positions[1], Positions[2], 1.0);


    TextureCoordsOut = vec2(TextureCoords[0], TextureCoords[1]);





Fragment Shader:


varying mediump vec3 N;

varying mediump vec3 v;


uniform lowp vec4 ModulateColor;

varying lowp vec2 TextureCoordsOut;

uniform sampler2D Texture;


//r, g, b, [ambient intensity]

uniform lowp vec4 Ambient;


//dirX, dirY, dirZ, [diffuse intensity]

uniform lowp vec4 Diffuse;


//Shininess, Specular Intensity

uniform lowp vec2 Specular;


void main (void) {


    mediump vec3 Direction = vec3(-Diffuse[0], -Diffuse[1], -Diffuse[2]);

    //highp vec3 SpotlightPosition = vec3(Specular[0], Specular[1], Specular[2]);


    //highp vec3 L = normalize(SpotlightPosition - v);

    mediump vec3 L = Direction;//normalize(SpotlightPosition - v);


    mediump vec3 E = normalize(v); // we are in Eye Coordinates, so EyePos is (0,0,0)

    mediump vec3 R = normalize(-reflect(L,N));


    //calculate Diffuse Term:

    lowp float Idiff = max(dot(N,L), 0.0) * Diffuse[3];

    Idiff = clamp(Idiff, 0.0, 1.0);


    // calculate Specular Term:

    lowp float Ispec = pow(max(dot(R, E), 0.0), Specular[0]) * Specular[1];

    Ispec = clamp(Ispec, 0.0, 100.0);

    lowp float Ilight = Ambient[3] + Idiff + Ispec

    gl_FragColor = vec4(Ilight, Ilight, Ilight, 1.0);   








Share this post

Link to post
Share on other sites

Try to normalize N varying vector, it should help a bit.

Direction vectors do not interpolate good so on the edges there is artifact always, but should not be this big.


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!