当前位置:网站首页>◰ GL shadow map core steps
◰ GL shadow map core steps
2022-04-23 16:41:31 【itzyjr】
The first round : Render the scene from the light angle
1. Create a custom framebuffer
glGenFramebuffers(1, &shadowBuffer);
2. Create and bind shadow textures , Set shadow map and parameters
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. Bind frame buffer
glBindFramebuffer(GL_FRAMEBUFFER, shadowBuffer);
4. Attach the depth information to the shadow texture attachment of the frame buffer
If the image of the texture object is attached to the frame buffer ,OpenGL Will perform “ Render to texture ”, That is, the depth information is in the shadow texture , In this way, the depth information in the texture can be used to judge whether the point is in the shadow .
// attach a level of a texture object as a logical buffer of a framebuffer object
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, shadowTex, 0);
5. Disable color buffering ( That is, disable color output )
// GL_NONE:No color buffers are written
glDrawBuffer(GL_NONE);
6. Enable depth testing
glEnable(GL_DEPTH_TEST);
7. Draw two objects in light space ( One obscures the other )
glDrawArrays(..one.);
glDrawArrays(..two.);
8. Vertex shader Ⅰ: The normal output passes through the illumination space MVP Coordinates after matrix transformation .
Fragment Shader Ⅰ: Do nothing .
Because the depth test is performed after the fragment shader ,Alpha test -> Template testing -> Depth testing . Therefore, it must be output normally MVP Coordinates after matrix transformation . Because you don't need to paint , So fragment shaders 1 Nothing to do .
// Vertex shader Ⅰ
gl_Position = shadowMVP * vec4(vertPos, 1.0);
// Fragment Shader Ⅰ
void main(void) {
}
The second round : Render the scene from the camera's perspective
1. Interrupt frame buffer
Since our frame buffer is not the default frame buffer , Rendering instructions will have no effect on the visual output of the window . For this reason , Rendering to a different frame buffer is called off screen rendering (Off-screen Rendering). Ensure that all rendering operations have visual effects in the main window , We need to activate again [ Default frame buffer ], Bind it to 0.
// Switch back to the default frame buffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
2. Activate #0 Texture cells and bind shadow textures
At this point, the texture with depth information is copied to the shadow texture unit .
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, shadowTex);
3. Enable color buffer ( Color output is enabled )
// GL_FRONT:Only the front left and front right color buffers are written
glDrawBuffer(GL_FRONT);
4. Build two MVP Matrix as a unified variable : One is camera space MVP matrix , A light space BMVP matrix .
5. Draw two objects in camera space ( One obscures the other )
6. Vertex shader Ⅱ:
[out] shadow_coord = shadowBMVP * vec4(vertPos, 1.0);
[out] gl_Position = proj_matrix * mv_matrix * vec4(vertPos, 1.0);
Fragment Shader Ⅱ:
in vec4 shadow_coord;
layout(binding = 0) uniform sampler2DShadow shadowTex;
main() {
// 0: In the shadow ,1: Not in the shadow
float inShadow = textureProj(shadowTex, shadow_coord);
}
版权声明
本文为[itzyjr]所创,转载请带上原文链接,感谢
/html/epMxfn.html
边栏推荐
- 如何建立 TikTok用户信任并拉动粉丝增长
- 无线鹅颈麦主播麦手持麦无线麦克风方案应当如何选择
- loggie 源码分析 source file 模块主干分析
- 关于局域网如何组建介绍
- PyTorch:train模式与eval模式的那些坑
- Dlib of face recognition framework
- NVIDIA graphics card driver error
- Installation and management procedures
- 5-minute NLP: text to text transfer transformer (T5) unified text to text task model
- Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
猜你喜欢
-
MySQL master-slave synchronization pit avoidance version tutorial
-
Construction of promtail + Loki + grafana log monitoring system
-
Public variables of robotframework
-
File upload and download of robot framework
-
Selenium IDE and XPath installation of chrome plug-in
-
Project framework of robot framework
-
Use case execution of robot framework
-
Use case labeling mechanism of robot framework
-
Deepinv20 installation MariaDB
-
Pycham connects to the remote server and realizes remote debugging
随机推荐
- DDT + Excel for interface test
- Pytorch: the pit between train mode and eval mode
- Introduction to how to set up LAN
- Loggie source code analysis source file module backbone analysis
- How to choose the wireless gooseneck anchor microphone and handheld microphone scheme
- How to build tiktok user trust and drive fan growth
- 【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
- Kunteng full duplex digital wireless transceiver chip kt1605 / kt1606 / kt1607 / kt1608 is suitable for interphone scheme
- English | day15, 16 x sentence true research daily sentence (clause disconnection, modification)
- Knowledge points and examples of [seven input / output systems]
- 详解牛客----手套
- How does flash cache data in memory?
- 批量制造测试数据的思路,附源码
- 聊一聊浏览器缓存控制
- 深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
- 博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
- ACL 2022 | DialogVED:用于对话回复生成的预训练隐变量编码-解码模型
- True math problems in 1959 college entrance examination
- MySQL master-slave replication
- ByteVCharts可视化图表库,你想要的我都有
- ◰GL-着色器处理程序封装
- ◰GL-阴影贴图核心步骤
- ∑GL-透视投影矩阵的推导
- Pseudo Distributed installation spark
- Zhongang Mining: Fluorite Flotation Process
- Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
- Modify the test case name generated by DDT
- 面试百分百问到的进程,你究竟了解多少
- Encapsulating the logging module
- Camtasia2022软件新增功能介绍
- PyMySQL
- Mock test
- Mock test using postman
- 正则过滤内网地址和网段
- 信息摘要、数字签名、数字证书、对称加密与非对称加密详解
- 计算饼状图百分比
- 扫码登录的原理你真的了解吗?
- vscode如何比较两个文件的异同
- Do you really understand the principle of code scanning login?
- Calculate pie chart percentage