NVIDIAGameWorks / kaolin

A PyTorch Library for Accelerating 3D Deep Learning Research
Apache License 2.0
4.31k stars 534 forks source link

About orthographic projection matrix #785

Open Ligo04 opened 3 months ago

Ligo04 commented 3 months ago

I don't particularly understand why the projection matrix function projection_matrix of the orthographic camera uses the following calculation formulas for top, bottom, right, left. The ndc coordinates calculated in this way are incorrect.

top = 1.0
bottom = -top
right = 1.0 * self.width / self.height
left = -right

Instead of using the standard form, the function code is commented out and the ndc calculated using the standard form is correct.

top = self.height / 2
bottom = -top
right = self.width / 2
left = -right
orperel commented 3 months ago

Hi @Ligo04 !

Off the top of my head (I still need to empirically test how it plays with the rest of the code) I think you're right -

The ortho matrix is:

$$ \left(\begin{array}{cc} \frac{2}{right-left} & 0 & 0 & -\frac{right+left}{right-left} \ 0 & \frac{2}{top-bottom} & 0 & -\frac{top+bottom}{top-bottom} \ 0 & 0 & -\frac{2}{far-near} & -\frac{far+near}{far-near} \ 0 & 0 & 0 & 1 \ \end{array}\right) $$

Subbing the current case (1):

$$ \left(\begin{array}{cc} \frac{H}{W} & 0 & 0 & 0 \ 0 & 1 & 0 & 0 \ 0 & 0 & -\frac{2}{far-near} & -\frac{far+near}{far-near} \ 0 & 0 & 0 & 1 \ \end{array}\right) $$

Subbing the commented out case (2):

$$ \left(\begin{array}{cc} \frac{2}{W} & 0 & 0 & 0 \ 0 & \frac{2}{H} & 0 & 0 \ 0 & 0 & -\frac{2}{far-near} & -\frac{far+near}{far-near} \ 0 & 0 & 0 & 1 \ \end{array}\right) $$

where $W,H$ are the image plane width, height.

For the default NDC space ranging [-1,1] (aligned with old OpenGL conventions) the viewing volume should be a 2 units wide cuboid, so case (2) should be the correct one.

Ligo04 commented 3 months ago

Hi @Ligo04 !

Off the top of my head (I still need to empirically test how it plays with the rest of the code) I think you're right -

The ortho matrix is:

$$ \left(\begin{array}{cc} \frac{2}{right-left} & 0 & 0 & -\frac{right+left}{right-left} \ 0 & \frac{2}{top-bottom} & 0 & -\frac{top+bottom}{top-bottom} \ 0 & 0 & -\frac{2}{far-near} & -\frac{far+near}{far-near} \ 0 & 0 & 0 & 1 \ \end{array}\right) $$

Subbing the current case (1):

$$ \left(\begin{array}{cc} \frac{H}{W} & 0 & 0 & 0 \ 0 & 1 & 0 & 0 \ 0 & 0 & -\frac{2}{far-near} & -\frac{far+near}{far-near} \ 0 & 0 & 0 & 1 \ \end{array}\right) $$

Subbing the commented out case (2):

$$ \left(\begin{array}{cc} \frac{2}{W} & 0 & 0 & 0 \ 0 & \frac{2}{H} & 0 & 0 \ 0 & 0 & -\frac{2}{far-near} & -\frac{far+near}{far-near} \ 0 & 0 & 0 & 1 \ \end{array}\right) $$

where $W,H$ are the image plane width, height.

For the default NDC space ranging [-1,1] (aligned with old OpenGL conventions) the viewing volume should be a 2 units wide cuboid, so case (2) should be the correct one.

Yes. Thanks~. Hope this can be updated