gl.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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(rand(texcoord_f),0.,0.,0.);
  34. // u d l r
  35. highp float r = rand(texcoord_f);
  36. int n = 5;
  37. if(texel.x < 1 || texel.x > int(dim)-2) {
  38. n - 1;
  39. }
  40. if(texel.y < 1 || texel.y > int(dim)-2) {
  41. n - 1;
  42. }
  43. which = int(r * n);
  44. switch(which) {
  45. case 0:
  46. if(texel.x < 1) {
  47. } else {
  48. break;
  49. }
  50. case 1:
  51. break;
  52. case 2:
  53. break;
  54. }
  55. if(texel.x < 1) {
  56. n - 1;
  57. }
  58. else if(texel.x > int(dim)-2) {
  59. n - 1;
  60. }
  61. if(texel.y < 1) {
  62. n - 1;
  63. }
  64. else if(texel.y > int(dim)-2) {
  65. n - 1;
  66. }
  67. return outcome;
  68. }
  69. void main() {
  70. if (enabled) {
  71. ivec2 texel = ivec2(texcoord_f * dim);
  72. gl_FragColor = decideOutcome(texel);
  73. }
  74. else {
  75. gl_FragColor = texture2D(sampler, texcoord_f);
  76. }
  77. }
  78. `;
  79. /*
  80. * Combine vertex&fragment source into compiled shader program
  81. */
  82. function initShader(gl, vsSource, fsSource) {
  83. const vertexShader = compileShader(gl, gl.VERTEX_SHADER, vsSource);
  84. const fragmentShader = compileShader(gl, gl.FRAGMENT_SHADER, fsSource);
  85. // Create the shader program
  86. const shaderProgram = gl.createProgram();
  87. gl.attachShader(shaderProgram, vertexShader);
  88. gl.attachShader(shaderProgram, fragmentShader);
  89. gl.linkProgram(shaderProgram);
  90. // If creating the shader program failed, alert
  91. if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
  92. alert('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram));
  93. return null;
  94. }
  95. return shaderProgram;
  96. }
  97. /*
  98. * Compile individual shader
  99. */
  100. function compileShader(gl, type, source) {
  101. const shader = gl.createShader(type);
  102. gl.shaderSource(shader, source);
  103. gl.compileShader(shader);
  104. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  105. alert('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader));
  106. gl.deleteShader(shader);
  107. return null;
  108. }
  109. return shader;
  110. }
  111. function initBuffers(gl) {
  112. const positionBuffer = gl.createBuffer();
  113. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  114. const positions = [
  115. 1.0, 1.0,
  116. -1.0, 1.0,
  117. 1.0, -1.0,
  118. -1.0, -1.0,
  119. ];
  120. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
  121. const textureBuffer = gl.createBuffer();
  122. gl.bindBuffer(gl.ARRAY_BUFFER, textureBuffer);
  123. const tex = [
  124. 0.0, 0.0,
  125. 1.0, 0.0,
  126. 1.0, 1.0,
  127. 0.0, 1.0,
  128. ];
  129. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(tex), gl.STATIC_DRAW);
  130. return {
  131. position: positionBuffer,
  132. texcoords: textureBuffer,
  133. };
  134. }
  135. function createTexture(gl, dim) {
  136. const texture = gl.createTexture();
  137. gl.bindTexture(gl.TEXTURE_2D, texture);
  138. const blue = [0, 0, 255, 255];
  139. const red = [255, 0, 0, 255]
  140. var sq = []
  141. for(var i = 0; i < dim*dim; i++) {
  142. if(i % dim < dim/2) {
  143. sq.push.apply(sq, red)
  144. } else {
  145. sq.push.apply(sq, blue)
  146. }
  147. }
  148. const data = new Uint8Array(sq); // opaque blue
  149. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, dim, dim, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
  150. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  151. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  152. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  153. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  154. return texture;
  155. }