Make a rectangle

This commit is contained in:
Brandon Dyck 2022-10-23 10:17:35 -06:00
parent 54ff9106db
commit 07380c8c91

View File

@ -113,6 +113,7 @@
0, 0, 0, 0, 0,
1, 0, 0, 1, 0,
1, 1, 0, 1, 1,
0, 1, 0, 0, 1,
]);
const vertexStride = 4 * 5;
const positionOffset = 0;
@ -122,6 +123,14 @@
const positionLocation = gl.getAttribLocation(program, "position");
const textureCoordLocation = gl.getAttribLocation(program, "textureCoord");
let indexBuffer = gl.createBuffer();
let indices = new Uint16Array([
0, 1, 2,
3, 0, 2,
]);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
// Load texture
let image = document.getElementById("image");
loadTexture(gl, image);
@ -139,7 +148,7 @@
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, vertexStride, positionOffset);
gl.drawArrays(gl.TRIANGLES, 0, 3);
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT, 0);
gl.enableVertexAttribArray(textureCoordLocation);
gl.vertexAttribPointer(textureCoordLocation, 2, gl.FLOAT, false, vertexStride, textureCoordOffset);