3Dto2D

Started by aleks1283, July 23, 2022, 01:40:00 PM

Previous topic - Next topic

aleks1283

Hello friends. How to find the distance to each point in 3d space from the screen. I want to make a height map from the 3d world displayed on the screen..

aleks1283

I would do it through interact3D2D maybe, but how to find the coordinates of all visible surfaces of objects???

MichaelJPCT

the screen is at the camera position in world space.
first find the local position of each point on 3D object, then convert the position into world space, then calc the distance from camera position.
converting involves matrix calculation that you need to know.

aleks1283

How first find the local position of each visible point on 3D?

aleks1283

In theory, this information is already somewhere in jpct-ae, since visible points are projected onto the screen, and so on with the calculation of the distance to the points .

aleks1283

Egon where are you? How to get this information??? :) :) :)

AeroShark333

#6
I see two possibilities:
1.
I'd first loop over all Object3D's in the world.
Then see which of these Object3D's are in the camera's FOV.
(Easiest way would be to do a dot product with the camera direction and the vector connecting the camera to each Object3D; then let's say > 0 falls in the camera view and < 0 is behind the camera's view.)

Then next, using the Object3D's in the camera's view, you'll be using polygon manager. https://www.jpct.net/jpct-ae/doc/com/threed/jpct/PolygonManager.html
You'll have something like this:
final Object3D obj = ...;
PolygonManager objPM = obj.getPolygonManager();
for(int id = 0; id < objPM.getMaxPolygonID(); id++){
if(objPM.getTransformedNormal(id).y > 0){
// ^Check if normal of polygon points up...
// Change if this doesn't work
SimpleVector v1 = objPM.getTransformedVertex(id, 0);
SimpleVector v2 = objPM.getTransformedVertex(id, 1);
SimpleVector v3 = objPM.getTransformedVertex(id, 2);
float maxHeight = Math.max(Math.max(v1.y, v2.y), v3.y);
// Do whatever with the max height of this polygon face
}
}

I sort of assumed here that the camera is looking down in the y-axis...

2. Shoot rays for every pixel on the screen and see with which Object3D and polygon face it interacts with. Then use the PolygonManager again to get the distance to that polygon (by averaging vertices or something)...
Make sure the algorithm is RAY:
https://www.jpct.net/jpct-ae/doc/com/threed/jpct/CollisionEvent.html
Maybe the code in reply #19 can help you with ray-polygon intersection:
https://www.jpct.net/forum2/index.php/topic,4821.msg32989.html#msg32989
It's up to you whether you want the ray to be from the camera's origin or parallel to the camera's direction...
To get the 3D position of the screen plane, I think you should use "reproject2D3DWS" in Interact2D. Then you can shoot a ray with vector "normalize((3D position of screen plane) - (camera position))"...
For ray-polygon picking, I think this might be useful: https://www.jpct.net/wiki/index.php/Picking and https://www.jpct.net/doc/com/threed/jpct/World.html#calcMinDistance(com.threed.jpct.SimpleVector,%20com.threed.jpct.SimpleVector,%20float)

Sorry for this messy explanation; typing on phone isn't ideal...

aleks1283


MichaelJPCT

Quote from: aleks1283 on July 24, 2022, 09:07:04 AM
In theory, this information is already somewhere in jpct-ae, since visible points are projected onto the screen, and so on with the calculation of the distance to the points .

it is calculated in GPU. maybe you can get that info by reading depth buffer. but even so, the read operation costs a lot of time, no good for real time application.

EgonOlsen

Sorry for not replying sooner, I was on holiday. But then again, I don't have much to add to what AeroShark333 already said. It highly depends on your application how to obtain the point in 3D.