<p>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:</p>

<pre><code>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);
</code></pre>

<p>Then I tried something like this too:</p>

<pre><code>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();
</code></pre>

<p>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?</p>

[{'answer_id': 40666, 'author': 'caramelcarrot', 'author_id': 3877, 'author_profile': 'https://Stackoverflow.com/users/3877', 'pm_score': 2, 'selected': False, 'text': '<p>If I get this right, you want a vector that is normal to the screen plane, but in world coordinates?</p>\n\n<p>In that case you want to <code>INVERT</code> the transformation from <code>World -&gt; Screen and do Screen -&gt; World</code> of <code>(0,0,-1)</code> or <code>(0,0,1)</code> depending on which axis the screen points down.</p>\n\n<p>Since the ModelView matrix is just a rotation matrix (ignoring the homogeneous transformation part), you can simply pull this out by taking the transpose of the rotational part, or simple reading in  the bottom row - as this transposes onto the <code>Z</code> coordinate column under transposition.</p>\n'}, {'answer_id': 41231, 'author': 'puri', 'author_id': 3388, 'author_profile': 'https://Stackoverflow.com/users/3388', 'pm_score': 1, 'selected': True, 'text': '<p>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:</p>\n\n<pre><code>Vector3f toScreenVector = new Vector3f(0, 0, 1);\n\nTransform3D t3d = new Transform3D();\ncanvas.getImagePlateToVworld(t3d);\nt3d.transform(toScreenVector);\n\ntg.getTransform(t3d); //tg is Transform Group of all objects in a scene\nt3d.transform(toScreenVector);\n</code></pre>\n\n<p>Thank you.</p>\n'}]
['https://Stackoverflow.com/questions/40028', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3388/']