当前位置:网站首页>OpenGL es learning (5) - Lighting
OpenGL es learning (5) - Lighting
2022-07-18 20:15:00 【Unlimited exchange】
1. Basic model of illumination
stay OpenGL ES 2.X Lighting model used in ( passageway ) branch 3 Kind of : The ambient light 、 Scattered light and Specular light .
1.1 The ambient light
The ambient light (Ambient) It refers to irradiating an object from all directions , comprehensive 360° Uniform light .

Mathematical model formula :
Ambient light exposure results = Reflection coefficient of material ∗ Ambient light intensity Ambient light exposure results = Reflection coefficient of material * Ambient light intensity Ambient light exposure results = Reflection coefficient of material ∗ Ambient light intensity
Complete code of vertex shader and slice shader in ambient light :
vertex.sh
uniform mat4 uMVPMatrix;// Total transformation matrix
attribute vec3 aPosition;// Vertex Position
varying vec3 vPosition;// Vertex position passed to the slice shader
varying vec4 vAmbient;// The ambient light component passed to the slice shader
void main(){
gl_Position = uMVPMatrix * vec4(aPosition,1);
vPosition = aPosition;
vAmbient = vec4(0.15,0.15,0.15,1.0);
}
frag.sh
precision mediump float;
uniform float uR;// The sphere
varying vec3 vPosition;// Vertex shader passed
varying vec4 vAmbient;// The intensity of the ambient light passed by the vertex shader
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);
gl_FragColor = finalColor * vAmbient;
}
【 Description of ambient light 】
- Ambient light has no directionality ;
- The illumination effect of ambient light is independent of the position of the light source ( Look at the following GIF chart );
- In actual development, the ambient light intensity is generally set to be weak ;

1.2 Scattered light
Scattered light (Diffuse), It refers to all directions from the surface of the object 360° Uniformly reflected light .

Mathematical model formula :
Final intensity of scattered light = Scattered light intensity ∗ m a x ( c o s ( Angle of incidence ) , 0 ) Scattered light irradiation results = Reflection coefficient of material ∗ Final intensity of scattered light Final intensity of scattered light = Scattered light intensity *max(cos( Angle of incidence ),0)\\ Scattered light irradiation results = Reflection coefficient of material * Final intensity of scattered light Final intensity of scattered light = Scattered light intensity ∗max(cos( Angle of incidence ),0) Scattered light irradiation results = Reflection coefficient of material ∗ Final intensity of scattered light
- Reflection coefficient of material : The color of the object being illuminated ;
- Scattered light intensity : In scattered light RGB3 Color channel intensity
Complete code of vertex shader and slice shader under scattered light :
vertex.sh
uniform mat4 uMVPMatrix;
uniform mat4 uMMatrix;
uniform vec3 uLightLocation;
attribute vec3 aPosition;
attribute vec3 aNormal;
varying vec3 vPosition;
varying vec4 vDiffuse;// The scattered light component used to pass to the slice shader
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);
vec3 vp = normalize(lightLocation-(uMMatrix*vec4(aPosition,1)).xyz);
vp = normalize(vp);
float nDotViewPosition=max(0.0,dot(newNormal,vp));
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;
}
frag.sh
precision mediump float;
uniform float uR;
varying vec3 vPosition;
varying vec4 vDiffuse;// Receive the final intensity of scattered light transmitted from the vertex
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);
gl_FragColor = finalColor * vDiffuse;
}
【 Scattered light description 】
- Scattered light , It can also be called diffuse reflection ;
- The reflected backscattered light is uniform in all directions ;
- The intensity of scattered light reflection is closely related to the intensity and angle of incident light , The larger the incident angle , The weaker the reflection intensity ( Look down GIF chart );

1.3 Specular light
Specular light (Specular), The reflected light concentrated in the direction when the smooth surface is illuminated .

