Giter VIP home page Giter VIP logo

lensdistortionopengl's Introduction

Implementation of lens distortion simulation in OpenGL

This repository is still in development. The project can be used, but some features are still being implemented.

Introduction

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.

2 approaches are proposed to simulate the distortion:

  • 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".

First Approach

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.

Radial distortion is model using the following equation:

$$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.

Tengential distortion is model using the following equation:
$$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.

Pros and Cons

Pros
  • Easy to implement
  • Fast to compute
  • Doesn't depend on the scene
Cons
  • Since the distortion is applied to the texture, it leave unfilled areas in the corners of the image.

Results

The following image is a demo of the lens distortion simulation using the first approach.

Demo


Second approach (Work in progress)

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.

$$\begin{bmatrix} x_{clip} \\\ y_{clip} \\\ z_{clip} \\\ w_{clip} \end{bmatrix} = MVP \times \begin{bmatrix} x_{model} \\\ y_{model} \\\ z_{model} \\\ 1 \end{bmatrix}$$ $$MVP = P \times V \times M$$

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

$$\begin{bmatrix} x_{ndc} \\\ y_{ndc} \\\ z_{ndc} \\\ 1 \end{bmatrix} = \begin{bmatrix} x_{clip} \\\ y_{clip} \\\ z_{clip} \\\ w_{clip} \end{bmatrix} / w_{clip}$$

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.

$$\begin{bmatrix} x_{clip} \\\ y_{clip} \\\ z_{clip} \\\ w_{clip} \end{bmatrix} = \begin{bmatrix} x_{distorted} \\\ y_{distorted} \\\ z_{distorted} \\\ 1 \end{bmatrix} \times w_{clip}$$

Finally, we can multiply the clip coordinates by the inverse of the MVP matrix to get the model coordinates.

$$\begin{bmatrix} x_{model} \\\ y_{model} \\\ z_{model} \\\ 1 \end{bmatrix} = MVP^{-1} \times \begin{bmatrix} x_{clip} \\\ y_{clip} \\\ z_{clip} \\\ w_{clip} \end{bmatrix}$$

Results

The following image is a demo of the lens distortion simulation using the second approach.

Demo2

Pros and Cons

Pros
  • Should resolve the unfilled areas in the corners of the image.
  • To be determined ...
Cons
  • Dependent on the polygon resolution of each models present in the scene.
  • To be determined ...

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.

Note

This approach need further investigation to check its validity. And find its pros and cons.


Dependencies

  • OpenGL
  • GLFW
  • GLEW
  • GLM
  • stb_image
  • Assimp

You can install the dependencies using setup.bash script.


License

Shield: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

CC BY-NC-SA 4.0

lensdistortionopengl's People

Contributors

vlhermitte avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.