本文是个人学习记录,学习建议看教程 https://learnopengl-cn.github.io/

非常感谢原作者JoeyDeVries和多为中文翻译者提供的优质教程

的内容为插入注释,可以先跳过

投光物(光源)

我们目前使用的光照都来自于空间中的一个点,它能给我们不错的效果,但现实世界中,我们有很多种类的光照,每种的表现都不同

将光投射(Cast)到物体的光源叫做投光物(Light Caster),我们将会讨论几种不同类型的投光物,学会模拟不同种类的光源是又一个能够进一步丰富场景的工具

我们首先将会讨论平行光(Directional Light),接下来是点光源(Point Light),它是我们之前学习的光源的拓展,最后我们将会讨论聚光(Spotlight)

之后我们将讨论如何将这些不同种类的光照类型整合到一个多光源场景之中

平行光

当一个光源处于很远的地方时,来自光源的每条光线就会近似于互相平行,不论物体和/或者观察者的位置,看起来好像所有的光都来自于同一个方向

当我们使用一个假设光源处于无限远处的模型时,它就被称为平行光,因为它的所有光线都有着相同的方向,它与光源的位置是没有关系的。

比如,太阳距离我们并不是无限远,但它已经远到在光照计算中可以把它视为无限远了,所以来自太阳的所有光线将被模拟为平行光线

因为所有的光线都是平行的,所以物体与光源的相对位置是不重要的,因为对场景中每一个物体光的方向都是一致的。由于光的位置向量保持一致,场景中每个物体的光照计算将会是类似的。

我们可以定义一个光线方向向量而不是位置向量来模拟一个平行光。着色器的计算基本保持不变,但这次我们将直接使用光的direction向量而不是通过direction来计算lightDir向量。

  1. //片段着色器
  2. struct Light {
  3. // vec3 position; // 使用平行光就不再需要了
  4. vec3 direction;
  5. vec3 ambient;
  6. vec3 diffuse;
  7. vec3 specular;
  8. };
  9. //main里
  10. vec3 lightDir = normalize(-light.direction);

注意我们首先对light.direction向量取反。我们目前使用的光照计算需求一个从片段光源的光线方向,但人们更习惯定义平行光为一个光源出发的全局方向,所以我们需要对全局光照方向向量取反来改变它的方向,它现在是一个指向光源的方向向量了(记得对向量进行标准化

最终的lightDir向量将和以前一样用在漫反射和镜面光计算中

为了清楚地展示平行光对多个物体具有相同的影响,我们将会再次使用坐标系统博客里最后的那个箱子派对的场景,我们先定义了十个不同的箱子位置

  1. glm::vec3 cubePositions[] = {
  2. glm::vec3( 0.0f, 0.0f, 0.0f),
  3. glm::vec3( 2.0f, 5.0f, -15.0f),
  4. glm::vec3(-1.5f, -2.2f, -2.5f),
  5. glm::vec3(-3.8f, -2.0f, -12.3f),
  6. glm::vec3( 2.4f, -0.4f, -3.5f),
  7. glm::vec3(-1.7f, 3.0f, -7.5f),
  8. glm::vec3( 1.3f, -2.0f, -2.5f),
  9. glm::vec3( 1.5f, 2.0f, -2.5f),
  10. glm::vec3( 1.5f, 0.2f, -1.5f),
  11. glm::vec3(-1.3f, 1.0f, -1.5f)
  12. };

并对每个箱子都生成了一个不同的模型矩阵,每个模型矩阵都包含了对应的局部-世界坐标变换:

  1. for(unsigned int i = 0; i < 10; i++)
  2. {
  3. glm::mat4 model;
  4. model = glm::translate(model, cubePositions[i]);
  5. float angle = 20.0f * i;
  6. model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
  7. lightingShader.setMat4("model", model);
  8. glDrawArrays(GL_TRIANGLES, 0, 36);
  9. }

同时,不要忘记定义光源的方向(注意我们将方向定义为光源出发的方向,你可以很容易看到光的方向朝下)。

  1. lightingShader.setVec3("light.direction", -0.2f, -1.0f, -0.3f);

我们一直将光的位置和位置向量定义为vec3,一些人会喜欢将所有的向量都定义为vec4,当我们将位置向量定义为一个vec4时,很重要的一点是要将w分量设置为1.0,这样变换和投影才能正确应用,然而,当我们定义一个方向向量为vec4的时候,我们不想让位移有任何的效果(因为它仅仅代表的是方向),所以我们将w分量设置为0.0

方向向量就会像这样来表示:vec4(0.2f, 1.0f, 0.3f, 0.0f)

这也可以作为一个快速检测光照类型的工具:你可以检测w分量是否等于1.0,来检测它是否是光的位置向量;w分量等于0.0,则它是光的方向向量,这样就能根据这个来调整光照计算了:

  1. if(lightVector.w == 0.0) // 注意浮点数据类型的误差
  2. // 执行平行光照计算
  3. else if(lightVector.w == 1.0)
  4. // 根据光源的位置做光照计算(与上一节一样)

这正是旧OpenGL(固定函数式)决定光源是平行光还是位置光源(Positional Light Source)的方法,并根据它来调整光照

如果你现在编译程序,在场景中自由移动,你就可以看到好像有一个太阳一样的光源对所有的物体投光

部分源码如下

  1. //平行光片段着色器
  2. #version 330 core
  3. struct Material {
  4. sampler2D diffuse;
  5. sampler2D specular;
  6. float shininess;
  7. };
  8. struct Light {
  9. // vec3 position; // 使用平行光就不再需要了
  10. vec3 direction;
  11. vec3 ambient;
  12. vec3 diffuse;
  13. vec3 specular;
  14. };
  15. in vec2 TexCoords;
  16. in vec3 Normal;
  17. in vec3 FragPos;
  18. out vec4 FragColor;
  19. uniform Material material;
  20. uniform Light light;
  21. uniform vec3 objectColor;
  22. uniform vec3 lightColor;
  23. uniform vec3 lightPos;
  24. uniform vec3 viewPos;
  25. float specularStrength = 0.5;
  26. void main()
  27. {
  28. // 环境光
  29. vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
  30. // 漫反射
  31. vec3 norm = normalize(Normal);
  32. //vec3 lightDir = normalize(lightPos - FragPos);
  33. vec3 lightDir = normalize(-light.direction);
  34. float diff = max(dot(norm, lightDir), 0.0);
  35. vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
  36. // 镜面光
  37. vec3 viewDir = normalize(viewPos - FragPos);
  38. vec3 reflectDir = reflect(-lightDir, norm);
  39. float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
  40. vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
  41. vec3 result = ambient + diffuse + specular;
  42. FragColor = vec4(result, 1.0);
  43. }
  1. //平行光渲染循环
  2. while (!glfwWindowShouldClose(window))
  3. {
  4. // 帧时间差
  5. float currentFrame = glfwGetTime();
  6. deltaTime = currentFrame - lastFrame;
  7. lastFrame = currentFrame;
  8. // 输入
  9. processInput(window);
  10. // 渲染指令
  11. glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  12. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  13. lightingShader.use();
  14. lightingShader.setVec3("light.direction", -0.2f, -1.0f, -0.3f);
  15. lightingShader.setVec3("viewPos", camera.Position);
  16. // 光照属性
  17. lightingShader.setVec3("light.ambient", 0.2f, 0.2f, 0.2f);
  18. lightingShader.setVec3("light.diffuse", 0.7f, 0.7f, 0.7f);
  19. lightingShader.setVec3("light.specular", 1.0f, 1.0f, 1.0f);
  20. // 材质属性
  21. lightingShader.setFloat("material.shininess", 32.0f);
  22. // 初始化view transformations
  23. glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
  24. glm::mat4 view = camera.GetViewMatrix();
  25. lightingShader.setMat4("projection", projection);
  26. lightingShader.setMat4("view", view);
  27. // 初始化world transformations
  28. glm::mat4 model = glm::mat4();
  29. lightingShader.setMat4("model", model);
  30. // 绑定漫反射贴图
  31. glActiveTexture(GL_TEXTURE0);
  32. glBindTexture(GL_TEXTURE_2D, diffuseMap);
  33. // 绑定镜面贴图
  34. glActiveTexture(GL_TEXTURE1);
  35. glBindTexture(GL_TEXTURE_2D, specularMap);
  36. // 渲染方块
  37. //glBindVertexArray(cubeVAO);
  38. //glDrawArrays(GL_TRIANGLES, 0, 36);
  39. glBindVertexArray(cubeVAO);
  40. for (unsigned int i = 0; i < 10; i++)
  41. {
  42. glm::mat4 model = glm::mat4(1.0f);
  43. model = glm::translate(model, cubePositions[i]);
  44. float angle = 20.0f * i;
  45. model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
  46. lightingShader.setMat4("model", model);
  47. glDrawArrays(GL_TRIANGLES, 0, 36);
  48. }
  49. // 现在有了平行光,不需要光源立方体
  50. //lampShader.use();
  51. //// 设置模型、视图和投影矩阵uniform
  52. //lampShader.setMat4("projection", projection);
  53. //lampShader.setMat4("view", view);
  54. //model = glm::mat4(1.0f);
  55. //model = glm::translate(model, lightPos);
  56. //model = glm::scale(model, glm::vec3(0.2f)); // 小点的立方体
  57. //lampShader.setMat4("model", model);
  58. //// 绘制灯立方体对象
  59. //glBindVertexArray(lightVAO);
  60. //glDrawArrays(GL_TRIANGLES, 0, 36);
  61. // 交换缓冲并查询IO事件
  62. glfwSwapBuffers(window);
  63. glfwPollEvents();
  64. }
  65. glDeleteVertexArrays(1, &cubeVAO);
  66. glDeleteVertexArrays(1, &lightVAO);
  67. glDeleteBuffers(1, &VBO);
  68. glfwTerminate();
  69. return 0;
  70. }

