OpenGL normalMap

参考zwqxin的博客  http://www.zwqxin.com/

shader 来自zwqxin,稍作修改

 

 

<-vertex->


attribute vec3 v_Pos;
attribute vec3 v_Normal;
attribute vec2 v_TexCoord;
attribute vec3 v_tangent;


uniform vec3 lightpos; //传入光源的模型坐标吧
uniform vec4 eyepos;
 
varying vec3 lightdir;
varying vec3 halfvec;
varying vec3 norm;
varying vec3 eyedir;
 

 
void main(void)
{
   vec4 pos = gl_ModelViewMatrix * vec4(v_Pos,1.0);
   pos = pos / pos.w;
   
//把光源和眼睛从模型空间转换到视图空间
   vec4 vlightPos = (gl_ModelViewMatrix * vec4(lightpos, 1.0));
   vec4 veyePos   = (gl_ModelViewMatrix * eyepos);
   
   lightdir = normalize(vlightPos.xyz - pos.xyz);
   vec3 eyedir = normalize(veyePos.xyz - pos.xyz);
   
  //模型空间下的TBN
   norm = normalize(gl_NormalMatrix * v_Normal);
 
   vec3 vtangent  = normalize(gl_NormalMatrix * v_tangent);
 
   vec3 vbinormal = cross(norm,vtangent);
   
   //将光源向量和视线向量转换到TBN切线空间
   lightdir.x = dot(vtangent,  lightdir);
   lightdir.y = dot(vbinormal, lightdir); 
   lightdir.z = dot(norm     , lightdir);
   lightdir = normalize(lightdir);
   
   eyedir.x = dot(vtangent,  eyedir);
   eyedir.y = dot(vbinormal, eyedir);
   eyedir.z = dot(norm     , eyedir);
   eyedir = normalize(eyedir);
   
   halfvec = normalize(lightdir + eyedir);
 

   
   gl_TexCoord[0].st = v_TexCoord;
   
   gl_Position = gl_ModelViewProjectionMatrix *vec4( v_Pos,1);
}



<-fragment->


 
uniform sampler2D bumptex;
uniform sampler2D basetex;
 
float amb = 0.2;
float diff = 0.2;
float spec = 0.6;
 
varying vec3 lightdir;
varying vec3 halfvec;
varying vec3 norm;
varying vec3 eyedir;
 
void main(void)
{
	float shiness =4;
	vec4 ambient = vec4(0.2,0.2,0.2,0.2);
	vec4 diffuse = vec4(0.3,0.3,0.3,0.3);
	vec4 specular = vec4(0.3,0.3,0.3,0.3);

   vec3 vlightdir = normalize(lightdir);
   vec3 veyedir = normalize(eyedir);
 
   vec3 vnorm =   normalize(norm);
   vec3 vhalfvec =  normalize(halfvec);  
   
   vec4 baseCol = texture2D(basetex, gl_TexCoord[0].xy); 
   
   //Normal Map里的像素normal定义于该像素的切线空间
   vec3 tbnnorm = texture2D(bumptex, gl_TexCoord[0].xy).xyz;
   
   tbnnorm = normalize((tbnnorm  - vec3(0.5))* 2.0); 
   
   float diffusefract =  max( dot(lightdir,tbnnorm) , 0.0); 
   float specularfract = max( dot(vhalfvec,tbnnorm) , 0.0);
   
   if(specularfract > 0.0){
   specularfract = pow(specularfract, shiness);
   }
   
   gl_FragColor = vec4(amb * ambient.xyz * baseCol.xyz
                 + diff * diffuse.xyz * diffusefract * baseCol.xyz
                 + spec * specular.xyz * specularfract ,1.0)*3.0;
}

 c++代码(用自己的框架不方便全部放出来)

glEnable(GL_TEXTURE_2D);
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D,m_img.Getid());
		
	

		glActiveTexture(GL_TEXTURE0+1);
		glBindTexture(GL_TEXTURE_2D,m_NormalImg.Getid());


		m_ShaderNormalMap.Enable();
		m_ShaderNormalMap.uniform("bumptex",1);
		m_ShaderNormalMap.uniform("basetex",0);
		m_ShaderNormalMap.uniform("lightpos",vec3(100,100,100));
		m_ShaderNormalMap.uniform("eyepos",vec3(0,0,10));
		glPushMatrix();
		glTranslatef(0,0,-100);

		static float angleA =0.0f;
		angleA +=0.02f;
		
		glRotatef(angleA,1,1,1);
		
		m_Mesh.RenderTest();
	
		glActiveTexture( GL_TEXTURE0);

 

 

-------------------------------------------分界线

2016年12月1日  

重新增加了VAO渲染,改写材质

 

 

 

 

posted on 2015-09-13 23:20  c_dragon  阅读(776)  评论(0编辑  收藏  举报

导航