Tweaks to parallax oclussion mapping.

Improved tangent calculations.
This commit is contained in:
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 vPosition;
varying vec3 tsEyeVec; varying vec3 tsEyeVec;
varying vec3 eyeVec; varying vec3 eyeVec;
varying vec3 normal;
void main (void) void main (void)
{ {
@ -30,7 +29,7 @@ void main (void)
if ((parallaxMappingMode == 1.0) && (use_normalmap > 0.0)) { if ((parallaxMappingMode == 1.0) && (use_normalmap > 0.0)) {
float map_height = texture2D(normalTexture, uv).a; float map_height = texture2D(normalTexture, uv).a;
float height = parallaxMappingScale * map_height - parallaxMappingBias; float height = parallaxMappingScale * map_height - parallaxMappingBias;
uv = uv + height * tsEye * normal.z; uv = uv + height * tsEye;
} }
if ((parallaxMappingMode == 2.0) && (use_normalmap > 0.0)) { if ((parallaxMappingMode == 2.0) && (use_normalmap > 0.0)) {
@ -38,7 +37,7 @@ void main (void)
float height = 1.0; float height = 1.0;
float step = 1.0 / numSteps; float step = 1.0 / numSteps;
vec4 NB = texture2D(normalTexture, uv); 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++) { for (float i = 0.0; i < numSteps; i++) {
if (NB.a < height) { if (NB.a < height) {
height -= step; height -= step;
@ -51,14 +50,14 @@ void main (void)
} }
if ((enable_bumpmapping == 1.0) && (use_normalmap > 0.0)) { 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 bump = normalize(texture2D(normalTexture, uv).xyz * 2.0 - 1.0);
vec3 R = reflect(-eyeVec, bump); vec3 R = reflect(-vVec, bump);
vec3 lVec = normalize(vec3(0.0, -0.4, 0.5)); // fake light vec3 lVec = normalize(vec3(0.0, -0.4, 0.5));
float diffuse = max(dot(lVec, bump), 0.0); float diffuse = max(dot(lVec, bump), 0.0);
color = diffuse * base;
float specular = pow(clamp(dot(R, lVec), 0.0, 1.0),1.0); 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 { } else {
color = texture2D(baseTexture, uv).rgb; color = texture2D(baseTexture, uv).rgb;
} }

View File

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