Author Topic: How to transform camera from original position using 4x4 transformation matrix  (Read 4574 times)

Offline oliw

  • byte
  • *
  • Posts: 2
    • View Profile
I have a question to do with building an Augmented Reality app using JPCT (and OpenCV) which places graphics over planar book covers.

Imagine this scenario.

I have a planar rectangle in my scene. The center of the rectangle is at world coordinate (0,0,0).

I have used OpenCV solvePNP to get a 4x4 Matrix containing a transformation (comprised of a translation and a rotation) from the world/model's coordinate system (they are the same) to where the camera should be placed.

My question is:
Where should my camera initially be placed and pointed in the world coordinate system and...
How should I then apply the 4x4 transformation to move and reorient the camera from its initial position and orientation so that it is in its the right position and orientation?

I should add that I am just rendering a primitive sphere at the origin for now.

Let me know if I should clarify! (I am a graphics novice)
Thanks in advance
Oli

« Last Edit: December 12, 2013, 08:35:57 pm by oliw »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
The cleanest way of doing it is to split the matrix into a 3x3 rotational part and a vec3 translation part. You can then translate the camera by that vector and set the matrix via Camera.setBack(<Matrix>);

However, you have to keep in mind that the coordinates system will most likely differ (http://www.jpct.net/wiki/index.php/Coordinate_system), so you have to convert between the two. That usually involves a simple rotation.

Offline kiffa

  • long
  • ***
  • Posts: 199
    • View Profile
Could you tell me why choose this coordinates system instead of a GL-like system?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Could you tell me why choose this coordinates system instead of a GL-like system?
That's a decision that i made for the software renderer back when i started with jPCT. I wanted the z-axis to go inside the screen to ease calculations for the depth buffer, so i rotated the whole thing. After all, it all doesn't matter...you just have to know how it is and then convert other systems to it. In 3ds for example, z goes up...

Offline oliw

  • byte
  • *
  • Posts: 2
    • View Profile
As far as I know, the coordinate system in OpenCV is the same as JPCT. In OpenCV X points to the right, Y points down and Z points into the screen. This implies I shouldn't have to change between coordinate systems?

I've followed your advice. I've taken my 4x4 Pose from OpenCV

r1 r2 r3 t1
r4 r5 r6 t2
r7 r8 r9 t3
0  0  0   1

and split them into a JPCT Matrix and JPCT SimpleVector
i.e.

cameraRotation
r1 r2 r3 0
r4 r5 r6 0
r7 r8 r9 0
0  0  0   1

and

cameraTranslation
t1
t2
t3

then I do

camera.setPosition(cameraTranslation);
camera.setBack(cameraRotation);

Does this make sense? (I still can't see my expected result but it COULD be an earlier mistake I make in OpenCV)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Looks reasonable to me.