Basic light.
5 var cameraSpeed = {"h": 0.1, "v": 0.1, "zoom": 0.5, "pitch": 0.5, "yaw": 0.5};
6 var keyActionMap = {'A': moveCameraLeft,
10 var mouseActionMap = {"pitch": [[true, false, false], pitchCamera],
11 "yaw": [[true, false, false], yawCamera],
13 include("scripts/cube.js");
14 include("scripts/renderer.js");
18 var canvas = document.getElementById("machine");
19 var context = new Context(canvas);
22 var object = new Cube(1, context);
23 gl.clearColor(0.0, 0.0, 0.0, 1.0);
24 gl.enable(gl.DEPTH_TEST);
25 machine = new Machine(object);
26 camera = new Camera(cameraSpeed, [0, 5, 20]);
27 renderer = new Renderer(camera, context);
28 controller = new Controller(keyActionMap, mouseActionMap, camera, machine, renderer);
30 window.onresize = expandContext;
31 document.onkeydown = handleKeyDown;
32 document.onkeyup = handleKeyUp;
33 canvas.onmousedown = handleMouseDown;
34 document.onmouseup = handleMouseUp;
35 document.onmousemove = handleMouseMove;
36 document.onmousewheel = handleMouseWheel;
37 //read("config/camera", configureCamera);
40 function configureCamera(config) {
46 requestAnimFrame(update);
48 machine.scene.rotation.x += (random(0, 2) - 2) * 0.001;
49 machine.scene.rotation.y += (random(0, 3) - 1) * 0.001;
50 machine.scene.rotation.z += (random(0, 2) - 1) * 0.001;
51 machine.update(new Date().getTime());
52 renderer.update(machine.scene);
55 function random(min, max)
57 return (min + Math.random() * (max - min));
60 function normalize(vectors)
62 for (var v in vectors) vec3.normalize(v);
65 function Camera(speed, pos, up, right, target)
68 this.pos = vec3.create(pos || [0, 0, 7]);
69 this.up = vec3.create(up || [0, 1, 0]);
70 this.right = vec3.create(right || [1, 0, 0]);
71 this.target = vec3.create(target || [0, 0, -1]);
72 normalize([this.up, this.right, this.target]);
73 this.matrix = mat4.create();
75 Camera.prototype.moveLeft = function()
77 var dir = vec3.create(this.right);
78 vec3.scale(dir, -this.speed.h);
79 vec3.add(this.pos, dir);
81 Camera.prototype.moveRight = function()
83 var dir = vec3.create(this.right);
84 vec3.scale(dir, this.speed.h);
85 vec3.add(this.pos, dir);
87 Camera.prototype.moveUp = function()
89 var dir = vec3.create(this.up);
90 vec3.scale(dir, this.speed.v);
91 vec3.add(this.pos, dir);
93 Camera.prototype.moveDown = function()
95 var dir = vec3.create(this.up);
96 vec3.scale(dir, -this.speed.v);
97 vec3.add(this.pos, dir);
99 Camera.prototype.zoom = function(delta)
101 var dir = vec3.create(this.target);
102 vec3.scale(dir, delta * this.speed.zoom);
103 vec3.add(this.pos, dir);
105 Camera.prototype.pitch = function(delta)
107 delta *= -this.speed.pitch;
108 vec3.scale(this.target, Math.cos(delta));
109 vec3.scale(this.up, Math.sin(delta));
110 vec3.add(this.target, this.up);
111 vec3.normalize(this.target);
112 vec3.cross(this.target, this.right, this.up);
113 vec3.scale(this.up, -1);
115 Camera.prototype.yaw = function(delta)
117 delta *= -this.speed.yaw;
118 vec3.scale(this.right, Math.cos(delta));
119 vec3.scale(this.target, Math.sin(delta));
120 vec3.add(this.right, this.target);
121 vec3.normalize(this.right);
122 vec3.cross(this.right, this.up, this.target);
123 vec3.scale(this.target, -1);
125 Camera.prototype.roll = function(delta)
129 Camera.prototype.update = function()
131 var x = vec3.dot(this.right, this.pos);
132 var y = vec3.dot(this.up, this.pos);
133 var z = vec3.dot(this.target, this.pos);
134 this.matrix = mat4.create([this.right[0], this.up[0], -this.target[0], 0,
135 this.right[1], this.up[1], -this.target[1], 0,
136 this.right[2], this.up[2], -this.target[2], 0,
140 function Controller(keyActionMap, mouseActionMap, camera, machine, renderer)
142 this.keyboard = new Keyboard(keyActionMap);
143 this.mouse = new Mouse(mouseActionMap);
146 Controller.prototype.update = function()
148 this.keyboard.handle();
152 function Mouse(actionMap)
154 this.actionMap = actionMap;
155 this.pressed = [false, false, false];
158 Mouse.prototype.buttonDown = function(event)
160 this.pressed[event.which-1] = true;
162 Mouse.prototype.buttonUp = function(event)
164 this.pressed[event.which-1] = false;
166 Mouse.prototype.move = function(event)
168 this.currentPos = [event.clientX, event.clientY];
170 Mouse.prototype.moveWheel = function(event)
172 this.wheelDelta += event.wheelDelta;
174 Mouse.prototype.handle = function()
176 var pos = this.currentPos;
177 if (pos && this.lastPos)
179 var delta = [pos[0] - this.lastPos[0], pos[1] - this.lastPos[1]];
180 delta = normaliseMoveDelta(delta);
181 var action = this.actionMap["pitch"];
182 if (delta[1] != 0 && action)
184 for (var i = 0; i < 3; i++)
186 if (this.pressed[i] != action[0][i]) break;
187 if (i == 2) action[1](delta[1]);
190 action = this.actionMap["yaw"];
191 if (delta[0] != 0 && action)
193 for (var i = 0; i < 3; i++)
195 if (this.pressed[i] != action[0][i]) break;
196 if (i == 2) action[1](delta[0]);
202 if (this.wheelDelta != 0)
204 var delta = normaliseWheelDelta(this.wheelDelta);
205 var action = this.actionMap["wheel"];
206 if (delta != 0 && action) action(delta);
212 function normaliseMoveDelta(delta)
214 var width = renderer.context.canvas.width;
215 var height = renderer.context.canvas.height;
216 return [delta[0] / width, delta[1] / height];
219 function normaliseWheelDelta(delta)
224 function Keyboard(actionMap)
226 this.actionMap = actionMap;
229 Keyboard.prototype.keyDown = function(event)
231 this.pressed[event.keyCode] = true;
233 Keyboard.prototype.keyUp = function(event)
235 this.pressed[event.keyCode] = false;
237 Keyboard.prototype.handle = function()
239 for (key in this.pressed)
241 if (this.pressed[key])
243 key = String.fromCharCode(key);
244 if (this.actionMap[key]) this.actionMap[key]();
249 function Machine(scene)
254 Machine.prototype.update = function(time)
256 if (this.lastUpdate != 0)
258 var diff = time - this.lastUpdate;
261 this.lastUpdate = time;
264 function read(file, handler)
266 var request = new XMLHttpRequest();
267 request.open("GET", file);
268 request.onreadystatechange = function() {
269 alert(request.readyState);
270 if (request.readyState == 4) {
271 handler(request.responseText);
278 function expandContext()
280 renderer.context.expand();
283 function moveCameraLeft()
288 function moveCameraRight()
293 function moveCameraUp()
298 function moveCameraDown()
303 function zoomCamera(delta)
308 function pitchCamera(delta)
313 function yawCamera(delta)
318 function handleKeyDown(event)
320 controller.keyboard.keyDown(event);
323 function handleKeyUp(event)
325 controller.keyboard.keyUp(event);
328 function handleMouseDown(event)
330 controller.mouse.buttonDown(event);
333 function handleMouseUp(event)
335 controller.mouse.buttonUp(event);
338 function handleMouseMove(event)
340 controller.mouse.move(event);
343 function handleMouseWheel(event)
345 controller.mouse.moveWheel(event);