Created
June 6, 2017 09:10
-
-
Save guidoschmidt/282f2fc311b6a5f3a88ed2bc9bb6f830 to your computer and use it in GitHub Desktop.
GLSL vertex normal computation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mat3 getTangentBasis( in vec3 tangent_y ) | |
{ | |
vec3 UpVector = vec3(0,1,0); | |
vec3 tangent_x = normalize( cross( UpVector, tangent_y ) ); | |
vec3 tangent_z = cross( tangent_y, tangent_x ); | |
return mat3( tangent_x, tangent_y, tangent_z ); | |
} | |
vec3 applyDeformation( in vec3 pos, in vec3 norm ) | |
{ | |
// .. displace vertex | |
} | |
vec3 computeVertexNormal( in vec3 pos, in vec3 norm ) | |
{ | |
float offset = 0.1; | |
mat3 basis = getTangentBasis( norm ); | |
vec3 xv = offset * basis[0]; | |
vec3 zv = offset * basis[2]; | |
vec3 x0 = applyDeformation( pos.xyz - xv, norm ).xyz; | |
vec3 x1 = applyDeformation( pos.xyz + xv, norm ).xyz; | |
vec3 z0 = applyDeformation( pos.xyz - zv, norm ).xyz; | |
vec3 z1 = applyDeformation( pos.xyz + zv, norm ).xyz; | |
return cross( x1 - x0, z1 - z0 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment