漫反射贴图

完整代码:https://github.com/DXT00/LearnOpenGL_study/tree/a9082da982458111eb505a22e92bcb4146bd8ee7

在着色器中使用漫反射贴图的方法和纹理教程中是完全一样的。但这次我们会将纹理储存为Material结构体中的一个sampler2D。我们将之前定义的vec3漫反射颜色向量替换为漫反射贴图.

struct Material{vec3 ambient;sampler2D diffuse;vec3 specular;float shininess;};

fragment shader:

#type fragment
#version 330 corestruct Material{vec3 ambient;sampler2D diffuse;vec3 specular;float shininess;};out vec4 FragColor;float ambientStrength = 0.1f;
float specularStrength = 1.0f;
//uniform float u_shininessStrength;//反光度因子,可以是2,4,8,16,..256,一个物体的反光度越高,反射光的能力越强,散射得越少,高光点就会越小in vec2 v_TexCoord;
in vec3 v_Normal;
in vec3 v_FragPos;      uniform Material u_Material;
uniform sampler2D u_Texture1;
uniform sampler2D u_Texture2;
uniform float u_MixValue;uniform vec3 u_LightColor;
uniform vec3 u_LightPos;
uniform vec3 u_CameraViewPos;//ambient
vec3 ambient = u_LightColor * u_Material.ambient* texture(u_Material.diffuse,v_TexCoord).rgb;//u_LightColor * u_Material.ambient//diffuse
vec3 lightDir = normalize(u_LightPos-v_FragPos);
vec3 norm = normalize(v_Normal);
float diff = max(dot(lightDir,norm),0.0f);
vec3 diffuse = u_LightColor * diff * texture(u_Material.diffuse,v_TexCoord).rgb;// u_Material.diffuse);u_LightColor//specular
vec3 reflectDir = normalize(reflect(-lightDir,norm));
vec3 viewDir = normalize(u_CameraViewPos-v_FragPos);
float spec = pow(max(dot(reflectDir,viewDir),0.0),u_Material.shininess);
vec3 specular =  u_LightColor * spec  * u_Material.specular;void main(){FragColor = vec4((diffuse + ambient + specular),1.0);// * mix(texture(u_Texture1, v_TexCoord), texture(u_Texture2, vec2(1.0 - v_TexCoord.x, v_TexCoord.y)), u_MixValue);
}

在Application.cpp中添加一个 DiffuseMap的纹理:

m_DiffuseMap.reset(new Texture("assets/texture/container2.png"));

并设置Uniform

由于之前已经有两个纹理了,所以DiffuseMap绑定为2号纹理:

m_Shader->SetUniform1i("u_Material.diffuse", 2);

renderer loop:

//render loopwhile (!glfwWindowShouldClose(m_Window->GetNativeWindow())) {float time = (float)glfwGetTime();Timestep ts = time - m_LastFrameTime;m_LastFrameTime = time;InputCheck(ts);if (glfwGetKey(m_Window->GetNativeWindow(), GLFW_KEY_ESCAPE) == GLFW_PRESS)glfwSetWindowShouldClose(m_Window->GetNativeWindow(), true);// render// ------//glClearColor(0.2f, 0.3f, 0.3f, 1.0f);glClearColor(0.1f, 0.1f, 0.1f, 0.1f);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// bind textures on corresponding texture unitsglActiveTexture(GL_TEXTURE0);m_Texture1->Bind();glActiveTexture(GL_TEXTURE1);m_Texture2->Bind();glActiveTexture(GL_TEXTURE2);m_DiffuseMap->Bind();Renderer::BeginScene(*m_Camera);glm::mat4 model = glm::mat4(1.0f);model = glm::translate(model, m_LightSource->GetPosition());model = glm::scale(model, glm::vec3(0.5f));Renderer::Submit(m_LightSource->GetVertexArray(), m_LightSource->GetShader(), model);glDrawArrays(GL_TRIANGLES, 0, 36);for (unsigned int i = 0; i < 10; i++){glm::mat4 model = glm::mat4(1.0f);model = glm::translate(model, cubePositions[i]);model = glm::rotate(model, (float)(glfwGetTime()), glm::vec3(1.0f, (float)i * 20, 0.0f));//(float)(glfwGetTime())m_Shader->SetUniformMat4f("u_TranInverseModel", glm::transpose(glm::inverse(model)));m_Shader->SetUniformVec3f("u_CameraViewPos", m_Camera->GetPosition());//glm::vec3 lightColor;// = m_LightSource->GetLightColor();//lightColor.x = sin(glfwGetTime() * 2.0f);//lightColor.y = sin(glfwGetTime() * 0.7f);//lightColor.z = sin(glfwGetTime() * 1.3f);m_Shader->SetUniformVec3f("u_LightColor", m_LightSource->GetLightColor());m_Shader->SetUniformVec3f("u_Material.ambient", glm::vec3(0.25,0.20725,0.20725));m_Shader->SetUniform1i("u_Material.diffuse", 2);//m_Shader->SetUniformVec3f("u_Material.diffuse", glm::vec3(1  ,0.829  ,0.829));m_Shader->SetUniformVec3f("u_Material.specular", glm::vec3(0.5,   0.5,0.5));m_Shader->SetUniform1f("u_Material.shininess",64.0f);Renderer::Submit(m_VertexArray, m_Shader, model);glDrawArrays(GL_TRIANGLES, 0, 36);}glfwSwapBuffers(m_Window->GetNativeWindow());glfwPollEvents();}

