Mars_999

08-29-2006, 03:19 PM

I am using a directional light, with GLSL and using the following code. The problem is my lighting isn't looking correct.

//vs

varying vec3 lightDir, halfAngle;

varying vec4 diffuse, ambient;

uniform vec4 lightpos, viewpos;

attribute vec3 vTangent, vBiTangent;

void main()

{

vec3 normal = gl_NormalMatrix * normalize(gl_Normal);

vec3 tangent = gl_NormalMatrix * normalize(vTangent);

vec3 bitangent = gl_NormalMatrix * normalize(vBiTangent);

vec3 temp = vec3(viewpos - gl_Vertex);

halfAngle.x = dot(temp, tangent);

halfAngle.y = dot(temp, bitangent);

halfAngle.z = dot(temp, normal);

temp = vec3(lightpos - gl_Vertex);

lightDir.x = dot(temp, tangent);

lightDir.y = dot(temp, bitangent);

lightDir.z = dot(temp, normal);

diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;

ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;

ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;

vec4 realPos = gl_ModelViewMatrix * gl_Vertex;

projCoord = gl_TextureMatrix[5] * realPos;

gl_TexCoord[0].xy = vec2(gl_MultiTexCoord0);

gl_Position = ftransform();

}//fs

varying vec3 lightDir, halfAngle;

varying vec4 diffuse, ambient;

uniform sampler2D texture, BumpTex;

void main()

{

vec4 lightColor = ambient;

vec4 modelColor = texture2D(texture, gl_TexCoord[0].xy);

vec3 lVec = normalize(lightDir);

vec3 normal = vec3(texture2D(BumpTex, gl_TexCoord[0].xy));

normal = vec3(2.0) * (normal - vec3(.5));

normal = normalize(normal);

float NdotL = max(dot(normal, lVec), 0.0);

lightColor += diffuse * NdotL;

if(NdotL &gt; 0.0)

{

vec3 halfVec = normalize(lVec + normalize(halfAngle));

float NdotHV = max(dot(normal, halfVec), 0.0);

lightColor += gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(NdotHV, gl_FrontMaterial.shininess);

}

lightColor = max(lightColor, 0.0);

modelColor = modelColor * lightColor;

gl_FragColor = modelColor;

}any help would be greatly appreciated for I am about to pull all my hair out the last week...

BTW here are the values I get for my TBN

Vertex # 0

Normals

X 0, Y -1, Z 0

Tangents

X -1, Y 0, Z 2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 1

Normals

X 0, Y -1, Z 0

Tangents

X -1, Y 0, Z 2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 2

Normals

X 0, Y -1, Z 0

Tangents

X -1, Y 0, Z 2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 3

Normals

X 0, Y -1, Z 0

Tangents

X -1, Y 0, Z 2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 4

Normals

X 0, Y 1, Z 0

Tangents

X 1, Y 0, Z -2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 5

Normals

X 0, Y 1, Z 0

Tangents

X 1, Y 0, Z -2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 6

Normals

X 0, Y 1, Z 0

Tangents

X 1, Y 0, Z -2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 7

Normals

X 0, Y 1, Z 0

Tangents

X 1, Y 0, Z -2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 8

Normals

X 0, Y 0, Z 1

Tangents

X 1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 9

Normals

X 0, Y 0, Z 1

Tangents

X 1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 10

Normals

X 0, Y 0, Z 1

Tangents

X 1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 11

Normals

X 0, Y 0, Z 1

Tangents

X 1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 12

Normals

X 1, Y 0, Z 0

Tangents

X 0, Y 0, Z -1

Bitangents

X 0, Y -1, Z 0

Vertex # 13

Normals

X 1, Y 0, Z 0

Tangents

X 0, Y 0, Z -1

Bitangents

X 0, Y -1, Z 0

Vertex # 14

Normals

X 1, Y 0, Z 0

Tangents

X 0, Y 0, Z -1

Bitangents

X 0, Y -1, Z 0

Vertex # 15

Normals

X 1, Y 0, Z 0

Tangents

X 0, Y 0, Z -1

Bitangents

X 0, Y -1, Z 0

Vertex # 16

Normals

X 0, Y 0, Z -1

Tangents

X -1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 17

Normals

X 0, Y 0, Z -1

Tangents

X -1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 18

Normals

X 0, Y 0, Z -1

Tangents

X -1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 19

Normals

X 0, Y 0, Z -1

Tangents

X -1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 20

Normals

X -1, Y 0, Z 0

Tangents

X 0, Y 0, Z 1

Bitangents

X 0, Y -1, Z 0

Vertex # 21

Normals

X -1, Y 0, Z 0

Tangents

X 0, Y 0, Z 1

Bitangents

X 0, Y -1, Z 0

Vertex # 22

Normals

X -1, Y 0, Z 0

Tangents

X 0, Y 0, Z 1

Bitangents

X 0, Y -1, Z 0

Vertex # 23

Normals

X -1, Y 0, Z 0

Tangents

X 0, Y 0, Z 1

Bitangents

X 0, Y -1, Z 0

//vs

