当前位置:网站首页>◰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
边栏推荐
- Sort by character occurrence frequency 451
- There is a problem with the light switch from 1 to 100
- How to upgrade openstack across versions
- Findstr is not an internal or external command workaround
- English | day15, 16 x sentence true research daily sentence (clause disconnection, modification)
- 英语 | Day15、16 x 句句真研每日一句(从句断开、修饰)
- Detailed explanation of UWA pipeline function | visual configuration automatic test
- Introduction notes to PHP zero Foundation (13): array related functions
- On the security of key passing and digital signature
- Public variables of robotframework
猜你喜欢

第九天 static 抽象类 接口

Sail soft calls the method of dynamic parameter transfer and sets parameters in the title

Set cell filling and ranking method according to the size of the value in the soft report

Installation and management procedures

DanceNN:字节自研千亿级规模文件元数据存储系统概述

Gartner 發布新興技術研究:深入洞悉元宇宙

Gartner 发布新兴技术研究:深入洞悉元宇宙

漫画:什么是IaaS、PaaS、SaaS?

Day (4) of picking up matlab

299. 猜数字游戏
随机推荐
299. 猜数字游戏
Summary according to classification in sail software
loggie 源码分析 source file 模块主干分析
力扣-198.打家劫舍
What is the experience of using prophet, an open source research tool?
Selenium IDE and XPath installation of chrome plug-in
Force buckle - 198 raid homes and plunder houses
04 Lua operator
Real time operation of vim editor
Server log analysis tool (identify, extract, merge, and count exception information)
There is a problem with the light switch from 1 to 100
第九天 static 抽象类 接口
Install MySQL on MAC
Cartoon: what are IAAs, PAAS, SaaS?
Win11 / 10 home edition disables the edge's private browsing function
About JMeter startup flash back
Xinwangda: HEV and Bev super fast charging fist products are shipped on a large scale
Set cell filling and ranking method according to the size of the value in the soft report
Force buckle-746 Climb stairs with minimum cost
The font of the soft cell changes color