Skip to content

Commit

Permalink
Refactor code that assumes viewport is the entire canvas.
Browse files Browse the repository at this point in the history
  • Loading branch information
bagnell committed Mar 23, 2016
1 parent 1bc2db6 commit 1063c83
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions Source/Scene/SceneTransforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ define([
var actualPositionScratch = new Cartesian4(0, 0, 0, 1);
var positionCC = new Cartesian4();
var viewProjectionScratch = new Matrix4();
var scratchViewport = new BoundingRectangle();

/**
* Transforms a position in WGS84 coordinates to window coordinates. This is commonly used to place an
Expand Down Expand Up @@ -64,7 +65,8 @@ define([
//>>includeEnd('debug');

// Transform for 3D, 2D, or Columbus view
var actualPosition = SceneTransforms.computeActualWgs84Position(scene.frameState, position, actualPositionScratch);
var frameState = scene.frameState;
var actualPosition = SceneTransforms.computeActualWgs84Position(frameState, position, actualPositionScratch);

if (!defined(actualPosition)) {
return undefined;
Expand All @@ -79,8 +81,17 @@ define([
return undefined;
}

result = SceneTransforms.clipToGLWindowCoordinates(scene, positionCC, result);
result.y = scene.canvas.clientHeight - result.y;
// Assuming viewport takes up the entire canvas...
var canvas = scene.canvas;
var viewport = scratchViewport;
viewport.x = 0;
viewport.y = 0;
viewport.width = canvas.clientWidth;
viewport.height = canvas.clientHeight;

result = SceneTransforms.clipToGLWindowCoordinates(viewport, positionCC, result);
result.y = canvas.clientHeight - result.y;

return result;
};

Expand Down Expand Up @@ -130,7 +141,10 @@ define([
return undefined;
}

return SceneTransforms.clipToDrawingBufferCoordinates(scene, positionCC, result);
// Assuming viewport takes up the entire canvas...
var viewport = BoundingRectangle.clone(scene._passState.viewport, scratchViewport);

return SceneTransforms.clipToDrawingBufferCoordinates(viewport, positionCC, result);
};

var projectedPosition = new Cartesian3();
Expand Down Expand Up @@ -173,24 +187,17 @@ define([

var positionNDC = new Cartesian3();
var positionWC = new Cartesian3();
var viewport = new BoundingRectangle();
var viewportTransform = new Matrix4();

/**
* @private
*/
SceneTransforms.clipToGLWindowCoordinates = function(scene, position, result) {
var canvas = scene.canvas;

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

// Assuming viewport takes up the entire canvas...
viewport.width = canvas.clientWidth;
viewport.height = canvas.clientHeight;
Matrix4.computeViewportTransformation(viewport, 0.0, 1.0, viewportTransform);

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

return Cartesian2.fromCartesian3(positionWC, result);
Expand All @@ -199,15 +206,12 @@ define([
/**
* @private
*/
SceneTransforms.clipToDrawingBufferCoordinates = function(scene, position, result) {
SceneTransforms.clipToDrawingBufferCoordinates = function(viewport, position, result) {
// Perspective divide to transform from clip coordinates to normalized device coordinates
Cartesian3.divideByScalar(position, position.w, positionNDC);

// Assuming viewport takes up the entire canvas...
BoundingRectangle.clone(scene._passState.viewport, viewport);
Matrix4.computeViewportTransformation(viewport, 0.0, 1.0, viewportTransform);

// 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);
Expand Down

0 comments on commit 1063c83

Please sign in to comment.