Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 define([ | 5 define([ |
| 6 'console', | 6 'console', |
| 7 'timer', | |
| 7 'mojo/apps/js/bindings/connector', | 8 'mojo/apps/js/bindings/connector', |
| 8 'mojo/apps/js/bindings/core', | 9 'mojo/apps/js/bindings/core', |
| 9 'mojo/apps/js/bindings/gl', | 10 'mojo/apps/js/bindings/gl', |
| 10 'mojo/apps/js/bindings/threading', | 11 'mojo/apps/js/bindings/threading', |
| 11 'mojom/native_viewport', | 12 'mojom/native_viewport', |
| 12 'mojom/gles2', | 13 'mojom/gles2', |
| 13 'mojom/shell', | 14 'mojom/shell', |
| 14 ], function(console, | 15 ], function(console, |
| 16 timer, | |
| 15 connector, | 17 connector, |
| 16 core, | 18 core, |
| 17 gljs, | 19 gljs, |
| 18 threading, | 20 threading, |
| 19 nativeViewport, | 21 nativeViewport, |
| 20 gles2, | 22 gles2, |
| 21 shell) { | 23 shell) { |
| 22 | 24 |
| 23 const VERTEX_SHADER_SOURCE = [ | 25 const VERTEX_SHADER_SOURCE = [ |
| 24 'uniform mat4 u_mvpMatrix;', | 26 'uniform mat4 u_mvpMatrix;', |
| 25 'attribute vec4 a_position;', | 27 'attribute vec4 a_position;', |
| 26 'void main()', | 28 'void main()', |
| 27 '{', | 29 '{', |
| 28 ' gl_Position = u_mvpMatrix * a_position;', | 30 ' gl_Position = u_mvpMatrix * a_position;', |
| 29 '}' | 31 '}' |
| 30 ].join('\n'); | 32 ].join('\n'); |
| 31 | 33 |
| 32 const FRAGMENT_SHADER_SOURCE = [ | 34 const FRAGMENT_SHADER_SOURCE = [ |
| 33 'precision mediump float;', | 35 'precision mediump float;', |
| 34 'void main()', | 36 'void main()', |
| 35 '{', | 37 '{', |
| 36 ' gl_FragColor = vec4( 0.0, 1.0, 0.0, 1.0 );', | 38 ' gl_FragColor = vec4( 0.0, 1.0, 0.0, 1.0 );', |
| 37 '}' | 39 '}' |
| 38 ].join('\n'); | 40 ].join('\n'); |
| 39 | 41 |
| 40 function ESMatrix() { | 42 function ESMatrix() { |
| 41 this.m = new Float32Array(16); | 43 this.m = new Float32Array(16); |
| 42 } | 44 } |
| 43 | 45 |
| 46 ESMatrix.prototype.getIndex = function(x, y) { | |
| 47 return x * 4 + y; | |
| 48 } | |
| 49 | |
| 50 ESMatrix.prototype.set = function(x, y, v) { | |
| 51 this.m[this.getIndex(x, y)] = v; | |
| 52 }; | |
| 53 | |
| 54 ESMatrix.prototype.get = function(x, y) { | |
| 55 return this.m[this.getIndex(x, y)]; | |
| 56 }; | |
| 57 | |
| 44 ESMatrix.prototype.loadZero = function() { | 58 ESMatrix.prototype.loadZero = function() { |
| 45 for (var i = 0; i < this.m.length; i++) { | 59 for (var i = 0; i < this.m.length; i++) { |
| 46 this.m[i] = 0; | 60 this.m[i] = 0; |
| 47 } | 61 } |
| 48 }; | 62 }; |
| 49 | 63 |
| 50 ESMatrix.prototype.loadIdentity = function() { | 64 ESMatrix.prototype.loadIdentity = function() { |
| 51 this.loadZero(); | 65 this.loadZero(); |
| 52 for (var i = 0; i < 4; i++) { | 66 for (var i = 0; i < 4; i++) { |
| 53 this.m[i*4+i] = 1; | 67 this.set(i, i, 1); |
| 54 } | 68 } |
| 55 }; | 69 }; |
| 56 | 70 |
| 71 ESMatrix.prototype.multiply = function(a, b) { | |
| 72 var result = new ESMatrix(); | |
| 73 for (var i = 0; i < 4; i++) { | |
| 74 result.set(i, 0, | |
| 75 (a.get(i, 0) * b.get(0, 0)) + | |
| 76 (a.get(i, 1) * b.get(1, 0)) + | |
| 77 (a.get(i, 2) * b.get(2, 0)) + | |
| 78 (a.get(i, 3) * b.get(3, 0))); | |
| 79 | |
| 80 result.set(i, 1, | |
| 81 (a.get(i, 0) * b.get(0, 1)) + | |
| 82 (a.get(i, 1) * b.get(1, 1)) + | |
| 83 (a.get(i, 2) * b.get(2, 1)) + | |
| 84 (a.get(i, 3) * b.get(3, 1))); | |
| 85 | |
| 86 result.set(i, 2, | |
| 87 (a.get(i, 0) * b.get(0, 2)) + | |
| 88 (a.get(i, 1) * b.get(1, 2)) + | |
| 89 (a.get(i, 2) * b.get(2, 2)) + | |
| 90 (a.get(i, 3) * b.get(3, 2))); | |
| 91 | |
| 92 result.set(i, 3, | |
| 93 (a.get(i, 0) * b.get(0, 3)) + | |
| 94 (a.get(i, 1) * b.get(1, 3)) + | |
| 95 (a.get(i, 2) * b.get(2, 3)) + | |
| 96 (a.get(i, 3) * b.get(3, 3))); | |
| 97 } | |
| 98 for (var i = 0; i < result.m.length; i++) { | |
| 99 this.m[i] = result.m[i]; | |
| 100 } | |
| 101 }; | |
| 102 | |
| 103 ESMatrix.prototype.frustrum = function(left, right, bottom, top, nearZ, | |
| 104 farZ) { | |
| 105 var deltaX = right - left; | |
| 106 var deltaY = top - bottom; | |
| 107 var deltaZ = farZ - nearZ; | |
| 108 | |
| 109 if (nearZ < 0 || farZ < 0 || deltaZ < 0 || deltaY < 0 || deltaX < 0) { | |
| 110 return; | |
| 111 } | |
| 112 | |
| 113 var frust = new ESMatrix(); | |
| 114 frust.set(0, 0, 2 * nearZ / deltaX); | |
| 115 | |
| 116 frust.set(1, 1, 2 * nearZ / deltaY); | |
| 117 | |
| 118 frust.set(2, 0, (right + left) / deltaX); | |
| 119 frust.set(2, 1, (top + bottom) / deltaY); | |
| 120 frust.set(2, 2, -(nearZ + farZ) / deltaZ); | |
| 121 frust.set(2, 3, -1); | |
| 122 | |
| 123 frust.set(3, 2, -2 * nearZ * farZ / deltaZ); | |
| 124 | |
| 125 this.multiply(frust, this); | |
| 126 }; | |
| 127 | |
| 128 ESMatrix.prototype.perspective = function(fovY, aspect, nearZ, farZ) { | |
| 129 var frustrumH = Math.tan(fovY / 360 * Math.PI) * nearZ; | |
| 130 var frustrumW = frustrumH * aspect; | |
| 131 this.frustrum(-frustrumW, frustrumW, -frustrumH, frustrumH, nearZ, farZ); | |
| 132 }; | |
| 133 | |
| 134 ESMatrix.prototype.translate = function(tx, ty, tz) { | |
| 135 this.set(3, 0, this.get(3, 0) + this.get(0, 0) * | |
| 136 tx + this.get(1, 0) * ty + this.get(2, 0) * tz); | |
| 137 this.set(3, 1, this.get(3, 1) + this.get(0, 1) * | |
| 138 tx + this.get(1, 1) * ty + this.get(2, 1) * tz); | |
| 139 this.set(3, 2, this.get(3, 2) + this.get(0, 2) * | |
| 140 tx + this.get(1, 2) * ty + this.get(2, 2) * tz); | |
| 141 this.set(3, 3, this.get(3, 3) + this.get(0, 3) * | |
| 142 tx + this.get(1, 3) * ty + this.get(2, 3) * tz); | |
| 143 }; | |
| 144 | |
| 145 ESMatrix.prototype.rotate = function(angle, x, y, z) { | |
| 146 var mag = Math.sqrt(x * x + y * y + z * z); | |
| 147 var sinAngle = Math.sin(angle * Math.PI / 180); | |
| 148 var cosAngle = Math.cos(angle * Math.PI / 180); | |
| 149 if (mag <= 0) { | |
| 150 return; | |
| 151 } | |
| 152 | |
| 153 var xx, yy, zz, xy, yz, zx, xs, ys, zs, oneMinusCos; | |
| 154 var rotation = new ESMatrix(); | |
| 155 | |
| 156 x /= mag; | |
| 157 y /= mag; | |
| 158 z /= mag; | |
| 159 | |
| 160 xx = x * x; | |
| 161 yy = y * y; | |
| 162 zz = z * z; | |
| 163 xy = x * y; | |
| 164 yz = y * z; | |
| 165 zx = z * x; | |
| 166 xs = x * sinAngle; | |
| 167 ys = y * sinAngle; | |
| 168 zs = z * sinAngle; | |
| 169 oneMinusCos = 1 - cosAngle; | |
| 170 | |
| 171 rotation.set(0, 0, (oneMinusCos * xx) + cosAngle); | |
| 172 rotation.set(0, 1, (oneMinusCos * xy) - zs); | |
| 173 rotation.set(0, 2, (oneMinusCos * zx) + ys); | |
| 174 rotation.set(0, 3, 0); | |
| 175 | |
| 176 rotation.set(1, 0, (oneMinusCos * xy) + zs); | |
| 177 rotation.set(1, 1, (oneMinusCos * yy) + cosAngle); | |
| 178 rotation.set(1, 2, (oneMinusCos * yz) - xs); | |
| 179 rotation.set(1, 3, 0); | |
| 180 | |
| 181 rotation.set(2, 0, (oneMinusCos * zx) - ys); | |
| 182 rotation.set(2, 1, (oneMinusCos * yz) + xs); | |
| 183 rotation.set(2, 2, (oneMinusCos * zz) + cosAngle); | |
| 184 rotation.set(2, 3, 0); | |
| 185 | |
| 186 rotation.set(3, 0, 0); | |
| 187 rotation.set(3, 1, 0); | |
| 188 rotation.set(3, 2, 0); | |
| 189 rotation.set(3, 3, 1); | |
| 190 | |
| 191 this.multiply(rotation, this); | |
| 192 }; | |
| 193 | |
| 57 function loadProgram(gl) { | 194 function loadProgram(gl) { |
| 58 var vertexShader = gl.createShader(gl.VERTEX_SHADER); | 195 var vertexShader = gl.createShader(gl.VERTEX_SHADER); |
| 59 gl.shaderSource(vertexShader, VERTEX_SHADER_SOURCE); | 196 gl.shaderSource(vertexShader, VERTEX_SHADER_SOURCE); |
| 60 gl.compileShader(vertexShader); | 197 gl.compileShader(vertexShader); |
| 61 | 198 |
| 62 var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); | 199 var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); |
| 63 gl.shaderSource(fragmentShader, FRAGMENT_SHADER_SOURCE); | 200 gl.shaderSource(fragmentShader, FRAGMENT_SHADER_SOURCE); |
| 64 gl.compileShader(fragmentShader); | 201 gl.compileShader(fragmentShader); |
| 65 | 202 |
| 66 var program = gl.createProgram(); | 203 var program = gl.createProgram(); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 gl.bindBuffer(gl.ARRAY_BUFFER, 0); | 269 gl.bindBuffer(gl.ARRAY_BUFFER, 0); |
| 133 | 270 |
| 134 vboIndices = gl.createBuffer(); | 271 vboIndices = gl.createBuffer(); |
| 135 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vboIndices); | 272 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vboIndices); |
| 136 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, cubeIndices, gl.STATIC_DRAW); | 273 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, cubeIndices, gl.STATIC_DRAW); |
| 137 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0); | 274 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0); |
| 138 | 275 |
| 139 return cubeIndices.length; | 276 return cubeIndices.length; |
| 140 } | 277 } |
| 141 | 278 |
| 142 function drawCube(gl, width, height, program, positionLocation, mvpLocation, | |
| 143 numIndices, mvpMatrix) { | |
| 144 gl.viewport(0, 0, width, height); | |
| 145 gl.clear(gl.COLOR_BUFFER_BIT); | |
| 146 gl.useProgram(program); | |
| 147 gl.bindBuffer(gl.ARRAY_BUFFER, vboVertices); | |
| 148 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vboIndices); | |
| 149 gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 12, 0); | |
| 150 gl.enableVertexAttribArray(positionLocation); | |
| 151 gl.uniformMatrix4fv(mvpLocation, false, mvpMatrix.m); | |
| 152 gl.drawElements(gl.TRIANGLES, numIndices, gl.UNSIGNED_SHORT, 0); | |
| 153 } | |
| 154 | |
| 155 function SampleApp(shell) { | 279 function SampleApp(shell) { |
| 156 this.shell_ = shell; | 280 this.shell_ = shell; |
| 157 | 281 |
| 158 var pipe = new core.createMessagePipe(); | 282 var pipe = new core.createMessagePipe(); |
| 159 new connector.Connection(pipe.handle0, NativeViewportClientImpl, | 283 new connector.Connection(pipe.handle0, NativeViewportClientImpl, |
| 160 nativeViewport.NativeViewportProxy); | 284 nativeViewport.NativeViewportProxy); |
| 161 this.shell_.connect('mojo:mojo_native_viewport_service', pipe.handle1); | 285 this.shell_.connect('mojo:mojo_native_viewport_service', pipe.handle1); |
| 162 } | 286 } |
| 163 // TODO(aa): It is a bummer to need this stub object in JavaScript. We should | 287 // TODO(aa): It is a bummer to need this stub object in JavaScript. We should |
| 164 // have a 'client' object that contains both the sending and receiving bits of | 288 // have a 'client' object that contains both the sending and receiving bits of |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 182 NativeViewportClientImpl.prototype.onCreated = function() { | 306 NativeViewportClientImpl.prototype.onCreated = function() { |
| 183 console.log('NativeViewportClientImpl.prototype.OnCreated'); | 307 console.log('NativeViewportClientImpl.prototype.OnCreated'); |
| 184 }; | 308 }; |
| 185 NativeViewportClientImpl.prototype.didOpen = function() { | 309 NativeViewportClientImpl.prototype.didOpen = function() { |
| 186 console.log('NativeViewportClientImpl.prototype.DidOpen'); | 310 console.log('NativeViewportClientImpl.prototype.DidOpen'); |
| 187 }; | 311 }; |
| 188 | 312 |
| 189 | 313 |
| 190 function GLES2ClientImpl(remote) { | 314 function GLES2ClientImpl(remote) { |
| 191 this.remote_ = remote; | 315 this.remote_ = remote; |
| 316 this.lastTime_ = Date.now(); | |
| 317 this.angle_ = 45; | |
| 192 } | 318 } |
| 193 GLES2ClientImpl.prototype = | 319 GLES2ClientImpl.prototype = |
| 194 Object.create(gles2.GLES2ClientStub.prototype); | 320 Object.create(gles2.GLES2ClientStub.prototype); |
| 195 | 321 |
| 196 GLES2ClientImpl.prototype.didCreateContext = function(encoded, | 322 GLES2ClientImpl.prototype.didCreateContext = function(encoded, |
| 197 width, | 323 width, |
| 198 height) { | 324 height) { |
| 199 var gl = new gljs.Context(encoded, width, height); | 325 this.width_ = width; |
| 200 var program = loadProgram(gl); | 326 this.height_ = height; |
| 201 var positionLocation = gl.getAttribLocation(program, 'a_position'); | 327 this.gl_ = new gljs.Context(encoded, width, height); |
| 202 var mvpLocation = gl.getUniformLocation(program, 'u_mvpMatrix'); | 328 this.program_ = loadProgram(this.gl_); |
| 203 var numIndices = generateCube(gl); | 329 this.positionLocation_ = |
| 204 var mvpMatrix = new ESMatrix(); | 330 this.gl_.getAttribLocation(this.program_, 'a_position'); |
| 331 this.mvpLocation_ = | |
| 332 this.gl_.getUniformLocation(this.program_, 'u_mvpMatrix'); | |
| 333 this.numIndices_ = generateCube(this.gl_); | |
| 334 this.mvpMatrix_ = new ESMatrix(); | |
| 335 this.mvpMatrix_.loadIdentity(); | |
| 205 | 336 |
| 206 gl.clearColor(0, 0, 0, 0); | 337 this.gl_.clearColor(0, 0, 0, 0); |
| 207 mvpMatrix.loadIdentity(); | 338 this.timer_ = timer.createRepeating(16, this.handleTimer.bind(this)); |
| 208 drawCube(gl, width, height, program, positionLocation, mvpLocation, | 339 }; |
| 209 numIndices, mvpMatrix); | 340 |
| 210 gl.swapBuffers(); | 341 GLES2ClientImpl.prototype.drawCube = function() { |
| 342 this.gl_.viewport(0, 0, this.width_, this.height_); | |
| 343 this.gl_.clear(this.gl_.COLOR_BUFFER_BIT); | |
| 344 this.gl_.useProgram(this.program_); | |
| 345 this.gl_.bindBuffer(this.gl_.ARRAY_BUFFER, vboVertices); | |
| 346 this.gl_.bindBuffer(this.gl_.ELEMENT_ARRAY_BUFFER, vboIndices); | |
| 347 this.gl_.vertexAttribPointer(this.positionLocation_, 3, this.gl_.FLOAT, | |
| 348 false, 12, 0); | |
| 349 this.gl_.enableVertexAttribArray(this.positionLocation_); | |
| 350 this.gl_.uniformMatrix4fv(this.mvpLocation_, false, this.mvpMatrix_.m); | |
| 351 this.gl_.drawElements(this.gl_.TRIANGLES, this.numIndices_, | |
| 352 this.gl_.UNSIGNED_SHORT, 0); | |
| 353 this.gl_.swapBuffers(); | |
| 354 }; | |
| 355 | |
| 356 GLES2ClientImpl.prototype.handleTimer = function() { | |
| 357 var now = Date.now(); | |
| 358 var timeDelta = Date.now() - this.lastTime_; | |
|
abarth-chromium
2014/01/07 23:01:04
We should be use Mojo's monotonic clock rather tha
Aaron Boodman
2014/01/07 23:22:38
I'm always happy to add more bindings using our sh
| |
| 359 this.lastTime_ = now; | |
| 360 | |
| 361 this.angle_ += this.getRotationForTimeDelgate(timeDelta); | |
| 362 this.angle_ = this.angle_ % 360; | |
| 363 | |
| 364 var aspect = this.width_ / this.height_; | |
| 365 | |
| 366 var perspective = new ESMatrix(); | |
| 367 perspective.loadIdentity(); | |
| 368 perspective.perspective(60, aspect, 1, 20); | |
| 369 | |
| 370 var modelView = new ESMatrix(); | |
| 371 modelView.loadIdentity(); | |
| 372 modelView.translate(0, 0, -2); | |
| 373 modelView.rotate(this.angle_, 1, 0, 1); | |
| 374 | |
| 375 this.mvpMatrix_.multiply(modelView, perspective); | |
| 376 | |
| 377 this.drawCube(); | |
| 378 }; | |
| 379 | |
| 380 GLES2ClientImpl.prototype.getRotationForTimeDelgate = function(timeDelta) { | |
| 381 return timeDelta * .04; | |
| 211 }; | 382 }; |
| 212 | 383 |
| 213 GLES2ClientImpl.prototype.contextLost = function() { | 384 GLES2ClientImpl.prototype.contextLost = function() { |
| 214 console.log('GLES2ClientImpl.prototype.contextLost'); | 385 console.log('GLES2ClientImpl.prototype.contextLost'); |
| 215 }; | 386 }; |
| 216 | 387 |
| 217 | 388 |
| 218 return function(handle) { | 389 return function(handle) { |
| 219 new connector.Connection(handle, SampleApp, shell.ShellProxy); | 390 new connector.Connection(handle, SampleApp, shell.ShellProxy); |
| 220 }; | 391 }; |
| 221 }); | 392 }); |
| OLD | NEW |