www.pudn.com > AndreasHalm-src.zip > cube_edges_bm8p_ati.fragmentshader


// the bumpmapping is calculated via accessing the texture 
// at the pixels in all eight directions. the difference in 
// alpha value is used to calculate a deviation vector 
// which is added to the current normal before normalization. 
// compared to the 4p-bumpmapping also the round parts 
// get very nice. 
 
// ati special: ati doesn't support fwidth() as of catalyst 
// 4.6 - so we acces the neighbouring texel instead. the 
// code below is for 256x256 textures, but it could be 
// modified to use a parameter (uniform) instead. 
// the bumpmapping effect is not as nice as with fwidth() 
// but it's ok for the time being. 
 
const vec4 AMBIENT = vec4( 0.1, 0.1, 0.1, 1.0 ); 
const vec4 SPECULAR = vec4( 1.0, 1.0, 1.0, 1.0 ); 
const float BUMPMAPHEIGHT = 0.3; 
 
varying vec4 Cd; 
varying vec4 V_eye; 
varying vec4 L_eye; 
varying vec4 N_eye; 
varying vec3 eyespace_position; 
 
uniform sampler2D tex_bm; 
uniform samplerCube texCube; 
 
// vectors in s,t direction (compared to tex0) 
varying vec4 tex0_s_eye; 
varying vec4 tex0_t_eye; 
 
void main(void) 
{ 
   // compute height/elevation 
   // ati cards can't handle fwidth() currently 
   vec2 fw = vec2(1.0/256.0,1.0/256.0); 
   vec2 tex_coord_top    = vec2( gl_TexCoord[0].s, gl_TexCoord[0].t-fw.t ); 
   vec2 tex_coord_left   = vec2( gl_TexCoord[0].s-fw.s, gl_TexCoord[0].t ); 
   vec2 tex_coord_bottom = vec2( gl_TexCoord[0].s, gl_TexCoord[0].t+fw.t ); 
   vec2 tex_coord_right  = vec2( gl_TexCoord[0].s+fw.s, gl_TexCoord[0].t ); 
   vec2 tex_coord_topleft     = vec2( gl_TexCoord[0].s-fw.s, gl_TexCoord[0].t-fw.t ); 
   vec2 tex_coord_topright    = vec2( gl_TexCoord[0].s+fw.s, gl_TexCoord[0].t-fw.t ); 
   vec2 tex_coord_bottomleft  = vec2( gl_TexCoord[0].s-fw.s, gl_TexCoord[0].t+fw.t ); 
   vec2 tex_coord_bottomright = vec2( gl_TexCoord[0].s+fw.s, gl_TexCoord[0].t+fw.t ); 
   float tex_top    = texture2D(tex_bm,tex_coord_top   ).a; 
   float tex_left   = texture2D(tex_bm,tex_coord_left  ).a; 
   float tex_bottom = texture2D(tex_bm,tex_coord_bottom).a; 
   float tex_right  = texture2D(tex_bm,tex_coord_right ).a; 
   float tex_topleft     = texture2D(tex_bm,tex_coord_topleft    ).a; 
   float tex_topright    = texture2D(tex_bm,tex_coord_topright   ).a; 
   float tex_bottomleft  = texture2D(tex_bm,tex_coord_bottomleft ).a; 
   float tex_bottomright = texture2D(tex_bm,tex_coord_bottomright).a; 
   float height_right = tex_right*0.5 + (tex_topright+tex_bottomright)*0.3; 
   float height_left  = tex_left*0.5  + (tex_topleft+tex_bottomleft)*0.3; 
   float s_elevation = BUMPMAPHEIGHT*(height_right-height_left); 
   float height_top    = tex_top*0.5    + (tex_topright+tex_topleft)*0.3; 
   float height_bottom = tex_bottom*0.5 + (tex_bottomright+tex_bottomleft)*0.3; 
   float t_elevation = BUMPMAPHEIGHT*(height_bottom-height_top); 
   vec4 elevation = s_elevation*tex0_s_eye + t_elevation*tex0_t_eye; 
    
  vec3 V = normalize(vec3(V_eye)); 
  vec3 L = normalize(vec3(L_eye)); 
  vec3 N = normalize(vec3(N_eye)+vec3(elevation)); 
  float diffuse = clamp(dot(L, N), 0.0, 1.0); 
  vec3 H = normalize(L + V); 
  float specular = clamp(pow(dot(N, H), 20.0), 0.0, 1.0); 
  vec3 u = normalize(eyespace_position); 
  vec3 texcube_coord = reflect(u,vec3(N_eye)); 
  vec4 texture_cubemap = textureCube(texCube,texcube_coord); 
  vec4 color = AMBIENT + (Cd*diffuse*texture_cubemap) + (SPECULAR*specular); 
  gl_FragColor = color; 
}