Added test texture.
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.LINEAR);
62 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
63 gl.generateMipmap(gl.TEXTURE_2D);
64 gl.bindTexture(gl.TEXTURE_2D, null);
67 function createScene(context)
70 var texture = new Texture("textures/test.png");
71 var vbo = new CubeVbo(size, context, texture);
72 var scene = new Node([0, 0, 0], [0, 0, 0], new Cube(vbo));
75 for (var x = 0; x < dim; x += 1) {
76 for (var y = 0; y < dim; y += 1) {
77 for (var z = 0; z < dim; z += 1) {
78 var pos = [x * d - dim * d / 2, y * d - dim * d / 2, -z * d];
79 scene.children.push(new Node(pos, [0, 0, 0], new Cube(vbo)));
86 function configureCamera(config) {
92 requestAnimFrame(update);
94 //machine.scene.rotation.x += (random(0, 2) - 2) * 0.001;
95 // machine.scene.rotation.y += (random(0, 3) - 1) * 0.001;
96 // machine.scene.rotation.z += (random(0, 2) - 1) * 0.001;
97 machine.update(new Date().getTime());
99 renderer.update(machine.scene);
102 function random(min, max)
104 return (min + Math.random() * (max - min));
107 function normalize(vectors)
109 for (var v in vectors) vec3.normalize(v);
112 function Controller(keyActionMap, mouseActionMap, camera, machine, renderer)
114 this.keyboard = new Keyboard(keyActionMap);
115 this.mouse = new Mouse(mouseActionMap);
117 Controller.prototype.update = function()
119 this.keyboard.handle();
123 function Mouse(actionMap)
125 this.actionMap = actionMap;
126 this.pressed = [false, false, false];
130 Mouse.prototype.buttonDown = function(event)
132 this.pressed[event.which-1] = true;
134 Mouse.prototype.buttonUp = function(event)
136 this.pressed[event.which-1] = false;
138 Mouse.prototype.move = function(event)
140 this.posStack.push([event.clientX, event.clientY]);
141 //this.currentPos = [event.clientX, event.clientY];
143 Mouse.prototype.moveWheel = function(event)
145 this.wheelDelta += event.wheelDelta;
147 Mouse.prototype.handle = function()
149 while (this.posStack.length > 0)
151 var pos = this.posStack.pop();
154 var delta = [pos[0] - this.lastPos[0], pos[1] - this.lastPos[1], 0];
155 delta = normaliseMoveDelta(delta);
156 var action = this.actionMap["pitch"];
157 if (delta[1] != 0 && action)
159 for (var i = 0; i < 3; i++)
161 if (this.pressed[i] != action[0][i]) break;
162 if (i == 2) action[1](delta[1]);
165 action = this.actionMap["yaw"];
166 if (delta[0] != 0 && action)
168 for (var i = 0; i < 3; i++)
170 if (this.pressed[i] != action[0][i]) break;
171 if (i == 2) action[1](delta[0]);
177 if (this.wheelDelta != 0)
179 var delta = normaliseWheelDelta(this.wheelDelta);
180 var action = this.actionMap["wheel"];
181 if (delta != 0 && action) action(delta);
186 function normaliseMoveDelta(delta)
188 var width = renderer.context.canvas.width;
189 var height = renderer.context.canvas.height;
190 return [delta[0] / width, delta[1] / height, 0];
193 function normaliseWheelDelta(delta)
198 function Keyboard(actionMap)
200 this.actionMap = actionMap;
203 Keyboard.prototype.keyDown = function(event)
205 this.pressed[event.keyCode] = true;
207 Keyboard.prototype.keyUp = function(event)
209 this.pressed[event.keyCode] = false;
211 Keyboard.prototype.handle = function()
213 for (key in this.pressed)
215 if (this.pressed[key])
217 key = String.fromCharCode(key);
218 if (this.actionMap[key]) this.actionMap[key]();
223 function Machine(scene)
228 Machine.prototype.update = function(time)
230 this.scene.update(time);
231 if (this.lastUpdate != 0)
233 var diff = time - this.lastUpdate;
236 this.lastUpdate = time;
239 function read(file, handler)
241 var request = new XMLHttpRequest();
242 request.open("GET", file);
243 request.onreadystatechange = function() {
244 alert(request.readyState);
245 if (request.readyState == 4) {
246 handler(request.responseText);
253 function expandContext()
255 renderer.context.expand();
258 function moveCameraLeft()
263 function moveCameraRight()
268 function moveCameraUp()
273 function moveCameraDown()
278 function zoomCamera(delta)
283 function pitchCamera(delta)
288 function yawCamera(delta)
293 function rotateCamera(angles)
295 camera.rotate(angles);
298 function handleKeyDown(event)
300 controller.keyboard.keyDown(event);
303 function handleKeyUp(event)
305 controller.keyboard.keyUp(event);
308 function handleMouseDown(event)
310 controller.mouse.buttonDown(event);
313 function handleMouseUp(event)
315 controller.mouse.buttonUp(event);
318 function handleMouseMove(event)
320 controller.mouse.move(event);
323 function handleMouseWheel(event)
325 controller.mouse.moveWheel(event);