点光源

除了平行光之外我们也需要一些分散在场景中的点光源(Point Light),点光源是处于世界中某一个位置的光源,它会朝着所有方向发光,但光线会随着距离逐渐衰减,比如作为光源的灯泡和火把,它们都是点光源

在之前的教程中,我们一直都在使用一个(简化的)点光源,我们在给定位置有一个光源,它会从它的光源位置开始朝着所有方向散射光线,但我们之前定义的光源模拟的是永远不会衰减的光线

如果你将10个箱子加入到上一节光照场景中,你会注意到在最后面的箱子和在灯面前的箱子都以相同的强度被照亮,并没有定义一个公式来将光随距离衰减,我们希望点光源强度有适当的随着距离增加而衰减,才能更好的模拟真实环境

衰减

随着光线传播距离的增长逐渐削减光的强度通常叫做衰减(Attenuation),其中一种方式是使用一个线性方程,这样的方程能够随着距离的增长线性地减少光的强度

然而,这样的线性方程通常会看起来比较假,在现实世界中,灯在近处通常会非常亮,但随着距离的增加光源的亮度一开始会下降非常快,但在远处时剩余的光强度就会下降的非常缓慢了,所以,我们需要一个不同的公式来减少光的强度

幸运的是一些聪明的人已经帮我们解决了这个问题,下面这个公式根据片段距光源的距离计算了衰减值,之后我们会将它乘以光的强度向量:

在这里d代表了片段距光源的距离,接下来为了计算衰减值,我们定义3个可配置项:

常数项Kc、一次项Kl和二次项Kq

  • 常数项Kc通常保持为1.0,它的主要作用是保证分母永远不会比1小,否则的话在某些距离上它反而会增加强度,这肯定不是我们想要的效果
  • 一次项Kl会与距离值相乘,以线性的方式减少强度
  • 二次项Kq会与距离的平方相乘,让光源以二次递减的方式减少强度,二次项在距离比较小的时候影响会比一次项小很多,但当距离值比较大的时候它就会比一次项更大了

由于二次项的存在,光线会在大部分时候以线性的方式衰退,直到距离变得足够大,让二次项超过一次项,光的强度会以更慢的速度下降

下面这张图显示了在100的距离内衰减的效果:

这正是我们想要的

但是,该对这三个项设置什么值呢?正确地设定它们的值取决于很多因素:环境、希望光覆盖的距离、光的类型等,在大多数情况下,这都是经验的问题

下面这个表格显示了模拟一个接近真实的,覆盖特定半径(距离)的光源时,这些项可能取的一些值

第一列指定的是在给定的三项时光所能覆盖的距离,这些值是大多数光源很好的起始点,它们由Ogre3D的Wiki所提供:

距离 常数项 一次项 二次项
7 1.0 0.7 1.8
13 1.0 0.35 0.44
20 1.0 0.22 0.20
32 1.0 0.14 0.07
50 1.0 0.09 0.032
65 1.0 0.07 0.017
100 1.0 0.045 0.0075
160 1.0 0.027 0.0028
200 1.0 0.022 0.0019
325 1.0 0.014 0.0007
600 1.0 0.007 0.0002
3250 1.0 0.0014 0.000007

你可以看到,常数项Kc在所有的情况下都是1.0,一次项Kl为了覆盖更远的距离通常都很小,二次项Kq甚至更小。尝试对这些值进行实验,看看它们在你的实现中有什么效果

在我们的环境中,32到100的距离对大多数的光源都足够了

代码实现

为了实现衰减,在片段着色器中我们还需要三个额外的值:也就是公式中的常数项、一次项和二次项

它们最好储存在之前定义的Light结构体中,注意我们使用上一篇博客中计算lightDir的方法,而不是上面平行光部分的

  1. vec3 lightDir = normalize(lightPos - FragPos);
  1. //片段着色器
  2. struct Light {
  3. vec3 position;
  4. vec3 ambient;
  5. vec3 diffuse;
  6. vec3 specular;
  7. float constant;
  8. float linear;
  9. float quadratic;
  10. };

然后我们将在OpenGL中设置这些项:我们希望光源能够覆盖50的距离,所以我们会使用表格中对应的常数项、一次项和二次项:

  1. //main.cpp
  2. lightingShader.setFloat("light.constant", 1.0f);
  3. lightingShader.setFloat("light.linear", 0.09f);
  4. lightingShader.setFloat("light.quadratic", 0.032f);

在片段着色器中实现衰减还是比较直接的:我们根据公式计算衰减值,之后再分别乘以环境光、漫反射和镜面光分量

我们仍需要公式中距光源的距离,还记得我们是怎么计算一个向量的长度的吗?我们可以通过获取片段和光源之间的向量差,并获取结果向量的长度作为距离项

我们可以使用GLSL内建的length函数来完成这一点:

  1. //片段着色器
  2. //衰减
  3. float distance = length(light.position - FragPos);
  4. float attenuation = 1.0 / (light.constant + light.linear * distance +
  5. light.quadratic * (distance * distance));

接下来,我们将包含这个衰减值到光照计算中,将它分别乘以环境光、漫反射和镜面光颜色。

我们可以将环境光分量保持不变,让环境光照不会随着距离减少,但是如果我们使用多于一个的光源,所有的环境光分量将会开始叠加,所以在这种情况下我们也希望衰减环境光照。简单实验一下,看看什么才能在你的环境中效果最好。

  1. ambient *= attenuation;
  2. diffuse *= attenuation;
  3. specular *= attenuation;

运行程序

