# How to get projected distorted 2D coordinates of verticies

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) ?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.