### Author Topic: 3Dto2D  (Read 8068 times)

#### aleks1283

• byte
• Posts: 49
##### 3Dto2D
« on: July 23, 2022, 01:40:00 pm »
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

• byte
• Posts: 49
##### Re: 3Dto2D
« Reply #1 on: July 23, 2022, 06:15:56 pm »
I would do it through interact3D2D maybe, but how to find the coordinates of all visible surfaces of objects???

#### MichaelJPCT

• float
• Posts: 267
##### Re: 3Dto2D
« Reply #2 on: July 24, 2022, 07:19:32 am »
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

• byte
• Posts: 49
##### Re: 3Dto2D
« Reply #3 on: July 24, 2022, 08:56:33 am »
How first find the local position of each visible point on 3D?

#### aleks1283

• byte
• Posts: 49
##### Re: 3Dto2D
« Reply #4 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 .

#### aleks1283

• byte
• Posts: 49
##### Re: 3Dto2D
« Reply #5 on: July 24, 2022, 11:33:27 am »
Egon where are you? How to get this information???

#### AeroShark333

• float
• Posts: 320
##### Re: 3Dto2D
« Reply #6 on: July 24, 2022, 07:08:03 pm »
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:
Code: [Select]
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
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...
« Last Edit: July 24, 2022, 07:23:21 pm by AeroShark333 »

#### aleks1283

• byte
• Posts: 49
##### Re: 3Dto2D
« Reply #7 on: July 25, 2022, 12:01:30 am »
Thanks

#### MichaelJPCT

• float
• Posts: 267
##### Re: 3Dto2D
« Reply #8 on: July 25, 2022, 07:19:09 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.