ssloy / tinyrenderer

A brief computer graphics / rendering course
https://github.com/ssloy/tinyrenderer/wiki
Other
20.2k stars 1.96k forks source link

Perspective division before Viewport transformation #97

Open Buzzefall opened 2 years ago

Buzzefall commented 2 years ago

I wonder why the main-branch implementation of triangle(...) rasterization works like that:

  1. pts[i] = Viewport * verts_clip[i]; ( where verts_clip[i] = Projection * verts[i])
  2. pts2[i] = pts[i] / pts[i].w

And it seems to produce adequate result.

I thought we should first project objects (with perspective division), then scale achieving [-1, 1]x[-1, 1] rectangle in plane Oxy and shift coordinates to make then positive as [0, wid]x[0, hei]. This way distant objects should shrink and vanish to the center of a screen.

But if we first make Viewport scale and shift, and then make perspective division, [-1, 1]x[-1, 1] rectangle should become [0, wid/(1+rz)]x[0, hei/(1+rz)] for corresponding z values. Then distant objects should shrink and vanish to the point (0, 0) on a screen.

Is it just fine-tuning of object positions for tutorial purpose and there is a mistake in code? Of I miss something simple?

XiaofanLinUS commented 2 years ago

According to my calculation, everything works as expected. IMG_0327

In case you need to examine it, I included my full study note on this one. Note 97.pdf