Remove duplication of setting up the VAO (Vertex Array Object)

Signed-off-by: Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>
Change-Id: I297b4c4296546552b76288c16525f61b1dbff9e9
This commit is contained in:
Tomaž Vajngerl 2024-07-15 17:28:03 +09:00 committed by Szymon Kłos
parent d091f56f28
commit 781abd872e

View file

@ -52,22 +52,24 @@ class SlideRenderer {
`;
}
public setupPositions() {
public setupPositions(xMin: number, xMax: number, yMin: number, yMax: number) : WebGLVertexArrayObject {
const gl = this._context.gl;
// 5 numbers -> 3 x vertex X,Y,Z and 2x texture X,Y
const positions = new Float32Array([
-1.0, -1.0, 0.0, 0.0, 1.0,
1.0, -1.0, 0.0, 1.0, 1.0,
-1.0, 1.0, 0.0, 0.0, 0.0,
1.0, 1.0, 0.0, 1.0, 0.0,
// vX vX vZ tX tY
xMin, -yMin, 0.0, 0.0, 1.0,
xMax, -yMin, 0.0, 1.0, 1.0,
xMin, -yMax, 0.0, 0.0, 0.0,
xMax, -yMax, 0.0, 1.0, 0.0,
]);
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
this._vao = gl.createVertexArray();
gl.bindVertexArray(this._vao);
const vao = gl.createVertexArray();
gl.bindVertexArray(vao);
const positionLocation = gl.getAttribLocation(this._program, 'a_position');
@ -78,6 +80,8 @@ class SlideRenderer {
gl.enableVertexAttribArray(texCoordLocation);
gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 5 * 4, 3 * 4);
return vao;
}
public setup(canvas: HTMLCanvasElement) {
@ -93,7 +97,7 @@ class SlideRenderer {
this._program = this._context.createProgram(vertexShader, fragmentShader);
this.setupPositions();
this._vao = this.setupPositions(-1.0, 1.0, 1.0, -1.0);
this._context.gl.useProgram(this._program);
}
@ -151,7 +155,7 @@ class SlideRenderer {
const video = new VideoRenderInfo;
video.videoElement = this.setupVideo(videoInfo.url);
video.texture = this.initTexture();
video.vao = this.setupVideoPosition(videoInfo.x, videoInfo.y, videoInfo.width, videoInfo.height, docWidth, docHeight);
video.vao = this.setupRectangleInDocumentPositions(videoInfo.x, videoInfo.y, videoInfo.width, videoInfo.height, docWidth, docHeight);
this._videos.push(video);
}
@ -159,7 +163,7 @@ class SlideRenderer {
requestAnimationFrame(this.render.bind(this));
}
setupVideoPosition(x: number, y: number, width: number, height: number, docWidth: number, docHeight: number): WebGLVertexArrayObject {
setupRectangleInDocumentPositions(x: number, y: number, width: number, height: number, docWidth: number, docHeight: number): WebGLVertexArrayObject {
const gl = this._context.gl;
var xMin = (x / docWidth) * 2.0 - 1.0;
@ -168,31 +172,7 @@ class SlideRenderer {
var yMin = (y / docHeight) * 2.0 - 1.0;
var yMax = ((y + height) / docHeight) * 2.0 - 1.0;
const positions = new Float32Array([
xMin, -yMin, 0.0, 0.0, 1.0,
xMax, -yMin, 0.0, 1.0, 1.0,
xMin, -yMax, 0.0, 0.0, 0.0,
xMax, -yMax, 0.0, 1.0, 0.0,
]);
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
const vao = gl.createVertexArray();
gl.bindVertexArray(vao);
const positionLocation = gl.getAttribLocation(this._program, 'a_position');
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 5 * 4, 0);
const texCoordLocation = gl.getAttribLocation(this._program, 'a_texCoord');
gl.enableVertexAttribArray(texCoordLocation);
gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 5 * 4, 3 * 4);
return vao;
return this.setupPositions(xMin, xMax, yMin, yMax);
}
private render() {