部分源码

  1. //点光源片段着色器
  2. #version 330 core
  3. struct Material {
  4. sampler2D diffuse;
  5. sampler2D specular;
  6. float shininess;
  7. };
  8. struct Light {
  9. vec3 position;
  10. //vec3 direction; 点光源就不需要了
  11. vec3 ambient;
  12. vec3 diffuse;
  13. vec3 specular;
  14. float constant; //常数项Kc
  15. float linear; //一次项Kl
  16. float quadratic; //二次项Kq
  17. };
  18. in vec3 FragPos;
  19. in vec3 Normal;
  20. in vec2 TexCoords;
  21. out vec4 FragColor;
  22. uniform Material material;
  23. uniform Light light;
  24. //uniform vec3 objectColor;
  25. //uniform vec3 lightColor;
  26. //uniform vec3 lightPos;
  27. uniform vec3 viewPos;
  28. float specularStrength = 0.5;
  29. void main()
  30. {
  31. // 环境光
  32. vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
  33. // 漫反射
  34. vec3 norm = normalize(Normal);
  35. //vec3 lightDir = normalize(lightPos - FragPos);
  36. vec3 lightDir = normalize(light.position - FragPos);
  37. float diff = max(dot(norm, lightDir), 0.0);
  38. vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
  39. // 镜面光
  40. vec3 viewDir = normalize(viewPos - FragPos);
  41. vec3 reflectDir = reflect(-lightDir, norm);
  42. float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
  43. vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
  44. //衰减
  45. float distance = length(light.position - FragPos);
  46. float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
  47. ambient *= attenuation;
  48. diffuse *= attenuation;
  49. specular *= attenuation;
  50. vec3 result = ambient + diffuse + specular;
  51. FragColor = vec4(result, 1.0);
  52. }
  1. //点光源渲染循环
  2. glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
  3. while (!glfwWindowShouldClose(window))
  4. {
  5. // 帧时间差
  6. float currentFrame = glfwGetTime();
  7. deltaTime = currentFrame - lastFrame;
  8. lastFrame = currentFrame;
  9. // 输入
  10. processInput(window);
  11. // 渲染指令
  12. glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  13. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  14. lightingShader.use();
  15. //lightingShader.setVec3("light.direction", -0.2f, -1.0f, -0.3f); //平行光
  16. lightingShader.setVec3("light.position", lightPos);
  17. lightingShader.setVec3("viewPos", camera.Position);
  18. // 光照属性
  19. lightingShader.setVec3("light.ambient", 0.2f, 0.2f, 0.2f);
  20. lightingShader.setVec3("light.diffuse", 0.7f, 0.7f, 0.7f);
  21. lightingShader.setVec3("light.specular", 1.0f, 1.0f, 1.0f);
  22. lightingShader.setFloat("light.constant", 1.0f);
  23. lightingShader.setFloat("light.linear", 0.09f);
  24. lightingShader.setFloat("light.quadratic", 0.032f);
  25. // 材质属性
  26. lightingShader.setFloat("material.shininess", 32.0f);
  27. // 初始化view transformations
  28. glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
  29. glm::mat4 view = camera.GetViewMatrix();
  30. lightingShader.setMat4("projection", projection);
  31. lightingShader.setMat4("view", view);
  32. // 初始化world transformations
  33. glm::mat4 model = glm::mat4();
  34. lightingShader.setMat4("model", model);
  35. // 绑定漫反射贴图
  36. glActiveTexture(GL_TEXTURE0);
  37. glBindTexture(GL_TEXTURE_2D, diffuseMap);
  38. // 绑定镜面贴图
  39. glActiveTexture(GL_TEXTURE1);
  40. glBindTexture(GL_TEXTURE_2D, specularMap);
  41. // 渲染方块
  42. //glBindVertexArray(cubeVAO);
  43. //glDrawArrays(GL_TRIANGLES, 0, 36);
  44. glBindVertexArray(cubeVAO);
  45. for (unsigned int i = 0; i < 10; i++)
  46. {
  47. glm::mat4 model = glm::mat4(1.0f);
  48. model = glm::translate(model, cubePositions[i]);
  49. float angle = 20.0f * i;
  50. model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
  51. lightingShader.setMat4("model", model);
  52. glDrawArrays(GL_TRIANGLES, 0, 36);
  53. }
  54. // 渲染点光源立方体
  55. lampShader.use();
  56. // 设置模型、视图和投影矩阵uniform
  57. lampShader.setMat4("projection", projection);
  58. lampShader.setMat4("view", view);
  59. model = glm::mat4(1.0f);
  60. model = glm::translate(model, lightPos);
  61. model = glm::scale(model, glm::vec3(0.2f)); // 小点的立方体
  62. lampShader.setMat4("model", model);
  63. // 绘制光源立方体对象
  64. glBindVertexArray(lightVAO);
  65. glDrawArrays(GL_TRIANGLES, 0, 36);
  66. // 交换缓冲并查询IO事件
  67. glfwSwapBuffers(window);
  68. glfwPollEvents();
  69. }
  70. glDeleteVertexArrays(1, &cubeVAO);
  71. glDeleteVertexArrays(1, &lightVAO);
  72. glDeleteBuffers(1, &VBO);
  73. glfwTerminate();
  74. return 0;
  75. }

聚光

我们要讨论的最后一种类型的光是聚光(Spotlight)。聚光是位于环境中某个位置的光源,它只朝一个特定方向而不是所有方向照射光线。这样的结果就是只有在聚光方向的特定半径内的物体才会被照亮,其它的物体都会保持黑暗,比如路灯和手电筒

OpenGL中聚光是用一个世界空间位置、一个方向和一个切光角(Cutoff Angle)来表示的,切光角指定了聚光的半径(是圆锥的半径),对于每个片段,我们会计算片段是否位于聚光的切光方向之间(也就是在锥形内),如果是的话,我们就会相应地照亮片段

  • LightDir:从片段指向光源的向量
  • SpotDir:聚光所指向的方向
  • Phiϕ:指定了聚光半径的切光角,落在这个角度之外的物体都不会被这个聚光所照亮。
  • Thetaθ:LightDir向量和SpotDir向量之间的夹角,在聚光内部的话θ值应该比ϕ值小

所以我们要做的就是计算LightDir向量和SpotDir向量之间的点积(返回两个单位向量夹角的余弦值),并将它与切光角ϕ值对比

你现在应该了解聚光究竟是什么了,下面我们将以手电筒的形式创建一个聚光

手电筒

手电筒(Flashlight)是一个位于观察者位置的聚光,通常它都会瞄准玩家视角的正前方

手电筒就是普通的聚光,但它的位置和方向会随着玩家的位置和朝向不断更新

所以,在片段着色器中我们需要的值有聚光的位置向量(来计算光的方向向量)、聚光的方向向量和一个切光角。我们可以将它们储存在Light结构体中:

  1. //片段着色器
  2. struct Light {
  3. vec3 position;
  4. vec3 direction;
  5. float cutOff;
  6. ...
  7. };

接下来我们将合适的值传到着色器中:

  1. lightingShader.setVec3("light.position", camera.Position);
  2. lightingShader.setVec3("light.direction", camera.Front);
  3. lightingShader.setFloat("light.cutOff", glm::cos(glm::radians(12.5f)));

你可以看到,我们并没有给切光角设置一个角度值,反而是用角度值计算了一个余弦值,将余弦结果传递到片段着色器中

这样做的原因是:在片段着色器中,我们会计算LightDirSpotDir向量的点积,这个点积返回的将是一个余弦值而不是角度值,所以我们不能直接使用角度值和余弦值进行比较,为了获取角度值我们需要计算点积结果的反余弦,这是一个开销很大的计算

所以为了节约一点性能开销,我们将会计算切光角对应的余弦值,并将它的结果传入片段着色器中,由于这两个角度现在都由余弦角来表示了,我们可以直接对它们进行比较而不用进行任何开销高昂的计算

接下来就是计算θ值,并将它和切光角ϕ对比,来决定是否在聚光的内部:

  1. float theta = dot(lightDir, normalize(-light.direction));
  2. if(theta > light.cutOff)
  3. {
  4. // 执行光照计算
  5. }
  6. else // 否则,使用环境光,让场景在聚光之外时不至于完全黑暗
  7. color = vec4(light.ambient * vec3(texture(material.diffuse, TexCoords)), 1.0);

我们首先计算了lightDir和取反的direction向量(取反的是因为我们想让向量指向光源而不是从光源出发)之间的点积,记住要对所有的相关向量标准化

你可能奇怪为什么在if条件中使用的是 > 符号而不是 < 符号,theta不应该比光的切光角更小才是在聚光内部吗?这并没有错,但别忘了角度值现在都由余弦值来表示的

好了,运行程序

