![]() |
|
#1
|
|||
|
|||
I went ahead and attached the model and textures.
Thanks, George Vertex: Code:
#version 120 uniform int NumLights; varying vec3 vPosition; varying vec3 tPosition; varying vec3 tView; varying vec3 tang; varying vec3 binorm; varying vec3 normal; varying vec3 tLightPosition[gl_MaxLights]; varying vec3 tLightDirection[gl_MaxLights]; attribute vec3 Tangent; attribute vec3 Binormal; void main() { //Calculate components in view space vec3 position = vec3(gl_ModelViewMatrix * gl_Vertex); normal = normalize(gl_NormalMatrix * gl_Normal); tang = normalize(gl_NormalMatrix * Tangent); binorm = normalize(gl_NormalMatrix * Binormal); mat3 mToTangentSpace = mat3( tang.x, binorm.x, normal.x, tang.y, binorm.y, normal.y, tang.z, binorm.z, normal.z); tView = mToTangentSpace * (position * -1.0); tPosition = mToTangentSpace * position; //Iterate over every light and transition position into tangent space for( int i = 0 ; i < NumLights ; i++) { tLightPosition[i] = mToTangentSpace * gl_LightSource[i].spotDirection.xyz; tLightPosition[i] = mToTangentSpace * gl_LightSource[i].position.xyz; } //Pass and transform texture coordinates gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; //Pass Perspecitive Coordinates gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; //Compute Fog gl_FogFragCoord = gl_FogCoord; } Code:
#version 120 uniform int NumLights; varying vec3 vPosition; varying vec3 tPosition; varying vec3 tView; varying vec3 normal; varying vec3 tang; varying vec3 binorm; varying vec3 tLightPosition[gl_MaxLights]; varying vec3 tLightDirection[gl_MaxLights]; uniform sampler2D Albedo; uniform sampler2D NormalMap; uniform sampler2D AmbientOcclusionMap; //Calculates and returns the contribution of a directional light void DirectionalLight(in int i, in vec3 position, in vec3 view, in vec3 normal, inout vec4 ambient, inout vec4 diffuse, inout vec4 specular) { float nDotL; //normal dotted with light vector float nDotH; //normal dotted with half vector float pf; nDotL = max(0.0, dot(normal, normalize(tLightPosition[i]))); nDotH = max(0.0, dot(normal, normalize( normalize(tLightPosition[i]) + view) )); if( nDotL > 0.0) pf = pow(nDotH, gl_FrontMaterial.shininess); else pf = 0.0; ambient += gl_LightSource[i].ambient; diffuse += gl_LightSource[i].diffuse * nDotL; specular += gl_LightSource[i].specular * pf; } //Caculates and returns the contribution of a spotlight void PointLight( in int i, in vec3 position, in vec3 view, in vec3 iNormal, inout vec4 ambient, inout vec4 diffuse, inout vec4 specular) { float nDotL; float nDotH; float pf; float attenuation; float d; vec3 LP; vec3 halfVector; LP = tLightPosition[i] - position; d = length(LP); // distance from surface to light for attenuation vec3 tLP = normalize(tLightPosition[i]); attenuation = 1.0 / (gl_LightSource[i].constantAttenuation + gl_LightSource[i].linearAttenuation * d + gl_LightSource[i].quadraticAttenuation * d * d); halfVector = normalize(tLP + view); nDotL = max(0.0, dot(iNormal, tLP)); nDotH = max(0.0, dot(iNormal, halfVector)); if (nDotL > 0.0) pf = pow(nDotH, gl_FrontMaterial.shininess); else pf = 0.0; ambient += gl_LightSource[i].ambient * attenuation; diffuse += gl_LightSource[i].diffuse * nDotL * attenuation; specular += gl_LightSource[i].specular * pf * attenuation; } void SpotLight(in int i, in vec3 position, in vec3 view, in vec3 normal, inout vec4 ambient, inout vec4 diffuse, inout vec4 specular) { float nDotL; float nDotH; float pf; float spotDot; float spotAttenuation; float attenuation; float d; vec3 LV; vec3 halfVector; LV = tLightPosition[i] - position; d = length(LV); LV = normalize(LV); attenuation = 1.0 / (gl_LightSource[i].constantAttenuation + gl_LightSource[i].linearAttenuation * d + gl_LightSource[i].quadraticAttenuation * d * d); spotDot = dot(LV, normalize( tLightDirection[i])); if(spotDot < gl_LightSource[i].spotCosCutoff) spotAttenuation = 0.0; else spotAttenuation = pow(spotDot, gl_LightSource[i].spotExponent); attenuation *= spotAttenuation; halfVector = normalize(LV + view); nDotL = max(0.0, dot(normal, LV)); nDotH = max(0.0, dot(normal, halfVector)); if(nDotL == 0.0) pf = 0.0; else pf = pow(nDotH, gl_FrontMaterial.shininess); ambient += gl_LightSource[i].ambient * attenuation; diffuse += gl_LightSource[i].diffuse * nDotL * attenuation; specular += gl_LightSource[i].specular * pf * attenuation; } void main() { vec4 albedo = texture2D(Albedo , gl_TexCoord[0].xy); vec3 texNormal = normalize(texture2D(NormalMap, gl_TexCoord[0].xy).rgb); vec3 ao = texture2D(AmbientOcclusionMap, gl_TexCoord[0].xy).rgb; vec4 ambient = vec4(0.0); vec4 diffuse = vec4(0.0); vec4 specular = vec4(0.0); vec3 ntView = normalize(tView); for(int i = 0; i < NumLights; i++) { if (gl_LightSource[i].position.w == 0.0) DirectionalLight(i, tPosition, ntView, texNormal, ambient, diffuse, specular); else if (gl_LightSource[i].spotCutoff == 180.0) PointLight(i, tPosition, ntView, texNormal, ambient, diffuse, specular); else SpotLight(i, tPosition, ntView, texNormal, ambient, diffuse, specular); } //Linear fog calculation float dist = abs(vPosition.z); float fogFactor =(gl_Fog.end - dist) * gl_Fog.scale; fogFactor = clamp(fogFactor, 0.0, 1.0); vec4 color = (ambient * gl_FrontMaterial.ambient + diffuse * gl_FrontMaterial.diffuse) * albedo; //Albedo color = color + (specular * gl_FrontMaterial.specular);//Spec //color = color * vec4(ao,1.0); color = mix( gl_Fog.color, color, fogFactor); //Fog gl_FragColor = color; } |
#2
|
|||
|
|||
Thanks. I think the problem is with your normal map lookup code. The texture color values range from 0 to 1. However, the normals need to range from -1 to 1. Also, it appears the green channel in your normal map is flipped. Try modifying your normal map lookup to the following:
Code:
vec3 texNormal = normalize(texture2D(NormalMap, gl_TexCoord[0].xy).rgb * 2.0 - 1.0) * vec3(1.0,-1.0,1.0); |
#3
|
|||
|
|||
Wow, I must have hit undo at some point in my code because I remember renormalizing the normal map from -1 to +1 ... but it sure isn't in that code...
I'll give your tips a try. Not sure why the green channel is flipped, I'll look into that as well. Thanks for your great support, ~George |
![]() |
Tags |
binormal, normal, tangent |
Thread Tools | |
Display Modes | Rate This Thread |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Per vertex attributes in OTF objects? | stefs | Vizard | 1 | 10-23-2009 03:36 PM |
Semi-circle array containing target and distractor objects | ptjt255 | Vizard | 3 | 08-04-2009 03:09 AM |
Normal Map Workflow for Vizard | shivanangel | Vizard | 2 | 03-10-2009 06:54 PM |
Could not find plugin to load objects... | halley | Vizard | 1 | 05-30-2006 11:01 AM |