3D objects offset

Started by deqleos, April 12, 2018, 11:45:05 AM

Previous topic - Next topic

deqleos

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);




EgonOlsen

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.

deqleos

#2
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.

EgonOlsen

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?

deqleos

.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) ?

EgonOlsen

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.

deqleos

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.

EgonOlsen

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...

deqleos

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!