部分源码

  1. //聚光片段着色器
  2. #version 330 core
  3. struct Material {
  4. sampler2D diffuse;
  5. sampler2D specular;
  6. float shininess;
  7. };
  8. struct Light {
  9. vec3 position;
  10. vec3 direction;
  11. float cutOff;
  12. vec3 ambient;
  13. vec3 diffuse;
  14. vec3 specular;
  15. float constant; //常数项Kc
  16. float linear; //一次项Kl
  17. float quadratic; //二次项Kq
  18. };
  19. in vec3 FragPos;
  20. in vec3 Normal;
  21. in vec2 TexCoords;
  22. out vec4 FragColor;
  23. uniform Material material;
  24. uniform Light light;
  25. //uniform vec3 objectColor;
  26. //uniform vec3 lightColor;
  27. //uniform vec3 lightPos;
  28. uniform vec3 viewPos;
  29. float specularStrength = 0.5;
  30. void main()
  31. {
  32. vec3 lightDir = normalize(light.position - FragPos);
  33. // 检测是否在范围内
  34. float theta = dot(lightDir, normalize(-light.direction));
  35. if(theta > light.cutOff) // 执行光照计算
  36. {
  37. // 环境光
  38. vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
  39. // 漫反射
  40. vec3 norm = normalize(Normal);
  41. //vec3 lightDir = normalize(lightPos - FragPos);
  42. //vec3 lightDir = normalize(light.position - FragPos);
  43. float diff = max(dot(norm, lightDir), 0.0);
  44. vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
  45. // 镜面光
  46. vec3 viewDir = normalize(viewPos - FragPos);
  47. vec3 reflectDir = reflect(-lightDir, norm);
  48. float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
  49. vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
  50. //衰减
  51. float distance = length(light.position - FragPos);
  52. float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
  53. //ambient *= attenuation;
  54. diffuse *= attenuation;
  55. specular *= attenuation;
  56. vec3 result = ambient + diffuse + specular;
  57. FragColor = vec4(result, 1.0);
  58. }
  59. else // 否则,使用环境光,让场景在聚光之外时不至于完全黑暗
  60. {
  61. FragColor = vec4(light.ambient * texture(material.diffuse, TexCoords).rgb, 1.0);
  62. }
  63. }
  1. //聚光渲染循环
  2. while (!glfwWindowShouldClose(window))
  3. {
  4. // 帧时间差
  5. float currentFrame = glfwGetTime();
  6. deltaTime = currentFrame - lastFrame;
  7. lastFrame = currentFrame;
  8. // 输入
  9. processInput(window);
  10. // 渲染指令
  11. glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  12. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  13. lightingShader.use();
  14. //lightingShader.setVec3("light.direction", -0.2f, -1.0f, -0.3f); //平行光
  15. lightingShader.setVec3("light.position", camera.Position);
  16. lightingShader.setVec3("light.direction", camera.Front);
  17. lightingShader.setFloat("light.cutOff", glm::cos(glm::radians(12.5f)));
  18. lightingShader.setVec3("viewPos", camera.Position);
  19. // 光照属性
  20. lightingShader.setVec3("light.ambient", 0.2f, 0.2f, 0.2f);
  21. lightingShader.setVec3("light.diffuse", 0.7f, 0.7f, 0.7f);
  22. lightingShader.setVec3("light.specular", 1.0f, 1.0f, 1.0f);
  23. lightingShader.setFloat("light.constant", 1.0f);
  24. lightingShader.setFloat("light.linear", 0.09f);
  25. lightingShader.setFloat("light.quadratic", 0.032f);
  26. // 材质属性
  27. lightingShader.setFloat("material.shininess", 32.0f);
  28. // 初始化view transformations
  29. glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
  30. glm::mat4 view = camera.GetViewMatrix();
  31. lightingShader.setMat4("projection", projection);
  32. lightingShader.setMat4("view", view);
  33. // 初始化world transformations
  34. glm::mat4 model = glm::mat4();
  35. lightingShader.setMat4("model", model);
  36. // 绑定漫反射贴图
  37. glActiveTexture(GL_TEXTURE0);
  38. glBindTexture(GL_TEXTURE_2D, diffuseMap);
  39. // 绑定镜面贴图
  40. glActiveTexture(GL_TEXTURE1);
  41. glBindTexture(GL_TEXTURE_2D, specularMap);
  42. // 渲染方块
  43. //glBindVertexArray(cubeVAO);
  44. //glDrawArrays(GL_TRIANGLES, 0, 36);
  45. glBindVertexArray(cubeVAO);
  46. for (unsigned int i = 0; i < 10; i++)
  47. {
  48. glm::mat4 model = glm::mat4(1.0f);
  49. model = glm::translate(model, cubePositions[i]);
  50. float angle = 20.0f * i;
  51. model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
  52. lightingShader.setMat4("model", model);
  53. glDrawArrays(GL_TRIANGLES, 0, 36);
  54. }
  55. //聚光,也不需要光源了
  56. //// 渲染点光源立方体
  57. //lampShader.use();
  58. //// 设置模型、视图和投影矩阵uniform
  59. //lampShader.setMat4("projection", projection);
  60. //lampShader.setMat4("view", view);
  61. //model = glm::mat4(1.0f);
  62. //model = glm::translate(model, lightPos);
  63. //model = glm::scale(model, glm::vec3(0.2f)); // 小点的立方体
  64. //lampShader.setMat4("model", model);
  65. //// 绘制光源立方体对象
  66. //glBindVertexArray(lightVAO);
  67. //glDrawArrays(GL_TRIANGLES, 0, 36);
  68. // 交换缓冲并查询IO事件
  69. glfwSwapBuffers(window);
  70. glfwPollEvents();
  71. }
  72. glDeleteVertexArrays(1, &cubeVAO);
  73. glDeleteVertexArrays(1, &lightVAO);
  74. glDeleteBuffers(1, &VBO);
  75. glfwTerminate();
  76. return 0;
  77. }

平滑聚光边缘

我们的手电筒的光略显生硬,现在我们为了创建一种看起来边缘平滑的聚光,我们需要模拟聚光有一个内圆锥(Inner Cone)和一个外圆锥(Outer Cone),我们可以将内圆锥设置为上一部分中的那个圆锥,但我们也需要一个外圆锥,来让光从内圆锥逐渐减暗,直到外圆锥的边界

为了创建一个外圆锥,我们只需要再定义一个余弦值来代表聚光方向向量和外圆锥向量(等于它的半径)的夹角,然后,如果一个片段处于内外圆锥之间,将会给它计算出一个0.0到1.0之间的强度值,如果片段在内圆锥之内,它的强度就是1.0,如果在外圆锥之外强度值就是0.0

我们可以用下面这个公式来计算这个值:

这里ϵ是内(ϕ)和外圆锥(γ)之间的余弦值差(ϵ=ϕ−γ),最终的I值就是在当前片段聚光的强度。

很难去表现这个公式是怎么工作的,所以我们用一些实例值来看看:

θ θ(角度) ϕ(内光切) ϕ(角度) γ(外光切) γ(角度) ϵϵ II
0.87 30 0.91 25 0.82 35 0.91 - 0.82 = 0.09 0.87 - 0.82 / 0.09 = 0.56
0.9 26 0.91 25 0.82 35 0.91 - 0.82 = 0.09 0.9 - 0.82 / 0.09 = 0.89
0.97 14 0.91 25 0.82 35 0.91 - 0.82 = 0.09 0.97 - 0.82 / 0.09 = 1.67
0.83 34 0.91 25 0.82 35 0.91 - 0.82 = 0.09 0.83 - 0.82 / 0.09 = 0.11
0.64 50 0.91 25 0.82 35 0.91 - 0.82 = 0.09 0.64 - 0.82 / 0.09 = -2.0
0.966 15 0.9978 12.5 0.953 17.5 0.966 - 0.953 = 0.0448 0.966 - 0.953 / 0.0448 = 0.29

你可以看到,我们基本是在内外余弦值之间根据θ插值

如果你仍不明白发生了什么,不必担心,只需要记住这个公式就好了,在你更聪明的时候再回来看看

我们现在有了一个在聚光外是负的,在内圆锥内大于1.0的,在边缘处于两者之间的强度值了,如果我们正确地约束(Clamp)这个值,在片段着色器中就不再需要if-else了,我们能够使用计算出来的强度值直接乘以光照分量:

  1. float theta = dot(lightDir, normalize(-light.direction));
  2. float epsilon = light.cutOff - light.outerCutOff;
  3. float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
  4. ...
  5. // 将不对环境光做出影响,让它总是能有一点光
  6. diffuse *= intensity;
  7. specular *= intensity;
  8. ...

注意我们使用了clamp函数,它把第一个参数约束(Clamp)在了0.0到1.0之间,这保证强度值不会在[0, 1]区间之外

确定你将outerCutOff值添加到了Light结构体之中,并在程序中设置它的uniform值

我们使用的内切光角是12.5,外切光角是17.5:

