View Full Version : Specular lighting looks backwards / inverted

02-26-2015, 09:58 PM
I'm trying to implement a basic Phong lighting shader. The diffuse and ambient lighting looks fine but the specular light is rendering very strangely. For every block of tiles it looks as if the section of light has been flipped. My model is a 2D plane of tiles, each of which are made up of 64 smaller tiles. I draw all 64 via one VBO. My normals are all 0,1,0 - for every vertex on the model as it is positioned along the x,z plane.

Does anyone have any idea what would be causing this? I've tried winding my vertices in different orders and all kinds of edits and tests in the fragment shader just to see if anything would alter this effect but nothing works. I haven't even been able to get it to look wrong any other way (aside from being totally black)!

Here is my vertex shader:

#version 430

layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexNormal;
layout (location = 2) in vec2 VertexTexCoord;

out vec3 Position;
out vec3 Normal;
out vec2 TexCoord;

out vec4 MVPPos;

uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
//uniform mat4 ProjectionMatrix;
//uniform mat4 MVP;
out mat4 MVP;

uniform mat4 Camera;
uniform mat4 Model;
uniform mat4 Projection;

void main()
TexCoord = VertexTexCoord;
Normal = normalize( NormalMatrix * VertexNormal );
Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) );
//gl_Position = MVP * vec4(VertexPosition,1.0);

MVP = Projection * Camera * Model;

MVPPos = MVP * vec4(VertexPosition,1.0);
gl_Position = MVPPos;

and the Fragment shader:

#version 430

in vec3 Position;
in vec3 Normal;
in vec2 TexCoord;

//uniform sampler2D Tex1;
layout(binding=0) uniform sampler2D Tex1;

struct LightPos {
vec4 LightVector;
mat4 CameraPos;
uniform LightPos SetLight;

struct LightInfo {
vec4 Position; //light pos in eye coords
vec3 Intensity; //ads light intensity
uniform LightInfo Light;

struct MaterialInfo {
vec3 Ka; //ambient reflectivity
vec3 Kd; //diffuse reflectivity
vec3 Ks; //specular reflectivity
float Shininess; //specular shininess factor
uniform MaterialInfo Material;

layout ( location = 0 ) out vec4 FragColor;

void phongModel (vec3 position, vec3 norm, out vec3 ambanddiff, out vec3 spec)

//vec4 LightPosition = SetLight.CameraPos * SetLight.LightVector;
vec4 LightPosition = SetLight.LightVector * SetLight.CameraPos ;

//vec3 s = normalize( vec3(Light.Position) - position);
vec3 s = normalize( vec3(LightPosition) - position);
vec3 v = normalize( -position.xyz );
vec3 r = reflect( -s, norm );
vec3 ambient = Light.Intensity * Material.Ka;
float sDotN = max( dot( s, norm ), 0.0);
vec3 diffuse = Light.Intensity * Material.Kd * sDotN;
spec = vec3(0.0);

if ( sDotN > 0.0 ) {
spec = Light.Intensity * Material.Ks * pow(max( dot(r,v), 0.0), Material.Shininess);

ambanddiff = ambient + diffuse;


void main(void)
vec3 ambanddiff, spec;
vec4 texColor = texture( Tex1, TexCoord );

phongModel (Position, Normal, ambanddiff, spec);

FragColor = vec4(ambanddiff, 1.0) * texColor + vec4(spec, 1.0);

Shader related snippet from the initialize function:

shader = new Shade();
shader.load(vertexs, Shade.ShaderType.VERTEX);
shader.load(frags, Shade.ShaderType.FRAGMENT);
//before linking, indicate which vars we will want to read back via Transform Feedback
//In this case I want the vertex values.
//String[] varyings = {"MVPPos"};
//glTransformFeedbackVaryings(shader.iProgram, 1, varyings, GL2ES3.GL_INTERLEAVED_ATTRIBS);
//glTransformFeedbackVaryings(shader.iProgram, new CharSequence[]{"MVPPos"}, GL_INTERLEAVED_ATTRIBS);

iModelView = shader.getUniform("ModelViewMatrix");
iNorm = shader.getUniform("NormalMatrix");

iCam = shader.getUniform("Camera");
iModel = shader.getUniform("Model");
iProj = shader.getUniform("Projection");

iLightCam = shader.getUniform("SetLight.CameraPos");
iLightVec = shader.getUniform("SetLight.LightVector");
float[] intensity = {0.4f,0.5f,0.4f};
FloatBuffer fbintensity = BufferUtils.createFloatBuffer(4);
fbintensity = FloatBuffer.wrap(intensity);
shader.setUniform("Light.Intensity", fbintensity );
shader.setUniform("Material.Ka", 0.9f, 0.7f, 0.7f);
shader.setUniform("Material.Kd", 0.1f, 0.4f, 0.2f );
shader.setUniform("Material.Ks", 0.6f, 0.6f, 0.6f);
shader.setUniform("Material.Shininess", 0.3f);

Normal, projection, camera and model passed to shader in Render function:


//set translate
vTrans.set(transx + x, (transy + y), transz + z);
Matrix4f.translate(vTrans, model1, model);

vTrans.set(-(transx + x), -(transy + y), -(transz + z));
Matrix4f.translate(vTrans, model, model);
Matrix4f.rotate((float) Math.toRadians(rotY), new Vector3f(0,1,0), model, model);
vTrans.set((transx + x), (transy + y), (transz + z));
Matrix4f.translate(vTrans, model, model);

Matrix4f.mul(model, camera, modelview);
shader.setUniform(iModelView, modelview);

Matrix3f norm = new Matrix3f();
norm.m00 = modelview.m00;
norm.m01 = modelview.m01;
norm.m02 = modelview.m02;
norm.m10 = modelview.m10;
norm.m11 = modelview.m11;
norm.m12 = modelview.m12;
norm.m20 = modelview.m20;
norm.m21 = modelview.m21;
norm.m22 = modelview.m22;

shader.setUniform(iNorm, norm);

shader.setUniform(iProj, projection);
shader.setUniform(iCam, camera);

shader.setUniform(iModel, model);

Any help would be greatly appreciated!