CubeMapFace.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*global define*/
  2. define([
  3. '../Core/defaultValue',
  4. '../Core/defineProperties',
  5. '../Core/DeveloperError',
  6. './PixelDatatype'
  7. ], function(
  8. defaultValue,
  9. defineProperties,
  10. DeveloperError,
  11. PixelDatatype) {
  12. "use strict";
  13. /**
  14. * @private
  15. */
  16. var CubeMapFace = function(gl, texture, textureTarget, targetFace, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY) {
  17. this._gl = gl;
  18. this._texture = texture;
  19. this._textureTarget = textureTarget;
  20. this._targetFace = targetFace;
  21. this._pixelFormat = pixelFormat;
  22. this._pixelDatatype = pixelDatatype;
  23. this._size = size;
  24. this._preMultiplyAlpha = preMultiplyAlpha;
  25. this._flipY = flipY;
  26. };
  27. defineProperties(CubeMapFace.prototype, {
  28. pixelFormat : {
  29. get : function() {
  30. return this._pixelFormat;
  31. }
  32. },
  33. pixelDatatype : {
  34. get : function() {
  35. return this._pixelDatatype;
  36. }
  37. },
  38. _target : {
  39. get : function() {
  40. return this._targetFace;
  41. }
  42. }
  43. });
  44. /**
  45. * Copies texels from the source to the cubemap's face.
  46. *
  47. * @param {Object} source The source ImageData, HTMLImageElement, HTMLCanvasElement, HTMLVideoElement, or an object with a width, height, and typed array as shown in the example.
  48. * @param {Number} [xOffset=0] An offset in the x direction in the cubemap where copying begins.
  49. * @param {Number} [yOffset=0] An offset in the y direction in the cubemap where copying begins.
  50. *
  51. * @exception {DeveloperError} xOffset must be greater than or equal to zero.
  52. * @exception {DeveloperError} yOffset must be greater than or equal to zero.
  53. * @exception {DeveloperError} xOffset + source.width must be less than or equal to width.
  54. * @exception {DeveloperError} yOffset + source.height must be less than or equal to height.
  55. * @exception {DeveloperError} This CubeMap was destroyed, i.e., destroy() was called.
  56. *
  57. * @example
  58. * // Create a cubemap with 1x1 faces, and make the +x face red.
  59. * var cubeMap = context.createCubeMap({
  60. * width : 1,
  61. * height : 1
  62. * });
  63. * cubeMap.positiveX.copyFrom({
  64. * width : 1,
  65. * height : 1,
  66. * arrayBufferView : new Uint8Array([255, 0, 0, 255])
  67. * });
  68. */
  69. CubeMapFace.prototype.copyFrom = function(source, xOffset, yOffset) {
  70. xOffset = defaultValue(xOffset, 0);
  71. yOffset = defaultValue(yOffset, 0);
  72. //>>includeStart('debug', pragmas.debug);
  73. if (!source) {
  74. throw new DeveloperError('source is required.');
  75. }
  76. if (xOffset < 0) {
  77. throw new DeveloperError('xOffset must be greater than or equal to zero.');
  78. }
  79. if (yOffset < 0) {
  80. throw new DeveloperError('yOffset must be greater than or equal to zero.');
  81. }
  82. if (xOffset + source.width > this._size) {
  83. throw new DeveloperError('xOffset + source.width must be less than or equal to width.');
  84. }
  85. if (yOffset + source.height > this._size) {
  86. throw new DeveloperError('yOffset + source.height must be less than or equal to height.');
  87. }
  88. //>>includeEnd('debug');
  89. var gl = this._gl;
  90. var target = this._textureTarget;
  91. // TODO: gl.pixelStorei(gl._UNPACK_ALIGNMENT, 4);
  92. gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, this._preMultiplyAlpha);
  93. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, this._flipY);
  94. gl.activeTexture(gl.TEXTURE0);
  95. gl.bindTexture(target, this._texture);
  96. if (source.arrayBufferView) {
  97. gl.texSubImage2D(this._targetFace, 0, xOffset, yOffset, source.width, source.height, this._pixelFormat, this._pixelDatatype, source.arrayBufferView);
  98. } else {
  99. gl.texSubImage2D(this._targetFace, 0, xOffset, yOffset, this._pixelFormat, this._pixelDatatype, source);
  100. }
  101. gl.bindTexture(target, null);
  102. };
  103. /**
  104. * Copies texels from the framebuffer to the cubemap's face.
  105. *
  106. * @param {Number} [xOffset=0] An offset in the x direction in the cubemap where copying begins.
  107. * @param {Number} [yOffset=0] An offset in the y direction in the cubemap where copying begins.
  108. * @param {Number} [framebufferXOffset=0] An offset in the x direction in the framebuffer where copying begins from.
  109. * @param {Number} [framebufferYOffset=0] An offset in the y direction in the framebuffer where copying begins from.
  110. * @param {Number} [width=CubeMap's width] The width of the subimage to copy.
  111. * @param {Number} [height=CubeMap's height] The height of the subimage to copy.
  112. *
  113. * @exception {DeveloperError} Cannot call copyFromFramebuffer when the texture pixel data type is FLOAT.
  114. * @exception {DeveloperError} This CubeMap was destroyed, i.e., destroy() was called.
  115. * @exception {DeveloperError} xOffset must be greater than or equal to zero.
  116. * @exception {DeveloperError} yOffset must be greater than or equal to zero.
  117. * @exception {DeveloperError} framebufferXOffset must be greater than or equal to zero.
  118. * @exception {DeveloperError} framebufferYOffset must be greater than or equal to zero.
  119. * @exception {DeveloperError} xOffset + source.width must be less than or equal to width.
  120. * @exception {DeveloperError} yOffset + source.height must be less than or equal to height.
  121. * @exception {DeveloperError} This CubeMap was destroyed, i.e., destroy() was called.
  122. *
  123. * @example
  124. * // Copy the framebuffer contents to the +x cube map face.
  125. * cubeMap.positiveX.copyFromFramebuffer();
  126. */
  127. CubeMapFace.prototype.copyFromFramebuffer = function(xOffset, yOffset, framebufferXOffset, framebufferYOffset, width, height) {
  128. xOffset = defaultValue(xOffset, 0);
  129. yOffset = defaultValue(yOffset, 0);
  130. framebufferXOffset = defaultValue(framebufferXOffset, 0);
  131. framebufferYOffset = defaultValue(framebufferYOffset, 0);
  132. width = defaultValue(width, this._size);
  133. height = defaultValue(height, this._size);
  134. //>>includeStart('debug', pragmas.debug);
  135. if (xOffset < 0) {
  136. throw new DeveloperError('xOffset must be greater than or equal to zero.');
  137. }
  138. if (yOffset < 0) {
  139. throw new DeveloperError('yOffset must be greater than or equal to zero.');
  140. }
  141. if (framebufferXOffset < 0) {
  142. throw new DeveloperError('framebufferXOffset must be greater than or equal to zero.');
  143. }
  144. if (framebufferYOffset < 0) {
  145. throw new DeveloperError('framebufferYOffset must be greater than or equal to zero.');
  146. }
  147. if (xOffset + width > this._size) {
  148. throw new DeveloperError('xOffset + source.width must be less than or equal to width.');
  149. }
  150. if (yOffset + height > this._size) {
  151. throw new DeveloperError('yOffset + source.height must be less than or equal to height.');
  152. }
  153. if (this._pixelDatatype === PixelDatatype.FLOAT) {
  154. throw new DeveloperError('Cannot call copyFromFramebuffer when the texture pixel data type is FLOAT.');
  155. }
  156. //>>includeEnd('debug');
  157. var gl = this._gl;
  158. var target = this._textureTarget;
  159. gl.activeTexture(gl.TEXTURE0);
  160. gl.bindTexture(target, this._texture);
  161. gl.copyTexSubImage2D(this._targetFace, 0, xOffset, yOffset, framebufferXOffset, framebufferYOffset, width, height);
  162. gl.bindTexture(target, null);
  163. };
  164. return CubeMapFace;
  165. });