1

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.

Jjang
  • 11,250
  • 11
  • 51
  • 87
  • Follow pingul answer and my comment. Usually, in the `reshape` method you calculate (and upload) the projection matrix and `glViewport`. View matrix instead at begin of `display` and the model matrix of each model at the begin of the rendering of the i-th model – elect Apr 19 '16 at 08:00

1 Answers1

0

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.

Community
  • 1
  • 1
pingul
  • 3,351
  • 3
  • 25
  • 43
  • Better `(1) model -> (2) world -> (3) view -> (4) screen` and `outPosition = viewToClip * worldToView * modelToWorld * inPosition`. The upcoming vertex is actually in model space *by definition* – elect Apr 19 '16 at 07:57
  • @elect I agree, that is usually how I write it. I chose the example above as it has the same amount of transformations as in the question, and that it is still sensible. E.g. I consider the `polygonToModel` transformation to useful: you might have a cube with sides of length 1 and decide you want to scale it - better then to add another transformation than redefine your points. – pingul Apr 19 '16 at 17:01
  • @elect That is a valid point, so it might have been a bad example on my part. I will wait with a potential edit to see if the asker finds the post in its current state useful or not, but I appreciate your comments. – pingul Apr 19 '16 at 20:18
  • Well, maybe there's some misunderstanding from my side. I receive the coordinates of the polygons, therefore I consider myself starting from the stage of "world coordinates" (don't know what is model coordinates). Now, from your answer I understand that any transformations should be applied on the WORLD coordinates and not the VIEW ones, that's first. I'll give that a try. Secondly, could you please clarify a bit more about the transformation to view coordinates and then the transformation to screen? I'm having some troubles with those... – Jjang Apr 19 '16 at 20:22
  • 1
    @Jjang That is fine - that means your `modelToWorld` transformation is the identity matrix. The `view` transformation means that we place the camera in the world. However, think of your monitor as a 'static' camera - we therefor need to move our 3D world to the monitor. The `view` transformations is thus just a rotation and translation of your 3D world (see the link I added). The frustum is essentially saying that "stuff closer should look bigger"; have a look at the link I provided and you might get some inspiration. – pingul Apr 19 '16 at 21:22