部分源码

  1. //片段着色器
  2. #version 330 core
  3. struct Material {
  4. sampler2D diffuse;
  5. sampler2D specular;
  6. float shininess;
  7. };
  8. struct Light {
  9. vec3 position;
  10. vec3 direction;
  11. float cutOff;
  12. float outerCutOff;
  13. vec3 ambient;
  14. vec3 diffuse;
  15. vec3 specular;
  16. float constant; //常数项Kc
  17. float linear; //一次项Kl
  18. float quadratic; //二次项Kq
  19. };
  20. in vec3 FragPos;
  21. in vec3 Normal;
  22. in vec2 TexCoords;
  23. out vec4 FragColor;
  24. uniform Material material;
  25. uniform Light light;
  26. //uniform vec3 objectColor;
  27. //uniform vec3 lightColor;
  28. //uniform vec3 lightPos;
  29. uniform vec3 viewPos;
  30. float specularStrength = 0.5;
  31. void main()
  32. {
  33. // 环境光
  34. vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
  35. // 漫反射
  36. vec3 norm = normalize(Normal);
  37. //vec3 lightDir = normalize(lightPos - FragPos);
  38. vec3 lightDir = normalize(light.position - FragPos);
  39. float diff = max(dot(norm, lightDir), 0.0);
  40. vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
  41. // 镜面光
  42. vec3 viewDir = normalize(viewPos - FragPos);
  43. vec3 reflectDir = reflect(-lightDir, norm);
  44. float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
  45. vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
  46. // 聚光 (平滑边缘)
  47. float theta = dot(lightDir, normalize(-light.direction));
  48. float epsilon = (light.cutOff - light.outerCutOff);
  49. float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
  50. diffuse *= intensity;
  51. specular *= intensity;
  52. //衰减
  53. float distance = length(light.position - FragPos);
  54. float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
  55. ambient *= attenuation;
  56. diffuse *= attenuation;
  57. specular *= attenuation;
  58. vec3 result = ambient + diffuse + specular;
  59. FragColor = vec4(result, 1.0);
  60. }

多光源

现在我们将结合之前学过的所有知识,创建一个包含六个光源的场景,我们将模拟一个类似太阳的平行光光源,四个分散在场景中的点光源,以及一个手电筒

为了在场景中使用多个光源,我们希望将光照计算封装到GLSL函数中

GLSL中的函数和C函数很相似,它有一个函数名、一个返回值类型,如果函数不是在main函数之前声明的,我们还必须在代码文件顶部声明一个原型,我们对每个光照类型都创建一个不同的函数:平行光、点光源和聚光

当我们在场景中使用多个光源时,通常使用以下方法:我们需要有一个单独的颜色向量代表片段的输出颜色,对于每一个光源,它对片段的贡献颜色将会加到片段的输出颜色向量上,所以场景中的每个光源都会计算它们各自对片段的影响,并结合为一个最终的输出颜色

  1. out vec4 FragColor;
  2. void main()
  3. {
  4. // 定义一个输出颜色值
  5. vec3 output;
  6. // 将平行光的贡献加到输出中
  7. output += someFunctionToCalculateDirectionalLight();
  8. // 对所有的点光源也做相同的事情
  9. for(int i = 0; i < nr_of_point_lights; i++)
  10. output += someFunctionToCalculatePointLight();
  11. // 也加上其它的光源(比如聚光)
  12. output += someFunctionToCalculateSpotLight();
  13. FragColor = vec4(output, 1.0);
  14. }

实际的代码对每一种实现都可能不同,但大体的结构都是差不多的,我们定义了几个函数,用来计算每个光源的影响,并将最终的结果颜色加到输出颜色向量上,例如,如果两个光源都很靠近一个片段,那么它们所结合的贡献将会形成一个比单个光源照亮时更加明亮的片段

平行光

我么需要在片段着色器中定义一个函数来计算平行光对相应片段的贡献:它接受一些参数并计算一个平行光照颜色。

首先,我们需要定义一个平行光源最少所需要的变量,我们可以将这些变量储存在一个叫做DirLight的结构体中,并将它定义为一个uniform,和上面写的是基本一样的

  1. struct DirLight {
  2. vec3 direction;
  3. vec3 ambient;
  4. vec3 diffuse;
  5. vec3 specular;
  6. };
  7. uniform DirLight dirLight;

接下来我们可以将dirLight传入一个有着一下原型的函数。

  1. vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);

和C/C++一样,如果我们想调用一个函数(这里是在main函数中调用),这个函数需要在调用者的行数之前被定义过,在这个例子中我们更喜欢在main函数以下定义函数,所以上面要求就不满足了。所以,我们需要在main函数之上定义函数的原型,这和C语言中是一样的

你可以看到,这个函数需要一个DirLight结构体和其它两个向量来进行计算。如果你认真完成了上一节的话,这个函数的内容应该理解起来很容易:

  1. vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
  2. {
  3. vec3 lightDir = normalize(-light.direction);
  4. // 漫反射着色
  5. float diff = max(dot(normal, lightDir), 0.0);
  6. // 镜面光着色
  7. vec3 reflectDir = reflect(-lightDir, normal);
  8. float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
  9. // 合并结果
  10. vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
  11. vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
  12. vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
  13. return (ambient + diffuse + specular);
  14. }

我们基本上只是从上一节中复制了代码,并使用函数参数的两个向量来计算平行光的贡献向量,最终环境光、漫反射和镜面光的贡献将会合并为单个颜色向量返回

点光源

和平行光一样,我们也希望定义一个用于计算点光源对相应片段贡献,以及衰减,的函数。同样,我们定义一个包含了点光源所需所有变量的结构体:

  1. struct PointLight {
  2. vec3 position;
  3. float constant;
  4. float linear;
  5. float quadratic;
  6. vec3 ambient;
  7. vec3 diffuse;
  8. vec3 specular;
  9. };
  10. #define NR_POINT_LIGHTS 4
  11. uniform PointLight pointLights[NR_POINT_LIGHTS];

你可以看到,我们在GLSL中使用了预处理指令来定义了我们场景中点光源的数量,接着我们使用了这个NR_POINT_LIGHTS常量来创建了一个PointLight结构体的数组,GLSL中的数组和C数组一样,可以使用一对方括号来创建,现在我们有四个待填充数据的PointLight结构体

我们也可以定义一个大的结构体(而不是为每种类型的光源定义不同的结构体),包含所有不同种光照类型所需的变量,并将这个结构体用到所有的函数中,只需要忽略用不到的变量就行了

个人觉得当前的方法会更直观一点,不仅能够节省一些代码,而且由于不是所有光照类型都需要所有的变量,这样也能节省一些内存

点光源函数的原型如下:

  1. vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);

这个函数从参数中获取所需的所有数据,并返回一个代表该点光源对片段的颜色贡献的vec3。我们再一次聪明地从之前的教程中复制粘贴代码,完成了下面这样的函数:

  1. vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
  2. {
  3. vec3 lightDir = normalize(light.position - fragPos);
  4. // 漫反射着色
  5. float diff = max(dot(normal, lightDir), 0.0);
  6. // 镜面光着色
  7. vec3 reflectDir = reflect(-lightDir, normal);
  8. float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
  9. // 衰减
  10. float distance = length(light.position - fragPos);
  11. float attenuation = 1.0 / (light.constant + light.linear * distance +
  12. light.quadratic * (distance * distance));
  13. // 合并结果
  14. vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
  15. vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
  16. vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
  17. ambient *= attenuation;
  18. diffuse *= attenuation;
  19. specular *= attenuation;
  20. return (ambient + diffuse + specular);
  21. }

将这些功能抽象到这样一个函数中的优点是,我们能够不用重复的代码而很容易地计算多个点光源的光照了。在main函数中,我们只需要创建一个循环,遍历整个点光源数组,对每个点光源调用CalcPointLight就可以了。

聚光

不再赘述,定义结构体:

  1. struct SpotLight {
  2. vec3 position;
  3. vec3 direction;
  4. float cutOff;
  5. float outerCutOff;
  6. float constant;
  7. float linear;
  8. float quadratic;
  9. vec3 ambient;
  10. vec3 diffuse;
  11. vec3 specular;
  12. };
  1. vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
  2. {
  3. vec3 lightDir = normalize(light.position - fragPos);
  4. // 漫反射着色
  5. float diff = max(dot(normal, lightDir), 0.0);
  6. // 镜面光着色
  7. vec3 reflectDir = reflect(-lightDir, normal);
  8. float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
  9. // 衰减
  10. float distance = length(light.position - fragPos);
  11. float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
  12. // 聚光强度
  13. float theta = dot(lightDir, normalize(-light.direction));
  14. float epsilon = light.cutOff - light.outerCutOff;
  15. float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
  16. // 合并结果
  17. vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
  18. vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
  19. vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
  20. ambient *= attenuation * intensity;
  21. diffuse *= attenuation * intensity;
  22. specular *= attenuation * intensity;
  23. return (ambient + diffuse + specular);
  24. }