varying vec3 lightDir, halfAngle;

varying vec4 diffuse, ambient;

uniform vec4 lightpos, viewpos;

attribute vec3 vTangent, vBiTangent;

void main()

{

vec3 normal = gl_NormalMatrix * normalize(gl_Normal);

vec3 tangent = gl_NormalMatrix * normalize(vTangent);

vec3 bitangent = gl_NormalMatrix * normalize(vBiTangent);

vec3 temp = vec3(viewpos - gl_Vertex);

halfAngle.x = dot(temp, tangent);

halfAngle.y = dot(temp, bitangent);

halfAngle.z = dot(temp, normal);

temp = vec3(lightpos - gl_Vertex);

lightDir.x = dot(temp, tangent);

lightDir.y = dot(temp, bitangent);

lightDir.z = dot(temp, normal);

diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;

ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;

ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;

vec4 realPos = gl_ModelViewMatrix * gl_Vertex;

projCoord = gl_TextureMatrix[5] * realPos;

gl_TexCoord[0].xy = vec2(gl_MultiTexCoord0);

gl_Position = ftransform();

}//fs

varying vec3 lightDir, halfAngle;

varying vec4 diffuse, ambient;

uniform sampler2D texture, BumpTex;

void main()

{

vec4 lightColor = ambient;

vec4 modelColor = texture2D(texture, gl_TexCoord[0].xy);

vec3 lVec = normalize(lightDir);

vec3 normal = vec3(texture2D(BumpTex, gl_TexCoord[0].xy));

normal = vec3(2.0) * (normal - vec3(.5));

normal = normalize(normal);

float NdotL = max(dot(normal, lVec), 0.0);

lightColor += diffuse * NdotL;

if(NdotL &gt; 0.0)

{

vec3 halfVec = normalize(lVec + normalize(halfAngle));

float NdotHV = max(dot(normal, halfVec), 0.0);

lightColor += gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(NdotHV, gl_FrontMaterial.shininess);

}

lightColor = max(lightColor, 0.0);

modelColor = modelColor * lightColor;

gl_FragColor = modelColor;

}any help would be greatly appreciated for I am about to pull all my hair out the last week...

BTW here are the values I get for my TBN

Vertex # 0

Normals

X 0, Y -1, Z 0

Tangents

X -1, Y 0, Z 2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 1

Normals

X 0, Y -1, Z 0

Tangents

X -1, Y 0, Z 2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 2

Normals

X 0, Y -1, Z 0

Tangents

X -1, Y 0, Z 2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 3

Normals

X 0, Y -1, Z 0

Tangents

X -1, Y 0, Z 2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 4

Normals

X 0, Y 1, Z 0

Tangents

X 1, Y 0, Z -2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 5

Normals

X 0, Y 1, Z 0

Tangents

X 1, Y 0, Z -2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 6

Normals

X 0, Y 1, Z 0

Tangents

X 1, Y 0, Z -2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 7

Normals

X 0, Y 1, Z 0

Tangents

X 1, Y 0, Z -2.34526e-007

Bitangents

X 2.34526e-007, Y 0, Z 1

Vertex # 8

Normals

X 0, Y 0, Z 1

Tangents

X 1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 9

Normals

X 0, Y 0, Z 1

Tangents

X 1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 10

Normals

X 0, Y 0, Z 1

Tangents

X 1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 11

Normals

X 0, Y 0, Z 1

Tangents

X 1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 12

Normals

X 1, Y 0, Z 0

Tangents

X 0, Y 0, Z -1

Bitangents

X 0, Y -1, Z 0

Vertex # 13

Normals

X 1, Y 0, Z 0

Tangents

X 0, Y 0, Z -1

Bitangents

X 0, Y -1, Z 0

Vertex # 14

Normals

X 1, Y 0, Z 0

Tangents

X 0, Y 0, Z -1

Bitangents

X 0, Y -1, Z 0

Vertex # 15

Normals

X 1, Y 0, Z 0

Tangents

X 0, Y 0, Z -1

Bitangents

X 0, Y -1, Z 0

Vertex # 16

Normals

X 0, Y 0, Z -1

Tangents

X -1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 17

Normals

X 0, Y 0, Z -1

Tangents

X -1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 18

Normals

X 0, Y 0, Z -1

Tangents

X -1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 19

Normals

X 0, Y 0, Z -1

Tangents

X -1, Y 0, Z 0

Bitangents

X 0, Y -1, Z 0

Vertex # 20

Normals

X -1, Y 0, Z 0

Tangents

X 0, Y 0, Z 1

Bitangents

X 0, Y -1, Z 0

Vertex # 21

Normals

X -1, Y 0, Z 0

Tangents

X 0, Y 0, Z 1

Bitangents

X 0, Y -1, Z 0

Vertex # 22

Normals

X -1, Y 0, Z 0

Tangents

X 0, Y 0, Z 1

Bitangents

X 0, Y -1, Z 0

Vertex # 23

Normals

X -1, Y 0, Z 0

Tangents

X 0, Y 0, Z 1

Bitangents

X 0, Y -1, Z 0