1

I'm coding a GLSL shader to do projection of 3D vertices using the Model, View and Projection matrices.

I used This for projection matrix. However I'm not getting the distorted image. In order to distort the points I used:

  vec4 distort(vec4 pos){
    // normalize
    float z = pos[2];
    float z_inv = 1 / z;
    float x1 = pos[0] * z_inv;
    float y1 = pos[1] * z_inv;
    // precalculations
    float x1_2 = x1 * x1;
    float y1_2 = y1 * y1;
    float x1_y1 = x1 * y1;
    float r2 = x1_2 + y1_2;
    float r4 = r2 * r2;
    float r6 = r4 * r2;
    // rational distortion factor
    float r_dist = (1 + k1 * r2 + k2 * r4 + k3 * r6)
     / (1 + k4 * r2 + k5 * r4 + k6 * r6);
    // full (rational + tangential) distortion
    float x2 = x1 * r_dist + 2 * p1 * x1_y1 + p2 * (r2 + 2 * x1_2);
    float y2 = y1 * r_dist + 2 * p2 * x1_y1 + p1 * (r2 + 2 * y1_2);
    // denormalize for projection (which is a linear operation)
    return vec4(x2*z, y2*z, z, pos[3]);}

and the projection is as following:

    vec4 view_pos = modelView * vec4(aPos, 1.0);
    vec4 dist_pos = distort(view_pos);
    gl_Position = projection * dist_pos;

The problem is when the camera is near the object, I get really bad projections of 3d points. Is there a way to check if the point is distordable or not and is there something wrong with the distortion function I'm using (i'm tryng to use OpenCV distortion) ?

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
Houssem
  • 11
  • 1
  • Does this answer your question? [Camera lens distortion in OpenGL](https://stackoverflow.com/questions/44489686/camera-lens-distortion-in-opengl) – Yakov Galka Apr 21 '20 at 18:53
  • It was actually from there that I got the equations. The problem is when I get too close to the object, it stats going really bad. So there are some limitations to those equations and I was wondering if I could predict when it would go wrong to remove the vertices before projection. – Houssem Apr 22 '20 at 08:58

1 Answers1

1

Don't distort individual vertices, since as you mentioned, it does not work well. The reason is that no matter how you distort your vertices, OpenGL still rasterizes triangles with straight edges. However, lens distortion causes straight lines to appear curved. You could mitigate that by subdividing your geometry to smaller triangles... but that won't ever fully solve the problem.

Instead, render your scene into an off-screen texture, and when done, render a full-screen quad that samples that textures with distorted texture coordinates. This distortion step can be combined with other post processing effects (e.g. tone mapping), making it a rather cheap option.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • It's not working not because of OpenGL, it's not working because the distortion model that I emplemented doen't work when the camera is too close (some vertices are far out of the distortion domain if I can say that) – Houssem Apr 21 '20 at 19:04