如何检测阴影贴图纹理GLSL的边缘

时间:2018-04-01 07:12:24

标签: glsl fragment-shader edge-detection texture2d shadow-mapping

我想知道如何在片段着色器中检测阴影贴图纹理的边缘。

我在youtube上看了下面的教程: https://www.youtube.com/watch?v=9sEHkT7N7RM 但在那个人的情况下,解决方案取决于相机的位置。我宁愿不依赖相机的位置。

问题: 如果对象移出阴影贴图渲染过程采样的区域,则会剪切对象的阴影(正常行为)。 所以,如果我知道当前片段的位置接近阴影贴图边框,我可以添加某种淡出机制。

提前干杯谢谢!

1 个答案:

答案 0 :(得分:0)

好吧,我实在无法相信,但我自己解决了这个问题,这很简单:

在我的顶点着色器中,我从灯光的角度计算顶点位置,然后计算它与纹理边缘的接近程度:

termcapinfo xterm* ti@:te@

现在,vShadowBorderFactor存储一个介于0和1之间的值。

我将vShadowBorderFactor传递给片段着色器,然后计算变暗量(通过投射阴影):

 #include <stdio.h>
   #include <stdlib.h>

     void main()
    {
        int storage, display_amount, item_amount, a, week;
        char storage_size[50], input_item[50];
        char item[50][100];
        float input_price;
        float price[50];

        printf("Storage Size:");
        printf("\n[1] Small Box (50cm x 50cm)-RM5 per week");
        printf("\n[2] Regular Box (100cm x 100cm)-RM15 per week");
        printf("\n[3] Large Box (200cm x 200cm)-RM25 per week");
        printf("\nPlease choose your storage: ");
        scanf("%i", &storage);

        if (storage == 1){
            strcpy(storage_size, "Small Box");
            display_amount = 5;
        }else if (storage == 2){
            strcpy(storage_size, "Regular Box");
            display_amount = 8;
        }else{
            strcpy(storage_size, "Large Box");
            display_amount = 12;
        }
        printf("\nYou have selected %s. How many items to display? (up to %i items): ", storage_size, display_amount);
        scanf("%i", &item_amount);



        for(int a = 0; a < item_amount; a++){
                printf("Item %i: ", a+1);
                scanf("%s", input_item);
                strcpy(item[a], input_item);

                printf("Price: RM ");
                scanf("%f", &input_price);
                price[a] = input_price;
            }

        printf("How many weeks to display the items?: ");
        scanf("%i", &week);

        printf("\nYou have entered %i item(s) to be displayed in your storage.", item_amount);

        printf("\n---------------------------------------");
        printf("\nItem                       Price       ");
        printf("\n---------------------------------------");

        for(int a = 0; a < item_amount; a++){
                printf("\n%s", item[a]);
                printf("                        RM %.2f", price[a]);
        }

        printf("\n---------------------------------------");
    }

然后我将暗化应用于场景整体颜色矢量。瞧!