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 "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 ], function(console, | 13 ], function(console, |
14 connector, | 14 connector, |
15 core, | 15 core, |
16 gljs, | 16 gljs, |
17 threading, | 17 threading, |
18 nativeViewport, | 18 nativeViewport, |
19 gles2) { | 19 gles2) { |
20 | 20 |
21 const VERTEX_SHADER_SOURCE = | 21 const VERTEX_SHADER_SOURCE = [ |
22 "uniform mat4 u_mvpMatrix; \n" + | 22 'uniform mat4 u_mvpMatrix;', |
23 "attribute vec4 a_position; \n" + | 23 'attribute vec4 a_position;', |
24 "void main() \n" + | 24 'void main()', |
25 "{ \n" + | 25 '{', |
26 " gl_Position = u_mvpMatrix * a_position; \n" + | 26 ' gl_Position = u_mvpMatrix * a_position;', |
27 "} \n"; | 27 '}' |
28 ].join('\n'); | |
29 | |
30 const FRAGMENT_SHADER_SOURCE = [ | |
31 'precision mediump float;', | |
32 'void main()', | |
33 '{', | |
34 ' gl_FragColor = vec4( 0.0, 1.0, 0.0, 1.0 );', | |
35 '}' | |
36 ].join('\n'); | |
37 | |
38 function ESMatrix() { | |
39 this.m = new Float32Array(16); | |
40 } | |
41 | |
42 ESMatrix.prototype.loadZero = function() { | |
43 for (var i = 0; i < this.m.length; i++) { | |
44 this.m[i] = 0; | |
45 } | |
46 }; | |
47 | |
48 ESMatrix.prototype.loadIdentity = function() { | |
49 this.loadZero(); | |
50 for (var i = 0; i < 4; i++) { | |
51 this.m[i*4+i] = 1; | |
52 } | |
53 }; | |
54 | |
55 function loadProgram(gl) { | |
56 var vertexShader = gl.createShader(gl.VERTEX_SHADER); | |
57 gl.shaderSource(vertexShader, VERTEX_SHADER_SOURCE); | |
58 gl.compileShader(vertexShader); | |
59 | |
60 var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); | |
61 gl.shaderSource(fragmentShader, FRAGMENT_SHADER_SOURCE); | |
62 gl.compileShader(fragmentShader); | |
63 | |
64 var program = gl.createProgram(); | |
65 gl.attachShader(program, vertexShader); | |
66 gl.attachShader(program, fragmentShader); | |
67 | |
68 gl.linkProgram(program); | |
69 // TODO(aa): Check for errors using getProgramiv and LINK_STATUS. | |
70 | |
71 gl.deleteShader(vertexShader); | |
72 gl.deleteShader(fragmentShader); | |
73 | |
74 return program; | |
75 } | |
76 | |
77 var vboVertices; | |
78 var vboIndices; | |
79 function generateCube(gl) { | |
80 var numVertices = 24 * 3; | |
81 var numIndices = 12 * 3; | |
82 | |
83 var cubeVertices = new Float32Array([ | |
84 -0.5, -0.5, -0.5, | |
85 -0.5, -0.5, 0.5, | |
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 ]); | |
109 | |
110 var cubeIndices = new Uint16Array([ | |
111 0, 2, 1, | |
112 0, 3, 2, | |
113 4, 5, 6, | |
114 4, 6, 7, | |
115 8, 9, 10, | |
116 8, 10, 11, | |
117 12, 15, 14, | |
118 12, 14, 13, | |
119 16, 17, 18, | |
120 16, 18, 19, | |
121 20, 23, 22, | |
122 20, 22, 21 | |
123 ]); | |
124 | |
125 // TODO(aa): The C++ program branches here on whether the pointer is | |
126 // non-NULL. | |
127 vboVertices = gl.createBuffer(); | |
128 gl.bindBuffer(gl.ARRAY_BUFFER, vboVertices); | |
129 gl.bufferData(gl.ARRAY_BUFFER, cubeVertices, gl.STATIC_DRAW); | |
130 gl.bindBuffer(gl.ARRAY_BUFFER, 0); | |
131 | |
132 vboIndices = gl.createBuffer(); | |
133 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vboIndices); | |
134 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, cubeIndices, gl.STATIC_DRAW); | |
135 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0); | |
136 | |
137 return cubeIndices.length; | |
138 } | |
139 | |
140 function drawCube(gl, width, height, program, positionLocation, mvpLocation, | |
141 numIndices, mvpMatrix) { | |
142 gl.viewport(0, 0, width, height); | |
143 gl.clear(gl.COLOR_BUFFER_BIT); | |
144 gl.useProgram(program); | |
145 gl.bindBuffer(gl.ARRAY_BUFFER, vboVertices); | |
146 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vboIndices); | |
147 gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 12, 0); | |
148 gl.enableVertexAttribArray(positionLocation); | |
149 gl.uniformMatrix4fv(mvpLocation, false, mvpMatrix.m); | |
150 gl.drawElements(gl.TRIANGLES, numIndices, gl.UNSIGNED_SHORT, 0); | |
151 } | |
28 | 152 |
29 function NativeViewportClientImpl() { | 153 function NativeViewportClientImpl() { |
30 } | 154 } |
31 | 155 |
32 // TODO(aa): It is a bummer to need this stub object in JavaScript. We should | 156 // 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 | 157 // 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 | 158 // 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. | 159 // a separate base class to inherit from to receive callbacks. |
36 NativeViewportClientImpl.prototype = | 160 NativeViewportClientImpl.prototype = |
37 Object.create(nativeViewport.NativeViewportClientStub.prototype); | 161 Object.create(nativeViewport.NativeViewportClientStub.prototype); |
38 | 162 |
39 NativeViewportClientImpl.prototype.didOpen = function() { | 163 NativeViewportClientImpl.prototype.didOpen = function() { |
40 console.log("NativeViewportClientImpl.prototype.DidOpen"); | 164 console.log(['NativeViewportClientImpl.prototype.DidOpen']); |
abarth-chromium
2013/12/18 01:19:30
Why the [ ] ?
Aaron Boodman
2013/12/18 02:19:08
Jochen recently changed it to be required:
http:/
| |
41 }; | 165 }; |
42 | 166 |
43 | 167 |
44 function GLES2ClientImpl() { | 168 function GLES2ClientImpl() { |
45 } | 169 } |
46 | 170 |
47 GLES2ClientImpl.prototype = | 171 GLES2ClientImpl.prototype = |
48 Object.create(gles2.GLES2ClientStub.prototype); | 172 Object.create(gles2.GLES2ClientStub.prototype); |
49 | 173 |
50 GLES2ClientImpl.prototype.didCreateContext = function(encoded, | 174 GLES2ClientImpl.prototype.didCreateContext = function(encoded, |
51 width, | 175 width, |
52 height) { | 176 height) { |
53 console.log("GLES2ClientImpl.prototype.didCreateContext"); | 177 console.log(['GLES2ClientImpl.prototype.didCreateContext']); |
54 var gl = new gljs.Context(encoded, width, height); | 178 var gl = new gljs.Context(encoded, width, height); |
179 var program = loadProgram(gl); | |
180 var positionLocation = gl.getAttribLocation(program, 'a_position'); | |
181 var mvpLocation = gl.getUniformLocation(program, 'u_mvpMatrix'); | |
182 var numIndices = generateCube(gl); | |
183 var mvpMatrix = new ESMatrix(); | |
55 | 184 |
56 var shader = gl.createShader(gl.VERTEX_SHADER); | 185 gl.clearColor(0, 0, 0, 0); |
57 console.log("shader is: ", String(shader)); | 186 mvpMatrix.loadIdentity(); |
58 gl.shaderSource(shader, VERTEX_SHADER_SOURCE); | 187 drawCube(gl, width, height, program, positionLocation, mvpLocation, |
59 gl.compileShader(shader); | 188 numIndices, mvpMatrix); |
60 console.log("all done"); | 189 gl.swapBuffers(); |
61 }; | 190 }; |
62 | 191 |
63 GLES2ClientImpl.prototype.contextLost = function() { | 192 GLES2ClientImpl.prototype.contextLost = function() { |
64 console.log("GLES2ClientImpl.prototype.contextLost"); | 193 console.log(['GLES2ClientImpl.prototype.contextLost']); |
65 }; | 194 }; |
66 | 195 |
67 return function(handle) { | 196 return function(handle) { |
68 var nativeViewportConnection = new connector.Connection( | 197 var nativeViewportConnection = new connector.Connection( |
69 handle, | 198 handle, |
70 NativeViewportClientImpl, | 199 NativeViewportClientImpl, |
71 nativeViewport.NativeViewportProxy); | 200 nativeViewport.NativeViewportProxy); |
72 | 201 |
73 var gles2Handles = core.createMessagePipe(); | 202 var gles2Handles = core.createMessagePipe(); |
74 var gles2Connection = new connector.Connection( | 203 var gles2Connection = new connector.Connection( |
75 gles2Handles.handle0, GLES2ClientImpl, gles2.GLES2Proxy); | 204 gles2Handles.handle0, GLES2ClientImpl, gles2.GLES2Proxy); |
76 | 205 |
77 nativeViewportConnection.remote.open(); | 206 nativeViewportConnection.remote.open(); |
78 nativeViewportConnection.remote.createGLES2Context(gles2Handles.handle1); | 207 nativeViewportConnection.remote.createGLES2Context(gles2Handles.handle1); |
79 }; | 208 }; |
80 }); | 209 }); |
OLD | NEW |