环境光照添加到场景里非常简单,用光的颜色乘以一个很小的常量环境因子,再乘以物体的颜色,然后将最终结果作为片段的颜色:
void main()
{
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;
vec3 result = ambient * objectColor;
FragColor = vec4(result, 1.0);
}
漫反射的计算稍微复杂些,计算漫反射时需要
1.法向量:一个垂直于顶点表面的向量
2.定向的光线:作为光源的位置与片段的位置之间向量差的方向向量。为了计算这个光线,我们需要光的位置向量和片段的位置向量。
漫反射的计算
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(sunPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * sunColor;
完整demo代码如下:
#include "stdio.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <soil/SOIL.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include "wrapperWindow.h"
#include "wrapperShader.h"
#include "data.h"
const int design_w = 800;
const int design_h = 600;
int main()
{
GLFWwindow *window = wrapperWindow(design_w, design_h).getWindow();
auto m_shader = wrapperShader::createWithFilePath("triangle.vsh", "triangle.fsh");
auto m_lightShader = wrapperShader::createWithFilePath("light.vsh", "light.fsh");
int arr_group_nun = 6;
GLuint vao, vbo;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeNormalArr), cubeNormalArr, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, arr_group_nun* sizeof(GLfloat), (GLvoid *)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, arr_group_nun* sizeof(GLfloat), (GLvoid *)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//--
GLuint lightVao;
glGenVertexArrays(1, &lightVao);
glBindVertexArray(lightVao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, arr_group_nun* sizeof(GLfloat), (GLvoid *)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//-----------
//启用深度测试
glEnable(GL_DEPTH_TEST);
glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_shader.useShaderProgram();
m_shader.setVec3("objectColor", 1.0f, 0.5f, 0.31f);
m_shader.setVec3("sunColor", 1.0f, 1.0f, 1.0f);
m_shader.setVec3("sunPos", lightPos);
glm::mat4 view;
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
m_shader.setMat4fv("view",view);
glm::mat4 projection;
projection = glm::perspective(glm::radians(45.0f), (float)(design_w*1.0 / design_h), 0.1f, 100.0f);
m_shader.setMat4fv("projection", projection);
glm::mat4 model;
model = glm::rotate(model, (float)glfwGetTime()*glm::radians(25.0f), glm::vec3(1.0f, -1.0f, 0.0f));
m_shader.setMat4fv("model", model);;
glBindVertexArray(vao);
//glBindTexture(GL_TEXTURE_2D,tex);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
//--
m_lightShader.useShaderProgram();
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
m_lightShader.setMat4fv("view", view);
projection = glm::perspective(glm::radians(45.0f), (float)(design_w*1.0 / design_h), 0.1f, 100.0f);
m_lightShader.setMat4fv("projection", projection);
model = glm::mat4();
model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.2f));
m_lightShader.setMat4fv("model", model);
glBindVertexArray(lightVao);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
效果图: