program.js
// Sets up keyboard and mouse events
Sets up keyboard and mouse events
Sets up keyboard and mouse events
function initWindowEvents() {
    var sensitivity = 1;
    var newRotationMatrix;
	var newLightRotationMatrix;
    var mousePressed = false;
    var startX, startY;
    canvas.onmousedown = function(e) {
        mousePressed = true;
        startX = e.clientX;
        startY = e.clientY;
    }
    canvas.onmousemove = function(e) {
        if (mousePressed) {
            var axis = vec3(e.clientY - startY, e.clientX - startX, 0.0);
            var angle = length(axis) * sensitivity;
            if (angle > 0.0) {
				if(shiftPressed == 1) {
					newLightRotationMatrix = mult(rotate(angle, axis), lightRotationMatrix);
					var newLightPos = vec4();
					for(var i = 0; i < 4; i++) {
						for(var j = 0; j < 4; j++) {
							newLightPos[i] = newLightPos[i] + lightPosition[i] * newLightRotationMatrix[i][j];
						}
					}
					updateLightPosition(newLightPos);
				}
				else {
					var rotationM = rotate(angle, axis);
					newRotationMatrix = mult(rotate(angle, axis), rotationMatrix);
					updateModelView(mult(viewMatrix, newRotationMatrix));
				}
            }
        }
    }
    window.onmouseup = function(e) {
        mousePressed = false;
		if(newLightRotationMatrix) {
			lightRotationMatrix = newLightRotationMatrix;
		}
		newLightRotationMatrix = null;
        if (newRotationMatrix) {
            rotationMatrix = newRotationMatrix;
        }
        newRotationMatrix = null;
    }
    var speed = 0.1; 
    window.onkeydown = function(e) {
        if (e.keyCode === 190) { 
            viewMatrix = mult(translate(0,0,speed), viewMatrix);
        }
        else if (e.keyCode === 188) { 
            viewMatrix = mult(translate(0,0,-speed), viewMatrix);
        }
        else if (e.keyCode === 37) { 
            viewMatrix = mult(translate(speed,0,0), viewMatrix);
            e.preventDefault(); 
        }
        else if (e.keyCode === 38) { 
            viewMatrix = mult(translate(0,-speed,0), viewMatrix);
            e.preventDefault();
        }
        else if (e.keyCode === 39) { 
            viewMatrix = mult(translate(-speed,0,0), viewMatrix);
            e.preventDefault();
        }
        else if (e.keyCode === 40) { 
            viewMatrix = mult(translate(0,speed,0), viewMatrix);
            e.preventDefault();
        }
		else if(e.keyCode === 16) {
			shiftPressed = 1;
		}
        updateModelView(mult(viewMatrix, rotationMatrix));
        render();
    }
	window.onkeyup = function(e) {
		if(e.keyCode === 16) {
			shiftPressed = 0;
			startX = e.clientX;
			startY = e.clientY;
		}
	}
}
function initWindowEvents ( ) { var sensitivity = 1 ; var newRotationMatrix ; var newLightRotationMatrix ; var mousePressed = false ; var startX , startY ; canvas . onmousedown = function ( e ) { mousePressed = true ; startX = e . clientX ; startY = e . clientY ; } canvas . onmousemove = function ( e ) { if ( mousePressed ) { var axis = vec3 ( e . clientY - startY , e . clientX - startX , 0.0 ) ; var angle = length ( axis ) * sensitivity ; if ( angle > 0.0 ) { if ( shiftPressed == 1 ) { newLightRotationMatrix = mult ( rotate ( angle , axis ) , lightRotationMatrix ) ; var newLightPos = vec4 ( ) ; for ( var i = 0 ; i < 4 ; i ++ ) { for ( var j = 0 ; j < 4 ; j ++ ) { newLightPos [ i ] = newLightPos [ i ] + lightPosition [ i ] * newLightRotationMatrix [ i ] [ j ] ; } } updateLightPosition ( newLightPos ) ; } else { var rotationM = rotate ( angle , axis ) ; newRotationMatrix = mult ( rotate ( angle , axis ) , rotationMatrix ) ; updateModelView ( mult ( viewMatrix , newRotationMatrix ) ) ; } } } } window . onmouseup = function ( e ) { mousePressed = false ; if ( newLightRotationMatrix ) { lightRotationMatrix = newLightRotationMatrix ; } newLightRotationMatrix = null ; if ( newRotationMatrix ) { rotationMatrix = newRotationMatrix ; } newRotationMatrix = null ; } var speed = 0.1 ; window . onkeydown = function ( e ) { if ( e . keyCode === 190 ) { viewMatrix = mult ( translate ( 0 , 0 , speed ) , viewMatrix ) ; } else if ( e . keyCode === 188 ) { viewMatrix = mult ( translate ( 0 , 0 , - speed ) , viewMatrix ) ; } else if ( e . keyCode === 37 ) { viewMatrix = mult ( translate ( speed , 0 , 0 ) , viewMatrix ) ; e . preventDefault ( ) ; } else if ( e . keyCode === 38 ) { viewMatrix = mult ( translate ( 0 , - speed , 0 ) , viewMatrix ) ; e . preventDefault ( ) ; } else if ( e . keyCode === 39 ) { viewMatrix = mult ( translate ( - speed , 0 , 0 ) , viewMatrix ) ; e . preventDefault ( ) ; } else if ( e . keyCode === 40 ) { viewMatrix = mult ( translate ( 0 , speed , 0 ) , viewMatrix ) ; e . preventDefault ( ) ; } else if ( e . keyCode === 16 ) { shiftPressed = 1 ; } updateModelView ( mult ( viewMatrix , rotationMatrix ) ) ; render ( ) ; } window . onkeyup = function ( e ) { if ( e . keyCode === 16 ) { shiftPressed = 0 ; startX = e . clientX ; startY = e . clientY ; } } }
Sets up keyboard and mouse events
Sets up keyboard and mouse events
// Affects how much the camera moves when the mouse is dragged. // Additional rotation caused by an active drag. // Whether or not the mouse button is currently being held down for a drag. // The place where a mouse drag was started. // A mouse drag started. // Remember where the mouse drag started. // Handle a mouse drag by constructing an axis-angle rotation matrix /*if(shiftReleased == 1) {
				startX = e.clientX;
				startY = e.clientY;
			}*/ // Update the temporary rotation matrix // Update the light position matrix. // Update the model-view matrix. // A mouse drag ended. // "Lock" the temporary rotation as the current rotation matrix. // Affects how fast the camera pans and "zooms" // '>' key // "Zoom" in // '<' key // "Zoom" out // Left key // Pan left // Prevent the page from scrolling, which is the default behavior for the arrow keys // Up key // Pan up // Prevent the page from scrolling, which is the default behavior for the arrow keys // Right key // Pan right // Prevent the page from scrolling, which is the default behavior for the arrow keys // Down key // Pan down  // Prevent the page from scrolling, which is the default behavior for the arrow keys // Update the model-view matrix and render.