1 /**
  2  * potree.js 
  3  * http://potree.org
  4  *
  5  * Copyright 2012, Markus Sch�tz
  6  * Licensed under the GPL Version 2 or later.
  7  * - http://potree.org/wp/?page_id=7
  8  * - http://www.gnu.org/licenses/gpl-3.0.html
  9  *
 10  */
 11 
 12 /**
 13  * @class
 14  */
 15 function Viewport(canvas, camera) {
 16 	this.canvas = canvas;
 17 	this.camera = camera;
 18 	this.velocity = [0,0,0];
 19 	this.targetVelocity = [0,0,0];
 20 	this.targetVelocityMultiplicator = 20.0;
 21 	this.x = 0;
 22 	this.y = 0;
 23 	if (canvas != null) {
 24 		this.width = canvas.width;
 25 		this.height = canvas.height;
 26 	}
 27 	if(this.camera != null){
 28 		this.camera.aspectRatio = this.width / this.height;
 29 	}
 30 }
 31 
 32 Viewport.prototype.setCamera = function(camera) {
 33 	this.camera = camera;
 34 	this.camera.aspectRatio = this.width / this.height;
 35 };
 36 
 37 /**
 38  * 
 39  * @param canvas
 40  * @returns
 41  */
 42 Viewport.prototype.setCanvas = function(canvas) {
 43 	this.canvas = canvas;
 44 	if (canvas != null) {
 45 		this.width = canvas.width;
 46 		this.height = canvas.height;
 47 	}
 48 	if(this.camera != null){
 49 		this.camera.aspectRatio = this.width / this.height;
 50 	}
 51 };
 52 
 53 Viewport.prototype.setDimension = function(x, y, width, height){
 54 	this.x = 0;
 55 	this.y = 0;
 56 	this.width = width;
 57 	this.height = height;
 58 	if(this.camera != null){
 59 		this.camera.aspectRatio = this.width / this.height;
 60 	}
 61 };
 62 
 63 Viewport.prototype.addTime = function(time){
 64 	this.velocity[0] = 0.2 *this.velocity[0] + 0.8 * this.targetVelocity[0] * this.targetVelocityMultiplicator;
 65 	this.velocity[1] = 0.2 *this.velocity[1] + 0.8 * this.targetVelocity[1] * this.targetVelocityMultiplicator;
 66 	this.velocity[2] = 0.2 *this.velocity[2] + 0.8 * this.targetVelocity[2] * this.targetVelocityMultiplicator;
 67 	
 68 	var t = [this.velocity[0]* time, this.velocity[1]* time, this.velocity[2] * time];
 69 	
 70 	this.camera.translate(t[0], t[1], t[2]);
 71 };
 72 
 73 Viewport.prototype.invokeKeyDown = function(event){
 74 	if(event.which == 83 ){
 75 		//s
 76 		//this.camera.translate(0,0,1);
 77 		this.targetVelocity[2] = 1;
 78 	}else if(event.which == 87 ){
 79 		//w
 80 		//this.camera.translate(0,0,-1);
 81 		this.targetVelocity[2] = -1;
 82 	}else if(event.which == 68 ){
 83 		//d
 84 		//this.camera.translate(1,0,0);
 85 		this.targetVelocity[0] = 1;
 86 	}else if(event.which == 65 ){
 87 		//a
 88 		//this.camera.translate(-1,0,0);
 89 		this.targetVelocity[0] = -1;
 90 	}
 91 };
 92 
 93 Viewport.prototype.invokeKeyUp = function(event){
 94 	if(event.which == 83 ){
 95 		//s
 96 		this.targetVelocity[2] = 0;
 97 	}else if(event.which == 87 ){
 98 		//w
 99 		this.targetVelocity[2] = 0;
100 	}else if(event.which == 68 ){
101 		//d
102 		this.targetVelocity[0] = 0;
103 	}else if(event.which == 65 ){
104 		//a
105 		this.targetVelocity[0] = 0;
106 	}
107 };
108 
109 Viewport.prototype.invokeKeyPress = function(event){
110 
111 };
112 
113 Viewport.prototype.invokeMouseDown = function(event){
114 };
115 
116 Viewport.prototype.invokeMouseUp = function(event){
117 };
118 
119 Viewport.prototype.invokeMouseMove = function(event, diffX, diffY){
120 	
121 };
122 
123 Viewport.prototype.invokeMouseDrag = function(event, pressedKeys, diffX, diffY){
124 	if(pressedKeys.length == 1 && pressedKeys.contains(1)){
125 		if(event.altKey){
126 			var pos = this.camera.localPosition;
127 			
128 			var toOrigin = M4x4.translate3(-pos[0], -pos[1], -pos[2], M4x4.I);
129 			var rotY = M4x4.rotate(-diffX / 100.0, this.camera.getUpVector(), M4x4.I);
130 			var rotX = M4x4.rotate(-diffY / 100.0, this.camera.getSideVector(), M4x4.I);
131 			var rotY = M4x4.rotate(-diffX / 100.0, [0,1,0], M4x4.I);
132 			var rotX = M4x4.rotate(-diffY / 100.0, this.camera.getSideVector(), M4x4.I);
133 			var backToPos = M4x4.translate3(pos[0], pos[1], pos[2], M4x4.I);
134 			
135 			
136 			
137 			var transform = M4x4.mul(toOrigin, this.camera.transform);
138 			transform = M4x4.mul(rotX, transform);
139 			transform = M4x4.mul(backToPos, transform);
140 			this.camera.setTransform(transform);
141 			this.camera.resolveTransformation();
142 			
143 			
144 			var transform = M4x4.mul(toOrigin, this.camera.transform);
145 			transform = M4x4.mul(rotY, transform);
146 			transform = M4x4.mul(backToPos, transform);
147 			this.camera.setTransform(transform);
148 			this.camera.resolveTransformation();
149 			
150 		}else{
151 			this.camera.translate(-diffX / 10.0, diffY / 10.0, 0);
152 		}
153 	}
154 };
155 
156 Viewport.prototype.invokeMouseWheel = function(delta){
157 	
158 	var amount = -delta / 100.0;
159 	this.camera.translate(0,0,amount);
160 	
161 	
162 	var scene = this.camera.scene;
163 	var root = scene.rootNode;
164 };
165 
166 
167 
168