Skip to content

Commit

Permalink
Tweaks to parallax oclussion mapping.
Browse files Browse the repository at this point in the history
Improved tangent calculations.
  • Loading branch information
RealBadAngel committed Nov 7, 2013
1 parent 6dc51f6 commit 6b5f230
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
17 changes: 8 additions & 9 deletions client/shaders/solids_shader/opengl_fragment.glsl
Expand Up @@ -15,7 +15,6 @@ uniform vec3 eyePosition;
varying vec3 vPosition;
varying vec3 tsEyeVec;
varying vec3 eyeVec;
varying vec3 normal;

void main (void)
{
Expand All @@ -26,19 +25,19 @@ 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)) {
const float numSteps = 40.0;
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;
Expand All @@ -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;
}
Expand Down
51 changes: 36 additions & 15 deletions client/shaders/solids_shader/opengl_vertex.glsl
@@ -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;

//This is temporary. TODO: Find a way to pass tangent to shader
tangent = normalize(gl_NormalMatrix * gl_Color.rgb);

binormal = cross(normal, tangent);
binormal = normalize(binormal);
mat3 TBNMatrix = mat3(tangent, binormal, normal);
eyeVec = normalize (eyePosition - vPosition);
tsEyeVec = eyeVec * TBNMatrix;
vec3 normal,tangent,binormal;
normal = normalize(gl_NormalMatrix * gl_Normal);

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));
}

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);
Expand Down Expand Up @@ -71,5 +93,4 @@ void main(void)
gl_FrontColor = gl_BackColor = color;

gl_TexCoord[0] = gl_MultiTexCoord0;

}

0 comments on commit 6b5f230

Please sign in to comment.