What's the correct unproject function to convert NDC coord to world coord.

51 views
Skip to first unread message

pangwa

unread,
Nov 10, 2011, 4:00:45 AM11/10/11
to PhiloGL
Hi there,

I'm implementing the ray picking algorithm and writes a function to
convert the NDC coord to world coord. Apprent I got a wrong result.

Below is the unproject function I wrote:
PhiloGL.unproject = function (pt, camera)
{
return camera.view.invert().mulMat4 (camera.projection.invert
()).mulVec3 (pt);
};

//
// I set the canvas width = height = 840,
//
camera = new PhiloGL.Camera (60, 1, 0.5, 100000);
camera.position = [0, 0, 30];
camera.target = [0, 0, 0];

And I drew a standard cube at the origin with the scale to 10.

var ndcx = e.x * 2 / this.canvas.width;
var ndcy = e.y * 2 / this.canvas.height;
var origin = PhiloGL.unproject ([ndcx, ndcy, -1.0], this.camera);

The result of origin was apprantly wrong here, for e( -302, 247), I
got the
ndcx, y, z as -0.41514232754707336, 0.3395369350910187, 59

It's apprantly wrong because the ray (to camera.position (0, 0, 30)
intersect with a cube at (0.32707035541534424, -0.25786416283969227,
10) which is apprantly wrong as it should at somewhere (-7, -8, 10).

I believe the ray code is correct as I can get the correct result if I
hard code the ray's origin/direction.

I think the camera things in PhiloGL is in a mess..... Anybody can
help?

Thanks in advance!

pangwa

unread,
Nov 10, 2011, 8:49:06 PM11/10/11
to PhiloGL
Ok, I found the issue finally...

It's because the implementation of funciton Matrix4.mulVec3 is
wrong...

Nicolas Garcia Belmonte

unread,
Nov 17, 2011, 12:39:07 PM11/17/11
to phi...@googlegroups.com
Damn, will merge your fixes ASAP!

--
Nicolas Garcia Belmonte - http://philogb.github.com/

Reply all
Reply to author
Forward
0 new messages