-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgluitls.js
161 lines (139 loc) · 4.48 KB
/
gluitls.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
function createShader(gl, source, type)
{
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
return shader;
}
function createProgram(gl, vertexShaderSource, fragmentShaderSource)
{
var program = gl.createProgram();
var vshader = createShader(gl, vertexShaderSource, gl.VERTEX_SHADER);
var fshader = createShader(gl, fragmentShaderSource, gl.FRAGMENT_SHADER);
gl.attachShader(program, vshader);
gl.deleteShader(vshader);
gl.attachShader(program, fshader);
gl.deleteShader(fshader);
gl.linkProgram(program);
var log = gl.getProgramInfoLog(program);
if (log) {
console.log(log);
}
log = gl.getShaderInfoLog(vshader);
if (log) {
console.log(log);
}
log = gl.getShaderInfoLog(fshader);
if (log) {
console.log(log);
}
return program;
};
function createGLTexture(gl, format,intFormat, w, h,wrapType, flip, genMipmaps, data)
{
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flip);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S,wrapType);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T,wrapType);
if (genMipmaps === true)
{
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
gl.texStorage2D(gl.TEXTURE_2D, 1, intFormat, w, h);
} else
{
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texStorage2D(gl.TEXTURE_2D, 1, intFormat, w, h);
}
if (data != null) {
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0,w,h, format, gl.UNSIGNED_BYTE, data);
}
if (genMipmaps === true) {
gl.generateMipmap(gl.TEXTURE_2D);
}
return texture;
}
function updateGLTexture(gl,texture, format,w,h, genMipmaps, data)
{
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0,w,h, format, gl.UNSIGNED_BYTE, data);
if (genMipmaps === true)
{
gl.generateMipmap(gl.TEXTURE_2D);
}
gl.bindTexture(gl.TEXTURE_2D, null);
}
function createFrameBuffer(gl,tex)
{
var fboObj = {};
var fbo = 0;
fbo = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
if (tex != null)
{
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
fboObj.texRT = tex;
}
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
fboObj.fbo = fbo;
return fboObj;
}
function createRenderBuffer(gl,format,w,h)
{
var renderBuff = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, renderBuff);
gl.renderbufferStorage(gl.RENDERBUFFER, format, w, h);
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
return renderBuff;
}
function createMultisampledRenderBuffer(gl,format,w,h,numSamples)
{
const maxSupportedSamples = gl.getParameter(gl.MAX_SAMPLES);
if(numSamples > maxSupportedSamples)
{
numSamples = maxSupportedSamples;
}
var renderBuff = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, renderBuff);
gl.renderbufferStorageMultisample(gl.RENDERBUFFER,
numSamples,
format,
w,
h);
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
return renderBuff;
}
/**
*
* @param {*} fboObj
* @param {*} renderBuff
* @param {*} attachment -gl.COLOR_ATTACHMENTi , DEPTH_ATTACHMENT,STENCIL_ATTACHMENT
*/
function bindRenderBuffer(gl,fboObj,renderBuff,attachment)
{
gl.bindFramebuffer(gl.FRAMEBUFFER, fboObj.fbo);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment, gl.RENDERBUFFER, renderBuff);
fboObj.buffRT = renderBuff;
//check for completeness:
var statis = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
if (statis != gl.FRAMEBUFFER_COMPLETE)
{
console.error("failed to create valid frame buffer");
}
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
function bindDepthBuffer(gl,fboObj,depthBuff)
{
gl.bindFramebuffer(gl.FRAMEBUFFER, fboObj.fbo);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuff);
fboObj.buffDepth = depthBuff;
//check for completeness:
var statis = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
if (statis != gl.FRAMEBUFFER_COMPLETE)
{
console.error("failed to create valid frame buffer");
return null;
}
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}