当前位置: 首页 > news >正文

LearnOpenGL学习(光照 -- 颜色,基础光照,材质)

光照 

glm::vec3 lightColor(0.0f, 1.0f, 0.0f);
glm::vec3 toyColor(1.0f, 0.5f, 0.31f);
glm::vec3 result = lightColor * toyColor; // = (0.0f, 0.5f, 0.0f);

说明:当我们把光源的颜色与物体的颜色值相乘,所得到的就是这个物体所反射的颜色。

创建一个光照场景

#shader vertex
#version 330 corelayout(location = 0) in vec3 aPos;uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;void main()
{gl_Position = projection * view * model * vec4(aPos, 1.0);
};#shader fragment
#version 330 coreout vec4 FragColor;uniform vec3 objectColor;
uniform vec3 lightColor;void main()
{FragColor = vec4(lightColor * objectColor, 1.0);
};

很简单,只需要计算一个光照

摄像机从正方体正中心看过去显示一个正方形,我还以为写错了,查了半天错,头都秃了。。。。

#include "TestLightColor.h"#include "Render.h"
#include "imgui/imgui.h"#include <glm/ext/matrix_clip_space.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <GLFW/glfw3.h>namespace test {void TestLightColor_scroll_callback(GLFWwindow* window, double xoffset, double yoffset);void TestLightColor_mouse_callback(GLFWwindow* window, double xposIn, double yposIn);TestLightColor::TestLightColor() : camera(glm::vec3(0.0f,0.0f,6.0f)) {float vertices[] = {-0.5f, -0.5f, -0.5f,0.5f, -0.5f, -0.5f,0.5f,  0.5f, -0.5f,0.5f,  0.5f, -0.5f,-0.5f,  0.5f, -0.5f,-0.5f, -0.5f, -0.5f,-0.5f, -0.5f,  0.5f,0.5f, -0.5f,  0.5f,0.5f,  0.5f,  0.5f,0.5f,  0.5f,  0.5f,-0.5f,  0.5f,  0.5f,-0.5f, -0.5f,  0.5f,-0.5f,  0.5f,  0.5f,-0.5f,  0.5f, -0.5f,-0.5f, -0.5f, -0.5f,-0.5f, -0.5f, -0.5f,-0.5f, -0.5f,  0.5f,-0.5f,  0.5f,  0.5f,0.5f,  0.5f,  0.5f,0.5f,  0.5f, -0.5f,0.5f, -0.5f, -0.5f,0.5f, -0.5f, -0.5f,0.5f, -0.5f,  0.5f,0.5f,  0.5f,  0.5f,-0.5f, -0.5f, -0.5f,0.5f, -0.5f, -0.5f,0.5f, -0.5f,  0.5f,0.5f, -0.5f,  0.5f,-0.5f, -0.5f,  0.5f,-0.5f, -0.5f, -0.5f,-0.5f,  0.5f, -0.5f,0.5f,  0.5f, -0.5f,0.5f,  0.5f,  0.5f,0.5f,  0.5f,  0.5f,-0.5f,  0.5f,  0.5f,-0.5f,  0.5f, -0.5f,};unsigned int indices[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35};GLCall(glEnable(GL_BLEND));GLCall(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));m_VAO = std::make_unique<VertexArray>();m_VertexBuffer = std::make_unique<VertexBuffer>(vertices, sizeof(vertices));VertexBufferLayout layout;layout.Push<float>(3);m_VAO->AddBuffer(*m_VertexBuffer, layout);m_IndexBuffer = std::make_unique<IndexBuffer>(indices, 36);m_ColorShader = std::make_unique<Shader>("res/shaders/LightColor.shader"); m_CubeShader = std::make_unique<Shader>("res/shaders/LightCube.shader");m_ColorShader->Bind();m_CubeShader->Bind();}TestLightColor::~TestLightColor() {}void TestLightColor::OnStart(GLFWwindow* window) {glEnable(GL_DEPTH_TEST);m_Window = window;glfwSetWindowUserPointer(m_Window, reinterpret_cast<void*>(this));glfwSetCursorPosCallback(m_Window, test::TestLightColor_mouse_callback);glfwSetScrollCallback(m_Window, test::TestLightColor_scroll_callback);glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);}void TestLightColor::OnUpdate(float delteTime) {}void TestLightColor::OnRender() {processInput(m_Window);float currentFrame = static_cast<float>(glfwGetTime());deltaTime = currentFrame - lastFrame;lastFrame = currentFrame;constexpr Render render;//processInput(m_Window);GLCall(glClearColor(0.0f, 0.0f, 0.0f, 1.0f));GLCall(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));GLCall(m_ColorShader->Bind());//GLCall(m_ColorShader->SetUniform3f("objectColor", 1.0f, 0.5f, 0.31f));//GLCall(m_ColorShader->SetUniform3f("lightColor", 1.0f, 1.0f, 1.0f));//glm::mat4 projection = glm::mat4(1.0f);glm::mat4  projection = glm::perspective(glm::radians(camera.Zoom),(float)test::SCR_WIDTH / (float)test::SCR_HEIGHT, 0.1f, 100.0f);glm::mat4 view = camera.GetViewMatrix();GLCall(m_ColorShader->SetUniformMat4f("projection", projection));GLCall(m_ColorShader->SetUniformMat4f("view", view));glm::mat4 model = glm::mat4(1.0f);GLCall(m_ColorShader->SetUniformMat4f("model", model));GLCall(m_VAO->Bind());GLCall(render.Draw(*m_VAO, *m_IndexBuffer, *m_ColorShader));GLCall(m_VAO->Bind());GLCall(m_CubeShader->Bind());GLCall(m_CubeShader->SetUniformMat4f("projection", projection));GLCall(m_CubeShader->SetUniformMat4f("view", view));model = glm::mat4(1.0f);model = glm::translate(model, lightPos);model = glm::scale(model, glm::vec3(2.0f));m_CubeShader->SetUniformMat4f("model", model);GLCall(render.Draw(*m_VAO, *m_IndexBuffer, *m_CubeShader));}void TestLightColor::OnImGuiRender() {}void TestLightColor::mouse_callback(GLFWwindow* window, double xposIn, double yposIn) {float xpos = static_cast<float>(xposIn);float ypos = static_cast<float>(yposIn);if (firstMouse){lastX = xpos;lastY = ypos;firstMouse = false;}float xoffset = xpos - lastX;float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to toplastX = xpos;lastY = ypos;camera.ProcessMouseMovement(xoffset, yoffset);}void TestLightColor::scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {camera.ProcessMouseScroll(static_cast<float>(yoffset));}void TestLightColor_mouse_callback(GLFWwindow* window, double xposIn, double yposIn) {TestLightColor* light = reinterpret_cast<TestLightColor*>(glfwGetWindowUserPointer(window));light->mouse_callback(window, xposIn, yposIn);}void TestLightColor_scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {TestLightColor* light = reinterpret_cast<TestLightColor*>(glfwGetWindowUserPointer(window));light->mouse_callback(window, xoffset, yoffset);}}

 基础光照

  • 环境光照(Ambient Lighting):即使在黑暗的情况下,世界上通常也仍然有一些光亮(月亮、远处的光),所以物体几乎永远不会是完全黑暗的。为了模拟这个,我们会使用一个环境光照常量,它永远会给物体一些颜色。
  • 漫反射光照(Diffuse Lighting):模拟光源对物体的方向性影响(Directional Impact)。它是风氏光照模型中视觉上最显著的分量。物体的某一部分越是正对着光源,它就会越亮。
  • 镜面光照(Specular Lighting):模拟有光泽物体上面出现的亮点。镜面光照的颜色相比于物体的颜色会更倾向于光的颜色。