镜面光贴图:

你可能会注意到,镜面高光看起来有些奇怪,因为我们的物体大部分都是木头,我们知道木头不应该有这么强的镜面高光的。我们可以将物体的镜面光材质设置为vec3(0.0)来解决这个问题,但这也意味着箱子钢制的边框将不再能够显示镜面高光了,我们知道钢铁应该是有一些镜面高光的。所以,我们想要让物体的某些部分以不同的强度显示镜面高光。

镜面光贴图上的每个像素都可以由一个颜色向量来表示,比如说黑色代表颜色向量vec3(0.0),灰色代表颜色向量vec3(0.5)。在片段着色器中,我们接下来会取样对应的颜色值将它乘以光源的镜面强度。一个像素越「白」,乘积就会越大,物体的镜面光分量就会越亮

由于箱子大部分都由木头所组成,而且木头材质应该没有镜面高光,所以漫反射纹理的整个木头部分全部都转换成了黑色。箱子钢制边框的镜面光强度是有细微变化的,钢铁本身会比较容易受到镜面高光的影响,而裂缝则不会。

把u_Material 的 specular设置为贴图--》Sampler2D

struct Material{vec3 ambient;sampler2D diffuse;sampler2D specular;float shininess;};

Application 添加 SpecularMap

 m_Shader->SetUniform1i("u_Material.specular", 3);m_SpecularMap.reset(new Texture("assets/texture/container2_specular.png"));

放射光贴图

对物体的发光部分贴图

完整代码:https://github.com/DXT00/LearnOpenGL_study/tree/e0127d6525036402a0d32319ddbe2144d4226a3a

//ambient
vec3 ambient = u_LightColor * u_Material.ambient* texture(u_Material.diffuse,v_TexCoord).rgb;//u_LightColor * u_Material.ambient//diffuse
vec3 lightDir = normalize(u_LightPos-v_FragPos);
vec3 norm = normalize(v_Normal);
float diff = max(dot(lightDir,norm),0.0f);
vec3 diffuse = u_LightColor * diff * texture(u_Material.diffuse,v_TexCoord).rgb;// u_Material.diffuse);u_LightColor//specular
vec3 reflectDir = normalize(reflect(-lightDir,norm));
vec3 viewDir = normalize(u_CameraViewPos-v_FragPos);
float spec = pow(max(dot(reflectDir,viewDir),0.0),u_Material.shininess);
vec3 specular =  u_LightColor * spec  * texture(u_Material.specular,v_TexCoord).rgb;//emission
vec3 emission = texture(u_Material.emission,v_TexCoord).rgb;
void main(){FragColor = vec4((diffuse + ambient + specular + emission),1.0);// * mix(texture(u_Texture1, v_TexCoord), texture(u_Texture2, vec2(1.0 - v_TexCoord.x, v_TexCoord.y)), u_MixValue);
}
 m_EmissionMap.reset(new Texture("assets/texture/matrix.jpg"));m_Shader->SetUniform1i("u_Material.emission", 4);

