-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgl-state.js
245 lines (225 loc) · 6.24 KB
/
gl-state.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* GLState manages WebGL state to prevent redundant state changes
* and provide a cleaner interface for WebGL operations.
*/
class GLState {
constructor(gl) {
this.gl = gl;
// Track current state
this.currentState = {
program: null,
vao: null,
activeTexture: null,
boundTextures: new Array(gl.getParameter(gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS)).fill(null),
blendEnabled: null,
framebuffer: null,
viewport: { x: 0, y: 0, width: 0, height: 0 },
clearColor: [0, 0, 0, 0],
depthTest: null,
cullFace: null,
frontFace: null
};
// Initialize WebGL state
this.gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
}
/**
* Set the current WebGL program
*/
useProgram(program) {
if (this.currentState.program !== program) {
this.gl.useProgram(program);
this.currentState.program = program;
return true;
}
return false;
}
/**
* Bind a Vertex Array Object
*/
bindVAO(vao) {
if (this.currentState.vao !== vao) {
this.gl.bindVertexArray(vao);
this.currentState.vao = vao;
return true;
}
return false;
}
/**
* Set the active texture unit
*/
activeTexture(unit) {
if (this.currentState.activeTexture !== unit) {
this.gl.activeTexture(this.gl.TEXTURE0 + unit);
this.currentState.activeTexture = unit;
return true;
}
return false;
}
/**
* Bind a texture to the current active texture unit
*/
bindTexture(texture, unit = 0) {
this.activeTexture(unit);
if (this.currentState.boundTextures[unit] !== texture) {
this.gl.bindTexture(this.gl.TEXTURE_2D, texture);
this.currentState.boundTextures[unit] = texture;
return true;
}
return false;
}
/**
* Enable/disable blending
*/
setBlending(enable) {
if (this.currentState.blendEnabled !== enable) {
if (enable) {
this.gl.enable(this.gl.BLEND);
} else {
this.gl.disable(this.gl.BLEND);
}
this.currentState.blendEnabled = enable;
return true;
}
return false;
}
/**
* Set blend function
*/
setBlendFunc(src, dst) {
this.gl.blendFunc(src, dst);
}
/**
* Bind framebuffer
*/
bindFramebuffer(framebuffer) {
if (this.currentState.framebuffer !== framebuffer) {
this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, framebuffer);
this.currentState.framebuffer = framebuffer;
return true;
}
return false;
}
/**
* Set viewport dimensions
*/
setViewport(x, y, width, height) {
const viewport = { x, y, width, height };
if (JSON.stringify(this.currentState.viewport) !== JSON.stringify(viewport)) {
this.gl.viewport(x, y, width, height);
this.currentState.viewport = viewport;
return true;
}
return false;
}
/**
* Set clear color
*/
setClearColor(r, g, b, a = 1.0) {
const color = [r, g, b, a];
if (JSON.stringify(this.currentState.clearColor) !== JSON.stringify(color)) {
this.gl.clearColor(r, g, b, 1.0); // Force alpha to 1.0
this.currentState.clearColor = color;
return true;
}
return false;
}
/**
* Enable/disable depth testing
*/
setDepthTest(enable) {
if (this.currentState.depthTest !== enable) {
if (enable) {
this.gl.enable(this.gl.DEPTH_TEST);
} else {
this.gl.disable(this.gl.DEPTH_TEST);
}
this.currentState.depthTest = enable;
return true;
}
return false;
}
/**
* Enable/disable face culling
*/
setCullFace(enable) {
if (this.currentState.cullFace !== enable) {
if (enable) {
this.gl.enable(this.gl.CULL_FACE);
} else {
this.gl.disable(this.gl.CULL_FACE);
}
this.currentState.cullFace = enable;
return true;
}
return false;
}
/**
* Set front face orientation
*/
setFrontFace(mode) {
if (this.currentState.frontFace !== mode) {
this.gl.frontFace(mode);
this.currentState.frontFace = mode;
return true;
}
return false;
}
/**
* Clear the current framebuffer
*/
clear(mask = this.gl.COLOR_BUFFER_BIT) {
// Ensure we're clearing with proper alpha
const currentColor = this.currentState.clearColor;
this.gl.clearColor(currentColor[0], currentColor[1], currentColor[2], 1.0);
// Clear the buffer
this.gl.clear(mask);
// Reset viewport to ensure full coverage
const viewport = this.currentState.viewport;
this.gl.viewport(viewport.x, viewport.y, viewport.width, viewport.height);
}
/**
* Reset all tracked state
*/
reset() {
this.currentState = {
program: null,
vao: null,
activeTexture: null,
boundTextures: new Array(this.gl.getParameter(this.gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS)).fill(null),
blendEnabled: null,
framebuffer: null,
viewport: { x: 0, y: 0, width: 0, height: 0 },
clearColor: [0, 0, 0, 0],
depthTest: null,
cullFace: null,
frontFace: null
};
}
/**
* Create a snapshot of current state
*/
saveState() {
return { ...this.currentState };
}
/**
* Restore state from a snapshot
*/
restoreState(savedState) {
if (savedState.program !== this.currentState.program) {
this.useProgram(savedState.program);
}
if (savedState.vao !== this.currentState.vao) {
this.bindVAO(savedState.vao);
}
if (savedState.blendEnabled !== this.currentState.blendEnabled) {
this.setBlending(savedState.blendEnabled);
}
if (savedState.framebuffer !== this.currentState.framebuffer) {
this.bindFramebuffer(savedState.framebuffer);
}
if (JSON.stringify(savedState.viewport) !== JSON.stringify(this.currentState.viewport)) {
this.setViewport(savedState.viewport.x, savedState.viewport.y,
savedState.viewport.width, savedState.viewport.height);
}
}
}