-
Notifications
You must be signed in to change notification settings - Fork 1
/
Camera.js
154 lines (129 loc) · 3.71 KB
/
Camera.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
function TargetCamera( Game )
{
var mViewInfo = null;
var mEye = {
rotZ: 0,
rotH: 46,
distanceFromTarget: 15
};
var mTarget = [ 0, 0, 0 ];
var mUp = [ 0, -1, 0 ];
var mLastOffset = null;
this.init = function( Game )
{
// Create the render graph for a view
mViewInfo = o3djs.rendergraph.createBasicView( Game.mPack, Game.mClient.root, Game.mClient.renderGraphRoot);
mViewInfo.clearBuffer.clearColor = [1, 1, 1, 1];
// Set up a perspective projection.
mViewInfo.drawContext.projection = o3djs.math.matrix4.perspective(
o3djs.math.degToRad(30), // 30 degree fov.
Game.mClient.width / Game.mClient.height,
1, // Near plane.
5000); // Far plane.
this.update( Game );
}
this.centerOnPuzzle = function( Game, Puzzle, ResetAngle )
{
this.centerOn( Game, Puzzle.getDimensions(), ResetAngle );
}
this.centerOn = function( Game, Location, ResetAngle )
{
mTarget[0] = Location[0] / 2.0;
mTarget[1] = Location[1] / 2.0;
mTarget[2] = Location[2] / 2.0;
if ( ResetAngle )
{
mEye.rotZ = -0.76;
mEye.rotH = 46;
}
mEye.distanceFromTarget = o3djs.math.distanceSquared( [0,0,0], [mTarget[0], mTarget[1], mTarget[2]] ) + 10;
this.update( Game );
}
this.update = function( Game )
{
if ( mEye.rotH < 44.5 )
{
mEye.rotH = 44.5;
}
if ( mEye.rotH > 46.6 )
{
mEye.rotH = 46.6;
}
if ( mEye.rotZ < 0 )
{
mEye.rotZ = 2 * Math.PI + mEye.rotZ;
} else
{
mEye.rotZ = mEye.rotZ % ( 2 * Math.PI );
}
mEye.x = mTarget[0] + Math.cos(mEye.rotZ) * mEye.distanceFromTarget * Math.sin(mEye.rotH);
mEye.y = mTarget[2] + Math.cos(mEye.rotH) * mEye.distanceFromTarget;
mEye.z = mTarget[1] + Math.sin(mEye.rotZ) * mEye.distanceFromTarget * Math.sin(mEye.rotH);
var EyeV = [mEye.x, mEye.y, mEye.z];
mViewInfo.drawContext.view = o3djs.math.matrix4.lookAt(EyeV, mTarget, mUp);
if ( Game && Game.mPuzzle )
{
Game.mPuzzle.updatePeeringArrowLocation( Game );
Game.doRender();
}
}
this.getTarget = function()
{
return mTarget;
}
this.getEye = function()
{
return mEye;
}
this.getViewInfo = function()
{
return mViewInfo;
}
this.handleMouseDown = function(Game, e)
{
mLastOffset =
{
x: e.x,
y: e.y
};
return false;
}
this.handleMouseMove = function(Game, e) {
if (e.x !== undefined && Game.mInputState.isMouseDown( Game.mO3d.Event.BUTTON_LEFT ) )
{
if ( mLastOffset == null )
{
mLastOffset =
{
x: e.x,
y: e.y
};
return false;
}
var offset =
{
x: e.x,
y: e.y
};
dX = (offset.x - mLastOffset.x);
dY = -1 * (offset.y - mLastOffset.y);
mLastOffset = offset;
mEye.rotZ -= dX / 100;
mEye.rotH -= dY / 100;
mEye.rotH = peg(mEye.rotH, 0.1, o3djs.math.PI - 0.1);
this.update( Game );
return true;
}
return false;
}
this.init( Game );
}
function peg(value, lower, upper) {
if (value < lower) {
return lower;
} else if (value > upper) {
return upper;
} else {
return value;
}
}