Permalink
Browse files

fixed perspective, added working ortho, changed variable names to agr…

…eed upon ones
  • Loading branch information...
1 parent e02fe51 commit 3599e8f40944a9660b322e80628eff343a4d6734 @aSydiK committed Feb 5, 2010
Showing with 106 additions and 80 deletions.
  1. +106 −80 processing.js
View
@@ -421,19 +421,19 @@
framesSinceLastFPS = 0;
//Camera Variables
- var cam = new Array( 16 ),
- camInv = new Array( 16 ),
- modelView = new Array( 16 ),
- modelViewInv = new Array( 16 ),
- projection = new Array( 16 ),
- frustumMode = false,
- cameraFOV = 60 * ( p.PI / 180 ),
- cameraX = curElement.width / 2,
- cameraY = curElement.height / 2,
- cameraZ = cameraY / Math.tan( cameraFOV / 2 ),
- cameraNear = cameraZ / 10,
- cameraFar = cameraZ * 10,
- cameraAspect = curElement.width / curElement.height;
+ var cam,
+ cameraInv,
+ modelView,
+ modelViewInv,
+ projection,
+ frustumMode = false,
+ cameraFOV = 60 * ( p.PI / 180 ),
+ cameraX = curElement.width / 2,
+ cameraY = curElement.height / 2,
+ cameraZ = cameraY / Math.tan( cameraFOV / 2 ),
+ cameraNear = cameraZ / 10,
+ cameraFar = cameraZ * 10,
+ cameraAspect = curElement.width / curElement.height;
var firstX, firstY, secondX, secondY, prevX, prevY;
@@ -2078,73 +2078,99 @@
// Camera functions
////////////////////////////////////////////////////////////////////////////
- p.camera = function camera(){
- if( arguments.length === 0 ){
- //in case canvas is resized
- cameraX = curElement.width / 2;
- cameraY = curElement.height / 2;
- cameraZ = cameraY / Math.tan( cameraFOV / 2 );
- p.camera( cameraX, cameraY, cameraZ,
- cameraX, cameraY, 0,
- 0, 1, 0 );
- }
- else{
- var a = arguments;
- var z = new p.PVector( a[ 0 ] - a[ 3 ], a[ 1 ] - a[ 4 ], a[ 2 ] - a[ 5 ] );
- var y = new p.PVector( a[ 6 ], a[ 7 ], a[ 8 ]);
- var transX, transY, transZ;
- z.normalize();
- var x = p.PVector.cross( y, z );
- y = p.PVector.cross( z, x );
- x.normalize();
- y.normalize();
- transX = a[ 0 ] * x.x + a[ 1 ] * x.y + a[ 2 ] * x.z;
- transY = a[ 0 ] * y.x + a[ 1 ] * y.y + a[ 2 ] * y.z;
- transZ = a[ 0 ] * z.x + a[ 1 ] * z.y + a[ 2 ] * z.z;
- cam = [ x.x, x.y, x.z, 0,
- y.x, y.y, y.z, 0,
- z.x, z.y, z.z, 0,
- -1*transX, -1*transY, -1*transZ, 1 ];
- camInv = [ x.x, x.y, x.z, 0,
- y.x, y.y, y.z, 0,
- z.x, z.y, z.z, 0,
- transX, transY, transZ, 1 ];
- modelView = cam;
- modelViewInv = camInv;
- }
- };
-
- p.perspective = function perspective(){
- if( arguments.length === 0 ){
- //in case canvas is resized
- cameraNear = cameraZ / 10;
- cameraFar = cameraZ * 10;
- cameraAspect = curElement.width / curElement.height;
- p.perspective( cameraFOV, cameraAspect, cameraNear, cameraFar );
- }
- else{
- var a = arguments;
- var yMax, yMin, xMax, xMin;
- yMax = a[ 2 ] * Math.tan( a[ 0 ] / 2 );
- yMin = -1 * yMax;
- xMax = yMax * a[ 1 ];
- xMin = yMin * a[ 1 ];
- p.frustum( xMin, xMax, yMin, yMax, a[ 2 ], a[ 3 ] );
- }
- };
-
- p.frustum = function frustum( left, right, bottom, top, near, far ){
- /* The following was the projection set within the java code:
- projection.set((2*znear)/(right-left), 0, (right+left)/(right-left), 0,
- 0, (2*znear)/(top-bottom), (top+bottom)/(top-bottom), 0,
- 0, 0, -(zfar+znear)/(zfar-znear), -(2*zfar*znear)/(zfar-znear),
- 0, 0, -1, 0);*/
- frustumMode = true;
- projection = [ (2*near)/(right-left), 0, (right+left)/(right-left), 0,
- 0, (2*near)/(top-bottom), (top+bottom)/(top-bottom), 0,
- 0, 0, -(far+near)/(far-near), -1,
- 0, 0, -(2*far*near)/(far-near), 0];
- };
+ p.camera = function camera(){
+ if( arguments.length === 0 ){
+ //in case canvas is resized
+ cameraX = curElement.width / 2;
+ cameraY = curElement.height / 2;
+ cameraZ = cameraY / Math.tan( cameraFOV / 2 );
+ p.camera( cameraX, cameraY, cameraZ,
+ cameraX, cameraY, 0,
+ 0, 1, 0 );
+ }
+ else{
+ var a = arguments;
+ var z = new p.PVector( a[ 0 ] - a[ 3 ], a[ 1 ] - a[ 4 ], a[ 2 ] - a[ 5 ] );
+ var y = new p.PVector( a[ 6 ], a[ 7 ], a[ 8 ]);
+ var transX, transY, transZ;
+ z.normalize();
+ var x = p.PVector.cross( y, z );
+ y = p.PVector.cross( z, x );
+ x.normalize();
+ y.normalize();
+ cam = new PMatrix3D();
+ cam.set( x.x, x.y, x.z, 0,
+ y.x, y.y, y.z, 0,
+ z.x, z.y, z.z, 0,
+ 0, 0, 0, 1 );
+ cam.translate( -a[ 0 ], -a[ 1 ], -a[ 2 ] );
+ cameraInv = new PMatrix3D();
+ cameraInv.set( x.x, x.y, x.z, 0,
+ y.x, y.y, y.z, 0,
+ z.x, z.y, z.z, 0,
+ 0, 0, 0, 1 );
+ cameraInv.translate( a[ 0 ], a[ 1 ], a[ 2 ] );
+ modelView = new PMatrix3D();
+ modelView.set( cam );
+ modelViewInv = new PMatrix3D();
+ modelViewInv.set( cameraInv );
+ }
+ };
+
+ p.perspective = function perspective(){
+ if( arguments.length === 0 ){
+ //in case canvas is resized\
+ cameraY = curElement.height / 2;
+ cameraZ = cameraY / Math.tan( cameraFOV / 2 );
+ cameraNear = cameraZ / 10;
+ cameraFar = cameraZ * 10;
+ cameraAspect = curElement.width / curElement.height;
+ p.perspective( cameraFOV, cameraAspect, cameraNear, cameraFar );
+ }
+ else{
+ var a = arguments;
+ var yMax, yMin, xMax, xMin;
+ yMax = a[ 2 ] * Math.tan( a[ 0 ] / 2 );
+ yMin = -1 * yMax;
+ xMax = yMax * a[ 1 ];
+ xMin = yMin * a[ 1 ];
+ p.frustum( xMin, xMax, yMin, yMax, a[ 2 ], a[ 3 ] );
+ }
+ };
+
+ p.frustum = function frustum( left, right, bottom, top, near, far ){
+ /* The following was the projection set within the java code:
+ projection.set((2*znear)/(right-left), 0, (right+left)/(right-left), 0,
+ 0, (2*znear)/(top-bottom), (top+bottom)/(top-bottom), 0,
+ 0, 0, -(zfar+znear)/(zfar-znear), -(2*zfar*znear)/(zfar-znear),
+ 0, 0, -1, 0);*/
+ frustumMode = true;
+ projection = new PMatrix3D();
+ projection.set( (2*near)/(right-left), 0, (right+left)/(right-left), 0,
+ 0, (2*near)/(top-bottom), (top+bottom)/(top-bottom), 0,
+ 0, 0, -(far+near)/(far-near), -1,
+ 0, 0, -(2*far*near)/(far-near), 0 );
+ };
+
+ p.ortho = function ortho(){
+ if( arguments.length === 0 )
+ p.ortho( 0, p.width, 0, p.height, -10, 10 );
+ else{
+ var a = arguments;
+ var x = 2 / ( a[ 1 ] - a[ 0 ] );
+ var y = 2 / ( a[ 3 ] - a[ 2 ] );
+ var z = -2 / ( a[ 5 ] - a[ 4 ] );
+ var tx = -( a[ 1 ] + a[ 0 ] ) / ( a[ 1 ] - a[ 0 ] );
+ var ty = -( a[ 3 ] + a[ 2 ] ) / ( a[ 3 ] - a[ 2 ] );
+ var tz = -( a[ 5 ] + a[ 4 ] ) / ( a[ 5 ] - a[ 4 ] );
+ projection = new PMatrix3D();
+ projection.set( x, 0, 0, 0,
+ 0, y, 0, 0,
+ 0, 0, z, 0,
+ tx, ty, tz, 1 );
+ frustumMode = false;
+ }
+ };
////////////////////////////////////////////////////////////////////////////

0 comments on commit 3599e8f

Please sign in to comment.