环境光照

void main()
{float ambientStrength = 0.1;vec3 ambient = ambientStrength * lightColor;vec3 result = ambient * objectColor;FragColor = vec4(result, 1.0);
}

漫反射光照

为了(只)得到两个向量夹角的余弦值,我们使用的是单位向量(长度为1的向量),所以我们需要确保所有的向量都是标准化的,否则点乘返回的就不仅仅是余弦值了

法向量

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
...

修改橙色方块的着色器:(增加了漫反射)

#shader vertex
#version 330 corelayout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;out vec3 Normal;
out vec3 FragPos;uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;void main()
{FragPos = vec3(model * vec4(aPos, 1.0));Normal = aNormal;gl_Position = projection * view * model * vec4(aPos, 1.0);
};#shader fragment
#version 330 corein vec3 Normal;
in vec3 FragPos;out vec4 FragColor;uniform vec3 objectColor;
uniform vec3 lightColor;
uniform vec3 lightPos;void main()
{//ambientfloat ambientStrength = 0.1;vec3 ambient = ambientStrength * lightColor;//diffusevec3 norm = normalize(Normal);vec3 lightDir = normalize(lightPos - FragPos);float diff = max(dot(norm, lightDir), 0.0);vec3 diffuse = diff * lightColor;vec3 result = (ambient + diffuse)* objectColor;FragColor = vec4(result, 1.0);
};

