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