LeeMcQueen / GameDemo

Demo
0 stars 0 forks source link

fragmentShader emission function #17

Open LeeMcQueen opened 3 years ago

LeeMcQueen commented 3 years ago

glm::translate

version 330 core

struct Material { sampler2D diffuse; //漫反射纹理 sampler2D specular; //镜面反射纹理 sampler2D emission; //发射纹理 float shininess; };

//光线也是由合成光导入的 struct Light { vec3 position; vec3 ambient; //环境光 vec3 diffuse; //漫反射 vec3 specular; //镜面反射 };

in vec3 FragPos; in vec3 Normal; in vec2 TexCoords;

out vec4 color;

uniform vec3 viewPos; uniform Material material; //类的变量宣言 uniform Light light; //类的变量宣言 uniform float time; //浮点型的时间变量,通过和glfwgettime得到实时的时间变换

void main() {

/*Ambient (环境光 * texture函数取得镜面反射纹理和位置)  */
vec3 ambient = light.ambient * vec3(texture(material.specular, TexCoords));   /*use specular texture */

/*Diffuse 漫反射 */
//法线正规化
vec3 norm = normalize(Normal);
//通过光线位置 - M矩阵位置得到光线射入向量
vec3 lightDir = normalize(light.position - FragPos);
//通过法线和光线射入向量的点乘得到角度
float diff = max(dot(norm, lightDir), 0.0);
//漫反射 光线的漫反射 * 光线射入角度 * texture函数取得镜面反射纹理和位置
vec3 diffuse = light.diffuse * diff * vec3(texture(material.specular, TexCoords));   /*use specular texture */

/*Specular 镜面反射 */
//观察矩阵位置 - M矩阵位置得到观察向量
vec3 viewDir = normalize(viewPos - FragPos);
//光线反射 反射函数用光和法线得到反射向量
vec3 reflectDir = reflect(-lightDir, norm);
//观察向量点乘反射向量得到角度
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));    /*use specular texture */

/*Emission 发射光 */
//发射变量
vec3 emission = vec3(0.0);
//镜面反射贴图的红色为0的情况下
if (texture(material.specular, TexCoords).r == 0.0)   /*rough check for blackbox inside spec texture */
{
    /*apply emission texture */
    //启用发射贴图 RGB
    emission = texture(material.emission, TexCoords).rgb;

    /*some extra fun stuff with "time uniform" */
    //发射贴图根据时间移动位置(TexCoords)
    emission = texture(material.emission, TexCoords + vec2(0.0,time)).rgb;   /*moving */
    //明暗波动 三角函数sin
    emission = emission * (sin(time) * 0.5 + 0.5) * 2.0;                     /*fading */
}

/output / color = vec4(ambient + diffuse + specular + emission, 1.0f); }