在顶点着色器中,我们可以使用inversetranspose函数自己生成这个法线矩阵,这两个函数对所有类型矩阵都有效。注意我们还要把被处理过的矩阵强制转换为3×3矩阵,来保证它失去了位移属性以及能够乘以vec3的法向量。

Normal = mat3(transpose(inverse(model))) * aNormal;

 镜面光照

//specular float specularStrength = 0.5f;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;

顶点着色器中实现的风氏光照模型叫做Gouraud着色(Gouraud Shading),而不是风氏着色(Phong Shading)。记住,由于插值,这种光照看起来有点逊色。风氏着色能产生更平滑的光照效果。

材质

#version 330 core
struct Material {vec3 ambient;vec3 diffuse;vec3 specular;float shininess;
}; uniform Material material;

在片段着色器中,我们创建一个结构体(Struct)来储存物体的材质属性。我们也可以把它们储存为独立的uniform值,但是作为一个结构体来储存会更有条理一些

void main()
{    // 环境光vec3 ambient = lightColor * material.ambient;// 漫反射 vec3 norm = normalize(Normal);vec3 lightDir = normalize(lightPos - FragPos);float diff = max(dot(norm, lightDir), 0.0);vec3 diffuse = lightColor * (diff * material.diffuse);// 镜面光vec3 viewDir = normalize(viewPos - FragPos);vec3 reflectDir = reflect(-lightDir, norm);  float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);vec3 specular = lightColor * (spec * material.specular);  vec3 result = ambient + diffuse + specular;FragColor = vec4(result, 1.0);
}

 忘写分号。。。

光的属性

物体过亮的原因是环境光、漫反射和镜面光这三个颜色对任何一个光源都全力反射。

如果我们假设 lightcolor 是 1.0,那么代码会是这样的:

vec3 ambient  = vec3(1.0) * material.ambient;
vec3 diffuse  = vec3(1.0) * (diff * material.diffuse);
vec3 specular = vec3(1.0) * (spec * material.specular);

所以物体的每个材质属性对每一个光照分量都返回了最大的强度。

为光照属性创建类似材质结构体的东西:

struct Light {vec3 position;vec3 ambient;vec3 diffuse;vec3 specular;
};uniform Light light;

参考:颜色 - LearnOpenGL CN

基础光照 - LearnOpenGL CN


http://www.mrgr.cn/news/78830.html

相关文章:

  • 深度学习案例:ResNet50模型+SE-Net
  • Electron-vue 框架升级 Babel7 并支持electron-preload webapck 4 打包过程记录
  • Linux安装部署Redis教程
  • uniapp Electron打包生成桌面应用exe文件
  • 我们项目要升级到flutter架构的几点原因
  • CAD深度清理工具-AVappsDrawingPurge9.0.0(2024.8.27版本) 支持版本CAD2022-2025-供大家学习研究参考
  • go使用mysql实现增删改查操作
  • 我们来学mysql -- 事务之概念(原理篇)
  • 深度学习 | pytorch + torchvision + python 版本对应及环境安装
  • spring boot3.3.5 logback-spring.xml 配置
  • create-vue创建vue3项目
  • 【2024】使用Docker搭建redis sentinel哨兵模式集群全流程(包含部署、测试、错误点指正以及直接部署)
  • dpwwn02靶场
  • uniapp手机端一些坑记录
  • 基于go语言探讨 Kubernetes 中 Rollout History 的实现与优化
  • Java启动通用参数,自动记录GC等信息到专门日志文件中
  • python学习笔记9-零散知识点
  • 微服务即时通讯系统的实现(服务端)----(2)
  • 工具:Zotero Better BibTex插件和Latex基础知识
  • 【动手学电机驱动】STM32-FOC(9)无感 FOC 电机转速调节
  • openjdk17 jvm堆空间分配
  • Qt 面试题学习11_2024-11-29
  • leetcode 之二分查找(Java实现)(1)
  • redis.conf
  • MySQL主从复制
  • ELK Fleet JAVA LOG收集与展示教程