插入GLSL光线跟踪器中的颜色

时间:2017-07-03 09:43:33

标签: colors glsl raytracing

我正在尝试在球体上的4个点之间插入颜色,我在片段着色器中进行了跟踪。我知道我必须给每条光线一个特定的颜色,我知道如何插入颜色。我的问题是我不知道如何为特定光线设置特定颜色。

下面:

img

是我场景的图像。补丁的角应该是红色(左下),绿色(左上),蓝色(右上)和黄色(右下),中间插入颜色。

这是我的片段着色器代码。

// FragmentProgram
#version 430

layout(location=0) out vec4 FragColor;
layout (std430, binding=3) buffer passColor
{
  float ssbo_Color[];
};
layout (std430, binding=4) buffer points
{
  float ssbo_Points[];
};

uniform vec3 camera_position;  

in vec3 ray_entry_point;

struct GridPoints
{
    vec3 p0;
    vec3 c0;
    vec3 p1;
    vec3 c1;
    vec3 p2;
    vec3 c2;
    vec3 p3;
    vec3 c3;
};

vec4 sph1 = vec4 (-2.0, 1.4, 0.0, 1.4);
vec4 sph2 = vec4 (2.0, 1.4, 0.0, 1.4);
GridPoints g;

float iSphere(in vec3 ro, in vec3 rd, in vec4 sph)
{
    vec3 o2c = ro - sph.xyz;
    float b = 2.0* dot(o2c, rd);
    float c = dot(o2c,o2c) - sph.w*sph.w;
    float h = b*b - 4.0*c;

    if (h<0.0){
        return -1.0;
    }
    float t = (-b - sqrt(h)) / 2.0;
    return t;
}

float iPlane(in vec3 ro, in vec3 rd)
{
    return -ro.y  / ( rd.y) ;
}

vec3 nPlane (in vec3 pos)
{
    return vec3(0.0, 1.0, 0.0);
}

float intersect(in vec3 ro, in vec3 rd, out float resT)
{
    resT = 10000000.0;
    float id = -1.0;
    float tsph1 = iSphere(ro, rd, sph1);
    float tsph2 = iSphere(ro, rd, sph2);
    float tpla = iPlane(ro, rd);

    if( tsph1 > 0.0){
        id = 1.0;
        resT = tsph1;
    }
    if( tsph2 > 0.0){
        id = 1.0;
        resT = tsph2;
    }
    if ( tpla > 0.0 && tpla < resT){
        id = 2.0;
        resT = tpla;
    }

    return id;
}

void main()
{
    vec3 light_pos = normalize(vec3(0.0,0.0,-1.0));
    vec4 col = vec4(0.2);

    vec3 ro = ray_entry_point + vec3(0.0,0.5,0.0);
    vec3 rd = -normalize(camera_position - ray_entry_point );
    float t;

    float id = intersect(ro, rd, t);
    // Test Points
    g.p0.x = ssbo_Points[0];
    g.p0.y = ssbo_Points[1];
    g.p0.z = ssbo_Points[2];

    g.p1.x = ssbo_Points[168];
    g.p1.y = ssbo_Points[169];
    g.p1.z = ssbo_Points[170];

    g.p2.x = ssbo_Points[168+3];
    g.p2.y = ssbo_Points[169+3];
    g.p2.z = ssbo_Points[170+3];

    g.p3.x = ssbo_Points[3];
    g.p3.y = ssbo_Points[4];
    g.p3.z = ssbo_Points[5];


    vec3 d = (g.p0+g.p1+g.p2+g.p3)/4; //Vector to midpoint of patch
    vec3 dc = d - sph1.xyz; // Vector to sphere center
    vec3 d_true = d -(sqrt(pow(sph1.w,2))-sqrt(pow(dc.x,2)+ pow(dc.y,2)+pow(dc.z,2))); // Real vector to midpoint of patch

    vec3 patch_col;
    vec3 pos;
    // Test Colors
    g.c0 = vec3(1,0,0);
    g.c1 = vec3(0,1,0);
    g.c2 = vec3(0,0,1);
    g.c3 = vec3(1,1,0);

    if( id > 0.0 && id < 1.5) {
        // hit sphere
        pos = ro + t*rd;
        col = vec4 (0.1, 0.1, 0.1, 1);
        if(abs(pos.y) > abs(g.p0.y) && abs(pos.y) < abs(g.p1.y) &&
           abs(pos.x) < abs(g.p0.x) && abs(pos.x) > abs(g.p2.x)){

            pos = ro + t*rd;
            // Interpolated Color
            patch_col = g.c0 + (g.c1-g.c0)/(g.p1.y-g.p0.y)*(pos.y-g.p1.y);

            col = vec4(patch_col,1);
        }
    }
    else if ( id > 1.5){
        // hit plane
        vec3 pos = ro + t*rd ;

        vec3 nor_L = nPlane(pos);
        float dif_L = clamp(dot( nor_L, light_pos), 0.0, 1.0);
        float amb_L = smoothstep(0.0, sph1.w, length(pos.xz-sph1.xz));

        vec3 nor_R = nPlane(pos);
        float dif_R = clamp(dot( nor_R, light_pos), 0.0, 1.0);
        float amb_R = smoothstep(0.0, sph2.w, length(pos.xz-sph2.xz));

        col = vec4(vec3(0.2, 0.2, 0.2) * amb_R*amb_L ,1.0);
    }
    FragColor = col;
}

任何提示都会有所帮助。

0 个答案:

没有答案