-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathFramebufferTexture.js
More file actions
163 lines (149 loc) · 6.97 KB
/
Copy pathFramebufferTexture.js
File metadata and controls
163 lines (149 loc) · 6.97 KB
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
/*
* Copyright 2003-2006, 2009, 2017, 2020 United States Government, as represented
* by the Administrator of the National Aeronautics and Space Administration.
* All rights reserved.
*
* The NASAWorldWind/WebWorldWind platform is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License
* at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* NASAWorldWind/WebWorldWind also contains the following 3rd party Open Source
* software:
*
* ES6-Promise – under MIT License
* libtess.js – SGI Free Software License B
* Proj4 – under MIT License
* JSZip – under MIT License
*
* A complete listing of 3rd Party software notices and licenses included in
* WebWorldWind can be found in the WebWorldWind 3rd-party notices and licenses
* PDF found in code directory.
*/
/**
* @exports FramebufferTexture
*/
define([
'../error/ArgumentError',
'../util/Logger',
'../util/WWMath'
],
function (ArgumentError,
Logger) {
"use strict";
/**
* Constructs a framebuffer texture with the specified dimensions and an optional depth buffer. Use the
* [DrawContext.bindFramebuffer]{@link DrawContext#bindFramebuffer} function to make the program current during rendering.
*
* @alias FramebufferTexture
* @constructor
* @classdesc Represents an off-screen WebGL framebuffer. The framebuffer has color buffer stored in a 32
* bit RGBA texture, and has an optional depth buffer of at least 16 bits. Applications typically do not
* interact with this class. WebGL framebuffers are created by instances of this class and made current when the
* DrawContext.bindFramebuffer function is invoked.
* @param {WebGLRenderingContext} gl The current WebGL rendering context.
* @param {Number} width The width of the framebuffer, in pixels.
* @param {Number} height The height of the framebuffer, in pixels.
* @param {Boolean} depth true to configure the framebuffer with a depth buffer of at least 16 bits, false to
* disable depth buffering.
* @throws {ArgumentError} If the specified draw context is null or undefined, or if the width or height is less
* than zero.
*/
var FramebufferTexture = function (gl, width, height, depth) {
if (!gl) {
throw new ArgumentError(Logger.logMessage(Logger.LEVEL_SEVERE, "FramebufferTexture", "constructor",
"missingGlContext"));
}
if (width < 0 || height < 0) {
throw new ArgumentError(Logger.logMessage(Logger.LEVEL_SEVERE, "FramebufferTexture", "constructor",
"The framebuffer width or height is less than zero."));
}
/**
* The width of this framebuffer, in pixels.
* @type {Number}
* @readonly
*/
this.width = width;
/**
* The height of this framebuffer, in pixels.
* @type {Number}
* @readonly
*/
this.height = height;
/**
* Indicates whether or not this framebuffer has a depth buffer.
* @type {Boolean}
* @readonly
*/
this.depth = depth;
/**
* Indicates the size of this framebuffer's WebGL resources, in bytes.
* @type {Number}
* @readonly
*/
this.size = (width * height * 4) + (depth ? width * height * 2 : 0);
/**
* Indicates the WebGL framebuffer object object associated with this framebuffer texture.
* @type {WebGLFramebuffer}
* @readonly
*/
this.framebufferId = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebufferId);
// Internal. Intentionally not documented. Configure this framebuffer's color buffer.
this.texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, this.texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER,
gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER,
gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S,
gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T,
gl.CLAMP_TO_EDGE);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0,
gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0,
gl.TEXTURE_2D, this.texture, 0);
// Internal. Intentionally not documented. Configure this framebuffer's optional depth buffer.
this.depthBuffer = null;
if (depth) {
this.depthBuffer = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, this.depthBuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16,
width, height);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT,
gl.RENDERBUFFER, this.depthBuffer);
}
var e = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
if (e != gl.FRAMEBUFFER_COMPLETE) {
Logger.logMessage(Logger.LEVEL_WARNING, "FramebufferTexture", "constructor",
"Error creating framebuffer: " + e);
this.framebufferId = null;
this.texture = null;
this.depthBuffer = null;
}
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
gl.bindTexture(gl.TEXTURE_2D, null);
};
/**
* Binds this off-screen framebuffer's texture in the current WebGL graphics context. This texture contains
* color fragments resulting from WebGL operations executed when this framebuffer is bound by a call to
* [FramebufferTexture.bindFramebuffer]{@link FramebufferTexture#bindFramebuffer}.
*
* @param {DrawContext} dc The current draw context.
* @returns {Boolean} true if this framebuffer's texture was bound successfully, otherwise false.
*/
FramebufferTexture.prototype.bind = function (dc) {
if (this.texture) {
dc.currentGlContext.bindTexture(gl.TEXTURE_2D, this.texture);
}
return !!this.texture;
};
return FramebufferTexture;
});