Author Topic: 3D objects offset  (Read 837 times)

Offline deqleos

  • byte
  • *
  • Posts: 8
    • View Profile
3D objects offset
« on: April 12, 2018, 11:45:05 am »
Hi,

I have a question regarding a weird offset I get with 3D objects.
I receive a camera preview with faces on it. I want to show 3d objects on these faces, think about masks etc.
What I do is get the position of the face on the 2d preview and translate it to the 3d space with Interact2D.reproject2D3DWS().
I get a very weird offset though, where i can't seem to get the 3d object on the position that I want it to be. For example, I have the top left coordinates of the face but the 3d object will appear significantly to the left side.

What would the ideal circumstances be for a 2dto3d translation, in terms of the z axes, 3d model itself etc.

EDIT: I'm kinda confused now how reproject2dto3dWS() works now. If i put the object at x, y=100, the object barely moves on the x axis on screen and is like halfway they axis.

EDIT2: The idea that I want to implement is very simple but I'm having a really hard time with it. See the image for a simple illustration.
I have the face's positions and just want to overlay it with a 3d model.
The code that should give me the correct world space position is:
SimpleVector simpleVector = Interact2D.reproject2D3DWS(world.getCamera(), GLFrameBuffer, (int) faceRect.tl().x, (int) faceRect.tl().y, 20);



« Last Edit: April 13, 2018, 04:18:02 pm by deqleos »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11972
    • View Profile
    • http://www.jpct.net
Re: 3D objects offset
« Reply #1 on: April 16, 2018, 07:38:19 am »
Depending on your screen setup, the coordinates in 2D on screen might not match the ones that the FrameBuffer uses. This can be caused by a bar at the top or something. Make sure that (0,0) in 2D really corresponds to (0,0) in the FB. You can simply blit something at (0,0) to the FB and see if it appears at the right position.

Offline deqleos

  • byte
  • *
  • Posts: 8
    • View Profile
Re: 3D objects offset
« Reply #2 on: April 16, 2018, 10:08:48 am »
Are there any other common causes? I don't have any bars on screen, just the camera preview.

EDIT: When debugging the reprojected object from 2d to 3d, I found out that projecting it back to 2d gives correct coordinates. I guess it's safe to assume the coordinates form screen match the framebuffer ones. Could my 3d model be the reason for this problem?

Edit2: See picture for what actually happens with the 3d object's position. In code I set it to be at the top left of the light grey square.
When the face moves closer, the 3d object moves more to the left of the set position, when the face moves further, the 3d objects moves more to the right of the set position.
« Last Edit: April 16, 2018, 03:21:31 pm by deqleos »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11972
    • View Profile
    • http://www.jpct.net
Re: 3D objects offset
« Reply #3 on: April 16, 2018, 08:21:24 pm »
Might be that object isn't exactly centered around the orgin in object space, but I'm not sure if that would explain the increasing offset...it might, though.

What does Object3D.getCenter(); returns on your object?

Offline deqleos

  • byte
  • *
  • Posts: 8
    • View Profile
Re: 3D objects offset
« Reply #4 on: April 17, 2018, 09:58:07 am »
.getCenter() returns this: (-3.552497,1.6156462,-6.28337).
I guess your thought is then correct otherwise it would be (0,0)?
When I use a library model(primitives.getCube), .getCenter() returns (-1.4901161E-8,0.0,-1.4901161E-8). With this model the visual offset I get is a little less.
Do I manually have to manipulate the object center in different software (like Meshlab) ?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11972
    • View Profile
    • http://www.jpct.net
Re: 3D objects offset
« Reply #5 on: April 17, 2018, 12:05:59 pm »
You could do it in code as well. Just call build(), translate it by -center, call translateMesh() and clear the translation afterwards. However, this center is a calculated one. It doesn't have to be a "real" center of an object.

Offline deqleos

  • byte
  • *
  • Posts: 8
    • View Profile
Re: 3D objects offset
« Reply #6 on: April 17, 2018, 03:16:01 pm »
Hmm, it doesn't seem to change anything.
Don't think there is something wrong with the code I used, just to be sure:

model.build();
SimpleVector center = model.getCenter();
model.translate(-center.x, -center.y, -center.z);
model.translateMesh();
model.clearTranslation();
world.addObject(model);

I'll look a little more into the theory behind 3d. Very new to this.
I can manage to get unnoticeable offset by using a huge model and a very far z index, which is okay for now I guess.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11972
    • View Profile
    • http://www.jpct.net
Re: 3D objects offset
« Reply #7 on: April 17, 2018, 08:24:40 pm »
Your code looks fine. It might have something to do with the depth value that you are assuming for the reprojection call....in this case 20. This implies that the "depth" of the 2D image is 20 as well, or in other words, if the 2D object would be a 3D object managed by jPCT instead, it has to have a depth of 20 to match your object's depth. If you roughly know the size of the 2D object in real world an on screen, you should be able to calculate a more or less accurate depth for your object...either that, or just guess something that looks reasonable...

Offline deqleos

  • byte
  • *
  • Posts: 8
    • View Profile
Re: 3D objects offset
« Reply #8 on: April 18, 2018, 11:08:07 am »
Yes that is exactly the issue, I've decided to keep the z at a fixed index and simulate object coming closer and further by scaling the 3d object. It has a decent visual experience.
Thanks a lot for helping!