当前位置:网站首页>◰GL-阴影贴图核心步骤
◰GL-阴影贴图核心步骤
2022-04-23 16:39:00 【itzyjr】
第一轮:在光源视角渲染场景
1.创建自定义帧缓冲区
glGenFramebuffers(1, &shadowBuffer);
2.创建并绑定阴影纹理,设置阴影贴图及参数
glGenTextures(1, &shadowTex);
glBindTexture(GL_TEXTURE_2D, shadowTex);
// specify a two-dimensional texture image
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32,
screenW, screenH, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
// may reduce shadow border artifacts
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3.绑定帧缓冲区
glBindFramebuffer(GL_FRAMEBUFFER, shadowBuffer);
4.将深度信息附加到帧缓冲区的阴影纹理附件上
如果纹理对象的图像被附加到帧缓冲区,OpenGL将执行“渲染到纹理”,即深度信息都在阴影纹理中了,这样就可用纹理中的深度信息来判断点是否在阴影里。
// attach a level of a texture object as a logical buffer of a framebuffer object
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, shadowTex, 0);
5.禁用颜色缓冲(即禁用颜色输出)
// GL_NONE:No color buffers are written
glDrawBuffer(GL_NONE);
6.启用深度测试
glEnable(GL_DEPTH_TEST);
7.在光照空间绘制两物体(一个遮挡另一个)
glDrawArrays(..one.);
glDrawArrays(..two.);
8.顶点着色器Ⅰ:正常输出经光照空间MVP矩阵变换后的坐标。
片段着色器Ⅰ:什么也不做。
因为深度测试是在片段着色器之后进行,Alpha测试->模板测试->深度测试。所以必须正常输出经MVP矩阵变换后的坐标。因为不需要上色,所以片段着色器1什么也不用做。
// 顶点着色器Ⅰ
gl_Position = shadowMVP * vec4(vertPos, 1.0);
// 片段着色器Ⅰ
void main(void) {
}
第二轮:在相机视角渲染场景
1.中断帧缓冲
由于我们的帧缓冲不是默认帧缓冲,渲染指令将不会对窗口的视觉输出有任何影响。出于这个原因,渲染到一个不同的帧缓冲被叫做离屏渲染(Off-screen Rendering)。要保证所有的渲染操作在主窗口中有视觉效果,我们需要再次激活[默认帧缓冲],将它绑定到0。
// 切换回默认帧缓冲
glBindFramebuffer(GL_FRAMEBUFFER, 0);
2.激活#0纹理单元并绑定阴影纹理
此时就将带有深度信息的纹理复制到阴影纹理单元了。
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, shadowTex);
3.启用颜色缓冲区(即启用颜色输出)
// GL_FRONT:Only the front left and front right color buffers are written
glDrawBuffer(GL_FRONT);
4.构建两个MVP矩阵作为统一变量:一个是相机空间MVP矩阵,一个光照空间BMVP矩阵。
5.在相机空间绘制两物体(一个遮挡另一个)
6.顶点着色器Ⅱ:
[out] shadow_coord = shadowBMVP * vec4(vertPos, 1.0);
[out] gl_Position = proj_matrix * mv_matrix * vec4(vertPos, 1.0);
片段着色器Ⅱ:
in vec4 shadow_coord;
layout(binding = 0) uniform sampler2DShadow shadowTex;
main() {
// 0:在阴影里,1:不在阴影里
float inShadow = textureProj(shadowTex, shadow_coord);
}
版权声明
本文为[itzyjr]所创,转载请带上原文链接,感谢
https://blog.csdn.net/itzyjr/article/details/124341723
边栏推荐
- 昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
- LVM与磁盘配额
- MySQL master-slave synchronization pit avoidance version tutorial
- Execution plan calculation for different time types
- Using JSON server to create server requests locally
- Gartner predicts that the scale of cloud migration will increase significantly; What are the advantages of cloud migration?
- Hyperbdr cloud disaster recovery v3 Version 2.1 release supports more cloud platforms and adds monitoring and alarm functions
- Hypermotion cloud migration helped China Unicom. Qingyun completed the cloud project of a central enterprise and accelerated the cloud process of the group's core business system
- 各大框架都在使用的Unsafe类,到底有多神奇?
- Install MySQL on MAC
猜你喜欢

The first line and the last two lines are frozen when paging

MySql主从复制

NVIDIA显卡驱动报错

G008-HWY-CC-ESTOR-04 华为 Dorado V6 存储仿真器配置

RecyclerView advanced use - to realize drag and drop function of imitation Alipay menu edit page

The font of the soft cell changes color
![[pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return](/img/f5/15b3731e75eb4d861bd9d29ae244da.png)
[pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return

Countdown 1 day ~ 2022 online conference of cloud disaster tolerance products is about to begin

LVM与磁盘配额

New project of OMNeT learning
随机推荐
JSP learning 3
Use case execution of robot framework
Detailed explanation of UWA pipeline function | visual configuration automatic test
Oak-d raspberry pie cloud project [with detailed code]
UWA Pipeline 功能详解|可视化配置自动测试
What is the experience of using prophet, an open source research tool?
What is cloud migration? The four modes of cloud migration are?
基于GPU实例的Nanopore数据预处理
About background image gradient()!
Custom implementation of Baidu image recognition (instead of aipocr)
Day 9 static abstract class interface
Gartner announces emerging technology research: insight into the meta universe
Flask如何在内存中缓存数据?
MySQL personal learning summary
PyTorch:train模式与eval模式的那些坑
详解牛客----手套
Using JSON server to create server requests locally
Questions about disaster recovery? Click here
Qipengyuan horizon credible meta universe social system meets diversified consumption and social needs
Findstr is not an internal or external command workaround