Fixed scene graph positioning bug.
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.0, 0.0, 0.0, 1.0);
26 gl.enable(gl.DEPTH_TEST);
27 machine = new Machine(scene);
28 camera = new Camera(cameraSpeed, [0, 5, 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 scene = new Node([0, 0, 0], [0, 0, 0], new Cube(1, context));
47 for (var x = 0; x < dim; x += 1) {
48 for (var y = 0; y < dim; y += 1) {
49 for (var z = 0; z < dim; z += 1) {
50 var pos = [x * d, y * d, -z * d];
51 scene.children.push(new Node(pos, [0, 0, 0], new Cube(1, context)));
58 function configureCamera(config) {
64 requestAnimFrame(update);
66 //machine.scene.rotation.x += (random(0, 2) - 2) * 0.001;
67 // machine.scene.rotation.y += (random(0, 3) - 1) * 0.001;
68 // machine.scene.rotation.z += (random(0, 2) - 1) * 0.001;
69 machine.update(new Date().getTime());
71 renderer.update(machine.scene);
74 function random(min, max)
76 return (min + Math.random() * (max - min));
79 function normalize(vectors)
81 for (var v in vectors) vec3.normalize(v);
84 function Controller(keyActionMap, mouseActionMap, camera, machine, renderer)
86 this.keyboard = new Keyboard(keyActionMap);
87 this.mouse = new Mouse(mouseActionMap);
89 Controller.prototype.update = function()
91 this.keyboard.handle();
95 function Mouse(actionMap)
97 this.actionMap = actionMap;
98 this.pressed = [false, false, false];
102 Mouse.prototype.buttonDown = function(event)
104 this.pressed[event.which-1] = true;
106 Mouse.prototype.buttonUp = function(event)
108 this.pressed[event.which-1] = false;
110 Mouse.prototype.move = function(event)
112 this.posStack.push([event.clientX, event.clientY]);
113 //this.currentPos = [event.clientX, event.clientY];
115 Mouse.prototype.moveWheel = function(event)
117 this.wheelDelta += event.wheelDelta;
119 Mouse.prototype.handle = function()
121 while (this.posStack.length > 0)
123 var pos = this.posStack.pop();
126 var delta = [pos[0] - this.lastPos[0], pos[1] - this.lastPos[1], 0];
127 delta = normaliseMoveDelta(delta);
128 var action = this.actionMap["pitch"];
129 if (delta[1] != 0 && action)
131 for (var i = 0; i < 3; i++)
133 if (this.pressed[i] != action[0][i]) break;
134 if (i == 2) action[1](delta[1]);
137 action = this.actionMap["yaw"];
138 if (delta[0] != 0 && action)
140 for (var i = 0; i < 3; i++)
142 if (this.pressed[i] != action[0][i]) break;
143 if (i == 2) action[1](delta[0]);
149 if (this.wheelDelta != 0)
151 var delta = normaliseWheelDelta(this.wheelDelta);
152 var action = this.actionMap["wheel"];
153 if (delta != 0 && action) action(delta);
158 function normaliseMoveDelta(delta)
160 var width = renderer.context.canvas.width;
161 var height = renderer.context.canvas.height;
162 return [delta[0] / width, delta[1] / height, 0];
165 function normaliseWheelDelta(delta)
170 function Keyboard(actionMap)
172 this.actionMap = actionMap;
175 Keyboard.prototype.keyDown = function(event)
177 this.pressed[event.keyCode] = true;
179 Keyboard.prototype.keyUp = function(event)
181 this.pressed[event.keyCode] = false;
183 Keyboard.prototype.handle = function()
185 for (key in this.pressed)
187 if (this.pressed[key])
189 key = String.fromCharCode(key);
190 if (this.actionMap[key]) this.actionMap[key]();
195 function Machine(scene)
200 Machine.prototype.update = function(time)
202 this.scene.update(time);
203 if (this.lastUpdate != 0)
205 var diff = time - this.lastUpdate;
208 this.lastUpdate = time;
211 function read(file, handler)
213 var request = new XMLHttpRequest();
214 request.open("GET", file);
215 request.onreadystatechange = function() {
216 alert(request.readyState);
217 if (request.readyState == 4) {
218 handler(request.responseText);
225 function expandContext()
227 renderer.context.expand();
230 function moveCameraLeft()
235 function moveCameraRight()
240 function moveCameraUp()
245 function moveCameraDown()
250 function zoomCamera(delta)
255 function pitchCamera(delta)
260 function yawCamera(delta)
265 function rotateCamera(angles)
267 camera.rotate(angles);
270 function handleKeyDown(event)
272 controller.keyboard.keyDown(event);
275 function handleKeyUp(event)
277 controller.keyboard.keyUp(event);
280 function handleMouseDown(event)
282 controller.mouse.buttonDown(event);
285 function handleMouseUp(event)
287 controller.mouse.buttonUp(event);
290 function handleMouseMove(event)
292 controller.mouse.move(event);
295 function handleMouseWheel(event)
297 controller.mouse.moveWheel(event);