Skip to content

Commit

Permalink
fixes plus addition of inverse PMatrix3D functions needed
Browse files Browse the repository at this point in the history
for inversion of camera [[processing-js#122] [processing-js#123] state:peer-review-requested]
  • Loading branch information
aSydiK committed Mar 15, 2010
1 parent 1e2a018 commit 9e37616
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
3 changes: 3 additions & 0 deletions examples/seneca/begin&endCamera/begin&endCamera.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>beginCamera() and endCamera() test</h1>
<script src="../../../processing.js"></script>
<canvas datasrc="begin&endCamera.pjs" width="100" height="100"></canvas>
12 changes: 12 additions & 0 deletions examples/seneca/begin&endCamera/begin&endCamera.pjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
size(100, 100, P3D);
noFill();

beginCamera();
printCamera();
rotateX(-PI/6);
printCamera();
endCamera();

translate(50, 50, 0);
rotateY(PI/3);
box(45);
54 changes: 48 additions & 6 deletions processing.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@
var cam,
cameraInv,
forwardTransform,
reverseTransform,
modelView,
modelViewInv,
userMatrixStack,
Expand Down Expand Up @@ -1300,13 +1301,15 @@
p.translate = function translate(x, y, z) {
if (p.use3DContext) {
forwardTransform.translate(x, y, z);
reverseTransform.invTranslate(x, y, z);
} else {
curContext.translate(x, y);
}
};
p.scale = function scale( x, y, z ) {
if ( p.use3DContext ) {
forwardTransform.scale( x, y, z );
reverseTransform.invScale( x, y, z );
} else {
curContext.scale( x, y || x );
}
Expand All @@ -1329,6 +1332,7 @@

p.resetMatrix = function resetMatrix() {
forwardTransform.reset();
reverseTransform.reset();
};

p.applyMatrix = function applyMatrix(){
Expand All @@ -1345,23 +1349,32 @@
a[4], a[5], a[6], a[7],
a[8], a[9], a[10], a[11],
a[12], a[13], a[14], a[15] );
reverseTransform.invApply( a[0], a[1], a[2], a[3],
a[4], a[5], a[6], a[7],
a[8], a[9], a[10], a[11],
a[12], a[13], a[14], a[15] );

};

p.rotateX = function( angleInRadians ) {
forwardTransform.rotateX( angleInRadians );
reverseTransform.invRotateX( angleInRadians );
};

p.rotateZ = function( angleInRadians ) {
forwardTransform.rotateZ( angleInRadians );
reverseTransform.invRotateZ( angleInRadians );
};

p.rotateY = function( angleInRadians ) {
forwardTransform.rotateY( angleInRadians );
reverseTransform.invRotateY( angleInRadians );
};

p.rotate = function rotate( angleInRadians ) {
if (p.use3DContext) {
forwardTransform.rotateZ( angleInRadians );
reverseTransform.invRotateZ( angleInRadians );
}else { curContext.rotate( angleInRadians ); }
};

Expand Down Expand Up @@ -2801,6 +2814,8 @@

p.camera();
p.perspective();
forwardTransform = modelView;
reverseTransform = modelViewInv;

userMatrixStack = new PMatrix3DStack();
}
Expand Down Expand Up @@ -3204,6 +3219,7 @@
var s = p.sin( angle );
this.apply([1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0, 0, 0, 0, 1]);
},

rotateY: function( angle ){
var c = p.cos( angle );
var s = p.sin( angle );
Expand Down Expand Up @@ -3391,6 +3407,30 @@
p.nfs(this.elements[15], digits, 4) + "\n";

p.println(output);
},
invTranslate: function( tx, ty, tz) {
this.preApply( 1, 0, 0, -tx,
0, 1, 0, -ty,
0, 0, 1, -tz,
0, 0, 0, 1);
},
invRotateX: function( angle ){
var c = p.cos( -angle );
var s = p.sin( -angle );
this.preApply([1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0, 0, 0, 0, 1]);
},
invRotateY: function( angle ){
var c = p.cos( -angle );
var s = p.sin( -angle );
this.preApply([c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0, 0, 0, 0, 1]);
},
invRotateZ: function( angle ){
var c = p.cos( -angle );
var s = p.sin( -angle );
this.preApply([c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
},
invScale: function( x, y, z ){
this.preApply([1/x, 0, 0, 0, 0, 1/y, 0, 0, 0, 0, 1/z, 0, 0, 0, 0, 1]);
}
};

Expand Down Expand Up @@ -3487,24 +3527,26 @@
// Camera functions
////////////////////////////////////////////////////////////////////////////

p.beginCamera() = function beginCamera(){
p.beginCamera = function beginCamera(){
if( manipulatingCamera ){
throw("You cannot call beginCamera() again before calling endCamera()");
}
else{
manipulatingCamera = true;
forwardTransform = cameraInv;
reverseTransform = cam;
}
}

p.endCamera() = function endCamera(){
p.endCamera = function endCamera(){
if( !manipulatingCamera ){
throw("You cannot call endCamera() before calling beginCamera()");
}
else{
modelView.set( cam );
modelViewInv.set( cameraInv );
forwardTransform = modelView;
reverseTransform = modelViewInv;
manipulatingCamera = false;
}
}
Expand Down Expand Up @@ -3538,6 +3580,7 @@
cam.translate( -eyeX, -eyeY, -eyeZ );

cameraInv = new PMatrix3D();
cameraInv.reset();
cameraInv.invApply(x.x, x.y, x.z, 0,
y.x, y.y, y.z, 0,
z.x, z.y, z.z, 0,
Expand All @@ -3546,13 +3589,12 @@
cameraInv.translate( eyeX, eyeY, eyeZ );

modelView = new PMatrix3D();
modelView.set( cam );

forwardTransform = modelView;
modelView.set( cam );

modelViewInv = new PMatrix3D();
modelViewInv.set( cameraInv );
modelViewInv.set( cameraInv );
}

};

p.perspective = function perspective(fov, aspect, near, far) {
Expand Down

0 comments on commit 9e37616

Please sign in to comment.