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