-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelState.js
230 lines (161 loc) · 5.16 KB
/
ModelState.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//ModelState.js
//Written by Brandon Poythress
//25APR2018
//Description:
//This is the top level model class
function ModelState(){
//Inherits from ModelObject but because it is the top level object
//it does not use id, parentId, or parentDirtyListCallback so they
//are set to null. It also does not need ModelObject methods,
//so ModelState doesnt grab its prototypes.
ModelObject.call(this, "State", null, null, true);
//The model state needs many things from the drawable object class.
//the color and parentCallback functions are set to null
//because it has no color or parent. Also, there is no
//need to inherit any methods from this class so its prototypes are
//not taken.
DrawableObject.call(this, null, null);
//the state object manages its own model matrix
//and view matrix. The renderer handles proj
//matrix...for now at least
this.modelMatrix = new Matrix4();
this.viewMatrix = new Matrix4();
this.inverseModelMatrix = new Matrix4();
this.inverseViewMatrix = new Matrix4();
this.tempWorldRay = new Vector4();
this.worldRay = new Vector3();
//some temporary code to test moving around
this.radius = -2500;
this.angle = 0;
//this is the state of the viewer or player or users
this.eyeX = 0.0;
this.eyeY = 1000.0;
this.eyeZ = 0.0;
this.lookX = -this.radius*Math.sin(this.angle);
this.lookY = 0.0;
this.lookZ = this.radius*Math.cos(this.angle);
this.isMovingForward = false;
this.isMovingBackward = false;
this.isMovingRight = false;
this.isMovingLeft = false;
this.isRotatingRight = false;
this.isRotatingLeft = false;
this.viewMatrix.setLookAt(this.eyeX, this.eyeY, this.eyeZ,
this.lookX, this.lookY, this.lookZ,
0, 1, 0);
//movement per millisecond
this.acceleration = 0.0005;
//the dirtyList and the renderList is the handshake between the "model"
//and the "view". The model and the renderer both have access to it.
//Whatever you place in the dirtyList will be re-presented to the GPU because it
//is new or it has changed...or it needs to be deleted.
//whatever is in the renderList will be renderered.
//i am going to try a single dirtyList for everything
this.dirtyList = [];
//initialize the scene.
this.scene = new LabScene(this.idGen.getId(),
this.id,
this.dirtyListCallback.bind(this));
//initialize one portal. Multiple portals will be supported in the final build.
//may need a portal list
this.portal = new Portal(this.idGen.getId(),
this.id,
this.dirtyListCallback.bind(this));
//each object that is the parent to renderable objects must have a renderList
this.renderList.push(this.scene);
this.renderList.push(this.portal);
this.viewMatrix.setLookAt(this.eyeX, this.eyeY, this.eyeZ,
this.lookX, this.lookY, this.lookZ,
0, 1, 0);
}
// ModelState.prototype.draw = function(gl, renderer){
// this.scene.draw(gl, renderer);
// this.portal.draw(gl, renderer);
// }
ModelState.prototype.dirtyListCallback = function(dirtyObject){
this.dirtyList.push(dirtyObject);
};
ModelState.prototype.update = function(delta){
if(this.isRotatingRight){
this.angle = this.angle + delta*this.acceleration;
console.log("rotating right");
}else if(this.isRotatingLeft){
console.log("rotating left");
this.angle = this.angle - delta*this.acceleration;
}
this.viewMatrix.setLookAt(this.eyeX, this.eyeY, this.eyeZ,
-this.radius*Math.sin(this.angle),
this.lookY, this.radius*Math.cos(this.angle),
0, 1, 0);
this.scene.update(delta);
this.portal.update(delta);
};
ModelState.prototype.onKeyDown = function(evt){
switch(evt.key){
case "w":
//this.isMovingForward = true;
break;
case "W":
//this.isMovingForward = true;
break;
case "s":
//this.isMovingBackward = true;
break;
case "S":
//this.isMovingBackward = true;
break;
case "a":
this.isRotatingLeft = true;
break;
case "A":
this.isRotatingLeft = true;
break;
case "d":
this.isRotatingRight = true;
break;
case "D":
this.isRotatingRight = true;
break;
}
};
ModelState.prototype.onKeyUp = function(evt){
switch(evt.key){
case "w":
//this.isMovingForward = false;
break;
case "W":
//this.isMovingForward = false;
break;
case "s":
//this.isMovingBackward = false;
break;
case "S":
//this.isMovingBackward = false;
break;
case "a":
this.isRotatingLeft = false;
break;
case "A":
this.isRotatingLeft = false;
break;
case "d":
this.isRotatingRight = false;
break;
case "D":
this.isRotatingRight = false;
break;
}
};
ModelState.prototype.onMouseUp = function(evt, eyeRay){
//step 4: get everything into world coordinates
this.inverseViewMatrix.setInverseOf(this.viewMatrix);
//fill a temporary Vector4 that will be reduced to a Vector3
this.tempWorldRay = this.inverseViewMatrix.multiplyVector4(eyeRay);
//fill the vector3
this.worldRay.elements[0] = this.tempWorldRay.elements[0];
this.worldRay.elements[1] = this.tempWorldRay.elements[1];
this.worldRay.elements[2] = this.tempWorldRay.elements[2];
this.worldRay.normalize();
console.log(this.worldRay);
this.scene.onMouseUp(this.eyeX, this.eyeY, this.eyeZ, this.worldRay);
};