OpenGL ES 3. Light - Light Scattering

Hello everyone, the next will introduce OpenGL ES 3. Light - scattering light.

OpenGL ES 3.0 lighting model used in the real world relative to a greatly simplified, the light is divided into three kinds of constituent elements (also referred to as 3 channels), including ambient light, scattered light and specular light.

1, the scattered light introduced

       There are only a ring of ambient light scene effect is poor, there is no sense of depth. Next will be described another realistic lighting effects much better - the scattered light (the Diffuse), which refers to the light, as shown below from the object surface is uniformly reflected by the full 360 °.


        Scattered light represents the specific surface of the object in the real world is irradiated with light, the reflected light substantially uniformly in all directions (also referred to as "diffuse reflection"), the figure illustrates the problem well.
        Although the scattering of the reflected light is uniform in all directions, but is closely related to the intensity of the reflected scattered light intensity of incident light and angle of incidence. Thus, when the position of the light source changes, the effect of scattered light can vary significantly. Is mainly shines brighter than the incident light when irradiated vertically to the object surface, the specific formula is as follows.
        Result = irradiating light scattered reflectance material scattered light intensity * * max (cos (angle of incidence), 0).

 

2, on the basis of the "sphere drawn" on the rendering ambient light        

#version 300 es
uniform mat4 uMVPMatrix; 					//总变换矩阵
uniform mat4 uMMatrix; 						//变换矩阵(包括平移、旋转、缩放)
uniform vec3 uLightLocation;			       
in vec3 aPosition;  						//顶点位置
in vec3 aNormal;    						//顶点法向量
out vec3 vPosition;							//用于传递给片元着色器的顶点位置
out vec4 vDiffuse;							//用于传递给片元着色器的散射光分量
void pointLight (							//散射光光照计算的方法
  in vec3 normal,							//法向量
  inout vec4 diffuse,						//散射光计算结果
  in vec3 lightLocation,					//光源位置
  in vec4 lightDiffuse						//散射光强度
){  
  vec3 normalTarget=aPosition+normal;				//计算变换后的法向量
  vec3 newNormal=(uMMatrix*vec4(normalTarget,1)).xyz-(uMMatrix*vec4(aPosition,1)).xyz;
  newNormal=normalize(newNormal);					//对法向量规格化
  //计算从表面点到光源位置的向量vp
  vec3 vp= normalize(lightLocation-(uMMatrix*vec4(aPosition,1)).xyz);
  vp=normalize(vp);									 
  float nDotViewPosition=max(0.0,dot(newNormal,vp)); //求法向量与vp向量的点积与0的最大值
  diffuse=lightDiffuse*nDotViewPosition;			 //计算散射光的最终强度
}
void main(){
   gl_Position = uMVPMatrix * vec4(aPosition,1); 	 //根据总变换矩阵计算此次绘制此顶点的位置 
   vec4 diffuseTemp=vec4(0.0,0.0,0.0,0.0);   
   pointLight(normalize(aNormal), diffuseTemp, uLightLocation, vec4(0.8,0.8,0.8,1.0));  
   vDiffuse=diffuseTemp;					//将散射光最终强度传给片元着色器
   vPosition = aPosition; 					//将顶点的位置传给片元着色器
}

#version 300 es
precision mediump float;
uniform float uR;
in vec3 vPosition;//接收从顶点着色器过来的顶点位置
in vec4 vDiffuse;//接收从顶点着色器过来的散射光最终强度
out vec4 fragColor;
void main()                         
{
   vec3 color;
   float n = 8.0;//一个坐标分量分的总份数
   float span = 2.0*uR/n;//每一份的长度
   //每一维在立方体内的行列数
   int i = int((vPosition.x + uR)/span);
   int j = int((vPosition.y + uR)/span);
   int k = int((vPosition.z + uR)/span);
   //计算当点应位于白色块还是黑色块中
   int whichColor = int(mod(float(i+j+k),2.0));
   if(whichColor == 1) {//奇数时为红色
   		color = vec3(0.678,0.231,0.129);//红色
   }
   else {//偶数时为白色
   		color = vec3(1.0,1.0,1.0);//白色
   }
   //最终颜色
   vec4 finalColor=vec4(color,0);
//根据散射光最终强度计算片元的最终颜色值
   fragColor=finalColor*vDiffuse;
}     

 

3, rendering results

Visible, after the introduction of the scattered light, a lot of obvious effect of light, layering more obvious.

 

Finally, we welcome the exchange of learning together: micro letter: liaosy666; QQ: 2209115372  .

 

 

Published 36 original articles · won praise 46 · views 20000 +

Guess you like

Origin blog.csdn.net/u010281924/article/details/105330801