float vertices[] = { // 详细看代码 }
GLuint VAO, VBO, lightVAO; glGenVertexArrays(1, &VAO); glGenVertexArrays(1, &lightVAO); glGenBuffers(1, &VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // box VAO glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0); glEnableVertexAttribArray(0); // light VAO glBindVertexArray(lightVAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *) 0); glEnableVertexAttribArray(0);
glm::vec3 lightPos = glm::vec3(1.2f, 1.0f, 2.0f);
objectShader.use(); objectShader.setVec3("objectColor", 1.0f, 0.5f, 0.31f); objectShader.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
objectShader.use(); objectShader.setVec3("lightPos", lightPos); glm::mat4 model = glm::mat4(1.0f); glm::mat4 view = camera.GetViewMatrix(); glm::mat4 projection = glm::perspective(glm::radians(camera.zoom), (float) SCR_WIDTH / (float) SCR_HEIGHT, 0.1f, 100.0f); objectShader.setMat4("model", model); objectShader.setMat4("view", view); objectShader.setMat4("projection", projection); glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 36);
lightShader.use(); model = glm::mat4(1.0f); model = glm::translate(model, lightPos); // reset position model = glm::scale(model, glm::vec3(0.2f)); // smaller lightShader.setMat4("model", model); lightShader.setMat4("view", view); lightShader.setMat4("projection", projection); glBindVertexArray(lightVAO); glDrawArrays(GL_TRIANGLES, 0, 36);
#version 410 core out vec4 FragColor; uniform vec3 objectColor; uniform vec3 lightColor; void main() { FragColor = vec4(lightColor * objectColor, 1.0); }
int main() { // 1. 环境光照 float ambientStrength = 0.1; vec3 ambient = ambientStrength * lightColor; }
in vec3 FragPos; // 着色片段位置 in vec3 Normal; // 法向量 uniform vec3 lightPos; // 光源位置 uniform vec3 lightColor; int main() { // ... vec3 norm = normalize(Normal);// 片段法向量 vec3 lightDir = normalize(lightPos - FragPos);// 片段 - 光源向量 float diff = max(dot(norm, lightDir), 0.0); vec3 diffuse = diff * lightColor;
float specularStrength = 0.5; vec3 viewDir = normalize(viewPos - FragPos); vec3 reflectDir = reflect(-lightDir, norm); float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32); vec3 specular = specularStrength * spec * lightColor;
vec3 result = (ambient + diffuse + specular) * objectColor;
#version 410 core layout (location = 0) in vec3 aPos;// 顶点坐标 layout (location = 1) in vec3 aNormal;// 顶点法向量 out vec3 FragPos; out vec3 Normal; uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { FragPos = vec3(model * vec4(aPos, 1.0)); Normal = mat3(transpose(inverse(model))) * aNormal; // 法线矩阵 gl_Position = projection * view * model * vec4(aPos, 1.0f); }
struct Material { vec3 ambient; vec3 diffuse; vec3 specular; float shininess; }; uniform Material material;
struct Light { vec3 position; vec3 ambient; vec3 diffuse; vec3 specular; }; uniform Light light;
// 1. 环境光照 vec3 ambient = light.ambient * material.ambient;
vec3 norm = normalize(Normal);// 片段法向量 vec3 lightDir = normalize(light.position - FragPos);// 片段 - 光源向量 float diff = max(dot(norm, lightDir), 0.0); vec3 diffuse = light.diffuse * (diff * material.diffuse);
// 3. 镜面高光 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 * material.specular);
vec3 result = ambient + diffuse + specular; FragColor = vec4(result, 1.0);
// set material objectShader.setVec3("material.ambient", 1.0f, 0.5f, 0.31f); objectShader.setVec3("material.diffuse", 1.0f, 0.5f, 0.31f); objectShader.setVec3("material.specular", 0.5f, 0.5f, 0.5f); objectShader.setFloat("material.shininess", 32.0f); objectShader.setVec3("light.ambient", 0.2f, 0.2f, 0.2f); objectShader.setVec3("light.diffuse", 0.5f, 0.5f, 0.5f); objectShader.setVec3("light.specular", 1.0f, 1.0f, 1.0f);
Title | Link |
---|---|
材质 - LearnOpenGL | https://learnopengl-cn.github.io/02%20Lighting/03%20Materials/ |
https://github.com/superfreeeee/Blog-code/tree/main/others/open_gl/open_gl_basic_light_materials