Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Matrix4.fromCamera #3946

Merged
merged 4 commits into from
May 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Change Log
* Added `terrainProviderChanged` event to `Scene` and `Globe`
* Added `CullingVolume.fromBoundingSphere`.
* Added `debugShowShadowVolume` to `GroundPrimitive`.
* Fixed issue where `Matrix4.fromCamera` was taking eye/target instead of position/direction. [3927](https://github.com/AnalyticalGraphicsInc/cesium/issues/3927)

### 1.21 - 2016-05-02

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to Cesiu
* [Thomas Pedbereznak](https://github.com/TomPed)
* [Rob Taglang](https://github.com/lasalvavida)
* [Todd Smith] (https://github.com/tsmith717)
* [Josh Becker] (https://github.com/JoshuaStorm)
* [NICTA](http://www.nicta.com.au/)
* [Chris Cooper](https://github.com/chris-cooper)
* [Kevin Ring](https://github.com/kring)
Expand Down
71 changes: 35 additions & 36 deletions Source/Core/Matrix4.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ define([
};

var fromCameraF = new Cartesian3();
var fromCameraS = new Cartesian3();
var fromCameraR = new Cartesian3();
var fromCameraU = new Cartesian3();

/**
Expand All @@ -564,57 +564,57 @@ define([
}
//>>includeEnd('debug');

var eye = camera.eye;
var target = camera.target;
var position = camera.position;
var direction = camera.direction;
var up = camera.up;

//>>includeStart('debug', pragmas.debug);
if (!defined(eye)) {
throw new DeveloperError('camera.eye is required.');
if (!defined(position)) {
throw new DeveloperError('camera.position is required.');
}
if (!defined(target)) {
throw new DeveloperError('camera.target is required.');
if (!defined(direction)) {
throw new DeveloperError('camera.direction is required.');
}
if (!defined(up)) {
throw new DeveloperError('camera.up is required.');
}
//>>includeEnd('debug');

Cartesian3.normalize(Cartesian3.subtract(target, eye, fromCameraF), fromCameraF);
Cartesian3.normalize(Cartesian3.cross(fromCameraF, up, fromCameraS), fromCameraS);
Cartesian3.normalize(Cartesian3.cross(fromCameraS, fromCameraF, fromCameraU), fromCameraU);
Cartesian3.normalize(direction, fromCameraF);
Cartesian3.normalize(Cartesian3.cross(fromCameraF, up, fromCameraR), fromCameraR);
Cartesian3.normalize(Cartesian3.cross(fromCameraR, fromCameraF, fromCameraU), fromCameraU);

var sX = fromCameraS.x;
var sY = fromCameraS.y;
var sZ = fromCameraS.z;
var sX = fromCameraR.x;
var sY = fromCameraR.y;
var sZ = fromCameraR.z;
var fX = fromCameraF.x;
var fY = fromCameraF.y;
var fZ = fromCameraF.z;
var uX = fromCameraU.x;
var uY = fromCameraU.y;
var uZ = fromCameraU.z;
var eyeX = eye.x;
var eyeY = eye.y;
var eyeZ = eye.z;
var t0 = sX * -eyeX + sY * -eyeY+ sZ * -eyeZ;
var t1 = uX * -eyeX + uY * -eyeY+ uZ * -eyeZ;
var t2 = fX * eyeX + fY * eyeY + fZ * eyeZ;

//The code below this comment is an optimized
//version of the commented lines.
//Rather that create two matrices and then multiply,
//we just bake in the multiplcation as part of creation.
//var rotation = new Matrix4(
// sX, sY, sZ, 0.0,
// uX, uY, uZ, 0.0,
// -fX, -fY, -fZ, 0.0,
// 0.0, 0.0, 0.0, 1.0);
//var translation = new Matrix4(
// 1.0, 0.0, 0.0, -eye.x,
// 0.0, 1.0, 0.0, -eye.y,
// 0.0, 0.0, 1.0, -eye.z,
// 0.0, 0.0, 0.0, 1.0);
//return rotation.multiply(translation);
var positionX = position.x;
var positionY = position.y;
var positionZ = position.z;
var t0 = sX * -positionX + sY * -positionY+ sZ * -positionZ;
var t1 = uX * -positionX + uY * -positionY+ uZ * -positionZ;
var t2 = fX * positionX + fY * positionY + fZ * positionZ;

// The code below this comment is an optimized
// version of the commented lines.
// Rather that create two matrices and then multiply,
// we just bake in the multiplcation as part of creation.
// var rotation = new Matrix4(
// sX, sY, sZ, 0.0,
// uX, uY, uZ, 0.0,
// -fX, -fY, -fZ, 0.0,
// 0.0, 0.0, 0.0, 1.0);
// var translation = new Matrix4(
// 1.0, 0.0, 0.0, -position.x,
// 0.0, 1.0, 0.0, -position.y,
// 0.0, 0.0, 1.0, -position.z,
// 0.0, 0.0, 0.0, 1.0);
// return rotation.multiply(translation);
if (!defined(result)) {
return new Matrix4(
sX, sY, sZ, t0,
Expand All @@ -639,7 +639,6 @@ define([
result[14] = t2;
result[15] = 1.0;
return result;

};

/**
Expand Down
20 changes: 10 additions & 10 deletions Specs/Core/Matrix4Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ defineSuite([
it('fromCamera works without a result parameter', function() {
var expected = Matrix4.IDENTITY;
var returnedResult = Matrix4.fromCamera({
eye : Cartesian3.ZERO,
target : Cartesian3.negate(Cartesian3.UNIT_Z, new Cartesian3()),
position : Cartesian3.ZERO,
direction : Cartesian3.negate(Cartesian3.UNIT_Z, new Cartesian3()),
up : Cartesian3.UNIT_Y
});
expect(expected).toEqual(returnedResult);
Expand All @@ -340,8 +340,8 @@ defineSuite([
var expected = Matrix4.IDENTITY;
var result = new Matrix4();
var returnedResult = Matrix4.fromCamera({
eye : Cartesian3.ZERO,
target : Cartesian3.negate(Cartesian3.UNIT_Z, new Cartesian3()),
position : Cartesian3.ZERO,
direction : Cartesian3.negate(Cartesian3.UNIT_Z, new Cartesian3()),
up : Cartesian3.UNIT_Y
}, result);
expect(returnedResult).toBe(result);
Expand Down Expand Up @@ -1096,19 +1096,19 @@ defineSuite([
}).toThrowDeveloperError();
});

it('fromCamera throws without eye', function() {
it('fromCamera throws without position', function() {
expect(function() {
Matrix4.fromCamera({
target : Cartesian3.negate(Cartesian3.UNIT_Z, new Cartesian3()),
direction : Cartesian3.negate(Cartesian3.UNIT_Z, new Cartesian3()),
up : Cartesian3.UNIT_Y
});
}).toThrowDeveloperError();
});

it('fromCamera throws without target', function() {
it('fromCamera throws without direction', function() {
expect(function() {
Matrix4.fromCamera({
eye : Cartesian3.ZERO,
position : Cartesian3.ZERO,
up : Cartesian3.UNIT_Y
});
}).toThrowDeveloperError();
Expand All @@ -1117,8 +1117,8 @@ defineSuite([
it('fromCamera throws without up', function() {
expect(function() {
Matrix4.fromCamera({
eye : Cartesian3.ZERO,
target : Cartesian3.negate(Cartesian3.UNIT_Z, new Cartesian3())
position : Cartesian3.ZERO,
direction : Cartesian3.negate(Cartesian3.UNIT_Z, new Cartesian3())
});
}).toThrowDeveloperError();
});
Expand Down
16 changes: 8 additions & 8 deletions Specs/Core/TransformsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,8 @@ defineSuite([

it('pointToGLWindowCoordinates works at the center', function() {
var view = Matrix4.fromCamera({
eye : Cartesian3.multiplyByScalar(Cartesian3.UNIT_X, 2.0, new Cartesian3()),
target : Cartesian3.ZERO,
position : Cartesian3.multiplyByScalar(Cartesian3.UNIT_X, 2.0, new Cartesian3()),
direction : Cartesian3.negate(Cartesian3.UNIT_X, new Cartesian3()),
up : Cartesian3.UNIT_Z
});
var mvpMatrix = Matrix4.multiply(perspective, view, new Matrix4());
Expand All @@ -621,8 +621,8 @@ defineSuite([

it('pointToGLWindowCoordinates works with a result parameter', function() {
var view = Matrix4.fromCamera({
eye : Cartesian3.multiplyByScalar(Cartesian3.UNIT_X, 2.0, new Cartesian3()),
target : Cartesian3.ZERO,
position : Cartesian3.multiplyByScalar(Cartesian3.UNIT_X, 2.0, new Cartesian3()),
direction : Cartesian3.negate(Cartesian3.UNIT_X, new Cartesian3()),
up : Cartesian3.UNIT_Z
});
var mvpMatrix = Matrix4.multiply(perspective, view, new Matrix4());
Expand Down Expand Up @@ -658,8 +658,8 @@ defineSuite([

it('pointToWindowCoordinates works at the center', function() {
var view = Matrix4.fromCamera({
eye : Cartesian3.multiplyByScalar(Cartesian3.UNIT_X, 2.0, new Cartesian3()),
target : Cartesian3.ZERO,
position : Cartesian3.multiplyByScalar(Cartesian3.UNIT_X, 2.0, new Cartesian3()),
direction : Cartesian3.negate(Cartesian3.UNIT_X, new Cartesian3()),
up : Cartesian3.UNIT_Z
});
var mvpMatrix = Matrix4.multiply(perspective, view, new Matrix4());
Expand All @@ -671,8 +671,8 @@ defineSuite([

it('pointToWindowCoordinates works with a result parameter', function() {
var view = Matrix4.fromCamera({
eye : Cartesian3.multiplyByScalar(Cartesian3.UNIT_X, 2.0, new Cartesian3()),
target : Cartesian3.ZERO,
position : Cartesian3.multiplyByScalar(Cartesian3.UNIT_X, 2.0, new Cartesian3()),
direction : Cartesian3.negate(Cartesian3.UNIT_X, new Cartesian3()),
up : Cartesian3.UNIT_Z
});
var mvpMatrix = Matrix4.multiply(perspective, view, new Matrix4());
Expand Down