-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotatingCamera.js
114 lines (103 loc) · 2.68 KB
/
rotatingCamera.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/** @constructor
* @extends {Camera}
*/
function RotatingCamera()
{
Camera.call(this);
this._btnDown = [false,false,false,false,false,false];
this._alpha = 0;
this._beta = 0;
this._dist = 1;
this._needCalcRotate = false;
}
RotatingCamera.prototype = new Camera();
RotatingCamera.prototype.UpdateMatrices = function()
{
if(this._needCalcRotate)
{
var sa = Math.sin(this._alpha), ca = Math.cos(this._alpha);
var sb = Math.sin(this._beta), cb = Math.cos(this._beta);
var v = [0,0,0], l = this.look();
v[0] = l[0] + ca * sb * this._dist;
v[1] = l[1] + sa * this._dist;
v[2] = l[2] + ca * cb * this._dist;
this.pos(v);
this.nearPlane(this._dist * 0.1);
this.farPlane(this._dist * 10);
}
Camera.prototype.UpdateMatrices.call(this);
this._needCalcRotate = false;
}
//function floatDiff(a,b) { var v = b-a; return (v>=1e-3) || (v<=-1e-3);}
RotatingCamera.prototype.alpha = function(v)
{
if(v!=undefined)
{
this._needCalcRotate=floatDiff(this._alpha,v);
this._alpha=v;
}
return this._alpha;
}
RotatingCamera.prototype.beta = function(v)
{
if(v!=undefined)
{
this._needCalcRotate=floatDiff(this._beta,v);
this._beta=v;
}
return this._beta;
}
RotatingCamera.prototype.dist = function(v)
{
if(v!=undefined)
{
this._needCalcRotate=floatDiff(this._dist,v);
this._dist=v;
}
return this._dist;
}
RotatingCamera.prototype.alphaDeg = function(v) { if(v!=undefined) { this.alpha(Math.PI*v/180); } }
RotatingCamera.prototype.betaDeg = function(v) { if(v!=undefined) { this.beta(Math.PI*v/180); } }
RotatingCamera.prototype.ApplyDeltas = function(dx,dy,dz)
{
this._alpha += dy / 100;
this._beta -= dx / 100;
if( dz < 0) this._dist *= 1.1;
if( dz > 0) this._dist /= 1.1;
this._needCalcRotate=true;
this.UpdateMatrices();
}
RotatingCamera.prototype.AddListeners = function(dest, paintFn)
{
dest = dest || document.body;
paintFn = paintFn || function() {};
var mouseDown = false;
var tc=this;
var exX,exY;
dest.addEventListener("mousedown",function(e) {
mouseDown = true;
exX = e.screenX;
exY = e.screenY;
});
document.body.addEventListener("mouseup",function() { mouseDown = false; } );
document.body.addEventListener("mousemove",function(e)
{
if( mouseDown )
{
var deltax = e.screenX-exX;
var deltay = e.screenY-exY;
exX =e.screenX;
exY =e.screenY;
tc.ApplyDeltas(deltax,deltay,0);
paintFn();
}
});
dest.addEventListener("wheel",function(e)
{
e.deltaY = e.deltaY ? e.deltaY : -e.wheelDelta ;
tc.ApplyDeltas(0,0,e.deltaY);
tc._needCalcRotate=true;
tc.UpdateMatrices();
paintFn();
});
}