现在我们的片段着色器是这样的:

  1. #version 330 core
  2. out vec4 FragColor;
  3. struct Material {
  4. sampler2D diffuse;
  5. sampler2D specular;
  6. float shininess;
  7. };
  8. struct DirLight {
  9. vec3 direction;
  10. vec3 ambient;
  11. vec3 diffuse;
  12. vec3 specular;
  13. };
  14. struct PointLight {
  15. vec3 position;
  16. float constant;
  17. float linear;
  18. float quadratic;
  19. vec3 ambient;
  20. vec3 diffuse;
  21. vec3 specular;
  22. };
  23. struct SpotLight {
  24. vec3 position;
  25. vec3 direction;
  26. float cutOff;
  27. float outerCutOff;
  28. float constant;
  29. float linear;
  30. float quadratic;
  31. vec3 ambient;
  32. vec3 diffuse;
  33. vec3 specular;
  34. };
  35. #define NR_POINT_LIGHTS 4
  36. in vec3 FragPos;
  37. in vec3 Normal;
  38. in vec2 TexCoords;
  39. uniform vec3 viewPos;
  40. uniform DirLight dirLight;
  41. uniform PointLight pointLights[NR_POINT_LIGHTS];
  42. uniform SpotLight spotLight;
  43. uniform Material material;
  44. vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);
  45. vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
  46. vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
  47. void main()
  48. {
  49. // 属性
  50. vec3 norm = normalize(Normal);
  51. vec3 viewDir = normalize(viewPos - FragPos);
  52. // 1.平行光
  53. vec3 result = CalcDirLight(dirLight, norm, viewDir);
  54. // 2.点光源
  55. for(int i = 0; i < NR_POINT_LIGHTS; i++)
  56. result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);
  57. // 3.手电筒
  58. result += CalcSpotLight(spotLight, norm, FragPos, viewDir);
  59. FragColor = vec4(result, 1.0);
  60. }
  61. vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
  62. {
  63. vec3 lightDir = normalize(-light.direction);
  64. // 漫反射着色
  65. float diff = max(dot(normal, lightDir), 0.0);
  66. // 镜面光着色
  67. vec3 reflectDir = reflect(-lightDir, normal);
  68. float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
  69. // 合并结果
  70. vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
  71. vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
  72. vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
  73. return (ambient + diffuse + specular);
  74. }
  75. vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
  76. {
  77. vec3 lightDir = normalize(light.position - fragPos);
  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. float distance = length(light.position - fragPos);
  85. float attenuation = 1.0 / (light.constant + light.linear * distance +
  86. light.quadratic * (distance * distance));
  87. // 合并结果
  88. vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
  89. vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
  90. vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
  91. ambient *= attenuation;
  92. diffuse *= attenuation;
  93. specular *= attenuation;
  94. return (ambient + diffuse + specular);
  95. }
  96. vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
  97. {
  98. vec3 lightDir = normalize(light.position - fragPos);
  99. // 漫反射着色
  100. float diff = max(dot(normal, lightDir), 0.0);
  101. // 镜面光着色
  102. vec3 reflectDir = reflect(-lightDir, normal);
  103. float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
  104. // 衰减
  105. float distance = length(light.position - fragPos);
  106. float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
  107. // 聚光强度
  108. float theta = dot(lightDir, normalize(-light.direction));
  109. float epsilon = light.cutOff - light.outerCutOff;
  110. float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
  111. // 合并结果
  112. vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
  113. vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
  114. vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
  115. ambient *= attenuation * intensity;
  116. diffuse *= attenuation * intensity;
  117. specular *= attenuation * intensity;
  118. return (ambient + diffuse + specular);
  119. }

合并结果

现在我们已经定义了一个计算平行光的函数和一个计算点光源的函数了,我们可以将它们合并放到main函数中。

  1. void main()
  2. {
  3. // 属性
  4. vec3 norm = normalize(Normal);
  5. vec3 viewDir = normalize(viewPos - FragPos);
  6. // 1.平行光
  7. vec3 result = CalcDirLight(dirLight, norm, viewDir);
  8. // 2.点光源
  9. for(int i = 0; i < NR_POINT_LIGHTS; i++)
  10. result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);
  11. // 3.手电筒
  12. result += CalcSpotLight(spotLight, norm, FragPos, viewDir);
  13. FragColor = vec4(result, 1.0);
  14. }

每个光源类型都将它们的贡献加到了最终的输出颜色上,直到所有的光源都处理完了,最终的颜色包含了场景中所有光源的颜色影响所合并的结果

设置平行光结构体的uniform应该非常熟悉了,但是你可能会在想我们该如何设置点光源的uniform值,因为点光源的uniform现在是一个PointLight的数组了,其实这并不复杂

  1. lightingShader.setFloat("pointLights[0].constant", 1.0f);

在这里我们索引了pointLights数组中的第一个PointLight,并获取了constant变量的位置。但这也意味着不幸的是我们必须对这四个点光源手动设置uniform值,这让点光源本身就产生了28个uniform调用,非常冗长

你也可以尝试将这些抽象出去一点,定义一个点光源类,让它来为你设置uniform值,但最后你仍然要用这种方式设置所有光源的uniform值

别忘了,我们还需要为每个点光源定义一个位置向量,所以我们让它们在场景中分散一点,我们会定义另一个glm::vec3数组来包含点光源的位置:

  1. glm::vec3 pointLightPositions[] = {
  2. glm::vec3( 0.7f, 0.2f, 2.0f),
  3. glm::vec3( 2.3f, -3.3f, -4.0f),
  4. glm::vec3(-4.0f, 2.0f, -12.0f),
  5. glm::vec3( 0.0f, 0.0f, -3.0f)
  6. };

接下来我们从pointLights数组中索引对应的PointLight,将它的position值设置为刚刚定义的位置值数组中的其中一个,同时我们还要保证现在绘制的是四个灯立方体而不是仅仅一个,只要对每个灯物体创建一个不同的模型矩阵就可以了,和我们之前对箱子的处理类似

  1. glBindVertexArray(lightVAO);
  2. for (unsigned int i = 0; i < 4; i++)
  3. {
  4. model = glm::mat4(1.0f);
  5. model = glm::translate(model, pointLightPositions[i]);
  6. model = glm::scale(model, glm::vec3(0.2f)); // Make it a smaller cube
  7. lampShader.setMat4("model", model);
  8. glDrawArrays(GL_TRIANGLES, 0, 36);
  9. }

运行程序:

OpenGl光照基础学习到此结束

