Search code examples
javagraphicsjava-3d

How to find a normal vector pointing directly from virtual world to screen in Java3D?


I think it can be done by applying the transformation matrix of the scenegraph to z-normal (0, 0, 1), but it doesn't work. My code goes like this:

Vector3f toScreenVector = new Vector3f(0, 0, 1);
Transform3D t3d = new Transform3D();
tg.getTransform(t3d); //tg is Transform Group of all objects in a scene
t3d.transform(toScreenVector);

Then I tried something like this too:

Point3d eyePos = new Point3d();
Point3d mousePos = new Point3d();
canvas.getCenterEyeInImagePlate(eyePos);
canvas.getPixelLocationInImagePlate(new Point2d(Main.WIDTH/2, Main.HEIGHT/2), mousePos); //Main is the class for main window.

Transform3D motion = new Transform3D();
canvas.getImagePlateToVworld(motion);
motion.transform(eyePos);
motion.transform(mousePos);

Vector3d toScreenVector = new Vector3f(eyePos);
toScreenVector.sub(mousePos);
toScreenVector.normalize();

But still this doesn't work correctly. I think there must be an easy way to create such vector. Do you know what's wrong with my code or better way to do so?


Solution

  • Yes, you got my question right. Sorry that I was a little bit confused yesterday. Now I have corrected the code by following your suggestion and mixing two pieces of code in the question together:

    Vector3f toScreenVector = new Vector3f(0, 0, 1);
    
    Transform3D t3d = new Transform3D();
    canvas.getImagePlateToVworld(t3d);
    t3d.transform(toScreenVector);
    
    tg.getTransform(t3d); //tg is Transform Group of all objects in a scene
    t3d.transform(toScreenVector);
    

    Thank you.