首页 > 其他分享 >Opengl-ShadowMapping(directional light)

Opengl-ShadowMapping(directional light)

时间:2023-03-07 16:45:40浏览次数:35  
标签:GL 1.0 directional fragment light depth shadow ShadowMapping

This is mainly about directional ligt. we can use depth map to find out which fragment should be shadow.
The normal way is like follows:

  1. Use kowledge about framebuffer to create a depth texture(in light perspective).
    When depth testing is enabled, OpenGL tests the depth value of a fragment against the content of the depth buffer. OpenGL performs a depth test and if this test passes, the fragment is rendered and the depth buffer is updated with the new depth value(In this way the depth buffer always contains the closest depth). If the depth test fails, the fragment is discarded.
  2. In vertex shader, transform vertex into light perspective. and rasterizer will automatically interpolate and have this light perspective postion for each fragment in fragment shader.(rasterizer 会对每个fragment进行插值,然后找到他们对应的在light perspective下的位置。用英语解释起来有点别扭)
  3. Use depth buffer texture's data to determine fragment that needs shadow effet(chect whether this fragment is the closest depth or not). And update those fragments in fragmetn shader.

Optimization

  1. Shadow acne
    Use bias in shadow calculation
float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005); 
float shadow = currentDepth - bias > closestDepth  ? 1.0 : 0.0;  

2.Peter panning
use face culling

glCullFace(GL_FRONT);
RenderSceneToDepthMap();
glCullFace(GL_BACK); 

3.Over sampling
some position not in the light perspective will be shadow. but it should lit in reality. use following way to lit those position

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
float borderColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);  
float ShadowCalculation(vec4 fragPosLightSpace)
{
    [...]
    if(projCoords.z > 1.0)
        shadow = 0.0;
    
    return shadow;
}  

4.PCF(percentage-closer filtering)
jagged blocky edges(可以理解为shadow锯齿).解决方法就是多次采样,取平均值。下面的只是一个例子

float shadow = 0.0;
vec2 texelSize = 1.0 / textureSize(shadowMap, 0);
for(int x = -1; x <= 1; ++x)
{
   for(int y = -1; y <= 1; ++y)
   {
       float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r; 
       shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0;        
   }    
}
shadow /= 9.0;

参考
https://learnopengl.com/Advanced-Lighting/Shadows/Shadow-Mapping

标签:GL,1.0,directional,fragment,light,depth,shadow,ShadowMapping
From: https://www.cnblogs.com/zhaobangliu/p/17188555.html

相关文章

  • LightDB ltjdbc驱动使用
    LightDB数据库既支持使用pg原生jdbc驱动,也有对应配套的ltjdbc驱动(修复了原生jdbc的若干缺陷,同时有功能增强)。以下为客户端DBeaver使用ltjdbc的使用方法。1.打开D......
  • LightDB数据库分布式部署实践
    当今做大型数据库应用的时候,随着业务越做越大,数据量也会越来越大,计算也会越来越复杂。对性能,可靠性,可扩展性的需求越来越强烈,集中式数据库显然已经满足不了需求。......
  • LightDB 日志审计功能介绍
    日志审计(ltaudit)ltaudit的目标是为LightDB用户提供生成审计日志的能力,这些日志通常需要符合政府、金融或ISO认证。其可通过标准的LightDB日志记录工具提供详细的会......
  • XGBoost和LightGBM时间序列预测对比
    GBoost和LightGBM都是目前非常流行的基于决策树的机器学习模型,它们都有着高效的性能表现,但是在某些情况下,它们也有着不同的特点。XGBoost和LightGBM简单对比训练速度Li......
  • LightDB分布式环境扩容DN节点
    1.使用LightDB安装包(http://www.light-pg.com/downloadList.html?key=lightDB_X)在服务器上安装单机版并创建实例(根据安装包提示进行安装即可,此处不再赘述,具体可参考安装手......
  • 基于Opendaylight的SFC部署及实验
    ps使用命令行下载慢的化可以先下载到本地,之后手动安装安装双系统【Ubuntu安装详细教程】https://www.bilibili.com/video/BV1CG4y1h7bx/?share_source=copy_web&vd_s......
  • WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示
    为方便描述,这里仅以正方形来做演示,其他图形从略。运行时效果图:XAML代码://Transform.XAML<CanvasWidth="700"Height="700"xmlns="​​http://schemas.microsoft.com/......
  • WPF,SilverLight中直线的样式示例
    XAML代码://LineStyle.xaml<ViewboxWidth="600"Height="500"xmlns="​​​http://schemas.microsoft.com/winfx/2006/xaml/presentation​​​"xmlns:x="​​​http:......
  • 【笔记】Spherical Harmonic Lighting 球谐光照再探
    其实就是想找一个demo,从一个图片(天空盒)里整出来球谐光照,但好像没找到短小的demo自己瞎摸索着写了一个(采样那里偷懒了,直接在cubebox上做的,这样似乎有偏,理论上应该是球上采......
  • Lightroom Classic for mac(Lrc2021) v10.4 激活版
    Lightroom Classic2021mac中文版是一款以后期制作为重点的图形工具软件,其增强的校正工具、强大的组织功能以及灵活的打印选项可以帮助您加快图片后期处理速度,将更多的时......