源码

  1. #include <glad/glad.h>
  2. #include <GLFW/glfw3.h>
  3. #include <iostream>
  4. #include "shader_s.h"
  5. #include "camera.h"
  6. #define STB_IMAGE_IMPLEMENTATION
  7. #include "stb_image.h"
  8. #include <glm/glm.hpp>
  9. #include <glm/gtc/matrix_transform.hpp>
  10. #include <glm/gtc/type_ptr.hpp>
  11. void framebuffer_size_callback(GLFWwindow* window, int width, int height);
  12. void mouse_callback(GLFWwindow* window, double xpos, double ypos);
  13. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
  14. void processInput(GLFWwindow *window);
  15. unsigned int loadTexture(char const * path);
  16. // settings
  17. const unsigned int SCR_WIDTH = 800;
  18. const unsigned int SCR_HEIGHT = 600;
  19. // camera
  20. Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
  21. float lastX = SCR_WIDTH / 2.0f;
  22. float lastY = SCR_HEIGHT / 2.0f;
  23. bool firstMouse = true;
  24. // timing
  25. float deltaTime = 0.0f; // 当前帧与上一帧的时间差
  26. float lastFrame = 0.0f; // 上一帧的时间
  27. glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
  28. int main()
  29. {
  30. // glfw初始化
  31. glfwInit();
  32. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  33. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  34. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  35. // glfw创建窗口
  36. GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
  37. if (window == NULL)
  38. {
  39. std::cout << "Failed to create GLFW window" << std::endl;
  40. glfwTerminate();
  41. return -1;
  42. }
  43. glfwMakeContextCurrent(window);
  44. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  45. glfwSetCursorPosCallback(window, mouse_callback);
  46. glfwSetScrollCallback(window, scroll_callback);
  47. // 捕捉鼠标
  48. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  49. // glad加载所有OpenGL函数指针
  50. if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  51. {
  52. std::cout << "Failed to initialize GLAD" << std::endl;
  53. return -1;
  54. }
  55. glEnable(GL_DEPTH_TEST);
  56. Shader lightingShader("colorVertex.txt", "colorFragment.txt");// 起什么名字自己定
  57. Shader lampShader("lampVertex.txt", "lampFragment.txt");
  58. float vertices[] = {
  59. // positions // normals // texture coords
  60. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
  61. 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
  62. 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
  63. 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
  64. -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
  65. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
  66. -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  67. 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
  68. 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
  69. 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
  70. -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
  71. -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  72. -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  73. -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
  74. -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
  75. -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
  76. -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  77. -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  78. 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  79. 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
  80. 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
  81. 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
  82. 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  83. 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  84. -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
  85. 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
  86. 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
  87. 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
  88. -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
  89. -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
  90. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
  91. 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
  92. 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
  93. 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
  94. -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
  95. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
  96. };
  97. // 立方体位置
  98. glm::vec3 cubePositions[] = {
  99. glm::vec3(0.0f, 0.0f, 0.0f),
  100. glm::vec3(2.0f, 5.0f, -15.0f),
  101. glm::vec3(-1.5f, -2.2f, -2.5f),
  102. glm::vec3(-3.8f, -2.0f, -12.3f),
  103. glm::vec3(2.4f, -0.4f, -3.5f),
  104. glm::vec3(-1.7f, 3.0f, -7.5f),
  105. glm::vec3(1.3f, -2.0f, -2.5f),
  106. glm::vec3(1.5f, 2.0f, -2.5f),
  107. glm::vec3(1.5f, 0.2f, -1.5f),
  108. glm::vec3(-1.3f, 1.0f, -1.5f)
  109. };
  110. // 点光源位置
  111. glm::vec3 pointLightPositions[] = {
  112. glm::vec3(0.7f, 0.2f, 2.0f),
  113. glm::vec3(2.3f, -3.3f, -4.0f),
  114. glm::vec3(-4.0f, 2.0f, -12.0f),
  115. glm::vec3(0.0f, 0.0f, -3.0f)
  116. };
  117. unsigned int VBO;
  118. glGenBuffers(1, &VBO);
  119. unsigned int cubeVAO;
  120. glGenVertexArrays(1, &cubeVAO);
  121. glBindVertexArray(cubeVAO);
  122. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  123. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  124. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
  125. glEnableVertexAttribArray(0);
  126. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
  127. glEnableVertexAttribArray(1);
  128. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
  129. glEnableVertexAttribArray(2);
  130. unsigned int lightVAO;
  131. glGenVertexArrays(1, &lightVAO);
  132. glBindVertexArray(lightVAO);
  133. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  134. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
  135. glEnableVertexAttribArray(0);
  136. unsigned int diffuseMap = loadTexture("container2.png");
  137. unsigned int specularMap = loadTexture("container2_specular.png");
  138. lightingShader.use();
  139. lightingShader.setInt("material.diffuse", 0);
  140. lightingShader.setInt("material.specular", 1);
  141. //渲染循环
  142. while (!glfwWindowShouldClose(window))
  143. {
  144. // 帧时间差
  145. float currentFrame = glfwGetTime();
  146. deltaTime = currentFrame - lastFrame;
  147. lastFrame = currentFrame;
  148. // 输入
  149. processInput(window);
  150. // 渲染指令
  151. glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  152. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  153. lightingShader.use();
  154. lightingShader.setVec3("light.position", camera.Position);
  155. lightingShader.setFloat("material.shininess", 32.0f);
  156. // 平行光
  157. lightingShader.setVec3("dirLight.direction", -0.2f, -1.0f, -0.3f);
  158. lightingShader.setVec3("dirLight.ambient", 0.05f, 0.05f, 0.05f);
  159. lightingShader.setVec3("dirLight.diffuse", 0.4f, 0.4f, 0.4f);
  160. lightingShader.setVec3("dirLight.specular", 0.5f, 0.5f, 0.5f);
  161. // 点光源 1
  162. lightingShader.setVec3("pointLights[0].position", pointLightPositions[0]);
  163. lightingShader.setVec3("pointLights[0].ambient", 0.05f, 0.05f, 0.05f);
  164. lightingShader.setVec3("pointLights[0].diffuse", 0.8f, 0.8f, 0.8f);
  165. lightingShader.setVec3("pointLights[0].specular", 1.0f, 1.0f, 1.0f);
  166. lightingShader.setFloat("pointLights[0].constant", 1.0f);
  167. lightingShader.setFloat("pointLights[0].linear", 0.09);
  168. lightingShader.setFloat("pointLights[0].quadratic", 0.032);
  169. // 点光源 2
  170. lightingShader.setVec3("pointLights[1].position", pointLightPositions[1]);
  171. lightingShader.setVec3("pointLights[1].ambient", 0.05f, 0.05f, 0.05f);
  172. lightingShader.setVec3("pointLights[1].diffuse", 0.8f, 0.8f, 0.8f);
  173. lightingShader.setVec3("pointLights[1].specular", 1.0f, 1.0f, 1.0f);
  174. lightingShader.setFloat("pointLights[1].constant", 1.0f);
  175. lightingShader.setFloat("pointLights[1].linear", 0.09);
  176. lightingShader.setFloat("pointLights[1].quadratic", 0.032);
  177. // 点光源 3
  178. lightingShader.setVec3("pointLights[2].position", pointLightPositions[2]);
  179. lightingShader.setVec3("pointLights[2].ambient", 0.05f, 0.05f, 0.05f);
  180. lightingShader.setVec3("pointLights[2].diffuse", 0.8f, 0.8f, 0.8f);
  181. lightingShader.setVec3("pointLights[2].specular", 1.0f, 1.0f, 1.0f);
  182. lightingShader.setFloat("pointLights[2].constant", 1.0f);
  183. lightingShader.setFloat("pointLights[2].linear", 0.09);
  184. lightingShader.setFloat("pointLights[2].quadratic", 0.032);
  185. // 点光源 4
  186. lightingShader.setVec3("pointLights[3].position", pointLightPositions[3]);
  187. lightingShader.setVec3("pointLights[3].ambient", 0.05f, 0.05f, 0.05f);
  188. lightingShader.setVec3("pointLights[3].diffuse", 0.8f, 0.8f, 0.8f);
  189. lightingShader.setVec3("pointLights[3].specular", 1.0f, 1.0f, 1.0f);
  190. lightingShader.setFloat("pointLights[3].constant", 1.0f);
  191. lightingShader.setFloat("pointLights[3].linear", 0.09);
  192. lightingShader.setFloat("pointLights[3].quadratic", 0.032);
  193. // 手电筒
  194. lightingShader.setVec3("spotLight.position", camera.Position);
  195. lightingShader.setVec3("spotLight.direction", camera.Front);
  196. lightingShader.setVec3("spotLight.ambient", 0.0f, 0.0f, 0.0f);
  197. lightingShader.setVec3("spotLight.diffuse", 1.0f, 1.0f, 1.0f);
  198. lightingShader.setVec3("spotLight.specular", 1.0f, 1.0f, 1.0f);
  199. lightingShader.setFloat("spotLight.constant", 1.0f);
  200. lightingShader.setFloat("spotLight.linear", 0.09);
  201. lightingShader.setFloat("spotLight.quadratic", 0.032);
  202. lightingShader.setFloat("spotLight.cutOff", glm::cos(glm::radians(12.5f)));
  203. lightingShader.setFloat("spotLight.outerCutOff", glm::cos(glm::radians(15.0f)));
  204. // 初始化view transformations
  205. glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
  206. glm::mat4 view = camera.GetViewMatrix();
  207. lightingShader.setMat4("projection", projection);
  208. lightingShader.setMat4("view", view);
  209. // 初始化world transformations
  210. glm::mat4 model = glm::mat4();
  211. lightingShader.setMat4("model", model);
  212. // 绑定漫反射贴图
  213. glActiveTexture(GL_TEXTURE0);
  214. glBindTexture(GL_TEXTURE_2D, diffuseMap);
  215. // 绑定镜面贴图
  216. glActiveTexture(GL_TEXTURE1);
  217. glBindTexture(GL_TEXTURE_2D, specularMap);
  218. // 渲染方块
  219. glBindVertexArray(cubeVAO);
  220. for (unsigned int i = 0; i < 10; i++)
  221. {
  222. glm::mat4 model = glm::mat4(1.0f);
  223. model = glm::translate(model, cubePositions[i]);
  224. float angle = 20.0f * i;
  225. model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
  226. lightingShader.setMat4("model", model);
  227. glDrawArrays(GL_TRIANGLES, 0, 36);
  228. }
  229. lampShader.use();
  230. lampShader.setMat4("projection", projection);
  231. lampShader.setMat4("view", view);;
  232. glBindVertexArray(lightVAO);
  233. for (unsigned int i = 0; i < 4; i++)
  234. {
  235. model = glm::mat4(1.0f);
  236. model = glm::translate(model, pointLightPositions[i]);
  237. model = glm::scale(model, glm::vec3(0.2f)); // Make it a smaller cube
  238. lampShader.setMat4("model", model);
  239. glDrawArrays(GL_TRIANGLES, 0, 36);
  240. }
  241. // 交换缓冲并查询IO事件
  242. glfwSwapBuffers(window);
  243. glfwPollEvents();
  244. }
  245. glDeleteVertexArrays(1, &cubeVAO);
  246. glDeleteVertexArrays(1, &lightVAO);
  247. glDeleteBuffers(1, &VBO);
  248. glfwTerminate();
  249. return 0;
  250. }
  251. void processInput(GLFWwindow *window)
  252. {
  253. if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)//是否按下了返回键
  254. glfwSetWindowShouldClose(window, true);
  255. if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
  256. camera.ProcessKeyboard(FORWARD, deltaTime);
  257. if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
  258. camera.ProcessKeyboard(BACKWARD, deltaTime);
  259. if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
  260. camera.ProcessKeyboard(LEFT, deltaTime);
  261. if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
  262. camera.ProcessKeyboard(RIGHT, deltaTime);
  263. }
  264. void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  265. {
  266. glViewport(0, 0, width, height);
  267. }
  268. void mouse_callback(GLFWwindow* window, double xpos, double ypos)
  269. {
  270. if (firstMouse)
  271. {
  272. lastX = xpos;
  273. lastY = ypos;
  274. firstMouse = false;
  275. }
  276. float xoffset = xpos - lastX;
  277. float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
  278. lastX = xpos;
  279. lastY = ypos;
  280. camera.ProcessMouseMovement(xoffset, yoffset);
  281. }
  282. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
  283. {
  284. camera.ProcessMouseScroll(yoffset);
  285. }
  286. unsigned int loadTexture(char const * path)
  287. {
  288. unsigned int textureID;
  289. glGenTextures(1, &textureID);
  290. int width, height, nrComponents;
  291. unsigned char *data = stbi_load(path, &width, &height, &nrComponents, 0);
  292. if (data)
  293. {
  294. GLenum format;
  295. if (nrComponents == 1)
  296. format = GL_RED;
  297. else if (nrComponents == 3)
  298. format = GL_RGB;
  299. else if (nrComponents == 4)
  300. format = GL_RGBA;
  301. glBindTexture(GL_TEXTURE_2D, textureID);
  302. glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
  303. glGenerateMipmap(GL_TEXTURE_2D);
  304. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  305. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  306. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  307. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  308. stbi_image_free(data);
  309. }
  310. else
  311. {
  312. std::cout << "Texture failed to load at path: " << path << std::endl;
  313. stbi_image_free(data);
  314. }
  315. return textureID;
  316. }

