Permalink
Browse files

X.camera3D.unproject(x,y,z) is working.

  • Loading branch information...
Daniel Haehn
Daniel Haehn committed Aug 14, 2013
1 parent 7c69186 commit 9084ab0a49ee89c9bb8e37f9f6e5704246fbfb6b
Showing with 63 additions and 0 deletions.
  1. +5 −0 math/matrix.js
  2. +16 −0 visualization/camera.js
  3. +42 −0 visualization/camera3D.js
View
@@ -204,6 +204,10 @@ X.matrix.makeOrtho = goog.vec.Mat4.makeOrtho;
*/
X.matrix.multiply = goog.vec.Mat4.multMat;
/**
* @see goog.vec.Mat4.multVec4
*/
X.matrix.multiplyByVec4 = goog.vec.Mat4.multVec4;
/**
* @see goog.vec.Mat4.translate
@@ -248,6 +252,7 @@ goog.exportSymbol('X.matrix.determinant', X.matrix.determinant);
goog.exportSymbol('X.matrix.invert', X.matrix.invert);
goog.exportSymbol('X.matrix.multiply', X.matrix.multiply);
goog.exportSymbol('X.matrix.multiplyByVector', X.matrix.multiplyByVector);
goog.exportSymbol('X.matrix.multiplyByVec4', X.matrix.multiplyByVec4);
goog.exportSymbol('X.matrix.makePerspective', X.matrix.makePerspective);
goog.exportSymbol('X.matrix.makeFrustum', X.matrix.makeFrustum);
goog.exportSymbol('X.matrix.makeOrtho', X.matrix.makeOrtho);
View
@@ -95,6 +95,22 @@ X.camera = function(width, height) {
*/
this._up = new X.vector(0, 0, 1);
/**
* The viewport width.
*
* @type {!number}
* @protected
*/
this._width = width;
/**
* The viewport height.
*
* @type {!number}
* @protected
*/
this._height = height;
/**
* The view matrix.
*
View
@@ -118,4 +118,46 @@ X.camera3D.prototype.lookAt_ = function(cameraPosition, targetPoint) {
};
/**
* Unproject from screen space to a 3D ray.
*
* @param {number} x The x-coordinate on the viewport.
* @param {number} y The y-coordinate on the viewport.
* @return {!Float32Array} the resulting direction
*/
X.camera3D.prototype.unproject = function (x,y,z) {
if (!goog.isDefAndNotNull(z)) {
return [this.unproject(x,y,0), this.unproject(x,y,1)]
}
var _vp = [0, 0, this._width, this._height]
var _in = new Float32Array(4);
var _out = new Float32Array(4);
var _m = new Float32Array(16);
var _A = new Float32Array(16);
// compute projection x modelview
X.matrix.multiply(this._perspective, this._view, _A);
// now invert _A
X.matrix.invert(_A, _m);
// create normalized coordinates
_in[0] = (x-_vp[0])/_vp[2]*2.0-1.0;
_in[1] = ((y-_vp[1])/_vp[3]*2.0-1.0)*-1; // invert y
_in[2] = 2.0*z-1.0;
_in[3] = 1.0;
X.matrix.multiplyByVec4(_m, _in, _out);
_out[3] = 1.0/_out[3];
_out[0] = _out[0]*_out[3] + ren3d._center[0];
_out[1] = _out[1]*_out[3] + ren3d._center[1];
_out[2] = _out[2]*_out[3] + ren3d._center[2];
return _out;
};
goog.exportSymbol('X.camera3D', X.camera3D);
goog.exportSymbol('X.camera3D.prototype.unproject', X.camera3D.prototype.unproject);

0 comments on commit 9084ab0

Please sign in to comment.