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

Side by Side Diff: mojo/apps/js/main.js

Issue 114883003: Implement more of the JavaScript GL API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: asdf Created 7 years 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 | Annotate | Revision Log
« no previous file with comments | « mojo/apps/js/bindings/gl/opaque.cc ('k') | mojo/mojo_apps.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "mojo/apps/js/bindings/connector", 7 'mojo/apps/js/bindings/connector',
8 "mojo/apps/js/bindings/core", 8 'mojo/apps/js/bindings/core',
9 "mojo/apps/js/bindings/gl", 9 'mojo/apps/js/bindings/gl',
10 "mojo/apps/js/bindings/threading", 10 'mojo/apps/js/bindings/threading',
11 "mojom/native_viewport", 11 'mojom/native_viewport',
12 "mojom/gles2", 12 'mojom/gles2',
13 'mojom/shell',
13 ], function(console, 14 ], function(console,
14 connector, 15 connector,
15 core, 16 core,
16 gljs, 17 gljs,
17 threading, 18 threading,
18 nativeViewport, 19 nativeViewport,
19 gles2) { 20 gles2,
20 21 shell) {
21 const VERTEX_SHADER_SOURCE = 22
22 "uniform mat4 u_mvpMatrix; \n" + 23 const VERTEX_SHADER_SOURCE = [
23 "attribute vec4 a_position; \n" + 24 'uniform mat4 u_mvpMatrix;',
24 "void main() \n" + 25 'attribute vec4 a_position;',
25 "{ \n" + 26 'void main()',
26 " gl_Position = u_mvpMatrix * a_position; \n" + 27 '{',
27 "} \n"; 28 ' gl_Position = u_mvpMatrix * a_position;',
28 29 '}'
29 function NativeViewportClientImpl() { 30 ].join('\n');
30 } 31
31 32 const FRAGMENT_SHADER_SOURCE = [
33 'precision mediump float;',
34 'void main()',
35 '{',
36 ' gl_FragColor = vec4( 0.0, 1.0, 0.0, 1.0 );',
37 '}'
38 ].join('\n');
39
40 function ESMatrix() {
41 this.m = new Float32Array(16);
42 }
43
44 ESMatrix.prototype.loadZero = function() {
45 for (var i = 0; i < this.m.length; i++) {
46 this.m[i] = 0;
47 }
48 };
49
50 ESMatrix.prototype.loadIdentity = function() {
51 this.loadZero();
52 for (var i = 0; i < 4; i++) {
53 this.m[i*4+i] = 1;
54 }
55 };
56
57 function loadProgram(gl) {
58 var vertexShader = gl.createShader(gl.VERTEX_SHADER);
59 gl.shaderSource(vertexShader, VERTEX_SHADER_SOURCE);
60 gl.compileShader(vertexShader);
61
62 var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
63 gl.shaderSource(fragmentShader, FRAGMENT_SHADER_SOURCE);
64 gl.compileShader(fragmentShader);
65
66 var program = gl.createProgram();
67 gl.attachShader(program, vertexShader);
68 gl.attachShader(program, fragmentShader);
69
70 gl.linkProgram(program);
71 // TODO(aa): Check for errors using getProgramiv and LINK_STATUS.
72
73 gl.deleteShader(vertexShader);
74 gl.deleteShader(fragmentShader);
75
76 return program;
77 }
78
79 var vboVertices;
80 var vboIndices;
81 function generateCube(gl) {
82 var numVertices = 24 * 3;
83 var numIndices = 12 * 3;
84
85 var cubeVertices = new Float32Array([
86 -0.5, -0.5, -0.5,
87 -0.5, -0.5, 0.5,
88 0.5, -0.5, 0.5,
89 0.5, -0.5, -0.5,
90 -0.5, 0.5, -0.5,
91 -0.5, 0.5, 0.5,
92 0.5, 0.5, 0.5,
93 0.5, 0.5, -0.5,
94 -0.5, -0.5, -0.5,
95 -0.5, 0.5, -0.5,
96 0.5, 0.5, -0.5,
97 0.5, -0.5, -0.5,
98 -0.5, -0.5, 0.5,
99 -0.5, 0.5, 0.5,
100 0.5, 0.5, 0.5,
101 0.5, -0.5, 0.5,
102 -0.5, -0.5, -0.5,
103 -0.5, -0.5, 0.5,
104 -0.5, 0.5, 0.5,
105 -0.5, 0.5, -0.5,
106 0.5, -0.5, -0.5,
107 0.5, -0.5, 0.5,
108 0.5, 0.5, 0.5,
109 0.5, 0.5, -0.5
110 ]);
111
112 var cubeIndices = new Uint16Array([
113 0, 2, 1,
114 0, 3, 2,
115 4, 5, 6,
116 4, 6, 7,
117 8, 9, 10,
118 8, 10, 11,
119 12, 15, 14,
120 12, 14, 13,
121 16, 17, 18,
122 16, 18, 19,
123 20, 23, 22,
124 20, 22, 21
125 ]);
126
127 // TODO(aa): The C++ program branches here on whether the pointer is
128 // non-NULL.
129 vboVertices = gl.createBuffer();
130 gl.bindBuffer(gl.ARRAY_BUFFER, vboVertices);
131 gl.bufferData(gl.ARRAY_BUFFER, cubeVertices, gl.STATIC_DRAW);
132 gl.bindBuffer(gl.ARRAY_BUFFER, 0);
133
134 vboIndices = gl.createBuffer();
135 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vboIndices);
136 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, cubeIndices, gl.STATIC_DRAW);
137 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0);
138
139 return cubeIndices.length;
140 }
141
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) {
156 this.shell_ = shell;
157
158 var pipe = new core.createMessagePipe();
159 new connector.Connection(pipe.handle0, NativeViewportClientImpl,
160 nativeViewport.NativeViewportProxy);
161 this.shell_.connect('mojo:mojo_native_viewport_service', pipe.handle1);
162 }
32 // TODO(aa): It is a bummer to need this stub object in JavaScript. We should 163 // TODO(aa): It is a bummer to need this stub object in JavaScript. We should
33 // have a 'client' object that contains both the sending and receiving bits of 164 // have a 'client' object that contains both the sending and receiving bits of
34 // the client side of the interface. Since JS is loosely typed, we do not need 165 // the client side of the interface. Since JS is loosely typed, we do not need
35 // a separate base class to inherit from to receive callbacks. 166 // a separate base class to inherit from to receive callbacks.
167 SampleApp.prototype = Object.create(shell.ShellClientStub.prototype);
168
169
170 function NativeViewportClientImpl(remote) {
171 this.remote_ = remote;
172
173 var pipe = core.createMessagePipe();
174 new connector.Connection(pipe.handle0, GLES2ClientImpl, gles2.GLES2Proxy);
175
176 this.remote_.open();
177 this.remote_.createGLES2Context(pipe.handle1);
178 }
36 NativeViewportClientImpl.prototype = 179 NativeViewportClientImpl.prototype =
37 Object.create(nativeViewport.NativeViewportClientStub.prototype); 180 Object.create(nativeViewport.NativeViewportClientStub.prototype);
38 181
182 NativeViewportClientImpl.prototype.onCreated = function() {
183 console.log(['NativeViewportClientImpl.prototype.OnCreated']);
184 };
39 NativeViewportClientImpl.prototype.didOpen = function() { 185 NativeViewportClientImpl.prototype.didOpen = function() {
40 console.log("NativeViewportClientImpl.prototype.DidOpen"); 186 console.log(['NativeViewportClientImpl.prototype.DidOpen']);
41 }; 187 };
42 188
43 189
44 function GLES2ClientImpl() { 190 function GLES2ClientImpl(remote) {
45 } 191 this.remote_ = remote;
46 192 }
47 GLES2ClientImpl.prototype = 193 GLES2ClientImpl.prototype =
48 Object.create(gles2.GLES2ClientStub.prototype); 194 Object.create(gles2.GLES2ClientStub.prototype);
49 195
50 GLES2ClientImpl.prototype.didCreateContext = function(encoded, 196 GLES2ClientImpl.prototype.didCreateContext = function(encoded,
51 width, 197 width,
52 height) { 198 height) {
53 console.log("GLES2ClientImpl.prototype.didCreateContext");
54 var gl = new gljs.Context(encoded, width, height); 199 var gl = new gljs.Context(encoded, width, height);
55 200 var program = loadProgram(gl);
56 var shader = gl.createShader(gl.VERTEX_SHADER); 201 var positionLocation = gl.getAttribLocation(program, 'a_position');
57 console.log("shader is: ", String(shader)); 202 var mvpLocation = gl.getUniformLocation(program, 'u_mvpMatrix');
58 gl.shaderSource(shader, VERTEX_SHADER_SOURCE); 203 var numIndices = generateCube(gl);
59 gl.compileShader(shader); 204 var mvpMatrix = new ESMatrix();
60 console.log("all done"); 205
206 gl.clearColor(0, 0, 0, 0);
207 mvpMatrix.loadIdentity();
208 drawCube(gl, width, height, program, positionLocation, mvpLocation,
209 numIndices, mvpMatrix);
210 gl.swapBuffers();
61 }; 211 };
62 212
63 GLES2ClientImpl.prototype.contextLost = function() { 213 GLES2ClientImpl.prototype.contextLost = function() {
64 console.log("GLES2ClientImpl.prototype.contextLost"); 214 console.log(['GLES2ClientImpl.prototype.contextLost']);
65 }; 215 };
216
66 217
67 return function(handle) { 218 return function(handle) {
68 var nativeViewportConnection = new connector.Connection( 219 new connector.Connection(handle, SampleApp, shell.ShellProxy);
69 handle,
70 NativeViewportClientImpl,
71 nativeViewport.NativeViewportProxy);
72
73 var gles2Handles = core.createMessagePipe();
74 var gles2Connection = new connector.Connection(
75 gles2Handles.handle0, GLES2ClientImpl, gles2.GLES2Proxy);
76
77 nativeViewportConnection.remote.open();
78 nativeViewportConnection.remote.createGLES2Context(gles2Handles.handle1);
79 }; 220 };
80 }); 221 });
OLDNEW
« no previous file with comments | « mojo/apps/js/bindings/gl/opaque.cc ('k') | mojo/mojo_apps.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698