Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: examples/js/cube.js

Issue 839553004: Mojo JS Bindings: Update User's Guide, examples (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « examples/js/README.md ('k') | examples/js/hello.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!mojo:js_content_handler 1 #!mojo:js_content_handler
2 2
3 // Copyright 2014 The Chromium Authors. All rights reserved. 3 // Copyright 2014 The Chromium Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be 4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file. 5 // found in the LICENSE file.
6 6
7 define("main", [ 7 define("main", [
8 "console", 8 "console",
9 "mojo/services/public/js/application", 9 "mojo/services/public/js/application",
10 "mojo/services/gpu/public/interfaces/command_buffer.mojom",
11 "mojo/services/geometry/public/interfaces/geometry.mojom", 10 "mojo/services/geometry/public/interfaces/geometry.mojom",
12 "mojo/services/gpu/public/interfaces/gpu.mojom", 11 "mojo/services/gpu/public/interfaces/gpu.mojom",
13 "mojo/services/gpu/public/interfaces/viewport_parameter_listener.mojom", 12 "mojo/services/gpu/public/interfaces/viewport_parameter_listener.mojom",
14 "mojo/services/native_viewport/public/interfaces/native_viewport.mojom", 13 "mojo/services/native_viewport/public/interfaces/native_viewport.mojom",
15 "mojo/public/js/core", 14 "mojo/public/js/core",
16 "services/js/modules/gl", 15 "services/js/modules/gl",
17 "services/js/modules/clock", 16 "services/js/modules/clock",
18 "timer", 17 "timer",
19 ], function(console, 18 ], function(console,
20 appModule, 19 application,
21 cbModule, 20 geometry,
22 geoModule, 21 gpu,
23 gpuModule, 22 vpl,
24 vplModule, 23 nv,
25 nvModule, 24 core,
26 coreModule, 25 gl,
27 glModule, 26 clock,
28 clockModule, 27 timer) {
29 timerModule) { 28
29 var Application = application.Application;
eseidel 2015/01/06 21:02:01 Can we use const?
30 var Context = gl.Context;
31 var Gpu = gpu.Gpu;
32 var Size = geometry.Size;
33 var ViewportParameterListener = vpl.ViewportParameterListener;
34 var NativeViewport = nv.NativeViewport;
30 35
31 const VERTEX_SHADER_SOURCE = [ 36 const VERTEX_SHADER_SOURCE = [
32 'uniform mat4 u_mvpMatrix;', 37 'uniform mat4 u_mvpMatrix;',
33 'attribute vec4 a_position;', 38 'attribute vec4 a_position;',
34 'void main()', 39 'void main()',
35 '{', 40 '{',
36 ' gl_Position = u_mvpMatrix * a_position;', 41 ' gl_Position = u_mvpMatrix * a_position;',
37 '}' 42 '}'
38 ].join('\n'); 43 ].join('\n');
39 44
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 vboIndices = gl.createBuffer(); 283 vboIndices = gl.createBuffer();
279 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vboIndices); 284 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vboIndices);
280 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, cubeIndices, gl.STATIC_DRAW); 285 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, cubeIndices, gl.STATIC_DRAW);
281 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0); 286 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0);
282 287
283 return cubeIndices.length; 288 return cubeIndices.length;
284 } 289 }
285 290
286 class GLES2ClientImpl { 291 class GLES2ClientImpl {
287 constructor (remotePipe, size) { 292 constructor (remotePipe, size) {
288 this.gl_ = new glModule.Context(remotePipe, this.contextLost.bind(this)); 293 this.gl_ = new Context(remotePipe, this.contextLost.bind(this));
289 this.lastTime_ = clockModule.seconds(); 294 this.lastTime_ = clock.seconds();
290 this.angle_ = 45; 295 this.angle_ = 45;
291 296
292 this.program_ = loadProgram(this.gl_); 297 this.program_ = loadProgram(this.gl_);
293 this.positionLocation_ = 298 this.positionLocation_ =
294 this.gl_.getAttribLocation(this.program_, 'a_position'); 299 this.gl_.getAttribLocation(this.program_, 'a_position');
295 this.mvpLocation_ = 300 this.mvpLocation_ =
296 this.gl_.getUniformLocation(this.program_, 'u_mvpMatrix'); 301 this.gl_.getUniformLocation(this.program_, 'u_mvpMatrix');
297 this.numIndices_ = generateCube(this.gl_); 302 this.numIndices_ = generateCube(this.gl_);
298 this.mvpMatrix_ = new ESMatrix(); 303 this.mvpMatrix_ = new ESMatrix();
299 this.mvpMatrix_.loadIdentity(); 304 this.mvpMatrix_.loadIdentity();
300 305
301 this.gl_.clearColor(0, 0, 0, 0); 306 this.gl_.clearColor(0, 0, 0, 0);
302 this.setDimensions(size); 307 this.setDimensions(size);
303 this.timer_ = 308 this.timer_ =
304 timerModule.createRepeating(16, this.handleTimer.bind(this)); 309 timer.createRepeating(16, this.handleTimer.bind(this));
305 } 310 }
306 311
307 setDimensions(size) { 312 setDimensions(size) {
308 this.width_ = size.width; 313 this.width_ = size.width;
309 this.height_ = size.height; 314 this.height_ = size.height;
310 this.gl_.resize(size.width, size.height, 1); 315 this.gl_.resize(size.width, size.height, 1);
311 } 316 }
312 317
313 drawCube() { 318 drawCube() {
314 this.gl_.viewport(0, 0, this.width_, this.height_); 319 this.gl_.viewport(0, 0, this.width_, this.height_);
315 this.gl_.clear(this.gl_.COLOR_BUFFER_BIT); 320 this.gl_.clear(this.gl_.COLOR_BUFFER_BIT);
316 this.gl_.useProgram(this.program_); 321 this.gl_.useProgram(this.program_);
317 this.gl_.bindBuffer(this.gl_.ARRAY_BUFFER, vboVertices); 322 this.gl_.bindBuffer(this.gl_.ARRAY_BUFFER, vboVertices);
318 this.gl_.bindBuffer(this.gl_.ELEMENT_ARRAY_BUFFER, vboIndices); 323 this.gl_.bindBuffer(this.gl_.ELEMENT_ARRAY_BUFFER, vboIndices);
319 this.gl_.vertexAttribPointer(this.positionLocation_, 3, this.gl_.FLOAT, 324 this.gl_.vertexAttribPointer(this.positionLocation_, 3, this.gl_.FLOAT,
320 false, 12, 0); 325 false, 12, 0);
321 this.gl_.enableVertexAttribArray(this.positionLocation_); 326 this.gl_.enableVertexAttribArray(this.positionLocation_);
322 this.gl_.uniformMatrix4fv(this.mvpLocation_, false, this.mvpMatrix_.m); 327 this.gl_.uniformMatrix4fv(this.mvpLocation_, false, this.mvpMatrix_.m);
323 this.gl_.drawElements(this.gl_.TRIANGLES, this.numIndices_, 328 this.gl_.drawElements(this.gl_.TRIANGLES, this.numIndices_,
324 this.gl_.UNSIGNED_SHORT, 0); 329 this.gl_.UNSIGNED_SHORT, 0);
325 this.gl_.swapBuffers(); 330 this.gl_.swapBuffers();
326 }; 331 };
327 332
328 handleTimer() { 333 handleTimer() {
329 var now = clockModule.seconds(); 334 var now = clock.seconds();
330 var secondsDelta = now - this.lastTime_; 335 var secondsDelta = now - this.lastTime_;
331 this.lastTime_ = now; 336 this.lastTime_ = now;
332 337
333 this.angle_ += this.getRotationForTimeDelta(secondsDelta); 338 this.angle_ += this.getRotationForTimeDelta(secondsDelta);
334 this.angle_ = this.angle_ % 360; 339 this.angle_ = this.angle_ % 360;
335 340
336 var aspect = this.width_ / this.height_; 341 var aspect = this.width_ / this.height_;
337 342
338 var perspective = new ESMatrix(); 343 var perspective = new ESMatrix();
339 perspective.loadIdentity(); 344 perspective.loadIdentity();
(...skipping 18 matching lines...) Expand all
358 }; 363 };
359 364
360 quit() { 365 quit() {
361 if (this.timer_) { 366 if (this.timer_) {
362 this.timer_.cancel(); 367 this.timer_.cancel();
363 this.timer_ = null; 368 this.timer_ = null;
364 } 369 }
365 } 370 }
366 } 371 }
367 372
368 class CubeDemo extends appModule.Application { 373 class CubeDemo extends Application {
369 initialize(args) { 374 initialize(args) {
370 this.viewport = this.shell.connectToService( 375 this.viewport = this.shell.connectToService(
371 "mojo:native_viewport_service", nvModule.NativeViewport, this); 376 "mojo:native_viewport_service", NativeViewport, this);
372 377
373 this.gpu = this.shell.connectToService( 378 this.gpu = this.shell.connectToService(
374 "mojo:native_viewport_service", gpuModule.Gpu); 379 "mojo:native_viewport_service", Gpu);
375 380
376 var app = this; 381 var app = this;
377 var viewportSize = new geoModule.Size({width: 800, height: 600}); 382 var viewportSize = new Size({width: 800, height: 600});
378 this.viewport.create(viewportSize).then( 383 this.viewport.create(viewportSize).then(
379 function(result) { 384 function(result) {
380 app.onViewportCreated(result.native_viewport_id, viewportSize); 385 app.onViewportCreated(result.native_viewport_id, viewportSize);
381 }); 386 });
382 387
383 this.viewport.setEventDispatcher(this); 388 this.viewport.setEventDispatcher(this);
384 this.viewport.show(); 389 this.viewport.show();
385 } 390 }
386 391
387 onViewportCreated(id, size) { 392 onViewportCreated(id, size) {
388 this.vpl = new vplModule.ViewportParameterListener.stubClass({ 393 this.vpl = new ViewportParameterListener.stubClass({
389 onVSyncParametersUpdated: function(timebase, interval) { 394 onVSyncParametersUpdated: function(timebase, interval) {
390 console.log("onVSyncParametersUpdated"); 395 console.log("onVSyncParametersUpdated");
391 } 396 }
392 }); 397 });
393 var pipe = coreModule.createMessagePipe(); 398 var pipe = core.createMessagePipe();
394 this.gpu.createOnscreenGLES2Context(id, size, pipe.handle1, this.vpl); 399 this.gpu.createOnscreenGLES2Context(id, size, pipe.handle1, this.vpl);
395 this.gles2_ = new GLES2ClientImpl(pipe.handle0, size); 400 this.gles2_ = new GLES2ClientImpl(pipe.handle0, size);
396 } 401 }
397 402
398 onEvent(event) { 403 onEvent(event) {
399 return Promise.resolve(); // This just gates the next event delivery 404 return Promise.resolve(); // This just gates the next event delivery
400 } 405 }
401 406
402 onSizeChanged(size) { 407 onSizeChanged(size) {
403 if (this.gles2_) 408 if (this.gles2_)
404 this.gles2_.setDimensions(size); 409 this.gles2_.setDimensions(size);
405 } 410 }
406 411
407 onDestroyed() { 412 onDestroyed() {
408 this.quit(); 413 this.quit();
409 } 414 }
410 } 415 }
411 416
412 return CubeDemo; 417 return CubeDemo;
413 }); 418 });
OLDNEW
« no previous file with comments | « examples/js/README.md ('k') | examples/js/hello.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698