Home Game Development glsl – How to do perspective transformation of linear depth in vertex shader

glsl – How to do perspective transformation of linear depth in vertex shader

0
glsl – How to do perspective transformation of linear depth in vertex shader

[ad_1]

I do know arithmetic of perspective transformation.

format(binding = 0, std140) uniform global_buffer {
  format(offset = 0) vec3 proj_S;
  format(offset = 16) vec3 proj_C;
  format(offset = 32) vec3 proj_c1;
  format(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[2]/p[2].
  //   = ... 
  return p/vec3(p[2],p[2],1)*proj_S + proj_C;
}

But the sudden error appears to be the clipping,
many out of vary triangles haven’t been clip.
Below is I do:

void major() {
  gl_Position = vec4(proj_transform( POSITION_VIEW ), 1.0);
}

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

vec4 clipproj_transform(vec3 p) {
  vec3 p1 = p/vec3(p[2],p[2],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.
Although the gl_FragDepth can be utilized to repair this error, it is rather costly (at most twice as sluggish as the unique), and there’s a hidden hazard that perspective interpolation is right.

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

I feel that after accurately understanding some particulars, might discover the fully right technique in opengl-glsl or vulkan-glsl. Hope the reply.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here