Viewing pipeline and 3D Transformations

2019-08-14 03:04发布

问题:

My question is quite basic in computer graphics field, but somehow I couldn't find an answer.

Prolog:

I want to write a program which prints polygons on a canvas, and I'd like to allow users to play with the polygons - scale, translate and rotate (around x/y/z axis).

I ought to use java AWT only; not JOGL (java's library for openGL).


I recieve the polygons as 3d world coordinates as via input file to the program, as well as "camera" properties such as coordinates of camera, look at point, up vector and a window size (used in the projection from 3d to 2d).


First question:

My first problem is to write the viewing pipeline, so the 3d world coordinats would convert to viewing coordinates (camera coordinates), then project it onto 2d coordinates and perform a clipping to create the perspective. I've seen countless videos and methods but I can't decide on the final matrices.


Second question:

My second problem is, where to apply the 3D transformation matrices. Should the transformations be applied on the original world coordinates (and then of course continue with the viewing pipeline) or directly to view coordinates (and continue the pipeline from that point)?

For clarification, let's denote viewing pipeline as A->B->C->D, which is re-calculated on each user transformation, and a user initiated transformation (could be any of the above) as T.

My concern is whether to do TA->B->C->D or A->TB->C->D.

Thanks for helpers.

回答1:

The way you usually go from vertex coordinates to screen coordinates is by

(1) vertex -> (2) model -> (3) world -> (4) view -> (5) screen

So to exemplify:

  1. The polygon coordinates you specify initially
  2. Maybe you scale/rotate/shear the points to create your model
  3. You place your model in the world
  4. You place your camera in the world (or rather, you move the world to fit your camera position). This is where you would put a lookAt matrix.
  5. You project it on the screen

As we multiply to the right, we need to switch the order when we write it in a program. The final statement would look like this:

outPosition = screenProjection*worldToView*modelToWorld*polygonToModel*inPosition

So you see, whether you would use TA->B->C->D or A->TB->C->D would generate two different effects: the first would be similar to the one I exemplified above (i.e. T would work as polygonToModel), or else it would work as the modelToWorld transformation. Both works, they just yield different results.

Note that all transformations here are 4 by 4 matrices. Transformations in steps 2 - 4 can all be identity matrices (i.e. no scaling/shearing/rotation or anything), however for the projection you need to create a frustum. You could create this one yourself by using something like is being stated here, but it might be easier to just use a 3D math library for java.