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