Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1452 | chris | 1 | // Copyright (c) 2008 the OpenTK Team. See license.txt for legal bla |
| 2 | |||
| 3 | // Material uniforms |
||
| 4 | uniform sampler2D Material_DiffuseAndHeight; |
||
| 5 | uniform sampler2D Material_NormalAndGloss; |
||
| 6 | uniform vec3 Material_ScaleBiasShininess; // x=Scale, y=Bias, z=Shininess |
||
| 7 | |||
| 8 | // Light uniforms |
||
| 9 | uniform vec3 Light_DiffuseColor; |
||
| 10 | uniform vec3 Light_SpecularColor; |
||
| 11 | |||
| 12 | // from VS |
||
| 13 | varying vec3 VaryingLightVector; |
||
| 14 | varying vec3 VaryingEyeVector; |
||
| 15 | |||
| 16 | vec3 normal; |
||
| 17 | |||
| 18 | void main() |
||
| 19 | { |
||
| 20 | vec3 lightVector = normalize( VaryingLightVector ); |
||
| 21 | vec3 eyeVector = normalize( VaryingEyeVector ); |
||
| 22 | |||
| 23 | // first, find the parallax displacement by reading only the height map |
||
| 24 | float parallaxOffset = texture2D( Material_DiffuseAndHeight, gl_TexCoord[0].st ).a * |
||
| 25 | Material_ScaleBiasShininess.x - Material_ScaleBiasShininess.y; |
||
| 26 | vec2 newTexCoords = gl_TexCoord[0].st + ( parallaxOffset * eyeVector.xy ); // displace texcoords according to viewer |
||
| 27 | |||
| 28 | // knowing the displacement, read RGB, Normal and Gloss |
||
| 29 | vec3 diffuseColor = texture2D( Material_DiffuseAndHeight, newTexCoords.st ).rgb; |
||
| 30 | vec4 temp = texture2D( Material_NormalAndGloss, newTexCoords.st ); |
||
| 31 | |||
| 32 | // build a usable normal vector |
||
| 33 | normal.xy = temp.ag * 2.0 - 1.0; // swizzle alpha and green to x/y and scale to [-1..+1] |
||
| 34 | normal.z = sqrt( 1.0 - normal.x*normal.x - normal.y*normal.y ); // z = sqrt(1-x²-y²) |
||
| 35 | |||
| 36 | // move other properties to be better readable |
||
| 37 | float gloss = temp.r; |
||
| 38 | |||
| 39 | // float alpha = temp.b; |
||
| 40 | // if ( alpha < 0.2 ) // optimization: should move this test before reading RGB texture |
||
| 41 | // discard; |
||
| 42 | |||
| 43 | // tweaked phong lighting |
||
| 44 | float lambert = max( dot( lightVector, normal ), 0.0 ); |
||
| 45 | |||
| 46 | gl_FragColor = vec4( Light_DiffuseColor * diffuseColor, 1.0 ) * |
||
| 47 | lambert; |
||
| 48 | |||
| 49 | if ( lambert > 0.0 ) |
||
| 50 | { |
||
| 51 | float specular = pow( |
||
| 52 | clamp( dot( reflect( -lightVector, normal ), eyeVector ), 0.0, 1.0 ), |
||
| 53 | Material_ScaleBiasShininess.z ); |
||
| 54 | |||
| 55 | gl_FragColor += vec4( Light_SpecularColor * diffuseColor, 1.0 ) * ( specular * gloss ); |
||
| 56 | } |
||
| 57 | } |