webgl: move drawRectangle out; make this.buffers a simple array

This commit is contained in:
Zellyn Hunter 2018-05-13 22:34:55 -04:00
parent 54f7e05af6
commit cffa70ea35

View File

@ -535,10 +535,7 @@ void main(void)
"IMAGE_PERSISTENCE", "IMAGE_PERSISTENCE",
]; ];
const BUFFER_NAMES = [ const BUFFER_COUNT = 3;
"POSITION",
"TEXCOORD",
];
const SHADER_NAMES = [ const SHADER_NAMES = [
"COMPOSITE", "COMPOSITE",
@ -678,7 +675,7 @@ void main(void)
this.gl = gl; this.gl = gl;
this.textures = {}; this.textures = {};
this.shaders = {}; this.shaders = {};
this.buffers = {}; this.buffers = [];
this.image = null; this.image = null;
this.display = null; this.display = null;
this.imageSampleRate = null; this.imageSampleRate = null;
@ -718,8 +715,8 @@ void main(void)
this.textures[name] = new TextureInfo(0, 0, gl.createTexture()); this.textures[name] = new TextureInfo(0, 0, gl.createTexture());
} }
for (let name of BUFFER_NAMES) { for (let i = 0; i < BUFFER_COUNT; i++) {
this.buffers[name] = gl.createBuffer(); this.buffers.push(gl.createBuffer());
} }
await this.loadTextures(); await this.loadTextures();
@ -737,8 +734,8 @@ void main(void)
gl.deleteTexture(this.textures[name].glTexture); gl.deleteTexture(this.textures[name].glTexture);
} }
for (let name of BUFFER_NAMES) { for (let buffer of this.buffers) {
gl.deleteBuffer(this.buffers[name]); gl.deleteBuffer(buffer);
} }
this.deleteShaders(); this.deleteShaders();
@ -1179,14 +1176,30 @@ void main(void)
// What is this, some ancient fixed pipeline nonsense? No way. // What is this, some ancient fixed pipeline nonsense? No way.
// glLoadIdentity(); // glLoadIdentity();
const positionLocation = gl.getAttribLocation(renderShader, "a_position"); this.drawRectangle(renderShader, canvasRect, textureRect);
const texcoordLocation = gl.getAttribLocation(renderShader, "a_texCoord");
const positionBuffer = this.buffers["POSITION"]; // Copy framebuffer
gl.bindTexture(gl.TEXTURE_2D, this.textures["IMAGE_DECODED"].glTexture);
gl.copyTexSubImage2D(gl.TEXTURE_2D, 0,
x, y, 0, 0,
clipSize.width, clipSize.height);
}
}
}
drawRectangle(shader, posRect, texRect, texRect2) {
const gl = this.gl;
const positionLocation = gl.getAttribLocation(shader, "a_position");
const texcoordLocation = gl.getAttribLocation(shader, "a_texCoord");
const positionBuffer = this.buffers[0];
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
const p_x1 = canvasRect.l; const p_x1 = posRect.l;
const p_x2 = canvasRect.r; const p_x2 = posRect.r;
const p_y1 = canvasRect.t; const p_y1 = posRect.t;
const p_y2 = canvasRect.b; const p_y2 = posRect.b;
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
p_x1, p_y1, p_x1, p_y1,
p_x2, p_y1, p_x2, p_y1,
@ -1196,12 +1209,12 @@ void main(void)
p_x2, p_y2, p_x2, p_y2,
]), gl.STATIC_DRAW); ]), gl.STATIC_DRAW);
const texcoordBuffer = this.buffers["TEXCOORD"]; const texcoordBuffer = this.buffers[1];
gl.bindBuffer(gl.ARRAY_BUFFER, texcoordBuffer); gl.bindBuffer(gl.ARRAY_BUFFER, texcoordBuffer);
const t_x1 = textureRect.l; const t_x1 = texRect.l;
const t_x2 = textureRect.r; const t_x2 = texRect.r;
const t_y1 = textureRect.t; const t_y1 = texRect.t;
const t_y2 = textureRect.b; const t_y2 = texRect.b;
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
t_x1, t_y1, t_x1, t_y1,
t_x2, t_y1, t_x2, t_y1,
@ -1223,15 +1236,6 @@ void main(void)
gl.vertexAttribPointer(texcoordLocation, 2, gl.FLOAT, false, 0, 0); gl.vertexAttribPointer(texcoordLocation, 2, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLES, 0, 6); gl.drawArrays(gl.TRIANGLES, 0, 6);
// Copy framebuffer
gl.bindTexture(gl.TEXTURE_2D, this.textures["IMAGE_DECODED"].glTexture);
gl.copyTexSubImage2D(gl.TEXTURE_2D, 0,
x, y, 0, 0,
clipSize.width, clipSize.height);
}
}
} }
drawDisplayCanvas() { drawDisplayCanvas() {