webgl/lookupTable2D.js

  1. var inherit = require('../inherit');
  2. var timestamp = require('../timestamp');
  3. var vgl = require('../vgl');
  4. /**
  5. * Switch to a specific texture unit.
  6. *
  7. * @param {vgl.renderState} renderState An object that contains the context
  8. * used for drawing.
  9. * @param {number} textureUnit The number of the texture unit [0-15].
  10. * @private
  11. */
  12. function activateTextureUnit(renderState, textureUnit) {
  13. if (textureUnit >= 0 && textureUnit <= 31) {
  14. renderState.m_context.activeTexture(vgl.GL.TEXTURE0 + textureUnit);
  15. } else {
  16. throw Error('[error] Texture unit ' + textureUnit + ' is not supported');
  17. }
  18. }
  19. /**
  20. * Create a new instance of class webgl_lookupTable2D.
  21. *
  22. * @class
  23. * @alias geo.webgl.lookupTable2D
  24. * @param {object} arg Options object.
  25. * @param {number} [arg.maxWidth] Maximum width to use for the texture. If the
  26. * number of colors set is less than this, the texture is 1D. If greater, it
  27. * will be a rectangle of maxWidth x whatever height is necessary.
  28. * @param {number[]} [arg.colorTable] Initial color table for the texture.
  29. * This is of the form RGBARGBA... where each value is an integer on the
  30. * scale [0,255].
  31. * @extends vgl.texture
  32. * @returns {geo.webgl.lookupTable2D}
  33. */
  34. var webgl_lookupTable2D = function (arg) {
  35. 'use strict';
  36. if (!(this instanceof webgl_lookupTable2D)) {
  37. return new webgl_lookupTable2D(arg);
  38. }
  39. arg = arg || {};
  40. vgl.texture.call(this);
  41. var m_setupTimestamp = timestamp(),
  42. m_maxWidth = arg.maxWidth || 4096,
  43. m_colorTable = new Uint8Array([0, 0, 0, 0]),
  44. m_colorTableOrig,
  45. m_this = this;
  46. /**
  47. * Create lookup table, initialize parameters, and bind data to it.
  48. *
  49. * @param {vgl.renderState} renderState An object that contains the context
  50. * used for drawing.
  51. */
  52. this.setup = function (renderState) {
  53. activateTextureUnit(renderState, m_this.textureUnit());
  54. renderState.m_context.deleteTexture(m_this.m_textureHandle);
  55. m_this.m_textureHandle = renderState.m_context.createTexture();
  56. renderState.m_context.bindTexture(vgl.GL.TEXTURE_2D, m_this.m_textureHandle);
  57. renderState.m_context.texParameteri(vgl.GL.TEXTURE_2D, vgl.GL.TEXTURE_MIN_FILTER, vgl.GL.NEAREST);
  58. renderState.m_context.texParameteri(vgl.GL.TEXTURE_2D, vgl.GL.TEXTURE_MAG_FILTER, vgl.GL.NEAREST);
  59. renderState.m_context.texParameteri(vgl.GL.TEXTURE_2D, vgl.GL.TEXTURE_WRAP_S, vgl.GL.CLAMP_TO_EDGE);
  60. renderState.m_context.texParameteri(vgl.GL.TEXTURE_2D, vgl.GL.TEXTURE_WRAP_T, vgl.GL.CLAMP_TO_EDGE);
  61. renderState.m_context.pixelStorei(vgl.GL.UNPACK_ALIGNMENT, 1);
  62. renderState.m_context.pixelStorei(vgl.GL.UNPACK_FLIP_Y_WEBGL, true);
  63. renderState.m_context.texImage2D(
  64. vgl.GL.TEXTURE_2D, 0, vgl.GL.RGBA, m_this.width, m_this.height, 0,
  65. vgl.GL.RGBA, vgl.GL.UNSIGNED_BYTE, m_colorTable);
  66. renderState.m_context.bindTexture(vgl.GL.TEXTURE_2D, null);
  67. m_setupTimestamp.modified();
  68. };
  69. /**
  70. * Get/set color table.
  71. *
  72. * @param {number[]} [val] An array of RGBARGBA... integers on a scale
  73. * of [0, 255]. `undefined` to get the current value.
  74. * @returns {number[]|this}
  75. */
  76. this.colorTable = function (val) {
  77. if (val === undefined) {
  78. return m_colorTableOrig;
  79. }
  80. m_colorTableOrig = val;
  81. if (val.length < 4) {
  82. val = [0, 0, 0, 0];
  83. }
  84. m_this.width = Math.min(m_maxWidth, val.length / 4);
  85. m_this.height = Math.ceil(val.length / 4 / m_maxWidth);
  86. if (!(val instanceof Uint8Array) || val.length !== m_this.width * m_this.height * 4) {
  87. if (val.length < m_this.width * m_this.height * 4) {
  88. val = val.concat(new Array(m_this.width * m_this.height * 4 - val.length).fill(0));
  89. }
  90. m_colorTable = new Uint8Array(val);
  91. } else {
  92. m_colorTable = val;
  93. }
  94. m_this.modified();
  95. return m_this;
  96. };
  97. /**
  98. * Get maxWidth value.
  99. *
  100. * @returns {number} The maxWidth of the texture used.
  101. */
  102. this.maxWidth = function () {
  103. return m_maxWidth;
  104. };
  105. this.colorTable(arg.colorTable || []);
  106. return this;
  107. };
  108. inherit(webgl_lookupTable2D, vgl.texture);
  109. module.exports = webgl_lookupTable2D;