当前位置:网站首页>◰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
边栏推荐
- Hyperbdr cloud disaster recovery v3 Version 2.1 release supports more cloud platforms and adds monitoring and alarm functions
- Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
- Detailed explanation of file operation (2)
- Set the color change of interlaced lines in cells in the sail software and the font becomes larger and red when the number is greater than 100
- 力扣-746.使用最小花费爬楼梯
- MySQL master-slave synchronization pit avoidance version tutorial
- JMeter installation tutorial and solutions to the problems I encountered
- Using JSON server to create server requests locally
- 昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
- 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
猜你喜欢
【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
Set cell filling and ranking method according to the size of the value in the soft report
English | day15, 16 x sentence true research daily sentence (clause disconnection, modification)
File upload and download of robot framework
Install MySQL on MAC
Gartner announces emerging technology research: insight into the meta universe
Gartner 發布新興技術研究:深入洞悉元宇宙
Sort by character occurrence frequency 451
LVM and disk quota
Use case execution of robot framework
随机推荐
Using JSON server to create server requests locally
Pytorch: the pit between train mode and eval mode
Best practice of cloud migration in education industry: Haiyun Jiexun uses hypermotion cloud migration products to implement progressive migration for a university in Beijing, with a success rate of 1
1959年高考数学真题
The most detailed knapsack problem!!!
Findstr is not an internal or external command workaround
Passing header request header information between services through feign
Oracle data pump usage
G008-hwy-cc-estor-04 Huawei Dorado V6 storage simulator configuration
Xinwangda: HEV and Bev super fast charging fist products are shipped on a large scale
Questions about disaster recovery? Click here
The most detailed Backpack issues!!!
JSP learning 3
Custom implementation of Baidu image recognition (instead of aipocr)
Countdown 1 day ~ 2022 online conference of cloud disaster tolerance products is about to begin
Day (5) of picking up matlab
How to build tiktok user trust and drive fan growth
昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
力扣-746.使用最小花费爬楼梯
最詳細的背包問題!!!