Added cube vbos and ids.
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.00, 0.0, 0.0, 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)
45 var vbo = new CubeVbo(size, context);
46 var scene = new Node([0, 0, 0], [0, 0, 0], new Cube(vbo));
49 for (var x = 0; x < dim; x += 1) {
50 for (var y = 0; y < dim; y += 1) {
51 for (var z = 0; z < dim; z += 1) {
52 var pos = [x * d - dim * d / 2, y * d - dim * d / 2, -z * d];
53 scene.children.push(new Node(pos, [0, 0, 0], new Cube(vbo)));
60 function configureCamera(config) {
66 requestAnimFrame(update);
68 //machine.scene.rotation.x += (random(0, 2) - 2) * 0.001;
69 // machine.scene.rotation.y += (random(0, 3) - 1) * 0.001;
70 // machine.scene.rotation.z += (random(0, 2) - 1) * 0.001;
71 machine.update(new Date().getTime());
73 renderer.update(machine.scene);
76 function random(min, max)
78 return (min + Math.random() * (max - min));
81 function normalize(vectors)
83 for (var v in vectors) vec3.normalize(v);
86 function Controller(keyActionMap, mouseActionMap, camera, machine, renderer)
88 this.keyboard = new Keyboard(keyActionMap);
89 this.mouse = new Mouse(mouseActionMap);
91 Controller.prototype.update = function()
93 this.keyboard.handle();
97 function Mouse(actionMap)
99 this.actionMap = actionMap;
100 this.pressed = [false, false, false];
104 Mouse.prototype.buttonDown = function(event)
106 this.pressed[event.which-1] = true;
108 Mouse.prototype.buttonUp = function(event)
110 this.pressed[event.which-1] = false;
112 Mouse.prototype.move = function(event)
114 this.posStack.push([event.clientX, event.clientY]);
115 //this.currentPos = [event.clientX, event.clientY];
117 Mouse.prototype.moveWheel = function(event)
119 this.wheelDelta += event.wheelDelta;
121 Mouse.prototype.handle = function()
123 while (this.posStack.length > 0)
125 var pos = this.posStack.pop();
128 var delta = [pos[0] - this.lastPos[0], pos[1] - this.lastPos[1], 0];
129 delta = normaliseMoveDelta(delta);
130 var action = this.actionMap["pitch"];
131 if (delta[1] != 0 && action)
133 for (var i = 0; i < 3; i++)
135 if (this.pressed[i] != action[0][i]) break;
136 if (i == 2) action[1](delta[1]);
139 action = this.actionMap["yaw"];
140 if (delta[0] != 0 && action)
142 for (var i = 0; i < 3; i++)
144 if (this.pressed[i] != action[0][i]) break;
145 if (i == 2) action[1](delta[0]);
151 if (this.wheelDelta != 0)
153 var delta = normaliseWheelDelta(this.wheelDelta);
154 var action = this.actionMap["wheel"];
155 if (delta != 0 && action) action(delta);
160 function normaliseMoveDelta(delta)
162 var width = renderer.context.canvas.width;
163 var height = renderer.context.canvas.height;
164 return [delta[0] / width, delta[1] / height, 0];
167 function normaliseWheelDelta(delta)
172 function Keyboard(actionMap)
174 this.actionMap = actionMap;
177 Keyboard.prototype.keyDown = function(event)
179 this.pressed[event.keyCode] = true;
181 Keyboard.prototype.keyUp = function(event)
183 this.pressed[event.keyCode] = false;
185 Keyboard.prototype.handle = function()
187 for (key in this.pressed)
189 if (this.pressed[key])
191 key = String.fromCharCode(key);
192 if (this.actionMap[key]) this.actionMap[key]();
197 function Machine(scene)
202 Machine.prototype.update = function(time)
204 this.scene.update(time);
205 if (this.lastUpdate != 0)
207 var diff = time - this.lastUpdate;
210 this.lastUpdate = time;
213 function read(file, handler)
215 var request = new XMLHttpRequest();
216 request.open("GET", file);
217 request.onreadystatechange = function() {
218 alert(request.readyState);
219 if (request.readyState == 4) {
220 handler(request.responseText);
227 function expandContext()
229 renderer.context.expand();
232 function moveCameraLeft()
237 function moveCameraRight()
242 function moveCameraUp()
247 function moveCameraDown()
252 function zoomCamera(delta)
257 function pitchCamera(delta)
262 function yawCamera(delta)
267 function rotateCamera(angles)
269 camera.rotate(angles);
272 function handleKeyDown(event)
274 controller.keyboard.keyDown(event);
277 function handleKeyUp(event)
279 controller.keyboard.keyUp(event);
282 function handleMouseDown(event)
284 controller.mouse.buttonDown(event);
287 function handleMouseUp(event)
289 controller.mouse.buttonUp(event);
292 function handleMouseMove(event)
294 controller.mouse.move(event);
297 function handleMouseWheel(event)
299 controller.mouse.moveWheel(event);