The correct way to convert world coordinate to screen cooridinate..

34 views
Skip to first unread message

pangwa

unread,
Oct 31, 2011, 2:29:13 AM10/31/11
to PhiloGL
Hi,

I'm working on to converting the vertices of a model into screen
cooridinate while picking.
I use below code to convert the vertices
(camera.projection*camera.view * p), but apprently it doesn't work
correctly... I checked the verterx shader's code and it looks like
using the sampe algorithm to do the math. But I don't know why I got
the wrong result... Any idea?

onClick: function (e, model){
....
var camera = this.camera;
var mWorld = camera.view.mulMat4 (model.matrix);
var mPv1 = camera.projection.mulMat4 (mWorld);
var dist1 = PhiloGL.Vec3.distTo (camera.position,
camera.target);
var width = 2 * Math.tan ((Math.PI * camera.fov /
360)) * dist1;
var scaleDim = this.canvas.width / width;
//mPv = mPv1.scale (this.canvas.width / width,
this.canvas.height / width, 1);
faces2 = faces.map (function (v){
return v.map (function (pt){
var pt = mPv1.mulVec3 (pt);
pt[0] *= scaleDim;
pt[1] *= scaleDim;
return pt;
});
});

pangwa

unread,
Oct 31, 2011, 2:29:51 AM10/31/11
to PhiloGL
BTW, the canas.width is equal to the canvas.height in my code..
Reply all
Reply to author
Forward
0 new messages