Open LeeMcQueen opened 3 years ago
今天研究有关于多光源的配置 Multiple lights
多光源 1.定向光 2.点光源 3.聚光灯 的大概逻辑和函数构造 out vec4 FragColor; void main() { vec3 output; //定向光源 output += someFunctionToCalculateDirectionaLight(); for(int i = 0; i < nr_of_point_lights; i++) //点光源 output += someFunctionToCalculatePointLight;
output += someFunctionCalculateSpotLight();
FragColor = vec4(output, 1.0); }
-----------------------------定向光----------------------------------- 定向光构造体 struct DirLight{ vec3 direction;
vec3 ambient; vec3 diffuse; vec3 specular;
} uniform DirLight dirLight;
定向光源实现函数 参数1.定向光结构体 2.vec3法线 3.vec3相机位置 返回值 vec3 vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir); { //光的方向取反在正规化 vec3 lightDir = normalize(-light.direction); //漫反射diffues着色 float diff = max(dot(normal, lightDir), 0.0); //镜面光着色 vec3 reflectDir = reflect(-lightDir, normal); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
//合并结果 vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords)); vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords)); vec3 specular = light.specular * spec * vec3(texture(material.specular, texCoords)); return(ambient + diffuse + specular);
----------------------------点光源----------------------------------- 点光源结构体 struct PointLight{ vec3 position;
//固定的是点的位置吗? float constant; float linear; float quadratic; vec3 ambient; vec3 diffuse; vec3 specular;
};
uniform PointLight pointLight[NR_POINT_LIGHTS];
点光源实现函数 参数1.点光源结构体 2.vec3法线 3.vec3目标位置 4.vec3相机位置 vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir) { vec3 lightDir = normalize(light.position - fragPos);
//漫反射着色 float diff = max(dot(normal, lightDir), 0.0); vec3 reflectDir = reflect(-lightDir, normal); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); //衰减 float distance = length(light.position - fragPos); float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); //合并结果 vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords)); vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords)); vec3 specular = light.specular * spec * vec3(textrue(material.specular, TexCoords)); //1.环境光 2.漫反射 3.镜面反射的衰减合并 ambient *= attenuation; diffuse *= att
}
今天研究有关于多光源的配置 Multiple lights
多光源 1.定向光 2.点光源 3.聚光灯 的大概逻辑和函数构造 out vec4 FragColor; void main() { vec3 output; //定向光源 output += someFunctionToCalculateDirectionaLight(); for(int i = 0; i < nr_of_point_lights; i++) //点光源 output += someFunctionToCalculatePointLight;
output += someFunctionCalculateSpotLight();
FragColor = vec4(output, 1.0); }
-----------------------------定向光----------------------------------- 定向光构造体 struct DirLight{ vec3 direction;
} uniform DirLight dirLight;
定向光源实现函数 参数1.定向光结构体 2.vec3法线 3.vec3相机位置 返回值 vec3 vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir); { //光的方向取反在正规化 vec3 lightDir = normalize(-light.direction); //漫反射diffues着色 float diff = max(dot(normal, lightDir), 0.0); //镜面光着色 vec3 reflectDir = reflect(-lightDir, normal); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
}
----------------------------点光源----------------------------------- 点光源结构体 struct PointLight{ vec3 position;
};
define NR_POINT_LIGHTS 4
uniform PointLight pointLight[NR_POINT_LIGHTS];
点光源实现函数 参数1.点光源结构体 2.vec3法线 3.vec3目标位置 4.vec3相机位置 vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir) { vec3 lightDir = normalize(light.position - fragPos);
}