WebGL:从世界到屏幕坐标

时间:2014-07-30 14:22:29

标签: math screen webgl

我已阅读有关它的所有帖子,但无法完成它。 我在webgl中有一个3d场景,需要知道画布上的2d是否有3d点。

我有这个矩阵:

Perspective    = M4x4.makePerspective( fovy, aspect, znear, zfar );
CameraView     = M4x4.makeLookAt ( eye,  center, up );  
ViewPerspectiv = M4x4.mul(  Perspective , CameraView ); 

该模型有:

this.ModelMatrix  = M4x4.I;  
this.ModelMatrix  = M4x4.makeTranslate3( this.pos_x, this.pos_y, this.pos_z ); 

我已经尝试了所有的帖子 - 它总是使用:view和Perspective and point ..有时是转置......

有人可以使用我的可变名称,只是发布我的行? PLS !! 这是我尝试过的......但它不起作用:

var point = get2dPoint( point,  this.ModelMatrix,  ViewPerspectiv ,  canvas.clientWidth,  canvas.clientHeight ) ;
...
function get2dPointV2( point3D,  viewMatrix,  projectionMatrix,  width,  height) 
{ 
   var tmp1 = multiMat4ByVec3( viewMatrix ,  point3D );
   var clip = multiMat4ByVec3( projectionMatrix ,  tmp1 );

   var ndcSpace = new Array ( clip[0] / clip[3]   ,   clip[1] / clip[3]   , clip[2] / clip[3]  ); 

   ndcSpace[0] = ((ndcSpace[0] + 1.0) / 2.0) * width ;
   ndcSpace[1] = ((ndcSpace[1] + 1.0) / 2.0) * height ;

   return ndcSpace;

}

现在这已经让我失去了12小时......

Thanx给任何试图帮助的人。

UWI

我发现基本的approche很好,但我使用的矩阵需要调换,这就是所有的缺失! 无论如何,还是谢谢!

1 个答案:

答案 0 :(得分:0)

你的第一句话听起来像你要投影3D - > 2D。这就是Perspective-Matrix的作用。

您的代码正试图反转Perspective-Matrix,从2D投影 - > 3D。这用于例如当试图找出你用鼠标点击的位置时。

你想做什么?如果你真的想要投影2D - > 3D我可能会在明天找到解释/发布一些代码的时间。

相关问题