This repository is still in development. The project can be used, but some features are still being implemented.
This project is an implementation of lens distortion simulation in OpenGL. The goal is to simulate the distortion that occurs when rendering an image through a lens.
The distortion can be caused by radial and tangential distortion. Radial distortion is caused by the lens not being perfectly spherical. Tangential distortion is caused by the lens not being perfectly parallel to the image plane.
- The first approach is to apply the distortion to the texture.
- The second approach is to apply the distortion to the vertices of the models.
In the file HeaderFile/GameState.h, you can change the approach by changing the value of the variable "gameState".
The scene is rendered to a custom framebuffer. The image rendered in the framebuffer is then used as a texture to a quad. The texture to the quad is then distorted using a lens distortion shader.
$$r = sqrt(x^2 + y^2) \\$$ $$x' = x * (1 + k1 * r^2 + k2 * r^4 + k3 * r^6) \\$$ $$y' = y * (1 + k1 * r^2 + k2 * r^4 + k3 * r^6) \\$$Where k1, k2 and k3 are the radial distortion coefficients.
$$x' = x + (2 * p1 * x * y + p2 * (r^2 + 2 * x^2)) \\$$ $$y' = y + (p1 * (r^2 + 2 * y^2) + 2 * p2 * x * y) \\$$Where p1 and p2 are the tangential distortion coefficients.
- Easy to implement
- Fast to compute
- Doesn't depend on the scene
- Since the distortion is applied to the texture, it leave unfilled areas in the corners of the image.
The following image is a demo of the lens distortion simulation using the first approach.
Instead of applying the distortion to the texture, the position of each vertex can be moved to simulate the distortion.
To compute the new position of each vertex, we need to apply transformation to get to the clipping coordinates, typically done using the projection-view-model (MVP) matrix.
Where P is the projection matrix, V is the view matrix and M is the model matrix.
We then perform division by w to get the normalized device coordinates (NDC).
Then we can apply the distortion to the normalized coordinates using the following equation:
Radial distortion:
$$r = sqrt(x_{ndc}^2 + y_{ndc}^2) \\$$ $$x_{distorted} = x_{ndc} * (1 + k1 * r^2 + k2 * r^4 + k3 * r^6) \\$$ $$y_{distorted} = y_{ndc} * (1 + k1 * r^2 + k2 * r^4 + k3 * r^6) \\$$Tangential distortion:
$$x_{distorted} = x_{ndc} + (2 * p1 * x_{ndc} * y_{ndc} + p2 * (r^2 + 2 * x_{ndc}^2)) \\$$ $$y_{distorted} = y_{ndc} + (p1 * (r^2 + 2 * y_{ndc}^2) + 2 * p2 * x_{ndc} * y_{ndc}) \\$$Where k1, k2 and k3 are the radial distortion coefficients and r is the distance from the center of the image to the vertex position.
Once the distortion is applied, we can multiply the distorted coordinates by w to get the clip coordinates.
Finally, we can multiply the clip coordinates by the inverse of the MVP matrix to get the model coordinates.
The following image is a demo of the lens distortion simulation using the second approach.
- Should resolve the unfilled areas in the corners of the image.
- Dependent on the polygon resolution of each models present in the scene.
- Slower to compute, since we need to recompute the position of each vertex.
To solve the dependency on the polygon resolution, we can use a geometry shader or tesselation shader to increase the number of vertices of each polygon. That way we don't need to load high-resolution models at the start of the application.
There is a bug in the second approach, negative values of k1, k2 and k3 cause an incorrect movement of some vertices. It needs further investigation.
- OpenGL
- GLFW
- GLEW
- GLM
- stb_image
- ImGUI
- Assimp
You can install the dependencies using setup.bash script.
bash setup.bash
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.