• OpenGL3.3_C++_Windows(9)


    最终效果

    demo演示

    多光源

    1. 原理:
    2. 所有投光物分别计算,对当前片段的影响,再+求和,渲染出物体的材质效果
    3. 每个投光物:根据冯氏光照(环境,漫反射,镜面)分解计算对片段的强度影响,再与当前片段颜色值(单一颜色 / 纹理颜色)* 相乘
    4. 每个投光物也会对(环境,漫反射,镜面)有不同的影响程度
    5. 通过struct和函数,进行整理归纳,比如绘制多个点光源,有不同的位置,之间把每次的位置传入点光源结构体中

    Fragment_glsl

    1. #version 330 core
    2. layout(location = 0) out vec4 FragColor;
    3. //in
    4. in vec3 v_Position;
    5. in vec2 v_TexCoord;
    6. in vec3 v_Normal;
    7. //材质
    8. struct Material {
    9. //
    10. sampler2D texture1;
    11. sampler2D texture2;
    12. //
    13. sampler2D specular_Texture;
    14. //光泽度
    15. float shininess;
    16. };
    17. //平行光
    18. struct DirLight {
    19. vec3 direction;//位置
    20. //冯氏光照
    21. vec3 ambient;
    22. vec3 diffuse;
    23. vec3 specular;
    24. };
    25. //点光
    26. struct PointLight {
    27. vec3 position;
    28. vec3 color;
    29. //冯氏光照
    30. vec3 ambient;
    31. vec3 diffuse;
    32. vec3 specular;
    33. };
    34. //聚光
    35. struct SpotLight {
    36. //聚光范围
    37. float cutOff;
    38. float outerCutOff;
    39. //冯氏光照
    40. vec3 ambient;
    41. vec3 diffuse;
    42. vec3 specular;
    43. };
    44. struct ConstVal{
    45. //camera
    46. vec3 camera_Position;
    47. vec3 camera_Direction;
    48. //衰减
    49. float constant;
    50. float linear;
    51. float quadratic;
    52. };
    53. //
    54. uniform Material material;
    55. uniform DirLight dirLight;
    56. uniform PointLight pointLights[10];
    57. uniform SpotLight spotLight;
    58. uniform ConstVal constVal;
    59. // function prototypes声明在此处,main可以用
    60. vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);
    61. vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
    62. vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
    63. //
    64. void main()
    65. {
    66. vec3 viewPos = normalize(constVal.camera_Position - v_Position);
    67. vec3 norm = normalize(v_Normal);
    68. vec3 result = CalcDirLight(dirLight, norm, viewPos);
    69. for(int i = 0; i < 10; i++)
    70. result += CalcPointLight(pointLights[i], norm, v_Position, viewPos);
    71. result += CalcSpotLight(spotLight, norm, v_Position, viewPos);
    72. FragColor = vec4(result, 1.0);//最终片段颜色
    73. }
    74. //
    75. vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
    76. {
    77. vec3 lightDir = normalize(-light.direction);//平行光向量
    78. //漫反射
    79. float diff = max(dot(normal, lightDir), 0.0);//漫反射
    80. // 镜面
    81. vec3 reflectDir = reflect(-lightDir, normal);
    82. float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
    83. //计算影响
    84. vec3 ambient = light.ambient * mix(texture(material.texture1, v_TexCoord), texture(material.texture2, v_TexCoord), 0.0).rgb;
    85. vec3 diffuse = light.diffuse * diff * mix(texture(material.texture1, v_TexCoord), texture(material.texture2, v_TexCoord), 0.0).rgb;
    86. vec3 specular = light.specular * spec * vec3(texture(material.specular_Texture, v_TexCoord));
    87. return (ambient + diffuse + specular);
    88. }
    89. //
    90. vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
    91. {
    92. vec3 lightDir = normalize(light.position - fragPos);//当前点光向量
    93. //漫反射
    94. float diff = max(dot(normal, lightDir), 0.0);
    95. // 镜面
    96. vec3 reflectDir = reflect(-lightDir, normal);
    97. float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
    98. // 衰减
    99. float distance = length(light.position - fragPos);
    100. float attenuation = 1.0 / (constVal.constant + constVal.linear * distance + constVal.quadratic * (distance * distance));
    101. //计算影响
    102. vec3 ambient = light.ambient * mix(texture(material.texture1, v_TexCoord), texture(material.texture2, v_TexCoord), 0.0).rgb;
    103. vec3 diffuse = light.color * light.diffuse * diff * mix(texture(material.texture1, v_TexCoord), texture(material.texture2, v_TexCoord), 0.0).rgb;
    104. vec3 specular = light.color * light.specular * spec * vec3(texture(material.specular_Texture, v_TexCoord));
    105. ambient *= attenuation;
    106. diffuse *= attenuation ;
    107. specular *= attenuation ;
    108. return (ambient + diffuse + specular);
    109. }
    110. //
    111. vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
    112. {
    113. vec3 lightDir = normalize(constVal.camera_Position - fragPos);//摄像机向量
    114. //漫反射
    115. float diff = max(dot(normal, lightDir), 0.0);
    116. // 镜面
    117. vec3 reflectDir = reflect(-lightDir, normal);
    118. float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
    119. // 衰减
    120. float distance = length(constVal.camera_Position - fragPos);
    121. float attenuation = 1.0 / (constVal.constant + constVal.linear * distance + constVal.quadratic * (distance * distance));
    122. // 范围
    123. float theta = dot(lightDir, normalize(-constVal.camera_Direction));
    124. float epsilon = light.cutOff - light.outerCutOff;
    125. float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
    126. //计算影响
    127. vec3 ambient = light.ambient * mix(texture(material.texture1, v_TexCoord), texture(material.texture2, v_TexCoord), 0.0).rgb;
    128. vec3 diffuse = light.diffuse * diff * mix(texture(material.texture1, v_TexCoord), texture(material.texture2, v_TexCoord), 0.0).rgb;
    129. vec3 specular = light.specular * spec * vec3(texture(material.specular_Texture, v_TexCoord));
    130. ambient *= attenuation * intensity;
    131. diffuse *= attenuation * intensity;
    132. specular *= attenuation * intensity;
    133. return (ambient + diffuse + specular);
    134. }

  • 相关阅读:
    wsl-Ubuntu18.04子系统使用cuda
    whistle启动时,输入命令w2 start报:w2 start‘w2‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    Linux应用基础——串口应用编程
    游戏开发中的按键操作管理2
    ExtJS - ExtJS最佳实践
    Go十大常见错误第7篇:不使用-race选项做并发竞争检测
    详细了解关于sentinel的实际应用
    02 重学js-原型链,this指向及改变this指向
    C++初阶 Stack和Queue的介绍和使用
    【MySQL】如何配置复制拓扑?
  • 原文地址:https://blog.csdn.net/sengyongan/article/details/139697367