/
CubeMapFace.js
212 lines (185 loc) · 8.5 KB
/
CubeMapFace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*global define*/
define([
'../Core/defaultValue',
'../Core/DeveloperError',
'./PixelDatatype'
], function(
defaultValue,
DeveloperError,
PixelDatatype) {
"use strict";
/**
* A face of a cube map (+x, -x, +y, -y, +z, or -z) that can be modified and rendered to.
*
* @alias CubeMapFace
* @internalConstructor
*
* @see CubeMap
*/
var CubeMapFace = function(gl, texture, textureTarget, targetFace, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY) {
this._gl = gl;
this._texture = texture;
this._textureTarget = textureTarget;
this._targetFace = targetFace;
this._pixelFormat = pixelFormat;
this._pixelDatatype = pixelDatatype;
this._size = size;
this._preMultiplyAlpha = preMultiplyAlpha;
this._flipY = flipY;
};
/**
* Copies texels from the source to the cubemap's face.
*
* @memberof CubeMapFace
*
* @param {Object} source The source ImageData, HTMLImageElement, HTMLCanvasElement, HTMLVideoElement, or an object with a width, height, and typed array as shown in the example.
* @param {Number} [xOffset=0] An offset in the x direction in the cubemap where copying begins.
* @param {Number} [yOffset=0] An offset in the y direction in the cubemap where copying begins.
*
* @exception {DeveloperError} source is required.
* @exception {DeveloperError} xOffset must be greater than or equal to zero.
* @exception {DeveloperError} yOffset must be greater than or equal to zero.
* @exception {DeveloperError} xOffset + source.width must be less than or equal to getWidth().
* @exception {DeveloperError} yOffset + source.height must be less than or equal to getHeight().
* @exception {DeveloperError} This CubeMap was destroyed, i.e., destroy() was called.
*
* @example
* // Create a cubemap with 1x1 faces, and make the +x face red.
* var cubeMap = context.createCubeMap({
* width : 1,
* height : 1
* });
* cubeMap.getPositiveX().copyFrom({
* width : 1,
* height : 1,
* arrayBufferView : new Uint8Array([255, 0, 0, 255])
* });
*/
CubeMapFace.prototype.copyFrom = function(source, xOffset, yOffset) {
if (!source) {
throw new DeveloperError('source is required.');
}
xOffset = defaultValue(xOffset, 0);
yOffset = defaultValue(yOffset, 0);
var width = source.width;
var height = source.height;
if (xOffset < 0) {
throw new DeveloperError('xOffset must be greater than or equal to zero.');
}
if (yOffset < 0) {
throw new DeveloperError('yOffset must be greater than or equal to zero.');
}
if (xOffset + width > this._size) {
throw new DeveloperError('xOffset + source.width must be less than or equal to getWidth().');
}
if (yOffset + height > this._size) {
throw new DeveloperError('yOffset + source.height must be less than or equal to getHeight().');
}
var gl = this._gl;
var target = this._textureTarget;
// TODO: gl.pixelStorei(gl._UNPACK_ALIGNMENT, 4);
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, this._preMultiplyAlpha);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, this._flipY);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(target, this._texture);
//Firefox bug: texSubImage2D has overloads and can't resolve our enums, so we use + to explicitly convert to a number.
if (source.arrayBufferView) {
gl.texSubImage2D(this._targetFace, 0, xOffset, yOffset, width, height, +this._pixelFormat, +this._pixelDatatype, source.arrayBufferView);
} else {
gl.texSubImage2D(this._targetFace, 0, xOffset, yOffset, +this._pixelFormat, +this._pixelDatatype, source);
}
gl.bindTexture(target, null);
};
/**
* Copies texels from the framebuffer to the cubemap's face.
*
* @memberof CubeMapFace
*
* @param {Number} [xOffset=0] An offset in the x direction in the cubemap where copying begins.
* @param {Number} [yOffset=0] An offset in the y direction in the cubemap where copying begins.
* @param {Number} [framebufferXOffset=0] An offset in the x direction in the framebuffer where copying begins from.
* @param {Number} [framebufferYOffset=0] An offset in the y direction in the framebuffer where copying begins from.
* @param {Number} [width=CubeMap's width] The width of the subimage to copy.
* @param {Number} [height=CubeMap's height] The height of the subimage to copy.
*
* @exception {DeveloperError} Cannot call copyFromFramebuffer when the texture pixel data type is FLOAT.
* @exception {DeveloperError} This CubeMap was destroyed, i.e., destroy() was called.
* @exception {DeveloperError} xOffset must be greater than or equal to zero.
* @exception {DeveloperError} yOffset must be greater than or equal to zero.
* @exception {DeveloperError} framebufferXOffset must be greater than or equal to zero.
* @exception {DeveloperError} framebufferYOffset must be greater than or equal to zero.
* @exception {DeveloperError} xOffset + source.width must be less than or equal to getWidth().
* @exception {DeveloperError} yOffset + source.height must be less than or equal to getHeight().
* @exception {DeveloperError} This CubeMap was destroyed, i.e., destroy() was called.
*
* @example
* // Copy the framebuffer contents to the +x cube map face.
* cubeMap.getPositiveX().copyFromFramebuffer();
*/
CubeMapFace.prototype.copyFromFramebuffer = function(xOffset, yOffset, framebufferXOffset, framebufferYOffset, width, height) {
xOffset = defaultValue(xOffset, 0);
yOffset = defaultValue(yOffset, 0);
framebufferXOffset = defaultValue(framebufferXOffset, 0);
framebufferYOffset = defaultValue(framebufferYOffset, 0);
width = defaultValue(width, this._size);
height = defaultValue(height, this._size);
if (xOffset < 0) {
throw new DeveloperError('xOffset must be greater than or equal to zero.');
}
if (yOffset < 0) {
throw new DeveloperError('yOffset must be greater than or equal to zero.');
}
if (framebufferXOffset < 0) {
throw new DeveloperError('framebufferXOffset must be greater than or equal to zero.');
}
if (framebufferYOffset < 0) {
throw new DeveloperError('framebufferYOffset must be greater than or equal to zero.');
}
if (xOffset + width > this._size) {
throw new DeveloperError('xOffset + source.width must be less than or equal to getWidth().');
}
if (yOffset + height > this._size) {
throw new DeveloperError('yOffset + source.height must be less than or equal to getHeight().');
}
if (this._pixelDatatype === PixelDatatype.FLOAT) {
throw new DeveloperError('Cannot call copyFromFramebuffer when the texture pixel data type is FLOAT.');
}
var gl = this._gl;
var target = this._textureTarget;
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(target, this._texture);
gl.copyTexSubImage2D(this._targetFace, 0, xOffset, yOffset, framebufferXOffset, framebufferYOffset, width, height);
gl.bindTexture(target, null);
};
/**
* Returns the pixel format of this cube map face. All faces in the same cube map have the same pixel format.
*
* @memberof CubeMapFace
*
* @returns {PixelFormat} The pixel format of this cubemap face.
*
* @exception {DeveloperError} This CubeMap was destroyed, i.e., destroy() was called.
*/
CubeMapFace.prototype.getPixelFormat = function() {
return this._pixelFormat;
};
/**
* Returns the pixel data type of this cube map face. All faces in the same cube map have the same pixel data type.
*
* @memberof CubeMapFace
*
* @returns {PixelFormat} The pixel data type of this cubemap face.
*
* @exception {DeveloperError} This CubeMap was destroyed, i.e., destroy() was called.
*/
CubeMapFace.prototype.getPixelDatatype = function() {
return this._pixelDatatype;
};
CubeMapFace.prototype._getTexture = function() {
return this._texture;
};
CubeMapFace.prototype._getTarget = function() {
return this._targetFace;
};
return CubeMapFace;
});