首页 > 解决方案 > 如何在片段着色器中实现折射光?

问题描述

我正在开发一个 OpenGL 光线追踪器,它能够加载 obj 文件并对其进行光线追踪。我的应用程序使用 assimp 加载 obj 文件,然后使用着色器存储对象将所有三角形面(以及原始坐标和 te 材质系数)发送到片段着色器。基本结构是将结果从片段着色器渲染到四边形。

我在片段着色器中的光线追踪部分遇到了麻烦,但首先让我们介绍一下。对于漫射光,使用兰伯特余弦定律,对于镜面光使用 Phong-Blinn 模型。在全反射的情况下,使用一个weight变量来使反射光也对其他物体产生影响。权重是通过 Schlick 方法通过近似菲涅耳方程计算的。在下图中,您可以看到,飞机就像一面镜子,反射上方立方体的图像。

在此处输入图像描述

我想让立方体看起来像一个玻璃物体(如玻璃球),它也有折射和反射效果。或者至少折射光线。在上图中,您可以看到立方体上的折射效果,但效果并不理想。我搜索了如何实现它的示例,但直到现在,我认识到必须像在反射部分一样使用菲涅耳方程。

这是我的着色器的片段:

 vec3 Fresnel(vec3 F0, float cosTheta) {
    return F0 + (vec3(1, 1, 1) - F0) * pow(1-cosTheta, 5);
}

float schlickApprox(float Ni, float cosTheta){
    float F0=pow((1-Ni)/(1+Ni), 2);
    return F0 + (1 - F0) * pow((1 - cosTheta), 5);
}


vec3 trace(Ray ray){
    vec3 weight = vec3(1, 1, 1);
    const float epsilon = 0.0001f;
    vec3 outRadiance = vec3(0, 0, 0);
    int maxdepth=5;

    for (int i=0; i < maxdepth; i++){
        Hit hit=traverseBvhTree(ray);

        if (hit.t<0){ return weight * lights[0].La; }

        vec4 textColor = texture(texture1, vec2(hit.u, hit.v));
        Ray shadowRay;
        shadowRay.orig = hit.orig + hit.normal * epsilon;
        shadowRay.dir  = normalize(lights[0].direction);

        // Ambient Light
        outRadiance+= materials[hit.mat].Ka.xyz * lights[0].La*textColor.xyz * weight;


        // Diffuse light based on Lambert's cosine law
        float cosTheta = dot(hit.normal, normalize(lights[0].direction));
        if (cosTheta>0 && traverseBvhTree(shadowRay).t<0) {
            outRadiance +=lights[0].La * materials[hit.mat].Kd.xyz * cosTheta * weight;

            // Specular light based on Phong-Blinn model
            vec3 halfway = normalize(-ray.dir + lights[0].direction);
            float cosDelta = dot(hit.normal, halfway);

            if (cosDelta > 0){
                outRadiance +=weight * lights[0].Le * materials[hit.mat].Ks.xyz * pow(cosDelta, materials[hit.mat].shininess); }
        }

        float fresnel=schlickApprox(materials[hit.mat].Ni, cosTheta);

        // For refractive materials
        if (materials[hit.mat].Ni < 3)
        {

         /*this is the under contruction part.*/



            ray.orig = hit.orig - hit.normal*epsilon;
            ray.dir = refract(ray.dir, hit.normal, materials[hit.mat].Ni);
        }

        // If the refraction index is more than 15, treat the material as mirror.
        else if (materials[hit.mat].Ni >= 15) {
            weight *= fresnel;
            ray.orig=hit.orig+hit.normal*epsilon;
            ray.dir=reflect(ray.dir, hit.normal);
        }
    }
    return outRadiance;
}

更新 1

我更新了着色器中的跟踪方法。就我对光的物理理解而言,如果有一种材料会反射和折射光,我就得按照这个来处理两种情况。

  1. 在这种反射的情况下,我在漫反射光计算中添加了一个权重: weight *= fresnel
  2. 在折射光的情况下,重量为weight*=1-fresnel

此外,我计算了ray.origray.dir连接的情况,折射计算仅在不是全内反射的情况下发生(fresnel小于1)。

修改后的trace方法:

