OpenGL GLSL阴影无法正常工作

时间:2015-08-31 10:42:42

标签: opengl mapping glsl shader shadow

我正在尝试使用GLSL在Java / OpenGL中实现阴影贴图。 用Java / OpenGL创建阴影贴图似乎是不可能的,透视投影几乎没有工作实例。

我认为矩阵计算效果不佳。

这是我的影子结果(相机视图/ proj =影子视图/ proj): enter image description here

在这里,我已经将线性化的深度缓冲区映射到一个矩形上,它有点旋转: enter image description here

似乎深度缓冲区被翻转,因为在我映射它的每个表面上,它都是x或/和y翻转。但也许它只是一个紫外线虫。

所以主要问题是:你能否暗示我可能会发生什么?

以下是一些代码段:

最终着色器:深度&阴影计算(uSamplerShadow是sampler2D)

float shadowValue=0.0;
vec4 lightVertexPosition2=vShadowCoord;
lightVertexPosition2/=lightVertexPosition2.w;
for(float x=-0.001;x<=0.001;x+=0.0005)
    for(float y=-0.001;y<=0.001;y+=0.0005)
    {
        if(texture2D(uSamplerShadow,lightVertexPosition2.xy+vec2(x,y)).r>=lightVertexPosition2.z)
            shadowValue+=1.0;
    }
shadowValue/=16.0;

float f = 100.0;
float n = 0.1;
float z = (2 * n) / (f + n - texture2D(uSamplerShadow,vTexCoords).x * (f - n));

outColor = vec4(vec3(z) , 1.0);

最终着色器:阴影坐标计算:(尚未实施偏置矩阵)

vShadowCoord = uProjectionMatrix * uShadowViewMatrix * uWorldMatrix * vec4(aPosition,1.0);

深度着色器

fragmentdepth = gl_FragCoord.z;

您也可以检查我的纹理属性,但我已经尝试过在google上找到的所有组合:)

            shadowTextureProperties.setMagFilter(EnumTextureFilter.NEAREST);
    shadowTextureProperties.setMinFilter(EnumTextureFilter.NEAREST);
    shadowTextureProperties.setWrapS(EnumTextureWrap.CLAMP_TO_EDGE);
    shadowTextureProperties.setWrapT(EnumTextureWrap.CLAMP_TO_EDGE);
    shadowTextureProperties.setInternalColorFormat(EnumTextureColorFormat.DEPTH_COMPONENT16);
    shadowTextureProperties.setSrcColorFormat(EnumTextureColorFormat.DEPTH_COMPONENT);
    shadowTextureProperties.setValueFormat(EnumValueFormat.FLOAT);
    shadowTextureProperties.setPname(new int[]{GL14.GL_TEXTURE_COMPARE_MODE, GL14.GL_TEXTURE_COMPARE_FUNC});
    shadowTextureProperties.setParam(new int[]{GL11.GL_NONE, GL11.GL_LEQUAL});

1 个答案:

答案 0 :(得分:0)

第一件事:

setTimeout(function() {
           toastr.options = {
              "closeButton": true,
              "debug": false,
              "progressBar": true,
              "positionClass": "toast-top-right",
              "onclick": null,
              "showDuration": "500",
              "hideDuration": "1000",
              "timeOut": "5000",
              "extendedTimeOut": "1000",
              "showEasing": "linear",
              "hideEasing": "linear",
              "showMethod": "fadeIn",
              "hideMethod": "fadeOut"
            };
            toastr.success("Welcome Admin", "Smartcomp Solutions Inc.");
        },1000);

shadowTextureProperties.setPname(new int[]{GL14.GL_TEXTURE_COMPARE_MODE, GL14.GL_TEXTURE_COMPARE_FUNC}); 不是GL_TEXTURE_COMPARE_FUNC的有效参数。根据{{​​3}},只允许GL_TEXTURE_COMPARE_MODEGL_NONE

<强> GL_COMPARE_R_TO_TEXTURE

必须使用影子采样器(GL_COMPARE_R_TO_TEXTURE)和相应的sampler2DShadow重载:

texture

这里,在位置P.xy处对采样器进行采样,并将读取值与P.z.进行比较。此操作的结果是闪电因子(完全阴影时为0.0,无阴影时为1.0)。

<强> GL_NONE

如果您想自己进行比较,则必须将float texture( sampler2DShadow sampler, vec3 P) 设置为GL_COMPARE_R_TO_TEXTURE