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