1// Copyright (c) 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5function NaClAMBulletInit() { 6 aM.addEventListener('sceneloaded', NaClAMBulletSceneLoadedHandler); 7 aM.addEventListener('noscene', NaClAMBulletStepSceneHandler); 8 aM.addEventListener('sceneupdate', NaClAMBulletStepSceneHandler); 9} 10 11function NaClAMBulletLoadScene(sceneDescription) { 12 aM.sendMessage('loadscene', sceneDescription); 13} 14 15function NaClAMBulletSceneLoadedHandler(msg) { 16 console.log('Scene loaded.'); 17 console.log('Scene object count = ' + msg.header.sceneobjectcount); 18} 19 20function NaClAMBulletPickObject(objectTableIndex, cameraPos, hitPos) { 21 aM.sendMessage('pickobject', {index: objectTableIndex, cpos: [cameraPos.x, cameraPos.y, cameraPos.z], pos: [hitPos.x,hitPos.y,hitPos.z]}); 22} 23 24function NaClAMBulletDropObject() { 25 aM.sendMessage('dropobject', {}); 26} 27 28// Values used to display simulation time every second. 29var fps = { 30 lastTimeMs: +new Date(), 31 sumSimTime: 0, 32 numSteps: 0 33}; 34 35function NaClAMBulletStepSceneHandler(msg) { 36 // Step the scene 37 var i; 38 var j; 39 var numTransforms = 0; 40 if (msg.header.cmd == 'sceneupdate') { 41 if (skipSceneUpdates > 0) { 42 skipSceneUpdates--; 43 return; 44 } 45 TransformBuffer = new Float32Array(msg.frames[0]); 46 numTransforms = TransformBuffer.length/16; 47 for (i = 0; i < numTransforms; i++) { 48 for (j = 0; j < 16; j++) { 49 objects[i].matrixWorld.elements[j] = TransformBuffer[i*16+j]; 50 } 51 } 52 53 var simTime = msg.header.simtime; 54 fps.sumSimTime += simTime; 55 fps.numSteps++; 56 57 // Update FPS. 58 var curTimeMs = +new Date(); 59 if (curTimeMs - fps.lastTimeMs > 1000) { // 1 sec 60 var meanSimTime = fps.sumSimTime / fps.numSteps; 61 $('simulationTime').textContent = meanSimTime.toFixed(0); 62 $('fps').textContent = 63 (fps.numSteps * 1000 / (curTimeMs - fps.lastTimeMs)).toFixed(1); 64 fps.lastTimeMs = curTimeMs; 65 fps.sumSimTime = 0; 66 fps.numSteps = 0; 67 } 68 } 69} 70