Mathematical model formula :
Final intensity of specular light = Specular light intensity ∗ m a x ( 0 , ( c o s ( The angle between half vector and normal vector ) ) Roughness ) Specular light irradiation results = Reflection coefficient of material ∗ Final intensity of specular light Final intensity of specular light = Specular light intensity *max(0,(cos( The angle between half vector and normal vector )) Roughness )\\ Specular light irradiation results = Reflection coefficient of material * Final intensity of specular light Final intensity of specular light = Specular light intensity ∗max(0,(cos( The angle between half vector and normal vector )) Roughness ) Specular light irradiation results = Reflection coefficient of material ∗ Final intensity of specular light
Complete code of vertex shader and slice shader under specular light :
vertex.sh
uniform mat4 uMVPMatrix;
uniform mat4 uMMatrix;
uniform vec3 uLightLocation;
uniform vec3 uCamera;
attribute vec3 aPosition;
attribute vec3 aNormal;
varying vec3 vPosition;
varying vec4 vSpecular;
void pointLight(in vec3 normal,inout vec4 specular,in vec3 lightLocation,in vec4 lightSpecular){
vec3 normalTarget = aPosition + normal;
vec3 newNormal = (uMMatrix*vec4(normalTarget,1)).xyz - (uMMatrix*vec4(aPosition,1)).xyz;
newNormal = normalize(newNormal);
vec3 eye = normalize(uCamera-(uMMatrix*vec4(aPosition,1)).xyz);
vec3 vp = normalize(lightLocation-(uMMatrix*vec4(aPosition,1)).xyz);
vp = normalize(vp);
vec3 halfVector = normalize(vp+eye);
float shininess = 50.0;
float nDotViewHalfVector=dot(newNormal,halfVector);
float powerFactor = max(0.0,pow(nDotViewHalfVector,shininess));
specular = lightSpecular*powerFactor;
}
void main(){
gl_Position = uMVPMatrix * vec4(aPosition,1);
vec4 specularTemp = vec4(0.0,0.0,0.0,0.0);
pointLight(normalize(aNormal),specularTemp,uLightLocation,vec4(0.7,0.7,0.7,1.0));
vSpecular = specularTemp;
vPosition = aPosition;
}
frag.sh
precision mediump float;
uniform float uR;
varying vec3 vPosition;
varying vec4 vSpecular;
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);
gl_FragColor = finalColor * vSpecular;
}
【 Specular light description 】
- The intensity of specular light depends on the angle between the incident light and the normal vector of the illuminated point 、 Observer position ;
- The less roughness , The larger the area of mirror light
- The specular light changes with the position of the light source

The effect of mixing the three lights GIF

Case source code download
Ambient light effect Demo
Scattered light effect Demo
Specular effect Demo
【 Reference resources 】
- https://www.khronos.org/opengles/
- 《OpenGL ES Application development practice guide Android volume Kevin Brothaler》
- https://en.wikipedia.org/wiki/OpenGL_ES
- https://developer.android.google.cn/guide/topics/graphics/opengl
- https://developer.android.google.cn/training/graphics/opengl
- Android3D Game development technology dictionary OpenGL ES 2.0
边栏推荐
- 12. Monotonic stack - solve the problems of rainwater connection and the largest rectangle in the histogram
- OpenGL ES学习(3)——着色语言入门
- 【OpenCV 例程200篇】232. 特征描述之頻譜方法
- wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
- Activity的生命周期
- 【Word】插入公式显示灰色,失效解决
- Parallel and distributed framework
- 卷积结构及其计算
- MySQL中 8 种常见的 SQL 错误用法
- Mental poker revised learning notes
猜你喜欢
随机推荐
Small program development of private forum circle community
Interview micro service
Virtualization architecture
【剑指 Offer II 041. 滑动窗口的平均值】
[word] formula typesetting
Single source shortest path
【机器视觉】Canny边缘检测MATLAB源码阅读
【读书会第13期】+FFmpeg命令
OpenGL ES学习(2)——顶点着色器和片元着色器
1.线程与进程
单源最短路径
[深入研究4G/5G/6G专题-38]: URLLC-9-《3GPP URLLC相关协议、规范、技术原理深度解读》-3-URLLC技术的分析思路与研究方法:深度、广度、时间
Codeforces Round #805 (Div. 3)(A,B,C,D)
支付流程面试思路
solidity的代码
绿色安装MySQL5.7版本----配置my.ini文件注意事项
Horizon 8 test environment deployment (5): UAG deployment and load balancing configuration-1
4.连接已终止的线程(回收线程)
Vivado ROM IP core
Musk's 76 year old father and stepdaughter have children. Huaqiangbei has another chip IPO. The former vice president of ant has joined AI pharmaceutical. Today, more big news is here









