I have camera rotating by dragging mouse.
Im trying to add reset button that restart the scene to initial view.
The rotating camera works as expected but I can't figure out how to reset it.
The scene rotating in weird directions, not as expected.
I have 2 vars - absX
, absY
which hold the overall change angle value from the init mode.
when reset applied:
mat4.rotate(getRotationCameraMat, degToRad(-absX/10), [0, 1, 0]);
mat4.rotate(getRotationCameraMat, degToRad(-absY/10), [1, 0, 0]);
absX=0;
absY=0;
apply the rotate:
mat4.multiply(mvMatrix, cameraRotationMat);
the camera code:
var mouseDown = false;
var prevMouseX = null;
var prevMouseY = null;
var cameraRotationMat = mat4.create();
mat4.identity(cameraRotationMat);
function handleMouseDown(event){
mouseDown = true;
prevMouseX = event.clientX;
prevMouseY = event.clientY;
}
function handleMouseUp(event){
mouseDown = false;
}
var absX=0;
var absY=0;
function handleMouseMove(event){
if (!mouseDown){
return;
}
var currentX = event.clientX;
var currentY = event.clientY;
var deltaX = currentX - prevMouseX;
absX+=deltaX;
var newRotationMatrix = mat4.create();
mat4.identity(newRotationMatrix);
mat4.rotate(newRotationMatrix, degToRad(deltaX/10), [0, 1, 0]);
var deltaY = currentY-prevMouseY;
absY+=deltaY;
mat4.rotate(newRotationMatrix, degToRad(deltaY/10), [1, 0, 0]);
mat4.multiply(newRotationMatrix, cameraRotationMat,cameraRotationMat);
prevMouseX = currentX;
prevMouseY = currentY;
}