当前位置:网站首页>◰ GL shadow map core steps
◰ GL shadow map core steps
2022-04-23 16:41:00 【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]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231639280838.html
边栏推荐
- vscode如何比较两个文件的异同
- 【Pygame小游戏】10年前风靡全球的手游《愤怒的小鸟》,是如何霸榜的?经典回归......
- Download and install mongodb
- How to upgrade openstack across versions
- File upload and download of robot framework
- vim编辑器的实时操作
- 详解牛客----手套
- Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
- Kunteng full duplex digital wireless transceiver chip kt1605 / kt1606 / kt1607 / kt1608 is suitable for interphone scheme
- 批量制造测试数据的思路,附源码
猜你喜欢
MySQL master-slave synchronization pit avoidance version tutorial
Modify the test case name generated by DDT
Postman batch production body information (realize batch modification of data)
众昂矿业:萤石浮选工艺
JIRA screenshot
Solution of garbled code on idea console
Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot
PyMySQL
Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
随机推荐
深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
G008-HWY-CC-ESTOR-04 华为 Dorado V6 存储仿真器配置
Sail soft calls the method of dynamic parameter transfer and sets parameters in the title
Gartner announces emerging technology research: insight into the meta universe
True math problems in 1959 college entrance examination
【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
Custom implementation of Baidu image recognition (instead of aipocr)
File upload and download of robot framework
PHP 零基础入门笔记(13):数组相关函数
Dlib of face recognition framework
Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
安装及管理程序
Modify the test case name generated by DDT
Pytorch: the pit between train mode and eval mode
On the security of key passing and digital signature
最详细的背包问题!!!
∑GL-透视投影矩阵的推导
Sail soft segmentation solution: take only one character (required field) of a string
Detailed explanation of file operation (2)
◰GL-阴影贴图核心步骤