GLSL Normal Mapping

Here is my version of GLSL Normal Mapping. A normal map is an RGB texture where the RGB value corresponds to the XYZ of the normal. Since we are only doing mapping onto a quad, we simply need to replace (not offset) the old normal with this new read normal. There is one caveat, however. RGB values range from [0,1] while normal directional values can range from [-1,1]. The same should go for green and blue.

I tried to do Blinn-Phong reflection but i am not sure if it is correct.

Vert Shader


uniform sampler2D my_color_texture;
varying vec2 texture_coord;

varying vec3 normal;
varying vec3 vertex_to_light;
varying vec3 vertex_to_camera;


void main()
{	 
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

  normal = normalize(gl_NormalMatrix * gl_Normal);

  vec4 vertex_modelview = gl_ModelViewMatrix * gl_Vertex;

  vertex_to_light = gl_LightSource[0].position.xyz - vertex_modelview.xyz;
  
  vertex_to_camera = -vertex_modelview.xyz; 

  texture_coord = vec2 (gl_MultiTexCoord0);

}

uniform sampler2D my_color_texture;

varying vec2 texture_coord;

varying vec3 normal;
varying vec3 vertex_to_light;
varying vec3 vertex_to_camera;


const float specularpower = 25.0;


void main()
{

  vec3 n_normal = 2.0 * texture2D (my_color_texture, texture_coord).rgb - 1.0;

  n_normal = vec3(n_normal.x * 2.0 - 1.0, n_normal.y * 2.0 - 1.0 , n_normal.z * 2.0 - 1.0);

  n_normal = normalize(n_normal);


  vec4 diffuse_color = vec4(0.0, 0.5, 1.0, 1.0) ;
  vec4 specular_color = vec4(1.0, 1.0, 1.0, 1.0)  ;

  vec3 halfvector = normalize(vertex_to_light + vertex_to_camera);
  
  vec3 n_vertex_to_light = normalize(vertex_to_light);
  float diffuse = max (dot (n_normal, n_vertex_to_light), 0.0);
  
  vec3 n_vertex_to_camera = normalize(vertex_to_camera);
  vec3 ref = reflect(-n_vertex_to_light, n_normal);

  float specular = max(dot (n_normal, halfvector), 0.0);
  specular = pow(specular, specularpower);
  
 gl_FragColor.xyz = diffuse * diffuse_color.rgb + specular * specular_color.rgb;

gl_FragColor.a = 1.0;

}


 


Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on

  • wholesale nfl jerseys
  • wholesale nfl jerseys china
  • wholesale sports jerseys