[SOLVED] Screen coordinates to viewport coordinates

Hello! I want to set a new camera viewport according to an object position but I have no idea how to do that.

There is a simple way to get screen coordinates and convert it to viewport coordinates?

I want to show a camera in a specific position at the screen.

For now I have a quad and a text where I want to put the camera.

Here is my code:

Vector3f textPos = obj.getWorldTranslation().clone();

Vector3f newPos = getCamera().getScreenCoordinates(textPos);

newPos.setY(newPos.y + 100f);

quad.setLocalTranslation(
	newPos.x - 8f, newPos.y - 35f, newPos.z - 1f
	);

textPos.setLocalTranslation(newPos);

Camera camera2 = getCamera().clone();

camera2.setViewPort(x1, x2, y1, y2);  //I need to find out these values.

Thanks in advance! :slight_smile:

1 Like

Have you read the wiki on cameras?

1 Like

Checkout the HelloPicking example frome the beginners section in the wiki. You will find the answer there

1 Like

I’ve read the wiki and the HelloPicking example but I can’t see how both applies to my question. I understand how to set a viewport and how to use multiple cameras. My problem is how to keep another camera on screen according to screen cordinates. Actually I realized that world coordinates to viewport would works too. I want to update the camera viewport using screen coordinates or world coordinates values. I don’t know how explain very well so I drew this to make it more clear. I tried to upload the image but I’m getting errors so I uploaded it to ImgBB.

Here is the link:

1 Like

At least this should be answered by the HelloPicking example IMHO.

1 Like

I have not seen any references to camera viewport in HelloPicking example. Could you please explain to me better?

1 Like

Damn I interpreted your question wrong, you can safely ignore me. Sorry for the inconvenience :frowning:

1 Like

No problem, thank you for trying to help. :slight_smile:

1 Like

@chalcosoma, so it looks like you are wanting to do like a “magnifying glass” type situation on one of your models? sounds like a neat idea, to me.

And I don’t know how much help this will be, but:
so for viewport coordinates it goes in the format: x1, x2, y1, y2 and the values ranging from 0.0f to 1.0f. bottom-left to top-right.

Some code for that might help would be to do up the jme samples (and include the test data jar so it will all work-ish) and check out multiviews in the renderer folder.

That should give ya a good handle that you can think of viewport coordinates as like a percent type situation.

then you would probably want to figure out a location on your mesh or object (maybe it’s spatial/node coordinates?) and use a ray-collision type situation with the camera… you would be reversing the camera’s direction mathematically to go from the object to the camera and see what 2D point you collide with on the camera’s near plane.

That’s probably the process you are looking for, though I can’t think of the right math to suggest for doing so… I’ve discarded a lot of that info off of the top of my head.

But… does that sound like a good clarification of what you are trying to do?

EDIT: oh wait… looks like you are using a good method up there in your code. LOL

Game On

1 Like

I don’t know how to do the math for this. Could anyone help me please? Thanks for all the help.

It’s not a magnifying glass. I want to show the top of the object in Camera2 (as seen in the image) and place the camera viewport close to the object. I’m making a dice roller app and the object is a dice. I want to show the dice result in the camera, showing the dice top face.

1 Like

Viewport coordinates go from [0,0] (bottom left) to [1,1] (upper right). Suppose you make the camera2 viewport take up 20% of the screen width and 20% of the screen height:

camera2.setViewPort(midX-0.1f, midX+0.1f, midY-0.1f, midY+0.1f);

That reduces the problem to positioning the center [midX, midY] of camera2’s viewport. For that I suggest picking an offset from the die’s viewport coordinates in the main camera.

Screen coordinates go from [0,0,z] (bottom left) to [screenWidth-1,screenHeight-1,z] (upper right), so you have to divide the screen [x,y] by the screen dimensions to get the viewport [x,y].

Camera mainCamera = getCamera();
float screenWidth = mainCamera.getWidth(); // in pixels
float screenHeight = mainCamera.getHeight(); // in pixels
Vector3f dieCenterScreenLocation = mainCamera.getScreenCoordinates(dieCenterWorldLocation);
float midX = dieCenterScreenLocation.x/screenWidth;
float midY = 0.2f + dieCenterScreenLocation.y/screenHeight;

You may want to make alternative arrangements in case the die is near the top of the screen (dieCenterScreenLocation.y > 0.7fscreenHeight) near the left edge (dieCenterScreenLocation.x < 0.1fscreenWidth) or near the right edge (dieCenterScreenLocation.x > 0.9f*screenWidth).

Does that help?

2 Likes

I’m not sure I’ve interpreted the question correctly but just in case you are trying to figure out where to place a 3D perspective camera such that an object is fully in view…

I had to do something similar when lining up the imposter camera for the SimArboreal tree editor. I wanted a reasonably tight fit so as not to waste texture space. There is a magic number (not really magic) in the camera’s projection matrix which can be used to find the distance for a particular width/height.

There is a comment in the source code here:

That, and the surrounding code, might be helpful when fully ingested.

1 Like

Thank you very much!! It works perfectly!
@pspeed Thank you too.

1 Like