gl.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * gl.js
  3. * Keep all webgl boilerplate over here
  4. */
  5. // Vertex shader program
  6. const vsSource = `
  7. attribute vec4 position;
  8. attribute vec2 texcoord;
  9. uniform mat4 projection;
  10. varying highp vec2 texcoord_f;
  11. void main() {
  12. texcoord_f = texcoord;
  13. gl_Position = projection * position;
  14. }
  15. `;
  16. const fsSource = `
  17. varying highp vec2 texcoord_f;
  18. uniform highp float dim;
  19. uniform highp vec2 rseed;
  20. uniform sampler2D sampler;
  21. uniform bool enabled;
  22. highp float rand(vec2 co)
  23. {
  24. co = co * rseed * dim * 10.;
  25. highp float a = 12.9898;
  26. highp float b = 78.233;
  27. highp float c = 43758.5453;
  28. highp float dt= dot(co.xy ,vec2(a,b));
  29. highp float sn= mod(dt,3.14);
  30. return fract(sin(sn) * c);
  31. }
  32. highp vec4 decideOutcome(ivec2 texel) {
  33. highp vec4 outcome = vec4(0., 0., 0., 0.);
  34. // u d l r
  35. highp float r = rand(texcoord_f);
  36. int which = int(r * 5.0);
  37. if(which == 0) {
  38. // center
  39. outcome = texture2D(sampler, texcoord_f);
  40. }
  41. else if(which == 1) {
  42. // up
  43. if(texel.y < 1) {
  44. // down instead
  45. }
  46. outcome = texture2D(sampler, vec2(texcoord_f.x, texcoord_f.y));
  47. }
  48. else if(which == 2) {
  49. }
  50. else if(which == 3) {
  51. }
  52. else if(which == 4) {
  53. }
  54. if(texel.x < 1) {
  55. }
  56. else if(texel.x > int(dim)-2) {
  57. }
  58. if(texel.y < 1) {
  59. }
  60. else if(texel.y > int(dim)-2) {
  61. }
  62. return outcome;
  63. }
  64. void main() {
  65. if (enabled) {
  66. ivec2 texel = ivec2(texcoord_f * dim);
  67. gl_FragColor = decideOutcome(texel);
  68. }
  69. else {
  70. gl_FragColor = texture2D(sampler, texcoord_f);
  71. }
  72. }
  73. `;
  74. /*
  75. * Combine vertex&fragment source into compiled shader program
  76. */
  77. function initShader(gl, vsSource, fsSource) {
  78. const vertexShader = compileShader(gl, gl.VERTEX_SHADER, vsSource);
  79. const fragmentShader = compileShader(gl, gl.FRAGMENT_SHADER, fsSource);
  80. // Create the shader program
  81. const shaderProgram = gl.createProgram();
  82. gl.attachShader(shaderProgram, vertexShader);
  83. gl.attachShader(shaderProgram, fragmentShader);
  84. gl.linkProgram(shaderProgram);
  85. // If creating the shader program failed, alert
  86. if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
  87. alert('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram));
  88. return null;
  89. }
  90. return shaderProgram;
  91. }
  92. /*
  93. * Compile individual shader
  94. */
  95. function compileShader(gl, type, source) {
  96. const shader = gl.createShader(type);
  97. gl.shaderSource(shader, source);
  98. gl.compileShader(shader);
  99. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  100. alert('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader));
  101. gl.deleteShader(shader);
  102. return null;
  103. }
  104. return shader;
  105. }
  106. function initBuffers(gl) {
  107. const positionBuffer = gl.createBuffer();
  108. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  109. const positions = [
  110. 1.0, 1.0,
  111. -1.0, 1.0,
  112. 1.0, -1.0,
  113. -1.0, -1.0,
  114. ];
  115. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
  116. const textureBuffer = gl.createBuffer();
  117. gl.bindBuffer(gl.ARRAY_BUFFER, textureBuffer);
  118. const tex = [
  119. 0.0, 0.0,
  120. 1.0, 0.0,
  121. 1.0, 1.0,
  122. 0.0, 1.0,
  123. ];
  124. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(tex), gl.STATIC_DRAW);
  125. return {
  126. position: positionBuffer,
  127. texcoords: textureBuffer,
  128. };
  129. }
  130. function createTexture(gl, dim) {
  131. const texture = gl.createTexture();
  132. gl.bindTexture(gl.TEXTURE_2D, texture);
  133. const blue = [0, 0, 255, 255];
  134. const red = [255, 0, 0, 255]
  135. var sq = []
  136. for(var i = 0; i < dim*dim; i++) {
  137. if(i % dim < dim/2) {
  138. sq.push.apply(sq, red)
  139. } else {
  140. sq.push.apply(sq, blue)
  141. }
  142. }
  143. const data = new Uint8Array(sq); // opaque blue
  144. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, dim, dim, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
  145. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  146. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  147. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  148. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  149. return texture;
  150. }