LearnOpenGL_study -- 光照贴图相关推荐

  1. unity中使用自定义shader进行光照贴图烘培无法出现透明度的坑爹问题

    最近开发中在对场景进行光照贴图烘焙时发现一个坑爹问题,在使用自定义shader的时候,shader命名中必须包含Transparent路径,否则烘焙的时候不对alpha通道进行计算,烘焙出来都是狗皮膏 ...

  2. OpenGL Lightmap光照贴图的实例

    OpenGL Lightmap光照贴图 先上图,再解答. 完整主要的源代码 源代码剖析 先上图,再解答. 完整主要的源代码 #include <glad/glad.h> #include ...

  3. OpenGL 光照贴图Lighting maps

    OpenGL光照贴图Lighting maps 光照贴图Lighting maps简介 漫反射贴图 镜面光贴图 采样镜面光贴图 光照贴图Lighting maps简介 在上一节中,我们讨论了让每个物体 ...

  4. UE3 展开光照贴图的 UV

    展开光照贴图的 UV 有关创建光照贴图 示例 光照贴图坐标索引 相邻 UV 以及间隔 有关创建光照贴图 光照贴图可能是场景美术制作中最难的环节.我们依靠它们可以得到光照和阴影清晰可见的细节.它们必须是 ...

  5. 关于unity2019.3.11.f在烘焙光照贴图时闪退的问题

    在用unity2019版本的项目时一直有个问题困扰着我,就是勾选lighttingSetting中Auto Generate后,unity开始烘焙光照贴图时大概率立马闪退且没有任何报错信息. 为何说是 ...

  6. unity android光照贴图格式,Unity3D-光照贴图技术

    概念 Lightmapping光照贴图技术是一种增强静态场景光照效果的技术,其优点是可以通过较少的性能消耗使静态场景看上去更加真实,丰富,更加具有立体感:缺点是不能用来实时地处理动态光照.当游戏场景包 ...

  7. UE4-(光照)光照贴图

    光照贴图是引擎内部自动生成的一种纹理,可以应用到模型表面. 在世界场景设置中: 找到光照贴图分段,就可以看到光照贴图.由引擎生成的光照贴图纹理要比一般光照贴图稍微复杂点,引擎会为这张贴图创建两个不同的 ...

  8. 第二十二章 Unity 光照贴图

    光照贴图过程将预先计算场景中静态物体表面的亮度,并将结果存储在称为"光照贴图"的纹理中供以后使用.光照贴图可以包含直接光照和间接光照,以及阴影效果.但是,烘焙到光照贴图中的数据无法 ...

  9. 开发中–渐进式光照贴图器

    We've been hard at work on the Progressive Lightmapper since we first showed it at GDC in March. Ple ...

最新文章

  1. 用Visual C#来清空回收站(1)
  2. 构建高性能服务(三)Java高性能缓冲设计 vs Disruptor vs LinkedBlockingQueue--转载
  3. “开店办厂,去望城!” ——望城区市场主体总量突破十万户
  4. wxpython是什么_请问wxpython中 event传递的什么参数?
  5. 二十六、数据挖掘电力窃漏电用户自动识别
  6. 百度下拉词推广是什么?
  7. python求解LeetCode习题Find Peak Element in Given num_list
  8. HttpStatusCode 枚举
  9. 元宇宙火了!终于有人把虚拟现实(AR/VR/MR)讲明白了
  10. QT与游戏手柄测试(数据与UI相连,ui界面作出反应)
  11. 常见笔顺错误的字_容易出错的汉字|汉字中哪些字笔顺容易错
  12. 无监督学习之稀疏编码,自编码
  13. Mac 给新人的入手指南
  14. python 地震数据可视化
  15. 如何做好客户需求分析
  16. maven依赖名词解释
  17. C# 给Word每一页设置不同图片水印
  18. 探讨!自媒体的推荐机制提高百家号阅读收益方法!
  19. 从go语言中找和*区别
  20. 电磁场与微波技术专业考研必读

热门文章

  1. 对MYSQL数据库进行数据恢复
  2. python实现Excel可视化柱状图
  3. python 打卡记录代码_python实现自动打卡的示例代码
  4. python自学到大牛_如何才能自学成为 Python 大牛?
  5. 微弱直流电压/电流信号的采样电路 --滤波跟随放大
  6. linux 监控网络出口流量,Linux下网络流量监控
  7. [数值计算-5]:一元二次非线性方程求解 - 解析法直接求解
  8. Vue.js+Mysq+java+springboot+商品推荐算法实现商品推荐网站+商品管理系统后台
  9. 前端网页预览word,pdf,excel等各类文档
  10. Ubuntu20.04或18.04下PX4(pixhawk)源码编译环境配置教程,及构建代码各种错误解决办法