Fixed scene position.
5 var cameraSpeed = {"h": 1.0, "v": 1.0, "zoom": 1.0, "pitch": 1.0, "yaw": 1.0, "roll": 1.0};
6 var keyActionMap = {'A': moveCameraLeft,
10 var mouseActionMap = {"pitch": [[true, false, false], pitchCamera],
11 "yaw": [[true, false, false], yawCamera],
12 "rotate": [[true, false, false], rotateCamera],
14 include("scripts/cube.js");
15 include("scripts/renderer.js");
16 include("scripts/camera.js");
20 var canvas = document.getElementById("machine");
21 var context = new Context(canvas);
24 var scene = createScene(context);
25 gl.clearColor(0.05, 0.05, 0.05, 1.0);
26 gl.enable(gl.DEPTH_TEST);
27 machine = new Machine(scene);
28 camera = new Camera(cameraSpeed, [0, 0, 20]);
29 renderer = new Renderer(camera, context);
30 controller = new Controller(keyActionMap, mouseActionMap, camera, machine, renderer);
32 window.onresize = expandContext;
33 document.onkeydown = handleKeyDown;
34 document.onkeyup = handleKeyUp;
35 canvas.onmousedown = handleMouseDown;
36 document.onmouseup = handleMouseUp;
37 document.onmousemove = handleMouseMove;
38 document.onmousewheel = handleMouseWheel;
39 //read("config/camera", configureCamera);
42 function createScene(context)
44 var cube = new Cube(1, context);
45 var scene = new Node([0, 0, 0], [0, 0, 0], cube);
48 for (var x = 0; x < dim; x += 1) {
49 for (var y = 0; y < dim; y += 1) {
50 for (var z = 0; z < dim; z += 1) {
51 var pos = [x * d - dim * d / 2, y * d - dim * d / 2, -z * d];
52 scene.children.push(new Node(pos, [0, 0, 0], cube));
59 function configureCamera(config) {
65 requestAnimFrame(update);
67 //machine.scene.rotation.x += (random(0, 2) - 2) * 0.001;
68 // machine.scene.rotation.y += (random(0, 3) - 1) * 0.001;
69 // machine.scene.rotation.z += (random(0, 2) - 1) * 0.001;
70 machine.update(new Date().getTime());
72 renderer.update(machine.scene);
75 function random(min, max)
77 return (min + Math.random() * (max - min));
80 function normalize(vectors)
82 for (var v in vectors) vec3.normalize(v);
85 function Controller(keyActionMap, mouseActionMap, camera, machine, renderer)
87 this.keyboard = new Keyboard(keyActionMap);
88 this.mouse = new Mouse(mouseActionMap);
90 Controller.prototype.update = function()
92 this.keyboard.handle();
96 function Mouse(actionMap)
98 this.actionMap = actionMap;
99 this.pressed = [false, false, false];
103 Mouse.prototype.buttonDown = function(event)
105 this.pressed[event.which-1] = true;
107 Mouse.prototype.buttonUp = function(event)
109 this.pressed[event.which-1] = false;
111 Mouse.prototype.move = function(event)
113 this.posStack.push([event.clientX, event.clientY]);
114 //this.currentPos = [event.clientX, event.clientY];
116 Mouse.prototype.moveWheel = function(event)
118 this.wheelDelta += event.wheelDelta;
120 Mouse.prototype.handle = function()
122 while (this.posStack.length > 0)
124 var pos = this.posStack.pop();
127 var delta = [pos[0] - this.lastPos[0], pos[1] - this.lastPos[1], 0];
128 delta = normaliseMoveDelta(delta);
129 var action = this.actionMap["pitch"];
130 if (delta[1] != 0 && action)
132 for (var i = 0; i < 3; i++)
134 if (this.pressed[i] != action[0][i]) break;
135 if (i == 2) action[1](delta[1]);
138 action = this.actionMap["yaw"];
139 if (delta[0] != 0 && action)
141 for (var i = 0; i < 3; i++)
143 if (this.pressed[i] != action[0][i]) break;
144 if (i == 2) action[1](delta[0]);
150 if (this.wheelDelta != 0)
152 var delta = normaliseWheelDelta(this.wheelDelta);
153 var action = this.actionMap["wheel"];
154 if (delta != 0 && action) action(delta);
159 function normaliseMoveDelta(delta)
161 var width = renderer.context.canvas.width;
162 var height = renderer.context.canvas.height;
163 return [delta[0] / width, delta[1] / height, 0];
166 function normaliseWheelDelta(delta)
171 function Keyboard(actionMap)
173 this.actionMap = actionMap;
176 Keyboard.prototype.keyDown = function(event)
178 this.pressed[event.keyCode] = true;
180 Keyboard.prototype.keyUp = function(event)
182 this.pressed[event.keyCode] = false;
184 Keyboard.prototype.handle = function()
186 for (key in this.pressed)
188 if (this.pressed[key])
190 key = String.fromCharCode(key);
191 if (this.actionMap[key]) this.actionMap[key]();
196 function Machine(scene)
201 Machine.prototype.update = function(time)
203 this.scene.update(time);
204 if (this.lastUpdate != 0)
206 var diff = time - this.lastUpdate;
209 this.lastUpdate = time;
212 function read(file, handler)
214 var request = new XMLHttpRequest();
215 request.open("GET", file);
216 request.onreadystatechange = function() {
217 alert(request.readyState);
218 if (request.readyState == 4) {
219 handler(request.responseText);
226 function expandContext()
228 renderer.context.expand();
231 function moveCameraLeft()
236 function moveCameraRight()
241 function moveCameraUp()
246 function moveCameraDown()
251 function zoomCamera(delta)
256 function pitchCamera(delta)
261 function yawCamera(delta)
266 function rotateCamera(angles)
268 camera.rotate(angles);
271 function handleKeyDown(event)
273 controller.keyboard.keyDown(event);
276 function handleKeyUp(event)
278 controller.keyboard.keyUp(event);
281 function handleMouseDown(event)
283 controller.mouse.buttonDown(event);
286 function handleMouseUp(event)
288 controller.mouse.buttonUp(event);
291 function handleMouseMove(event)
293 controller.mouse.move(event);
296 function handleMouseWheel(event)
298 controller.mouse.moveWheel(event);