Mapping Floor Plan Coordinates to Render/3DView Coordiantes

This topic has been viewed 8424 times and has 12 replies
Thread status: Active
Questions about extending features and developing plug-ins [img]http://www.sweethome3d.com/images/flags/en_small.gif[/img]
shmuelzon
Posts: 14
Joined: Sun Dec 24, 2023 6:13 am
Country: Israel

Re: Mapping Floor Plan Coordinates to Render/3DView Coordiantes

Post by shmuelzon »

If I'm not mistaken, that code is only the transformation part, i.e., move the target object to the coordinate system of the camera, but the projection itself is done as a part of the renderer's internal code as SH3D provides the lens type, FOV, aspect ratio, etc.
I didn't see the perspective projection part in both renderers but maybe I missed it...
User avatar
Daniels118
Posts: 503
Joined: Wed Nov 17, 2021 9:04 pm
Country: Italy

Re: Mapping Floor Plan Coordinates to Render/3DView Coordiantes

Post by Daniels118 »

Whops, seems you're right... this tells how to compute the projection matrix:
https://gamedev.stackexchange.com/quest ... -in-opengl
But you don't need to do it by hand, because Java3D has a ready made mathod for this:
https://download.java.net/media/java3d/ ... %20double)
Java3D is embedded in SH3D installation, so you just need to add it to your build path, you don't need to embed it in your plugin.
shmuelzon
Posts: 14
Joined: Sun Dec 24, 2023 6:13 am
Country: Israel

Re: Mapping Floor Plan Coordinates to Render/3DView Coordiantes

Post by shmuelzon »

I finally got it to work, thanks Daniele!

For future reference, if anyone will stumble upon this post, here's what I ended up with:

Code: Select all

int renderWidth = 1024;
int renderHeight = 576;

/* Set up */
Camera camera = getHome().getCamera();
cameraPosition = new Vector4d(camera.getX(), camera.getZ(), camera.getY(), 0);

Transform3D yawRotation = new Transform3D();
yawRotation.rotY(camera.getYaw());

Transform3D pitchRotation = new Transform3D();
pitchRotation.rotX(-camera.getPitch());

Transform3D perspectiveTransform = new Transform3D();
perspectiveTransform.perspective(camera.getFieldOfView(), (double)renderWidth / renderHeight, 0.1, 100);
perspectiveTransform.mul(pitchRotation);
perspectiveTransform.mul(yawRotation);

HomePieceOfFurniture furniture = /* Get some furniture... */;
Vector4d objectPosition = new Vector4d(furniture.getX(), furniture.getElevation(), furniture.getY(), 0);

objectPosition.sub(cameraPosition);
perspectiveTransform.transform(objectPosition);
objectPosition.scale(1 / objectPosition.w);

System.out.println(String.format("Projected pixel: (%d, %d)",
  (int)((objectPosition.x * 0.5 + 0.5) * renderWidth),
  (int)((objectPosition.y * 0.5 + 0.5) * renderHeight)
));
Post Reply New Topic

Who is online

Users browsing this forum: No registered users and 1 guest