scripts/machine.js
author Eugen Sawin <sawine@me73.com>
Fri, 29 Apr 2011 17:10:15 +0200
changeset 35 7769f6e1e2c5
parent 33 2af408534f28
child 36 a828c4cde5b3
permissions -rwxr-xr-x
Added cube vbos and ids.
     1 var machine;
     2 var renderer;
     3 var controller;
     4 var camera;
     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,
     7 		 'D': moveCameraRight,
     8 		 'W': moveCameraUp,
     9 		 'S': moveCameraDown};
    10 var mouseActionMap = {"pitch": [[true, false, false], pitchCamera],
    11 		      "yaw": [[true, false, false], yawCamera],
    12 		      "rotate": [[true, false, false], rotateCamera],
    13                       "wheel": zoomCamera};
    14 include("scripts/cube.js");
    15 include("scripts/renderer.js");
    16 include("scripts/camera.js");
    17 
    18 function main()
    19 {
    20     var canvas = document.getElementById("machine");
    21     var context = new Context(canvas);
    22     context.expand();
    23     var gl = context.gl;
    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);
    31     update();
    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);
    40 }
    41 
    42 function createScene(context)
    43 {
    44     var size = 1;
    45     var vbo = new CubeVbo(size, context);
    46     var scene = new Node([0, 0, 0], [0, 0, 0], new Cube(vbo));
    47     var dim = 10;
    48     var d = size * 4;
    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)));
    54 	    }
    55 	}
    56     }
    57     return scene;
    58 }
    59 
    60 function configureCamera(config) {
    61     alert(config);
    62 }
    63 
    64 function update()
    65 {    
    66     requestAnimFrame(update);
    67     controller.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());
    72     camera.update();
    73     renderer.update(machine.scene);
    74 }
    75 
    76 function random(min, max)
    77 {
    78     return (min + Math.random() * (max - min));
    79 }
    80 
    81 function normalize(vectors)
    82 {
    83     for (var v in vectors) vec3.normalize(v);
    84 }
    85 
    86 function Controller(keyActionMap, mouseActionMap, camera, machine, renderer)
    87 { 
    88     this.keyboard = new Keyboard(keyActionMap);
    89     this.mouse = new Mouse(mouseActionMap);   
    90 }
    91 Controller.prototype.update = function()
    92 {
    93     this.keyboard.handle();
    94     this.mouse.handle();
    95 }
    96 
    97 function Mouse(actionMap)
    98 {
    99     this.actionMap = actionMap;
   100     this.pressed = [false, false, false];   
   101     this.wheelDelta = 0;
   102     this.posStack = [];
   103 }
   104 Mouse.prototype.buttonDown = function(event)
   105 {
   106     this.pressed[event.which-1] = true;
   107 }
   108 Mouse.prototype.buttonUp = function(event)
   109 {
   110     this.pressed[event.which-1] = false;
   111 }
   112 Mouse.prototype.move = function(event)
   113 {
   114     this.posStack.push([event.clientX, event.clientY]);
   115     //this.currentPos = [event.clientX, event.clientY];
   116 }
   117 Mouse.prototype.moveWheel = function(event)
   118 { 
   119     this.wheelDelta += event.wheelDelta;
   120 }
   121 Mouse.prototype.handle = function()
   122 {
   123     while (this.posStack.length > 0)
   124     {
   125 	var pos = this.posStack.pop();
   126 	if (this.lastPos)
   127 	{
   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) 
   132 	    {
   133 		for (var i = 0; i < 3; i++)
   134 		{
   135 		    if (this.pressed[i] != action[0][i]) break;
   136 		    if (i == 2) action[1](delta[1]);
   137 		}
   138 	    } 
   139 	    action = this.actionMap["yaw"];
   140 	    if (delta[0] != 0 && action) 
   141 	    {
   142 		for (var i = 0; i < 3; i++)
   143 		{
   144 		    if (this.pressed[i] != action[0][i]) break;
   145 		    if (i == 2) action[1](delta[0]);
   146 	       	}
   147 	    }
   148 	}
   149 	this.lastPos = pos;
   150     }   
   151     if (this.wheelDelta != 0)
   152     {
   153 	var delta = normaliseWheelDelta(this.wheelDelta);
   154 	var action = this.actionMap["wheel"];
   155 	if (delta != 0 && action) action(delta);
   156 	this.wheelDelta = 0;
   157     } 
   158 }
   159 
   160 function normaliseMoveDelta(delta)
   161 {
   162     var width = renderer.context.canvas.width;
   163     var height = renderer.context.canvas.height;
   164     return [delta[0] / width, delta[1] / height, 0];
   165 }
   166 
   167 function normaliseWheelDelta(delta)
   168 {
   169     return delta / 60;
   170 }
   171 
   172 function Keyboard(actionMap)
   173 {
   174     this.actionMap = actionMap;
   175     this.pressed = {};
   176 }
   177 Keyboard.prototype.keyDown = function(event)
   178 {
   179     this.pressed[event.keyCode] = true;    
   180 }
   181 Keyboard.prototype.keyUp = function(event)
   182 {
   183     this.pressed[event.keyCode] = false;
   184 }
   185 Keyboard.prototype.handle = function()
   186 {
   187     for (key in this.pressed)
   188     {
   189 	if (this.pressed[key])
   190 	{
   191 	    key = String.fromCharCode(key);
   192 	    if (this.actionMap[key]) this.actionMap[key](); 
   193 	}
   194     }
   195 }
   196 
   197 function Machine(scene)
   198 {
   199     this.scene = scene;
   200     this.lastUpdate = 0;
   201 }
   202 Machine.prototype.update = function(time)
   203 {
   204     this.scene.update(time);
   205     if (this.lastUpdate != 0)
   206     {
   207 	var diff = time - this.lastUpdate;
   208 	
   209     }
   210     this.lastUpdate = time;
   211 }
   212 
   213 function read(file, handler)
   214 {
   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);
   221 	}
   222     }
   223     request.send();
   224 }		
   225 
   226 // event handler
   227 function expandContext()
   228 {
   229     renderer.context.expand();
   230 }
   231 
   232 function moveCameraLeft()
   233 {
   234     camera.moveLeft();
   235 }
   236 
   237 function moveCameraRight()
   238 {
   239     camera.moveRight();
   240 }
   241 
   242 function moveCameraUp()
   243 {
   244     camera.moveUp();
   245 }
   246 
   247 function moveCameraDown()
   248 {
   249     camera.moveDown();
   250 }
   251 
   252 function zoomCamera(delta)
   253 {
   254     camera.zoom(delta);
   255 }
   256 
   257 function pitchCamera(delta)
   258 {
   259     camera.pitch(delta);
   260 }
   261 
   262 function yawCamera(delta)
   263 {
   264     camera.yaw(delta);
   265 }
   266 
   267 function rotateCamera(angles)
   268 {
   269     camera.rotate(angles);
   270 }
   271 
   272 function handleKeyDown(event)
   273 {
   274     controller.keyboard.keyDown(event);
   275 }
   276 
   277 function handleKeyUp(event)
   278 {
   279     controller.keyboard.keyUp(event);
   280 }
   281 
   282 function handleMouseDown(event)
   283 {
   284     controller.mouse.buttonDown(event);
   285 }
   286 
   287 function handleMouseUp(event)
   288 {
   289     controller.mouse.buttonUp(event);
   290 }
   291 
   292 function handleMouseMove(event)
   293 {
   294     controller.mouse.move(event);
   295 }
   296 
   297 function handleMouseWheel(event)
   298 {
   299     controller.mouse.moveWheel(event);
   300 }