scripts/machine.js
author Eugen Sawin <sawine@me73.com>
Tue, 26 Apr 2011 03:06:56 +0200
changeset 25 3b66f92dbc4d
parent 24 8c2ee41d3727
child 27 130cff40c734
permissions -rwxr-xr-x
Basic light.
     1 var machine;
     2 var renderer;
     3 var controller;
     4 var camera;
     5 var cameraSpeed = {"h": 0.1, "v": 0.1, "zoom": 0.5, "pitch": 0.5, "yaw": 0.5};
     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                       "wheel": zoomCamera};
    13 include("scripts/cube.js");
    14 include("scripts/renderer.js");
    15 
    16 function main()
    17 {
    18     var canvas = document.getElementById("machine");
    19     var context = new Context(canvas);
    20     context.expand();
    21     var gl = context.gl;
    22     var object = new Cube(1, context);
    23     gl.clearColor(0.0, 0.0, 0.0, 1.0);
    24     gl.enable(gl.DEPTH_TEST);
    25     machine = new Machine(object);       
    26     camera = new Camera(cameraSpeed, [0, 5, 20]);
    27     renderer = new Renderer(camera, context);
    28     controller = new Controller(keyActionMap, mouseActionMap, camera, machine, renderer);
    29     update();
    30     window.onresize = expandContext;
    31     document.onkeydown = handleKeyDown;
    32     document.onkeyup = handleKeyUp;
    33     canvas.onmousedown = handleMouseDown;
    34     document.onmouseup = handleMouseUp;
    35     document.onmousemove = handleMouseMove;
    36     document.onmousewheel = handleMouseWheel;
    37     //read("config/camera", configureCamera);
    38 }
    39 
    40 function configureCamera(config) {
    41     alert(config);
    42 }
    43 
    44 function update()
    45 {    
    46     requestAnimFrame(update);
    47     controller.update();
    48     machine.scene.rotation.x += (random(0, 2) - 2) * 0.001; 
    49     machine.scene.rotation.y += (random(0, 3) - 1) * 0.001; 
    50     machine.scene.rotation.z += (random(0, 2) - 1) * 0.001;
    51     machine.update(new Date().getTime());
    52     renderer.update(machine.scene);
    53 }
    54 
    55 function random(min, max)
    56 {
    57     return (min + Math.random() * (max - min));
    58 }
    59 
    60 function normalize(vectors)
    61 {
    62     for (var v in vectors) vec3.normalize(v);
    63 }
    64 
    65 function Camera(speed, pos, up, right, target)
    66 {
    67     this.speed = speed;
    68     this.pos = vec3.create(pos || [0, 0, 7]);
    69     this.up = vec3.create(up || [0, 1, 0]);
    70     this.right = vec3.create(right || [1, 0, 0]);
    71     this.target = vec3.create(target || [0, 0, -1]);
    72     normalize([this.up, this.right, this.target]);
    73     this.matrix = mat4.create();
    74 }
    75 Camera.prototype.moveLeft = function()
    76 {
    77     var dir = vec3.create(this.right);
    78     vec3.scale(dir, -this.speed.h);
    79     vec3.add(this.pos, dir);
    80 }
    81 Camera.prototype.moveRight = function()
    82 {
    83     var dir = vec3.create(this.right);
    84     vec3.scale(dir, this.speed.h);
    85     vec3.add(this.pos, dir);
    86 }
    87 Camera.prototype.moveUp = function()
    88 {  
    89     var dir = vec3.create(this.up);
    90     vec3.scale(dir, this.speed.v);
    91     vec3.add(this.pos, dir);
    92 }
    93 Camera.prototype.moveDown = function()
    94 { 
    95     var dir = vec3.create(this.up);
    96     vec3.scale(dir, -this.speed.v);
    97     vec3.add(this.pos, dir);
    98 }
    99 Camera.prototype.zoom = function(delta) 
   100 {
   101     var dir = vec3.create(this.target);
   102     vec3.scale(dir, delta * this.speed.zoom);
   103     vec3.add(this.pos, dir); 
   104 }
   105 Camera.prototype.pitch = function(delta)
   106 {
   107     delta *= -this.speed.pitch;
   108     vec3.scale(this.target, Math.cos(delta));
   109     vec3.scale(this.up, Math.sin(delta));
   110     vec3.add(this.target, this.up);
   111     vec3.normalize(this.target);
   112     vec3.cross(this.target, this.right, this.up);
   113     vec3.scale(this.up, -1);
   114 }
   115 Camera.prototype.yaw = function(delta)
   116 {
   117     delta *= -this.speed.yaw;
   118     vec3.scale(this.right, Math.cos(delta));
   119     vec3.scale(this.target, Math.sin(delta));
   120     vec3.add(this.right, this.target);
   121     vec3.normalize(this.right);
   122     vec3.cross(this.right, this.up, this.target);
   123     vec3.scale(this.target, -1);
   124 }
   125 Camera.prototype.roll = function(delta)
   126 {
   127     
   128 }
   129 Camera.prototype.update = function()
   130 {
   131     var x = vec3.dot(this.right, this.pos);
   132     var y = vec3.dot(this.up, this.pos);
   133     var z = vec3.dot(this.target, this.pos);
   134     this.matrix = mat4.create([this.right[0], this.up[0], -this.target[0], 0,
   135 			       this.right[1], this.up[1], -this.target[1], 0,
   136 			       this.right[2], this.up[2], -this.target[2], 0,
   137 			       -x, -y, z, 1]);    
   138 }
   139 
   140 function Controller(keyActionMap, mouseActionMap, camera, machine, renderer)
   141 { 
   142     this.keyboard = new Keyboard(keyActionMap);
   143     this.mouse = new Mouse(mouseActionMap);
   144    
   145 }
   146 Controller.prototype.update = function()
   147 {
   148     this.keyboard.handle();
   149     this.mouse.handle();
   150 }
   151 
   152 function Mouse(actionMap)
   153 {
   154     this.actionMap = actionMap;
   155     this.pressed = [false, false, false];   
   156     this.wheelDelta = 0;
   157 }
   158 Mouse.prototype.buttonDown = function(event)
   159 {
   160     this.pressed[event.which-1] = true;
   161 }
   162 Mouse.prototype.buttonUp = function(event)
   163 {
   164     this.pressed[event.which-1] = false;
   165 }
   166 Mouse.prototype.move = function(event)
   167 {
   168     this.currentPos = [event.clientX, event.clientY];
   169 }
   170 Mouse.prototype.moveWheel = function(event)
   171 { 
   172     this.wheelDelta += event.wheelDelta;
   173 }
   174 Mouse.prototype.handle = function()
   175 {
   176     var pos = this.currentPos;
   177     if (pos && this.lastPos)
   178     {
   179 	var delta = [pos[0] - this.lastPos[0], pos[1] - this.lastPos[1]];
   180 	delta = normaliseMoveDelta(delta);
   181 	var action = this.actionMap["pitch"];
   182 	if (delta[1] != 0 && action) 
   183 	{
   184 	    for (var i = 0; i < 3; i++)
   185 	    {
   186 		if (this.pressed[i] != action[0][i]) break;
   187 		if (i == 2) action[1](delta[1]);
   188 	    }
   189 	} 
   190 	action = this.actionMap["yaw"];
   191 	if (delta[0] != 0 && action) 
   192 	{
   193 	    for (var i = 0; i < 3; i++)
   194 	    {
   195 		if (this.pressed[i] != action[0][i]) break;
   196 		if (i == 2) action[1](delta[0]);
   197 	    }
   198 	}
   199     }
   200     this.lastPos = pos;
   201     
   202     if (this.wheelDelta != 0)
   203     {
   204 	var delta = normaliseWheelDelta(this.wheelDelta);
   205 	var action = this.actionMap["wheel"];
   206 	if (delta != 0 && action) action(delta);
   207 	this.wheelDelta = 0;
   208     }
   209     
   210 }
   211 
   212 function normaliseMoveDelta(delta)
   213 {
   214     var width = renderer.context.canvas.width;
   215     var height = renderer.context.canvas.height;
   216     return [delta[0] / width, delta[1] / height];
   217 }
   218 
   219 function normaliseWheelDelta(delta)
   220 {
   221     return delta / 60;
   222 }
   223 
   224 function Keyboard(actionMap)
   225 {
   226     this.actionMap = actionMap;
   227     this.pressed = {};
   228 }
   229 Keyboard.prototype.keyDown = function(event)
   230 {
   231     this.pressed[event.keyCode] = true;    
   232 }
   233 Keyboard.prototype.keyUp = function(event)
   234 {
   235     this.pressed[event.keyCode] = false;
   236 }
   237 Keyboard.prototype.handle = function()
   238 {
   239     for (key in this.pressed)
   240     {
   241 	if (this.pressed[key])
   242 	{
   243 	    key = String.fromCharCode(key);
   244 	    if (this.actionMap[key]) this.actionMap[key](); 
   245 	}
   246     }
   247 }
   248 
   249 function Machine(scene)
   250 {
   251     this.scene = scene;
   252     this.lastUpdate = 0;
   253 }
   254 Machine.prototype.update = function(time)
   255 {
   256     if (this.lastUpdate != 0)
   257     {
   258 	var diff = time - this.lastUpdate;
   259 	
   260     }
   261     this.lastUpdate = time;
   262 }
   263 
   264 function read(file, handler)
   265 {
   266     var request = new XMLHttpRequest();
   267     request.open("GET", file);
   268     request.onreadystatechange = function() {
   269 	alert(request.readyState);
   270 	if (request.readyState == 4) {
   271 	    handler(request.responseText);
   272 	}
   273     }
   274     request.send();
   275 }		
   276 
   277 // event handler
   278 function expandContext()
   279 {
   280     renderer.context.expand();
   281 }
   282 
   283 function moveCameraLeft()
   284 {
   285     camera.moveLeft();
   286 }
   287 
   288 function moveCameraRight()
   289 {
   290     camera.moveRight();
   291 }
   292 
   293 function moveCameraUp()
   294 {
   295     camera.moveUp();
   296 }
   297 
   298 function moveCameraDown()
   299 {
   300     camera.moveDown();
   301 }
   302 
   303 function zoomCamera(delta)
   304 {
   305     camera.zoom(delta);
   306 }
   307 
   308 function pitchCamera(delta)
   309 {
   310     camera.pitch(delta);
   311 }
   312 
   313 function yawCamera(delta)
   314 {
   315     camera.yaw(delta);
   316 }
   317 
   318 function handleKeyDown(event)
   319 {
   320     controller.keyboard.keyDown(event);
   321 }
   322 
   323 function handleKeyUp(event)
   324 {
   325     controller.keyboard.keyUp(event);
   326 }
   327 
   328 function handleMouseDown(event)
   329 {
   330     controller.mouse.buttonDown(event);
   331 }
   332 
   333 function handleMouseUp(event)
   334 {
   335     controller.mouse.buttonUp(event);
   336 }
   337 
   338 function handleMouseMove(event)
   339 {
   340     controller.mouse.move(event);
   341 }
   342 
   343 function handleMouseWheel(event)
   344 {
   345     controller.mouse.moveWheel(event);
   346 }