Skip to content

Commit

Permalink
Add reset button for the camera.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephomi committed Oct 2, 2013
1 parent 4820022 commit 0f1c4d6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
16 changes: 13 additions & 3 deletions gui/gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ function Gui(sculptgl)
this.resetBg_ = this.resetBackground; //reset background
this.importBg_ = this.importBackground; //import background image

//functions
this.resetCamera_ = this.resetCamera; //reset camera position and rotation

//misc
this.dummyFunc_ = function () {}; //empty function... stupid trick to get a simple button in dat.gui
}
Expand Down Expand Up @@ -100,6 +103,7 @@ Gui.prototype = {

//Camera fold
var cameraFold = gui.addFolder('Camera');
cameraFold.add(this, 'resetCamera_').name('Reset');
var optionsCameraMode = {
'Spherical': Camera.mode.SPHERICAL,
'Plane': Camera.mode.PLANE
Expand Down Expand Up @@ -130,8 +134,7 @@ Gui.prototype = {
var ctrlPivot = cameraFold.add(main.camera_, 'usePivot_').name('Picking pivot');
ctrlPivot.onChange(function ()
{
if (main.mesh_)
main.camera_.reset(main.mesh_);
main.camera_.toggleUsePivot();
main.render();
});
cameraFold.open();
Expand Down Expand Up @@ -277,6 +280,13 @@ Gui.prototype = {
}
},

/** Immort background */
resetCamera: function ()
{
this.sculptgl_.camera_.reset();
this.sculptgl_.render();
},

/** Immort background */
importBackground: function ()
{
Expand Down Expand Up @@ -347,4 +357,4 @@ Gui.prototype = {
}
Export.exportSketchfab(this.sculptgl_.mesh_, this.keySketchfab_);
}
};
};
31 changes: 17 additions & 14 deletions math3d/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,15 @@ Camera.prototype = {
updateView: function ()
{
var view = this.view_;
var tx = this.usePivot_ ? 0 : this.transX_;
var ty = this.usePivot_ ? 0 : this.transY_;
var tx = this.transX_;
var ty = this.transY_;
if (this.type_ === Camera.projType.PERSPECTIVE)
mat4.lookAt(view, [tx, ty, this.zoom_], [tx, ty, 0], [0, 1, 0]);
else
mat4.lookAt(view, [tx, ty, 1000], [tx, ty, 0], [0, 1, 0]);
mat4.mul(view, view, mat4.fromQuat(mat4.create(), this.rot_));
if (this.usePivot_)
{
var center = this.center_;
mat4.translate(view, view, [-center[0], -center[1], -center[2]]);
}
var center = this.center_;
mat4.translate(view, view, [-center[0], -center[1], -center[2]]);
},

/** Update projection matrix */
Expand Down Expand Up @@ -155,16 +152,22 @@ Camera.prototype = {
return vec3.transformMat3(pos, pos, mat3.transpose(rot, rot));
},

/** Reset camera when we toggle usePivot */
toggleUsePivot: function ()
{
this.transX_ = 0;
this.transY_ = 0;
},

/** Reset camera */
reset: function (mesh)
reset: function ()
{
this.rot_ = quat.create();
this.zoom_ = 0;
this.center_ = [0, 0, 0];
this.transX_ = 0;
this.transY_ = 0;
var length = vec3.dist(mesh.octree_.aabbLoose_.max_, mesh.octree_.aabbLoose_.min_);
this.speed_ = length;
this.speed_ = Mesh.globalScale_ * 1.4;
this.rot_ = quat.create();
this.center_ = [0, 0, 0];
this.zoom_ = 0;
this.zoom(-0.4);
},

Expand Down Expand Up @@ -210,4 +213,4 @@ Camera.prototype = {
var height = this.height_;
return [(vec[0] / w + 1) * this.width_ * 0.5, height - (vec[1] / w + 1) * height * 0.5, (vec[2] / w + 1) * 0.5];
}
};
};
2 changes: 1 addition & 1 deletion sculptgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ SculptGL.prototype = {

mesh.initMesh(this.textures_, this.shaders_);
mesh.moveTo([0, 0, 0]);
this.camera_.reset(mesh);
this.camera_.reset();
this.gui_.updateMesh(mesh);
this.render();
},
Expand Down

0 comments on commit 0f1c4d6

Please sign in to comment.