Tuesday, November 15, 2022
HomeGame Developmentc++ - OpenGL 4 depth of area

c++ – OpenGL 4 depth of area


I’m implementing depth of area. My fragment shader has the next code in it:

#model 430

uniform sampler2D depth_tex; // texture uniform
uniform sampler2D colour_tex; // texture uniform


uniform int img_width;
uniform int img_height;
uniform float model_distance;
uniform float close to;
uniform float far;

uniform int cam_factor; 


vec2 img_size = vec2(img_width, img_height);

in vec2 ftexcoord;

format(location = 0) out vec4 frag_colour;

float to_distance(float depth_colour)
{
    float dist = (2.0*close to*far) / (far + close to - depth_colour*(far - close to)); 
    return dist;
}

float to_depth(float dist)
{
    float depth = (far*(dist - 2.0*close to) + close to*dist)/(dist*(far - close to));
    return depth;
}

vec4 get_blurred_pixel(vec2 tc)
{   
    float depth_colour = texture(depth_tex, tc).r;

    float distance_to_pixel = to_distance(depth_colour);

    float x = clamp(distance_to_pixel, 0.0, 2*model_distance) / (2*model_distance);

    // tent operate
    if(x > 0.5)
         x = 1.0 - x;

    x = 1.0 - pow(x, 1.0/10.0);

   const float pi_times_2 = 6.28318530718; // Pi*2
    
    float instructions = 16.0; // BLUR instructions (Default 16.0 - Extra is healthier however slower)
    float high quality = 10.0; // BLUR high quality (Default 4.0 - Extra is healthier however slower)
    float measurement = 8.0; // BLUR measurement (radius)
        vec2 radius = vec2(measurement/img_size.x * cam_factor, measurement/img_size.y * cam_factor);

   vec4 blurred_colour = texture(colour_tex, tc);
    
    for( float d=0.0; d<pi_times_2; d+= pi_times_2/instructions)
        for(float i=1.0/high quality; i<=1.0; i+=1.0/high quality)
            blurred_colour += texture( colour_tex, tc + vec2(cos(d),sin(d))*radius*x*i);    
    
    // Output to display
    blurred_colour /= high quality * instructions - 15.0;

    return blurred_colour;
}

// https://www.shadertoy.com/view/Xltfzj
// https://www.blitzcoder.org/discussion board/subject.php?id=124
void essential()
{
    frag_colour.rgb = get_blurred_pixel(ftexcoord).rgb;
    frag_colour.a = 1.0;    
}

The traces the place I take advantage of x as a blur issue are:

float x = clamp(distance_to_pixel, 0.0, 2*model_distance) / (2*model_distance);

// tent operate
if(x > 0.5)
     x = 1.0 - x;

x = 1.0 - pow(x, 1.0/10.0);

This code works for a lot distances, however not for shut distances. I can not determine why it doesn’t work for shut distances. Please let me know in case you want extra code. Thanks in your time.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments