Added optional texturing.
6 var cameraSpeed = {"h": 1.0, "v": 1.0, "zoom": 1.0, "pitch": 1.0, "yaw": 1.0, "roll": 1.0};
7 var keyActionMap = {'A': moveCameraLeft,
11 var mouseActionMap = {"pitch": [[true, false, false], pitchCamera],
12 "yaw": [[true, false, false], yawCamera],
13 "rotate": [[true, false, false], rotateCamera],
15 include("scripts/cube.js");
16 include("scripts/renderer.js");
17 include("scripts/camera.js");
21 var canvas = document.getElementById("machine");
22 var context = new Context(canvas);
25 var scene = createScene(context);
26 gl.clearColor(0.00, 0.0, 0.0, 1.0);
27 gl.enable(gl.DEPTH_TEST);
28 machine = new Machine(scene);
29 camera = new Camera(cameraSpeed, [0, 0, 20]);
30 renderer = new Renderer(camera, context);
31 controller = new Controller(keyActionMap, mouseActionMap, camera, machine, renderer);
33 window.onresize = expandContext;
34 document.onkeydown = handleKeyDown;
35 document.onkeyup = handleKeyUp;
36 canvas.onmousedown = handleMouseDown;
37 document.onmouseup = handleMouseUp;
38 document.onmousemove = handleMouseMove;
39 document.onmousewheel = handleMouseWheel;
40 //read("config/camera", configureCamera);
45 function Texture(file)
47 this.id = textures.length;
49 this.hnd = gl.createTexture();
50 this.image = new Image();
51 this.image.onload = function() { handleLoadedTexture(); }
52 this.image.src = file;
55 function handleLoadedTexture()
57 var texture = textures[textures.length-1];
58 gl.bindTexture(gl.TEXTURE_2D, texture.hnd);
59 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
60 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);
61 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
62 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
63 gl.bindTexture(gl.TEXTURE_2D, null);
66 function createScene(context)
69 var texture = new Texture("textures/nehe.gif");
70 var vbo = new CubeVbo(size, context, texture);
71 var scene = new Node([0, 0, 0], [0, 0, 0], new Cube(vbo));
74 for (var x = 0; x < dim; x += 1) {
75 for (var y = 0; y < dim; y += 1) {
76 for (var z = 0; z < dim; z += 1) {
77 var pos = [x * d - dim * d / 2, y * d - dim * d / 2, -z * d];
78 scene.children.push(new Node(pos, [0, 0, 0], new Cube(vbo)));
85 function configureCamera(config) {
91 requestAnimFrame(update);
93 //machine.scene.rotation.x += (random(0, 2) - 2) * 0.001;
94 // machine.scene.rotation.y += (random(0, 3) - 1) * 0.001;
95 // machine.scene.rotation.z += (random(0, 2) - 1) * 0.001;
96 machine.update(new Date().getTime());
98 renderer.update(machine.scene);
101 function random(min, max)
103 return (min + Math.random() * (max - min));
106 function normalize(vectors)
108 for (var v in vectors) vec3.normalize(v);
111 function Controller(keyActionMap, mouseActionMap, camera, machine, renderer)
113 this.keyboard = new Keyboard(keyActionMap);
114 this.mouse = new Mouse(mouseActionMap);
116 Controller.prototype.update = function()
118 this.keyboard.handle();
122 function Mouse(actionMap)
124 this.actionMap = actionMap;
125 this.pressed = [false, false, false];
129 Mouse.prototype.buttonDown = function(event)
131 this.pressed[event.which-1] = true;
133 Mouse.prototype.buttonUp = function(event)
135 this.pressed[event.which-1] = false;
137 Mouse.prototype.move = function(event)
139 this.posStack.push([event.clientX, event.clientY]);
140 //this.currentPos = [event.clientX, event.clientY];
142 Mouse.prototype.moveWheel = function(event)
144 this.wheelDelta += event.wheelDelta;
146 Mouse.prototype.handle = function()
148 while (this.posStack.length > 0)
150 var pos = this.posStack.pop();
153 var delta = [pos[0] - this.lastPos[0], pos[1] - this.lastPos[1], 0];
154 delta = normaliseMoveDelta(delta);
155 var action = this.actionMap["pitch"];
156 if (delta[1] != 0 && action)
158 for (var i = 0; i < 3; i++)
160 if (this.pressed[i] != action[0][i]) break;
161 if (i == 2) action[1](delta[1]);
164 action = this.actionMap["yaw"];
165 if (delta[0] != 0 && action)
167 for (var i = 0; i < 3; i++)
169 if (this.pressed[i] != action[0][i]) break;
170 if (i == 2) action[1](delta[0]);
176 if (this.wheelDelta != 0)
178 var delta = normaliseWheelDelta(this.wheelDelta);
179 var action = this.actionMap["wheel"];
180 if (delta != 0 && action) action(delta);
185 function normaliseMoveDelta(delta)
187 var width = renderer.context.canvas.width;
188 var height = renderer.context.canvas.height;
189 return [delta[0] / width, delta[1] / height, 0];
192 function normaliseWheelDelta(delta)
197 function Keyboard(actionMap)
199 this.actionMap = actionMap;
202 Keyboard.prototype.keyDown = function(event)
204 this.pressed[event.keyCode] = true;
206 Keyboard.prototype.keyUp = function(event)
208 this.pressed[event.keyCode] = false;
210 Keyboard.prototype.handle = function()
212 for (key in this.pressed)
214 if (this.pressed[key])
216 key = String.fromCharCode(key);
217 if (this.actionMap[key]) this.actionMap[key]();
222 function Machine(scene)
227 Machine.prototype.update = function(time)
229 this.scene.update(time);
230 if (this.lastUpdate != 0)
232 var diff = time - this.lastUpdate;
235 this.lastUpdate = time;
238 function read(file, handler)
240 var request = new XMLHttpRequest();
241 request.open("GET", file);
242 request.onreadystatechange = function() {
243 alert(request.readyState);
244 if (request.readyState == 4) {
245 handler(request.responseText);
252 function expandContext()
254 renderer.context.expand();
257 function moveCameraLeft()
262 function moveCameraRight()
267 function moveCameraUp()
272 function moveCameraDown()
277 function zoomCamera(delta)
282 function pitchCamera(delta)
287 function yawCamera(delta)
292 function rotateCamera(angles)
294 camera.rotate(angles);
297 function handleKeyDown(event)
299 controller.keyboard.keyDown(event);
302 function handleKeyUp(event)
304 controller.keyboard.keyUp(event);
307 function handleMouseDown(event)
309 controller.mouse.buttonDown(event);
312 function handleMouseUp(event)
314 controller.mouse.buttonUp(event);
317 function handleMouseMove(event)
319 controller.mouse.move(event);
322 function handleMouseWheel(event)
324 controller.mouse.moveWheel(event);