Skip to content

Commit

Permalink
Update debug camera for orthographic projections.
Browse files Browse the repository at this point in the history
  • Loading branch information
bagnell committed Mar 9, 2017
1 parent 620c090 commit b0f06c5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
61 changes: 49 additions & 12 deletions Source/Scene/DebugCameraPrimitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,8 @@ define([
}

if (!defined(this._outlinePrimitive)) {
var view = this._camera.viewMatrix;
var projection = this._camera.frustum.projectionMatrix;
var viewProjection = Matrix4.multiply(projection, view, scratchMatrix);
var inverseViewProjection = Matrix4.inverse(viewProjection, scratchMatrix);
var camera = this._camera;
var frustum = camera.frustum;

var frustumSplits = frameState.frustumSplits;
var numFrustums = frustumSplits.length - 1;
Expand All @@ -135,20 +133,59 @@ define([
numFrustums = 1;
}

var view = this._camera.viewMatrix;
var inverseView;
var inverseViewProjection;
if (defined(camera.frustum.fovy)) {
var projection = this._camera.frustum.projectionMatrix;
var viewProjection = Matrix4.multiply(projection, view, scratchMatrix);
inverseViewProjection = Matrix4.inverse(viewProjection, scratchMatrix);
} else {
inverseView = Matrix4.inverseTransformation(view, scratchMatrix);
}


var positions = new Float64Array(3 * 4 * (numFrustums + 1));
var f;
for (f = 0; f < numFrustums + 1; ++f) {
for (var i = 0; i < 4; ++i) {
var corner = Cartesian4.clone(frustumCornersNDC[i], scratchFrustumCorners[i]);

Matrix4.multiplyByVector(inverseViewProjection, corner, corner);
Cartesian3.divideByScalar(corner, corner.w, corner); // Handle the perspective divide
Cartesian3.subtract(corner, this._camera.positionWC, corner);
Cartesian3.normalize(corner, corner);

var fac = Cartesian3.dot(this._camera.directionWC, corner);
Cartesian3.multiplyByScalar(corner, frustumSplits[f] / fac, corner);
Cartesian3.add(corner, this._camera.positionWC, corner);
var worldCoords;
if (!defined(inverseViewProjection)) {
if (defined(frustum._offCenterFrustum)) {
frustum = frustum._offCenterFrustum;
}

var near;
var far;
if (f === numFrustums) {
near = frustumSplits[f - 1];
far = frustumSplits[f];
} else {
near = frustumSplits[f];
far = frustumSplits[f + 1];
}
corner.x = (corner.x * (frustum.right - frustum.left) + frustum.left + frustum.right) * 0.5;
corner.y = (corner.y * (frustum.top - frustum.bottom) + frustum.bottom + frustum.top) * 0.5;
corner.z = (corner.z * (near - far) - near - far) * 0.5;
corner.w = 1.0;

worldCoords = Matrix4.multiplyByVector(inverseView, corner, corner);
} else {
corner = Matrix4.multiplyByVector(inverseViewProjection, corner, corner);

// Reverse perspective divide
var w = 1.0 / corner.w;
Cartesian3.multiplyByScalar(corner, w, corner);

Cartesian3.subtract(corner, this._camera.positionWC, corner);
Cartesian3.normalize(corner, corner);

var fac = Cartesian3.dot(this._camera.directionWC, corner);
Cartesian3.multiplyByScalar(corner, frustumSplits[f] / fac, corner);
Cartesian3.add(corner, this._camera.positionWC, corner);
}

positions[12 * f + i * 3] = corner.x;
positions[12 * f + i * 3 + 1] = corner.y;
Expand Down
14 changes: 0 additions & 14 deletions Source/Scene/SceneTransforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,20 +283,6 @@ define([
return Cartesian2.fromCartesian3(positionWC, result);
};

/**
* @private
*/
SceneTransforms.clipToDrawingBufferCoordinates = function(viewport, position, result) {
// Perspective divide to transform from clip coordinates to normalized device coordinates
Cartesian3.divideByScalar(position, position.w, positionNDC);

// Viewport transform to transform from clip coordinates to drawing buffer coordinates
Matrix4.computeViewportTransformation(viewport, 0.0, 1.0, viewportTransform);
Matrix4.multiplyByPoint(viewportTransform, positionNDC, positionWC);

return Cartesian2.fromCartesian3(positionWC, result);
};

/**
* @private
*/
Expand Down
8 changes: 4 additions & 4 deletions Source/Scene/Sun.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ define([
*
* @example
* scene.sun = new Cesium.Sun();
*
*
* @see Scene#sun
*/
function Sun() {
Expand Down Expand Up @@ -299,11 +299,11 @@ define([
positionEC.w = 1;

var positionCC = Matrix4.multiplyByVector(projMatrix, positionEC, scratchCartesian4);
var positionWC = SceneTransforms.clipToDrawingBufferCoordinates(passState.viewport, positionCC, scratchPositionWC);
var positionWC = SceneTransforms.clipToGLWindowCoordinates(passState.viewport, positionCC, scratchPositionWC);

positionEC.x = CesiumMath.SOLAR_RADIUS;
var limbCC = Matrix4.multiplyByVector(projMatrix, positionEC, scratchCartesian4);
var limbWC = SceneTransforms.clipToDrawingBufferCoordinates(passState.viewport, limbCC, scratchLimbWC);
var limbWC = SceneTransforms.clipToGLWindowCoordinates(passState.viewport, limbCC, scratchLimbWC);

this._size = Math.ceil(Cartesian2.magnitude(Cartesian2.subtract(limbWC, positionWC, scratchCartesian4)));
this._size = 2.0 * this._size * (1.0 + 2.0 * this._glowLengthTS);
Expand Down Expand Up @@ -340,7 +340,7 @@ define([
*
* @example
* sun = sun && sun.destroy();
*
*
* @see Sun#isDestroyed
*/
Sun.prototype.destroy = function() {
Expand Down

0 comments on commit b0f06c5

Please sign in to comment.