PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/sdk/tests/conformance/more/demos/opengl_web.html

https://github.com/gabadie/WebGL
HTML | 607 lines | 484 code | 97 blank | 26 comment | 0 complexity | 38a6d34af26b6a327b391bdcf8ca4f66 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <!--
  6. /*
  7. ** Copyright (c) 2012 The Khronos Group Inc.
  8. **
  9. ** Permission is hereby granted, free of charge, to any person obtaining a
  10. ** copy of this software and/or associated documentation files (the
  11. ** "Materials"), to deal in the Materials without restriction, including
  12. ** without limitation the rights to use, copy, modify, merge, publish,
  13. ** distribute, sublicense, and/or sell copies of the Materials, and to
  14. ** permit persons to whom the Materials are furnished to do so, subject to
  15. ** the following conditions:
  16. **
  17. ** The above copyright notice and this permission notice shall be included
  18. ** in all copies or substantial portions of the Materials.
  19. **
  20. ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  24. ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  25. ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  26. ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
  27. */
  28. -->
  29. <title>OpenGL for the web</title>
  30. <script type="application/x-javascript" src="../util.js"></script>
  31. <script type="application/x-javascript">
  32. function log(msg) {
  33. document.getElementById('note').textContent += "\n"+msg;
  34. }
  35. function init(ev) {
  36. var canvas = document.getElementById('canvas');
  37. var gl = getGLContext(canvas);
  38. var shader = new Shader(gl, "ppix-vert", "ppix-frag");
  39. shader.compile();
  40. var fbo = new FBO(gl, canvas.width, canvas.height);
  41. var fbo2 = new FBO(gl, canvas.width, canvas.height);
  42. var fbo3 = new FBO(gl, canvas.width, canvas.height);
  43. var depth = new Shader(gl, "depth-vert", "depth-frag");
  44. var identity = new Filter(gl, "identity-vert", "identity-frag");
  45. var unpremult = new Filter(gl, "identity-vert", "unpremult-frag");
  46. var hblur = new Filter(gl, "identity-vert", "hblur-frag");
  47. var vblur = new Filter(gl, "identity-vert", "vblur-frag");
  48. var hdof = new Filter(gl, "identity-vert", "hdof-frag");
  49. var vdof = new Filter(gl, "identity-vert", "vdof-frag");
  50. redraw(canvas, gl, shader, fbo, fbo2, fbo3, depth, identity, unpremult, hblur, vblur, hdof, vdof);
  51. setInterval(function(){
  52. redraw(canvas, gl, shader, fbo, fbo2, fbo3, depth, identity, unpremult, hblur, vblur, hdof, vdof);
  53. }, 33);
  54. }
  55. function drawCube (gl, shader, angle, axis, x,y,z, s, va, na, ta) {
  56. Matrix.copyMatrix(look, vmat);
  57. Matrix.translate3InPlace(x,y,z,vmat);
  58. Matrix.scale1InPlace(s,vmat);
  59. Matrix.rotateInPlace(angle, axis, vmat);
  60. // Note: we could just use mat3(MVMatrix) as the normal matrix
  61. // as MVMatrix has only rotations, translations and uniform scaling
  62. // <=> MVMatrix is a scaled orthonormal matrix
  63. // hence normalize(mat3(MVMatrix)*v) == normalize(mat3(transpose(inverse(MVMatrix))*v)
  64. //
  65. // But let's do it the hard way to see if Matrix.inverse3x3 works...
  66. Matrix.inverseTo3x3InPlace(vmat, nmat);
  67. Matrix.transpose3x3InPlace(nmat);
  68. shader.uniformMatrix4fv("MVMatrix", vmat);
  69. shader.uniformMatrix3fv("NMatrix", nmat);
  70. var cube = Cube.getCachedVBO(gl);
  71. cube.draw(va, na, ta);
  72. }
  73. var carr = [];
  74. for (var i=0; i<25; i++) {
  75. carr.push([Math.random(), Math.random(), Math.random()]);
  76. }
  77. function drawScene (gl, shader, va, na, ta) {
  78. var ot = new Date().getTime();
  79. var t = ot;
  80. shader.uniformMatrix4fv("PMatrix", pmat);
  81. for (var i=0; i<carr.length; i++){
  82. var c = carr[i];
  83. var f = c[1] < 0.5 ? 1 : -1;
  84. var t = ot;
  85. drawCube(gl, shader,
  86. (t/(f*400*(c[0]+0.5))) % (2*Math.PI), c,
  87. 0.45+0.8*c[2],
  88. -0.4+Math.cos((i/carr.length*Math.PI*2)+t/1000),
  89. 0.8+Math.sin((i/carr.length*Math.PI*2)+t/1000)*3.2,
  90. 0.05 + Math.pow((c[0]+c[1]+c[2])*0.33, 2)*0.3,
  91. va, na, ta);
  92. }
  93. }
  94. var nmat = Matrix.newIdentity3x3();
  95. var vmat = Matrix.newIdentity();
  96. var vmat2 = Matrix.newIdentity();
  97. var pmat = null;
  98. var look = Matrix.lookAt([4,-1,8], [-0.2,0,0], [0,1,0]);
  99. var useDoF = false;
  100. var firstFrame = true;
  101. function redraw(canvas, gl, shader, fbo, fbo2, fbo3, depth, identity, unpremult, hblur, vblur, hdof, vdof) {
  102. var doDoF = useDoF;
  103. gl.viewport(0, 0, canvas.width, canvas.height);
  104. gl.clearColor(0.0, 0.0, 0.0, 0.0);
  105. gl.enable(gl.DEPTH_TEST);
  106. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  107. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  108. fbo.use();
  109. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  110. shader.use();
  111. var va = shader.attrib("Vertex");
  112. var na = shader.attrib("Normal");
  113. var ta = shader.attrib("Tex");
  114. if (pmat == null)
  115. pmat = Matrix.perspective(30, canvas.width/canvas.height, 1, 100);
  116. shader.uniform4f("MaterialSpecular", 0.95, 0.9, 0.6, 1);
  117. shader.uniform4f("MaterialDiffuse", 0.50, 0.35, 0.35, 1);
  118. shader.uniform4f("MaterialAmbient", 0.0, 0.1, 0.2, 1);
  119. shader.uniform1f("MaterialShininess", 1.5);
  120. shader.uniform4f("GlobalAmbient", 0.1, 0.1, 0.1, 1);
  121. shader.uniform4f("LightPos", 1, 5, 3, 1.0);
  122. shader.uniform4f("LightSpecular", 0.9, 0.9, 0.9, 1);
  123. shader.uniform4f("LightDiffuse", 0.8, 0.8, 0.8, 1);
  124. shader.uniform4f("LightAmbient", 0.0, 0.06, 0.2, 1);
  125. shader.uniform1f("LightConstantAtt", 0.0);
  126. shader.uniform1f("LightLinearAtt", 0.1);
  127. shader.uniform1f("LightQuadraticAtt", 0.0);
  128. drawScene(gl, shader, va, na);
  129. if (doDoF || firstFrame) {
  130. fbo3.use();
  131. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  132. depth.use();
  133. var dva = depth.attrib("Vertex");
  134. drawScene(gl, depth, dva);
  135. gl.disable(gl.DEPTH_TEST);
  136. gl.activeTexture(gl.TEXTURE1);
  137. gl.bindTexture(gl.TEXTURE_2D, fbo3.texture);
  138. gl.activeTexture(gl.TEXTURE0);
  139. for (var i=0; i<3; i++) {
  140. fbo2.use();
  141. gl.bindTexture(gl.TEXTURE_2D, fbo.texture);
  142. hdof.apply(function(f){
  143. f.uniform1i("Texture", 0);
  144. f.uniform1i("Depth", 1);
  145. f.uniform1f("iter", i);
  146. f.uniform1f("step", 1.0/canvas.width);
  147. });
  148. fbo.use();
  149. gl.bindTexture(gl.TEXTURE_2D, fbo2.texture);
  150. vdof.apply(function(f){
  151. f.uniform1i("Texture", 0);
  152. f.uniform1i("Depth", 1);
  153. f.uniform1f("iter", i);
  154. f.uniform1f("step", 1.0/canvas.width);
  155. });
  156. }
  157. }
  158. firstFrame = false;
  159. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  160. gl.activeTexture(gl.TEXTURE1);
  161. gl.bindTexture(gl.TEXTURE_2D, null);
  162. gl.activeTexture(gl.TEXTURE0);
  163. gl.bindTexture(gl.TEXTURE_2D, fbo.texture);
  164. // The DoF blur blurs the color from the transparent black background with
  165. // the cubes. To get rid of the border, we can treat it as premultiplied alpha.
  166. // To see the problem, try replacing unpremult with identity.
  167. unpremult.apply(function(f){
  168. f.uniform1i("Texture", 0);
  169. });
  170. }
  171. window.addEventListener("load", init, false);
  172. </script>
  173. <script id="ppix-vert" type="x-shader/x-vertex">
  174. attribute vec3 Vertex;
  175. attribute vec3 Normal;
  176. attribute vec2 Tex;
  177. uniform mat4 PMatrix;
  178. uniform mat4 MVMatrix;
  179. uniform mat3 NMatrix;
  180. uniform vec4 MaterialAmbient;
  181. uniform vec4 MaterialDiffuse;
  182. uniform vec4 LightAmbient;
  183. uniform vec4 LightDiffuse;
  184. uniform vec4 GlobalAmbient;
  185. uniform vec4 LightPos;
  186. varying vec4 diffuse, ambientGlobal, ambient;
  187. varying vec3 normal, lightDir, halfVector;
  188. varying float dist;
  189. void main()
  190. {
  191. vec4 worldPos;
  192. vec3 lightVector;
  193. vec4 v = vec4(Vertex, 1.0);
  194. /* transform vertex normal into world space and normalize */
  195. normal = normalize(NMatrix * Normal);
  196. /* transform vertex into world space and compute the vector
  197. from it to the light */
  198. worldPos = MVMatrix * v;
  199. lightVector = vec3(LightPos - worldPos);
  200. lightDir = normalize(lightVector);
  201. dist = length(lightVector);
  202. /* Half-vector used in Blinn-Phong shading due to computational efficiency */
  203. halfVector = normalize(lightVector - vec3(worldPos));
  204. diffuse = MaterialDiffuse * LightDiffuse;
  205. /* The ambient terms have been separated since one of them */
  206. /* suffers attenuation */
  207. ambient = MaterialAmbient * LightAmbient;
  208. ambientGlobal = GlobalAmbient * MaterialAmbient;
  209. gl_Position = PMatrix * worldPos;
  210. }
  211. </script>
  212. <script id="ppix-frag" type="x-shader/x-fragment">
  213. precision mediump float;
  214. uniform vec4 LightSpecular;
  215. uniform vec4 MaterialSpecular;
  216. uniform float MaterialShininess;
  217. uniform float LightConstantAtt;
  218. uniform float LightLinearAtt;
  219. uniform float LightQuadraticAtt;
  220. varying vec4 diffuse,ambientGlobal, ambient;
  221. varying vec3 normal, lightDir, halfVector;
  222. varying float dist;
  223. void main()
  224. {
  225. vec3 n, halfV, viewV, ldir;
  226. float NdotL, NdotHV;
  227. vec4 color = ambientGlobal;
  228. float att;
  229. n = normalize(normal);
  230. NdotL = max(dot(n, normalize(lightDir)), 0.0);
  231. if (NdotL > 0.0) {
  232. att = 1.0 / (LightConstantAtt + LightLinearAtt * dist + LightQuadraticAtt * dist * dist);
  233. color += att * (diffuse * NdotL + ambient);
  234. halfV = normalize(halfVector);
  235. NdotHV = max( dot(normal, halfV), 0.0 );
  236. color += att * MaterialSpecular * LightSpecular * pow(NdotHV, MaterialShininess);
  237. }
  238. gl_FragColor = color;
  239. }
  240. </script>
  241. <script id="depth-vert" type="x-shader/x-vertex">
  242. attribute vec3 Vertex;
  243. uniform mat4 PMatrix;
  244. uniform mat4 MVMatrix;
  245. varying float depth;
  246. void main()
  247. {
  248. gl_Position = PMatrix * (MVMatrix * vec4(Vertex, 1.0));
  249. depth = 1.0-(gl_Position.z / gl_Position.w);
  250. }
  251. </script>
  252. <script id="depth-frag" type="x-shader/x-fragment">
  253. precision mediump float;
  254. varying float depth;
  255. void main()
  256. {
  257. vec4 c = vec4(depth, 0.0, 0.0, 1.0);
  258. gl_FragColor = c;
  259. }
  260. </script>
  261. <script id="identity-vert" type="x-shader/x-vertex">
  262. attribute vec3 Vertex;
  263. attribute vec2 Tex;
  264. varying vec4 texCoord0;
  265. void main()
  266. {
  267. texCoord0 = vec4(Tex,0.0,0.0);
  268. gl_Position = vec4(Vertex, 1.0);
  269. }
  270. </script>
  271. <script id="identity-frag" type="x-shader/x-fragment">
  272. precision mediump float;
  273. uniform sampler2D Texture;
  274. varying vec4 texCoord0;
  275. void main()
  276. {
  277. vec4 c = texture2D(Texture, texCoord0.st);
  278. gl_FragColor = c;
  279. }
  280. </script>
  281. <script id="premult-frag" type="x-shader/x-fragment">
  282. precision mediump float;
  283. uniform sampler2D Texture;
  284. varying vec4 texCoord0;
  285. void main()
  286. {
  287. vec4 c = texture2D(Texture, texCoord0.st);
  288. float a = c.a;
  289. c *= a;
  290. c.a = a;
  291. gl_FragColor = c;
  292. }
  293. </script>
  294. <script id="unpremult-frag" type="x-shader/x-fragment">
  295. precision mediump float;
  296. uniform sampler2D Texture;
  297. varying vec4 texCoord0;
  298. void main()
  299. {
  300. vec4 c = texture2D(Texture, texCoord0.st);
  301. float a = c.a;
  302. c /= a;
  303. c.a = a;
  304. gl_FragColor = c;
  305. }
  306. </script>
  307. <script id="hblur-frag" type="x-shader/x-fragment">
  308. precision mediump float;
  309. uniform sampler2D Texture;
  310. uniform float step;
  311. float kernel[7] = float[](0.046, 0.111, 0.202, 0.283, 0.202, 0.111, 0.046);
  312. varying vec4 texCoord0;
  313. void main()
  314. {
  315. int i=0;
  316. if (texture2D(Texture, texCoord0.st).a > 0.0) {
  317. vec4 sum = vec4(0.0);
  318. for (i=0; i<7; i++) {
  319. vec4 tmp = texture2D(Texture, texCoord0.st + vec2(i*step,0));
  320. sum += tmp * kernel[i];
  321. }
  322. gl_FragColor = sum;
  323. } else {
  324. gl_FragColor = texture2D(Texture, texCoord0.st);
  325. }
  326. }
  327. </script>
  328. <script id="vblur-frag" type="x-shader/x-fragment">
  329. precision mediump float;
  330. uniform sampler2D Texture;
  331. uniform float step;
  332. float kernel[7] = float[](0.046, 0.111, 0.202, 0.283, 0.202, 0.111, 0.046);
  333. varying vec4 texCoord0;
  334. void main()
  335. {
  336. int i=0;
  337. if (texture2D(Texture, texCoord0.st).a > 0.0) {
  338. vec4 sum = vec4(0.0);
  339. for (i=0; i<7; i++) {
  340. vec4 tmp = texture2D(Texture, texCoord0.st + vec2(0,i*step));
  341. sum += tmp * kernel[i];
  342. }
  343. gl_FragColor = sum;
  344. } else {
  345. gl_FragColor = texture2D(Texture, texCoord0.st);
  346. }
  347. }
  348. </script>
  349. <script id="hdof-frag" type="x-shader/x-fragment">
  350. precision mediump float;
  351. uniform sampler2D Texture;
  352. uniform sampler2D Depth;
  353. uniform float step;
  354. uniform float iter;
  355. float kernel[7] = { 0.046, 0.111, 0.202, 0.283, 0.202, 0.111, 0.046 };
  356. varying vec4 texCoord0;
  357. void main()
  358. {
  359. vec4 tmp;
  360. vec4 sum = vec4(0.0);
  361. bool b = (iter < -26.0+36.0*(1.0-texture2D(Depth, texCoord0.st).r) && texture2D(Texture, texCoord0.st).a > 0.0);
  362. tmp = texture2D(Texture, texCoord0.st + vec2(float(0-3)*step,0));
  363. sum += tmp * kernel[0];
  364. tmp = texture2D(Texture, texCoord0.st + vec2(float(1-3)*step,0));
  365. sum += tmp * kernel[1];
  366. tmp = texture2D(Texture, texCoord0.st + vec2(float(2-3)*step,0));
  367. sum += tmp * kernel[2];
  368. tmp = texture2D(Texture, texCoord0.st + vec2(float(3-3)*step,0));
  369. sum += tmp * kernel[3];
  370. tmp = texture2D(Texture, texCoord0.st + vec2(float(4-3)*step,0));
  371. sum += tmp * kernel[4];
  372. tmp = texture2D(Texture, texCoord0.st + vec2(float(5-3)*step,0));
  373. sum += tmp * kernel[5];
  374. tmp = texture2D(Texture, texCoord0.st + vec2(float(6-3)*step,0));
  375. sum += tmp * kernel[6];
  376. gl_FragColor = mix(texture2D(Texture, texCoord0.st), sum, b ? 1.0 : 0.0);
  377. }
  378. </script>
  379. <script id="vdof-frag" type="x-shader/x-fragment">
  380. precision mediump float;
  381. uniform sampler2D Texture;
  382. uniform sampler2D Depth;
  383. uniform float step;
  384. uniform float iter;
  385. float kernel[7] = float[7](0.046, 0.111, 0.202, 0.283, 0.202, 0.111, 0.046);
  386. varying vec4 texCoord0;
  387. void main()
  388. {
  389. vec4 tmp;
  390. vec4 sum = vec4(0.0);
  391. bool b = (iter < -26.0+36.0*(1.0-texture2D(Depth, texCoord0.st).r) && texture2D(Texture, texCoord0.st).a > 0.0);
  392. tmp = texture2D(Texture, texCoord0.st + vec2(0,float(0-3)*step));
  393. sum += tmp * kernel[0];
  394. tmp = texture2D(Texture, texCoord0.st + vec2(0,float(1-3)*step));
  395. sum += tmp * kernel[1];
  396. tmp = texture2D(Texture, texCoord0.st + vec2(0,float(2-3)*step));
  397. sum += tmp * kernel[2];
  398. tmp = texture2D(Texture, texCoord0.st + vec2(0,float(3-3)*step));
  399. sum += tmp * kernel[3];
  400. tmp = texture2D(Texture, texCoord0.st + vec2(0,float(4-3)*step));
  401. sum += tmp * kernel[4];
  402. tmp = texture2D(Texture, texCoord0.st + vec2(0,float(5-3)*step));
  403. sum += tmp * kernel[5];
  404. tmp = texture2D(Texture, texCoord0.st + vec2(0,float(6-3)*step));
  405. sum += tmp * kernel[6];
  406. gl_FragColor = mix(texture2D(Texture, texCoord0.st), sum, b ? 1.0 : 0.0);
  407. }
  408. </script>
  409. <style>
  410. * { margin: 0px; padding: 0px; }
  411. html {
  412. background-color: #707888;
  413. color: #222222;
  414. }
  415. #canvas {
  416. position: absolute;
  417. cursor: pointer;
  418. top: 115px; left: 550px;
  419. }
  420. #note {
  421. position: absolute;
  422. top: 4px;
  423. left: 4px;
  424. }
  425. #content {
  426. margin-left: 99px;
  427. padding-left: 8px;
  428. padding-right: 8px;
  429. padding-bottom: 16px;
  430. width: 600px;
  431. background-color: rgba(255,255,255,1.0);
  432. text-align: center;
  433. border-left: 1px solid rgba(0,0,0,0.5);
  434. border-right: 2px solid rgba(0,0,0,0.75);
  435. }
  436. h1 {
  437. padding-top: 24px;
  438. padding-bottom: 16px;
  439. margin-bottom: 24px;
  440. border-bottom: 1px solid black;
  441. font-family: Times New Roman, Serif;
  442. font-weight: bold;
  443. font-size: 40px;
  444. }
  445. #content p {
  446. text-indent: 24px;
  447. margin-left: 24px;
  448. margin-right: 32px;
  449. text-align: justify;
  450. font-family: Serif;
  451. padding-bottom: 16px;
  452. }
  453. #above {
  454. position: absolute;
  455. top: 300px;
  456. left: 716px;
  457. padding: 10px 20px;
  458. background-color: rgba(0,225,0,0.5);
  459. border-left: 2px solid rgba(0,64,0,0.75);
  460. color: white;
  461. font-size: small;
  462. font-family: sans-serif;
  463. }
  464. #above p {
  465. text-align: center;
  466. }
  467. </style>
  468. </head><body>
  469. <canvas id="canvas" width="400" height="400" title="Click to toggle DOF shader" onclick="useDoF = !useDoF"></canvas>
  470. <pre id="note"></pre>
  471. <div id="content">
  472. <h1>OpenGL for the web</h1>
  473. <p>
  474. The WebGL specification gives web developers access to an
  475. OpenGL ES 2.0 drawing context for the canvas tag. What that means is
  476. that you can finally harness the power of the GPU for awesome visuals
  477. and heavy-duty number crunching in your web apps. </p><p> OpenGL ES 2.0 is a subset of OpenGL 2.0 aimed at embedded
  478. devices and game consoles. As such, it's a very minimalistic low-level
  479. API, even more so than desktop OpenGL. In fact, if you took desktop
  480. OpenGL and stripped out everything but shaders, vertex arrays and
  481. textures, you'd get something quite like OpenGL ES 2.0. </p>
  482. <p>
  483. As there is no fixed-function pipeline, you need to write GLSL shaders to draw <i>anything</i>.
  484. And you need to do your own transformation math, including keeping
  485. track of the transformation matrix stack. So the raw API is really not
  486. for the faint of heart; you do need to know your 3D math and shading
  487. equations. </p>
  488. <p> For example, to draw the spinning cubes on the
  489. right - around 200 lines of application code, 250 lines of shaders and
  490. 800 lines of library code - I had to scrounge the internet for <a href="http://www.lighthouse3d.com/opengl/glsl/index.php?pointlight">GLSL shaders</a>
  491. to do the transformation and lighting, write a small matrix math
  492. library in JavaScript and a DOF blur shader. While highly educational,
  493. it was also a rather steep hill to climb. </p>
  494. <p> So, the intended audience of the raw context
  495. interface are not really end-users, but library developers who can
  496. write easy-to-use interfaces to the functionality, and 3D developers
  497. who require a high level of control over the rendering pipeline. </p>
  498. <p> The really cool thing about the OpenGL Canvas is
  499. that it doesn't make policy decisions. There's no single set-in-stone
  500. use case for it: In addition to 3D graphics, you can also use it for
  501. filtering images, visualizing fluid dynamics, doing real-time video
  502. effects, or just crunching a whole lot of FP math. If you can do it on
  503. the GPU, you're in luck! </p>
  504. </div>
  505. <div id="above">
  506. <p>You can also place content above the canvas</p>
  507. </div>
  508. </body></html>