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 loadProgram(gl) { |
| 39 var vertexShader = gl.createShader(gl.VERTEX_SHADER); |
| 40 gl.shaderSource(vertexShader, VERTEX_SHADER_SOURCE); |
| 41 gl.compileShader(vertexShader); |
| 42 |
| 43 var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); |
| 44 gl.shaderSource(fragmentShader, FRAGMENT_SHADER_SOURCE); |
| 45 gl.compileShader(fragmentShader); |
| 46 |
| 47 var program = gl.createProgram(); |
| 48 gl.attachShader(program, vertexShader); |
| 49 gl.attachShader(program, fragmentShader); |
| 50 |
| 51 gl.linkProgram(program); |
| 52 // TODO(aa): The C program checks for errors here, but I don't see how to do |
| 53 // this in WebGL. |
| 54 |
| 55 gl.deleteShader(vertexShader); |
| 56 gl.deleteShader(fragmentShader); |
| 57 console.log('done creating program'); |
| 58 |
| 59 return program; |
| 60 } |
| 61 |
| 62 var vboVertices; |
| 63 var vboIndices; |
| 64 function generateCube(gl) { |
| 65 var numVertices = 24 * 3; |
| 66 var numIndices = 12 * 3; |
| 67 |
| 68 var cubeVertices = new Float32Array([ |
| 69 -0.5, -0.5, -0.5, |
| 70 -0.5, -0.5, 0.5, |
| 71 0.5, -0.5, 0.5, |
| 72 0.5, -0.5, -0.5, |
| 73 -0.5, 0.5, -0.5, |
| 74 -0.5, 0.5, 0.5, |
| 75 0.5, 0.5, 0.5, |
| 76 0.5, 0.5, -0.5, |
| 77 -0.5, -0.5, -0.5, |
| 78 -0.5, 0.5, -0.5, |
| 79 0.5, 0.5, -0.5, |
| 80 0.5, -0.5, -0.5, |
| 81 -0.5, -0.5, 0.5, |
| 82 -0.5, 0.5, 0.5, |
| 83 0.5, 0.5, 0.5, |
| 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 ]); |
| 94 |
| 95 var cubeIndices = new Float32Array([ |
| 96 0, 2, 1, |
| 97 0, 3, 2, |
| 98 4, 5, 6, |
| 99 4, 6, 7, |
| 100 8, 9, 10, |
| 101 8, 10, 11, |
| 102 12, 15, 14, |
| 103 12, 14, 13, |
| 104 16, 17, 18, |
| 105 16, 18, 19, |
| 106 20, 23, 22, |
| 107 20, 22, 21 |
| 108 ]); |
| 109 |
| 110 // TODO(aa): The C++ program branches here on whether the pointer is |
| 111 // non-NULL. |
| 112 vboVertices = gl.createBuffer(); |
| 113 gl.bindBuffer(gl.ARRAY_BUFFER, vboVertices); |
| 114 gl.bufferData(gl.ARRAY_BUFFER, cubeVertices, gl.STATIC_DRAW); |
| 115 gl.bindBuffer(gl.ARRAY_BUFFER, 0); |
| 116 |
| 117 vboIndices = gl.createBuffer(); |
| 118 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vboIndices); |
| 119 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, cubeIndices, gl.STATIC_DRAW); |
| 120 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0); |
| 121 |
| 122 return cubeIndices.length; |
| 123 } |
| 124 |
| 125 function drawCube(gl, width, height, program, positionLocation) { |
| 126 gl.viewport(0, 0, width, height); |
| 127 gl.clear(gl.COLOR_BUFFER_BIT); |
| 128 gl.useProgram(program); |
| 129 gl.bindBuffer(gl.ARRAY_BUFFER, vboVertices); |
| 130 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vboIndices); |
| 131 gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 3 * 32, 0); |
| 132 gl.enableVertexAttribArray(positionLocation); |
| 133 // TODO(aa): Moar. |
| 134 } |
28 | 135 |
29 function NativeViewportClientImpl() { | 136 function NativeViewportClientImpl() { |
30 } | 137 } |
31 | 138 |
32 // TODO(aa): It is a bummer to need this stub object in JavaScript. We should | 139 // 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 | 140 // 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 | 141 // 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. | 142 // a separate base class to inherit from to receive callbacks. |
36 NativeViewportClientImpl.prototype = | 143 NativeViewportClientImpl.prototype = |
37 Object.create(nativeViewport.NativeViewportClientStub.prototype); | 144 Object.create(nativeViewport.NativeViewportClientStub.prototype); |
38 | 145 |
39 NativeViewportClientImpl.prototype.didOpen = function() { | 146 NativeViewportClientImpl.prototype.didOpen = function() { |
40 console.log("NativeViewportClientImpl.prototype.DidOpen"); | 147 console.log('NativeViewportClientImpl.prototype.DidOpen'); |
41 }; | 148 }; |
42 | 149 |
43 | 150 |
44 function GLES2ClientImpl() { | 151 function GLES2ClientImpl() { |
45 } | 152 } |
46 | 153 |
47 GLES2ClientImpl.prototype = | 154 GLES2ClientImpl.prototype = |
48 Object.create(gles2.GLES2ClientStub.prototype); | 155 Object.create(gles2.GLES2ClientStub.prototype); |
49 | 156 |
50 GLES2ClientImpl.prototype.didCreateContext = function(encoded, | 157 GLES2ClientImpl.prototype.didCreateContext = function(encoded, |
51 width, | 158 width, |
52 height) { | 159 height) { |
53 console.log("GLES2ClientImpl.prototype.didCreateContext"); | 160 console.log('GLES2ClientImpl.prototype.didCreateContext'); |
54 var gl = new gljs.Context(encoded, width, height); | 161 var gl = new gljs.Context(encoded, width, height); |
| 162 var program = loadProgram(gl); |
| 163 var positionLocation = gl.getAttribLocation(program, 'a_position'); |
| 164 var mvpLocation = gl.getUniformLocation(program, 'u_mvpMatrix'); |
| 165 var numIndices = generateCube(gl); |
55 | 166 |
56 var shader = gl.createShader(gl.VERTEX_SHADER); | 167 gl.clearColor(0, 0, 0, 0); |
57 console.log("shader is: ", String(shader)); | 168 console.log(String(program), String(positionLocation), String(mvpLocation), |
58 gl.shaderSource(shader, VERTEX_SHADER_SOURCE); | 169 String(numIndices)); |
59 gl.compileShader(shader); | 170 |
60 console.log("all done"); | 171 drawCube(gl, width, height, program, positionLocation); |
61 }; | 172 }; |
62 | 173 |
63 GLES2ClientImpl.prototype.contextLost = function() { | 174 GLES2ClientImpl.prototype.contextLost = function() { |
64 console.log("GLES2ClientImpl.prototype.contextLost"); | 175 console.log('GLES2ClientImpl.prototype.contextLost'); |
65 }; | 176 }; |
66 | 177 |
67 return function(handle) { | 178 return function(handle) { |
68 var nativeViewportConnection = new connector.Connection( | 179 var nativeViewportConnection = new connector.Connection( |
69 handle, | 180 handle, |
70 NativeViewportClientImpl, | 181 NativeViewportClientImpl, |
71 nativeViewport.NativeViewportProxy); | 182 nativeViewport.NativeViewportProxy); |
72 | 183 |
73 var gles2Handles = core.createMessagePipe(); | 184 var gles2Handles = core.createMessagePipe(); |
74 var gles2Connection = new connector.Connection( | 185 var gles2Connection = new connector.Connection( |
75 gles2Handles.handle0, GLES2ClientImpl, gles2.GLES2Proxy); | 186 gles2Handles.handle0, GLES2ClientImpl, gles2.GLES2Proxy); |
76 | 187 |
77 nativeViewportConnection.remote.open(); | 188 nativeViewportConnection.remote.open(); |
78 nativeViewportConnection.remote.createGLES2Context(gles2Handles.handle1); | 189 nativeViewportConnection.remote.createGLES2Context(gles2Handles.handle1); |
79 }; | 190 }; |
80 }); | 191 }); |
OLD | NEW |