Tweaks to parallax oclussion mapping.

Improved tangent calculations.
master
RealBadAngel 2013-11-07 04:18:36 +01:00
parent 6dc51f6b2c
commit 6b5f23003b
2 changed files with 42 additions and 22 deletions

View File

@ -15,7 +15,6 @@ uniform vec3 eyePosition;
varying vec3 vPosition;
varying vec3 tsEyeVec;
varying vec3 eyeVec;
varying vec3 normal;
void main (void)
{
@ -26,11 +25,11 @@ void main (void)
vec2 uv = gl_TexCoord[0].st;
float height;
vec2 tsEye = -tsEyeVec.xy;
if ((parallaxMappingMode == 1.0) && (use_normalmap > 0.0)) {
float map_height = texture2D(normalTexture, uv).a;
float height = parallaxMappingScale * map_height - parallaxMappingBias;
uv = uv + height * tsEye * normal.z;
uv = uv + height * tsEye;
}
if ((parallaxMappingMode == 2.0) && (use_normalmap > 0.0)) {
@ -38,7 +37,7 @@ void main (void)
float height = 1.0;
float step = 1.0 / numSteps;
vec4 NB = texture2D(normalTexture, uv);
vec2 delta = tsEye * parallaxMappingScale * normal.z / numSteps;
vec2 delta = tsEye * parallaxMappingScale / numSteps;
for (float i = 0.0; i < numSteps; i++) {
if (NB.a < height) {
height -= step;
@ -51,14 +50,14 @@ void main (void)
}
if ((enable_bumpmapping == 1.0) && (use_normalmap > 0.0)) {
vec3 base = texture2D(baseTexture, uv).rgb;
vec4 base = texture2D(baseTexture, uv);
vec3 vVec = normalize(tsEyeVec);
vec3 bump = normalize(texture2D(normalTexture, uv).xyz * 2.0 - 1.0);
vec3 R = reflect(-eyeVec, bump);
vec3 lVec = normalize(vec3(0.0, -0.4, 0.5)); // fake light
vec3 R = reflect(-vVec, bump);
vec3 lVec = normalize(vec3(0.0, -0.4, 0.5));
float diffuse = max(dot(lVec, bump), 0.0);
color = diffuse * base;
float specular = pow(clamp(dot(R, lVec), 0.0, 1.0),1.0);
color += vec3(0.1 * specular * diffuse);
color = diffuse * base + 0.1 * specular * diffuse;
} else {
color = texture2D(baseTexture, uv).rgb;
}

View File

@ -1,32 +1,54 @@
uniform mat4 mWorldViewProj;
uniform mat4 mInvWorld;
uniform mat4 mTransWorld;
uniform float dayNightRatio;
uniform float dayNightRatio;
uniform vec3 eyePosition;
varying vec3 vPosition;
varying vec3 eyeVec;
varying vec3 tsEyeVec;
varying vec3 normal;
void main(void)
{
gl_Position = mWorldViewProj * gl_Vertex;
vPosition = (mWorldViewProj * gl_Vertex).xyz;
normal = normalize (gl_NormalMatrix * gl_Normal);
vec3 tangent,binormal;
vec3 normal,tangent,binormal;
normal = normalize(gl_NormalMatrix * gl_Normal);
//This is temporary. TODO: Find a way to pass tangent to shader
tangent = normalize(gl_NormalMatrix * gl_Color.rgb);
if (gl_Normal.x > 0.5) {
// 1.0, 0.0, 0.0
tangent = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, -1.0));
binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
} else if (gl_Normal.x < -0.5) {
// -1.0, 0.0, 0.0
tangent = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, 1.0));
binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
} else if (gl_Normal.y > 0.5) {
// 0.0, 1.0, 0.0
tangent = normalize(gl_NormalMatrix * vec3( 1.0, 0.0, 0.0));
binormal = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, 1.0));
} else if (gl_Normal.y < -0.5) {
// 0.0, -1.0, 0.0
tangent = normalize(gl_NormalMatrix * vec3( 1.0, 0.0, 0.0));
binormal = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, 1.0));
} else if (gl_Normal.z > 0.5) {
// 0.0, 0.0, 1.0
tangent = normalize(gl_NormalMatrix * vec3( 1.0, 0.0, 0.0));
binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
} else if (gl_Normal.z < -0.5) {
// 0.0, 0.0, -1.0
tangent = normalize(gl_NormalMatrix * vec3(-1.0, 0.0, 0.0));
binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
}
binormal = cross(normal, tangent);
binormal = normalize(binormal);
mat3 TBNMatrix = mat3(tangent, binormal, normal);
eyeVec = normalize (eyePosition - vPosition);
tsEyeVec = eyeVec * TBNMatrix;
mat3 tbnMatrix = mat3( tangent.x, binormal.x, normal.x,
tangent.y, binormal.y, normal.y,
tangent.z, binormal.z, normal.z);
eyeVec = (gl_ModelViewMatrix * gl_Vertex).xyz;
tsEyeVec = normalize(eyeVec * tbnMatrix);
vec4 color;
//color = vec4(1.0, 1.0, 1.0, 1.0);
@ -71,5 +93,4 @@ void main(void)
gl_FrontColor = gl_BackColor = color;
gl_TexCoord[0] = gl_MultiTexCoord0;
}