Wednesday, September 28, 2022
HomeGame Developmentglsl - Learn how to do perspective transformation of linear depth in...

# glsl – Learn how to do perspective transformation of linear depth in vertex shader

I do know arithmetic of perspective transformation.

``````structure(binding = 0, std140) uniform global_buffer {
structure(offset = 0) vec3 proj_S;
structure(offset = 16) vec3 proj_C;
structure(offset = 32) vec3 proj_c1;
structure(offset = 48) vec3 proj_r1;
...
};
vec3 proj_transform(vec3 p) {
//p1 = (p*{s,s,1} - (nc - nr*{1,1,0}))/nr*r1*{1,1,2} + (c1 - r1), s = nc/p.
//   = ...
return p/vec3(p,p,1)*proj_S + proj_C;
}
``````

However the surprising error appears to be the clipping,
many out of vary triangles haven’t been clip.
Under is I do:

``````void most important() {
gl_Position = vec4(proj_transform( POSITION_VIEW ), 1.0);
}
``````

I attempted some strategies, multiplying the vector p1 by p.z (similar as final part of nolinear perspective transformation consequence), that the proper clipping was obtained:

``````vec4 clipproj_transform(vec3 p) {
vec3 p1 = p/vec3(p,p,1)*proj_S + proj_C;
return vec4(p1, 1.0) * p.z;
}
``````

The efficiency continues to be regular right here, however the depth take a look at have some error.
Though the gl_FragDepth can be utilized to repair this error, it is vitally costly (at most twice as sluggish as the unique), and there’s a hidden hazard that perspective interpolation is appropriate.

``````vec4 clipproj_transform(vec3 p, out float depth) {
vec3 p1 = p/vec3(p,p,1)*proj_S + proj_C;
depth = p1.z;
return vec4(p1, 1.0) * p.z;
}
...
void most important() {
gl_FragDepth = fragin_depth;
}
``````

I believe that after accurately understanding some particulars, could discover the fully appropriate technique in opengl-glsl or vulkan-glsl. Hope the reply.

RELATED ARTICLES