OpenGL光照3:光源的更多相关文章

  1. OpenGL光照和颜色

    OpenGL光照和颜色 转自:http://www.cnblogs.com/kekec/archive/2011/08/16/2140789.html OpenGL场景中模型颜色的产生,大致为如下的流 ...

  2. 浅析OpenGL光照

    浅析OpenGL光照 之前从来都没有涉及光照的内容,心想只要能通过常规的方法渲染出几何体甚至是模型就可以了,然而没有光照的日子注定是苦涩的,因为仅凭几何体和模型的颜色无法达到真是渲染的效果,在实际中有 ...

  3. OpenGL光照1:颜色和基础光照

    本文是个人学习记录,学习建议看教程 https://learnopengl-cn.github.io/ 非常感谢原作者JoeyDeVries和多为中文翻译者提供的优质教程 的内容为插入注释,可以先跳过 ...

  4. OpenGL光照测试

    OpenGL光照测试 花了大概半个月,研究了OpenGL的光照.请注意是固定管线渲染的光照,如果使用着色器的高手们请飘过.这个程序是通过光照对模型的照射,来研究OpenGL光照的性质.以后可以通过这个 ...

  5. 【狼】openGL 光照的学习

    小狼学习原创,欢迎批评指正 http://www.cnblogs.com/zhanlang96/p/3859439.html 先上代码 #include "stdafx.h" #i ...

  6. OpenGL光照设置

    一.设置光源 (1)光源的种类 环境光 环境光是一种无处不在的光.环境光源放出的光线被认为来自任何方向.因此,当你仅为场景指定环境光时,所有的物体无论法向量如何,都将表现为同样的明暗程度. 点光源 由 ...

  7. 实验7 OpenGL光照

    一.实验目的: 了解掌握OpenGL程序的光照与材质,能正确使用光源与材质函数设置所需的绘制效果. 二.实验内容: (1)下载并运行Nate Robin教学程序包中的lightmaterial程序,试 ...

  8. OpenGL光照2:材质和光照贴图

    本文是个人学习记录,学习建议看教程 https://learnopengl-cn.github.io/ 非常感谢原作者JoeyDeVries和多为中文翻译者提供的优质教程 的内容为插入注释,可以先跳过 ...

  9. 第07课 OpenGL 光照和键盘(1)

    光照和键盘控制: 在这一课里,我们将添加光照和键盘控制,它让程序看起来更美观. 这一课我会教您如何使用三种不同的纹理滤波方式.教您如何使用键盘来移动场景中的对象,还会教您在OpenGL场景中应用简单的 ...

随机推荐

  1. nginx学习(六):日志切割

    现有的日志都会存在 access.log 文件中,但是随着时间的推移,这个文件的内容会越来越多,体积会越来越大,不便于运维人员查看,所以我们可以通过把这个大的日志文件切割为多份不同的小文件作为日志,切 ...

  2. C++入门到理解阶段二基础篇(8)——C++指针

    1.什么是指针? 为了更加清楚的了解什么是指针?我们首先看下变量和内存的关系,当我们定义了int a=10之后.相当于在内存之中找了块4个字节大小的空间,并且存储10,要想操作这块空间,就通过a这个变 ...

  3. HDU - 5952 Counting Cliques

    Counting Cliques HDU - 5952 OJ-ID: hdu-5952 author:Caution_X date of submission:20191110 tags:dfs,gr ...

  4. 使用过Redis,我竟然还不知道Rdb

    目录 使用过Redis,那就先说说使用过那些场景吧 Rdb文件是什么,它是干什么的 分析工具 小结 联想 推荐阅读 使用过Redis,那就先说说使用过那些场景吧 字符串缓存 //举例 $redis-& ...

  5. 基于SpringCloud实现Shard-Jdbc的分库分表模式,数据库扩容方案

    本文源码:GitHub·点这里 || GitEE·点这里 一.项目结构 1.工程结构 2.模块命名 shard-common-entity: 公共代码块 shard-open-inte: 开放接口管理 ...

  6. ASP.NET Core 2.2 WebApi 系列【五】MiniProfiler与Swagger集成

    MiniProfiler 是一款性能分析的轻量级程序,可以基于action(request)记录每个阶段的耗时时长,还是可以显示访问数据库时的SQL(支持EF.EF Code First)等 一.安装 ...

  7. Java入门——在Linux环境下安装JDK并配置环境变量

    Java入门——在Linux环境下安装JDK并配置环境变量 摘要:本文主要说明在Linux环境下JDK的安装,以及安装完成之后环境变量的配置. 使用已下载的压缩包进行安装 下载并解压 在Java的官网 ...

  8. 【转载】Android绘图之Path总结

    Path作为Android中一种相对复杂的绘图方式,官方文档中的有些解释并不是很好理解,这里作一个相对全面一些的总结,供日后查看,也分享给大家,共同进步. 1.基本绘图方法 addArc(RectF ...

  9. Andorid Studio 新建模拟器无法联网问题

    1.查看自己本机的dns cmd  -> ipconfing /all 2.修改模拟器的dns 跟PC本机一致.  开启模拟器 -> cmd -> adb root  (需要root ...

  10. CSAPP 3 程序的机器级表示

    1 本章总述 1) 通过让编译器产生机器级程序的汇编表示, 学习了编译器及其优化能力, 以及机器.数据类型和指令集; 2) 学习了程序如何将数据存储在不同的内存区域中 -- 程序开发人员需要知道一个变 ...