• Create Account

Strange specular with normal map (BlinnPhong)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

#1JackShannon  Members

492
Like
0Likes
Like

Posted 25 April 2013 - 08:21 AM

SOLVED

Please can someone take a look at my normal map code, the diffuse is fine.

Without normal map:

With normal map:

/* The following can be defined:
*      TEXTURED
*      NORMALMAPPED
*/

in vec3 v_position;
#if defined(TEXTURED) || defined(NORMALMAPPED)
in vec2 v_uv;
#endif
in vec3 v_normal;
#ifdef NORMALMAPPED
in vec4 v_tangent;
#endif

layout(std140) uniform Transform
{
mat4 t_model_view_proj;
mat4 t_model_view;
mat3 t_normal;
};

#if defined(TEXTURED) || defined(NORMALMAPPED)
out vec2 f_uv;
#endif

#ifdef NORMALMAPPED
layout(std140) uniform Light
{
int l_type;
vec3 l_orientation;
float l_attenuation;
vec3 l_ambient;
vec3 l_diffuse;
vec3 l_specular;
};
out vec3 tbn_lightDirection;
out vec3 tbn_viewDirection;
#else
out vec3 f_position;
out vec3 f_normal;
out vec3 f_viewDirection;
#endif

void main()
{
gl_Position = t_model_view_proj * vec4(v_position, 1.0);

#if defined(TEXTURED) || defined(NORMALMAPPED)
f_uv = v_uv;
#endif

#ifdef NORMALMAPPED
vec3 v_bitangent = v_tangent.z * cross(v_normal, v_tangent.xyz); // tangent.z stores m value which is the determinant of the object space to tangent space matrix

mat3 TBN = transpose(mat3(v_tangent.xyz,
v_bitangent,
v_normal));

tbn_lightDirection = normalize(TBN * l_orientation);
tbn_viewDirection = -normalize(TBN * v_position);
#else
f_position = (t_model_view * vec4(v_position, 1.0)).xyz;
f_normal = normalize(t_normal * v_normal);
f_viewDirection = -normalize(t_model_view * vec4(v_position, 1.0)).xyz;
#endif
}


/* The following can be defined:
*      TEXTURED
*      NORMALMAPPED
*/

#define LTYPE_DIRECTIONAL 0
#define LTYPE_POINT 1

#if defined(TEXTURED) || defined(NORMALMAPPED)
in vec2 f_uv;
#endif
#ifdef TEXTURED
uniform sampler2D diffuseTexture;
#endif

#ifdef NORMALMAPPED
uniform sampler2D normalTexture;
in vec3 tbn_lightDirection;
in vec3 tbn_viewDirection;
#else
in vec3 f_position;
in vec3 f_normal;
in vec3 f_viewDirection;
#endif

layout(std140) uniform Light
{
int l_type;
vec3 l_orientation;
float l_attenuation;
vec3 l_ambient;
vec3 l_diffuse;
vec3 l_specular;
};

layout(std140) uniform Material
{
vec3 m_ambient;
vec3 m_diffuse;
vec3 m_specular; // scale specular by shininess strength in external tool
float m_shininess;
};

out vec4 fragment;

float lambert(vec3 lightDirection, vec3 normal)
{
float lambertTerm = dot(lightDirection, normal);
lambertTerm = clamp(lambertTerm, 0, 1);
return lambertTerm;
}

float blinnPhong(vec3 lightDirection, vec3 normal, vec3 viewDirection)
{
vec3 halfwayDirection = normalize(lightDirection + viewDirection);
float blinnTerm = dot(normal, halfwayDirection);
blinnTerm = clamp(blinnTerm, 0, 1);
blinnTerm = pow(blinnTerm, m_shininess);
return blinnTerm;
}

// not normalized
vec3 getL()
{
#ifdef NORMALMAPPED
return normalize(tbn_lightDirection);
#else
if (l_type == LTYPE_DIRECTIONAL) {
return l_orientation;
}
else if (l_type == LTYPE_POINT) {
return l_orientation - f_position;
}
#endif
}

vec3 getN()
{
#ifdef NORMALMAPPED
vec3 tbnNormal = texture(normalTexture, f_uv).rgb * 2.0 - 1.0;
return normalize(tbnNormal);
#else
return normalize(f_normal);
#endif
}

vec3 getV()
{
#ifdef NORMALMAPPED
return normalize(tbn_viewDirection);
#else
return normalize(f_viewDirection);
#endif
}

void main()
{
vec3 lightDiffuse, lightSpecular = vec3(0);

vec3 lightVector = getL();
vec3 l = normalize(lightVector);
vec3 n = getN();

float lambertTerm = lambert(l, n);

lightDiffuse = l_diffuse * lambertTerm;
if (lambertTerm > 0) {
vec3 v = getV();
lightSpecular = l_specular * blinnPhong(l, n, v);
}

#ifdef TEXTURED
vec3 diffuse = texture(diffuseTexture, f_uv).xyz * lightDiffuse;
#else
vec3 diffuse = m_diffuse * lightDiffuse;
#endif
vec3 specular = m_specular * lightSpecular;

float attenuation;
if (l_type == LTYPE_DIRECTIONAL) {
attenuation = 1;
}
else if (l_type == LTYPE_POINT) {
float distanceToLight = length(lightVector);
float attenuation = 1.0 / (1.0 + l_attenuation * pow(distanceToLight, 2));
}

fragment = vec4(m_ambient + attenuation * (diffuse + specular), 1.0);
//fragment = vec4(diffuse + specular, 1.0);
}


Edited by JackShannon, 25 April 2013 - 04:00 PM.

#2JackShannon  Members

492
Like
0Likes
Like

Posted 25 April 2013 - 03:37 PM

It turns out that the speculars are correct, but are only being rendered on half of the pane. This demonstrates it more clearly:

Why could this be?

EDIT: solved! It turned out that I shouldn't have normalised tbn_viewDirection, this fixed it:

Edited by JackShannon, 27 April 2013 - 09:54 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.