Reputation: 11
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) ?
Upvotes: 1
Views: 630
Reputation: 72479
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.
Upvotes: 1