vec3 trace(Ray ray){
    vec3 weight = vec3(1, 1, 1);
    const float epsilon = 0.0001f;
    vec3 outRadiance = vec3(0, 0, 0);
    int maxdepth=3;

    for (int i=0; i < maxdepth; i++){
        Hit hit=traverseBvhTree(ray);

        if (hit.t<0){ return weight * lights[0].La; }

        vec4 textColor = texture(texture1, vec2(hit.u, hit.v));
        Ray shadowRay;
        shadowRay.orig = hit.orig + hit.normal * epsilon;
        shadowRay.dir  = normalize(lights[0].direction);

        // Ambient Light
        outRadiance+= materials[hit.mat].Ka.xyz * lights[0].La*textColor.xyz * weight;


        // Diffuse light based on Lambert's cosine law
        float cosTheta = dot(hit.normal, normalize(lights[0].direction));
        if (cosTheta>0 && traverseBvhTree(shadowRay).t<0) {
            outRadiance +=lights[0].La * materials[hit.mat].Kd.xyz * cosTheta * weight;

            // Specular light based on Phong-Blinn model
            vec3 halfway = normalize(-ray.dir + lights[0].direction);
            float cosDelta = dot(hit.normal, halfway);

            if (cosDelta > 0){
                outRadiance +=weight * lights[0].Le * materials[hit.mat].Ks.xyz * pow(cosDelta, materials[hit.mat].shininess); }
        }

        float fresnel=schlickApprox(materials[hit.mat].Ni, cosTheta);

        // For refractive/reflective  materials
        if (materials[hit.mat].Ni < 7)
        {
            bool outside = dot(ray.dir, hit.normal) < 0;

            // compute refraction if it is not a case of total internal reflection
            if (fresnel < 1) {

                ray.orig = outside ? hit.orig-hit.normal*epsilon : hit.orig+hit.normal*epsilon;
                ray.dir = refract(ray.dir, hit.normal,materials[hit.mat].Ni);
                weight *= 1-fresnel;
                continue;

            }
            // compute reflection
            ray.orig= outside ? hit.orig+hit.normal*epsilon : hit.orig-hit.normal*epsilon;
            ray.dir= reflect(ray.dir, hit.normal);
            weight *= fresnel;
            continue;

        }
        // If the refraction index is more than 15, treat the material as mirror: total reflection
        else if (materials[hit.mat].Ni >= 7) {
            weight *= fresnel;
            ray.orig=hit.orig+hit.normal*epsilon;
            ray.dir=reflect(ray.dir, hit.normal);
        }
    }
    return outRadiance;
}

这是连接到更新的快照。稍微好一点,我猜。

在此处输入图像描述


更新 2:

我发现了一个迭代算法,它使用堆栈来可视化 opengl 中的折射和反射光线:它在第 68 页

我根据这个修改了我的碎片着色器。几乎没问题,除了背面完全是黑色的。附上一些图片。在此处输入图像描述

这是我的碎片着色器的跟踪方法:

vec3 trace(Ray ray){
    vec3 color;
    float epsilon=0.001;
    Stack stack[8];// max depth
    int stackSize = 0;// current depth
    int bounceCount = 0;
    vec3 coeff = vec3(1, 1, 1);
    bool continueLoop = true;

    while (continueLoop){
        Hit hit = traverseBvhTree(ray);
        if (hit.t>0){

            bounceCount++;
            //----------------------------------------------------------------------------------------------------------------
            Ray shadowRay;
            shadowRay.orig = hit.orig + hit.normal * epsilon;
            shadowRay.dir  = normalize(lights[0].direction);

            color+= materials[hit.mat].Ka.xyz * lights[0].La * coeff;
            // Diffuse light
            float cosTheta = dot(hit.normal, normalize(lights[0].direction));// Lambert-féle cosinus törvény alapján.
            if (cosTheta>0 && traverseBvhTree(shadowRay).t<0) {
                color +=lights[0].La * materials[hit.mat].Kd.xyz * cosTheta * coeff;
                vec3 halfway = normalize(-ray.dir + lights[0].direction);
                float cosDelta = dot(hit.normal, halfway);

                // Specular light
                if (cosDelta > 0){
                    color +=coeff * lights[0].Le * materials[hit.mat].Ks.xyz * pow(cosDelta, materials[hit.mat].shininess); }
            }
            //---------------------------------------------------------------------------------------------------------------
              if (materials[hit.mat].indicator > 3.0 && bounceCount <=2){

                  float eta = 1.0/materials[hit.mat].Ni;
                  Ray refractedRay;
                  refractedRay.dir = dot(ray.dir, hit.normal) <= 0.0 ? refract(ray.dir, hit.normal, eta) : refract(ray.dir, -hit.normal, 1.0/eta);
                  bool totalInternalReflection = length(refractedRay.dir) < epsilon;
                  if(!totalInternalReflection){

                      refractedRay.orig = hit.orig + hit.normal*epsilon*sign(dot(ray.dir, hit.normal));
                      refractedRay.dir = normalize(refractedRay.dir);

                          stack[stackSize].coeff = coeff *(1 - schlickApprox(materials[hit.mat].Ni, dot(ray.dir, hit.normal)));
                          stack[stackSize].depth = bounceCount;
                          stack[stackSize++].ray = refractedRay;

                  }

                  else{

                      ray.dir = reflect(ray.dir, -hit.normal);
                      ray.orig = hit.orig - hit.normal*epsilon;
                  }
              }

            else if (materials[hit.mat].indicator == 0){
                coeff *= schlickApprox(materials[hit.mat].Ni, dot(-ray.dir, hit.normal));
                ray.orig=hit.orig+hit.normal*epsilon;
                ray.dir=reflect(ray.dir, hit.normal);
            }


            else { //Diffuse Material
                continueLoop=false;
            }
        }

        else {
            color+= coeff * lights[0].La;
            continueLoop=false;
        }

        if (!continueLoop && stackSize > 0){
            ray = stack[stackSize--].ray;
            bounceCount = stack[stackSize].depth;
            coeff = stack[stackSize].coeff;
            continueLoop = true;
        }
    }
    return color;
}

标签: openglglslfragment-shaderraytracingfresnel

解决方案


推荐阅读