70 lines
2.1 KiB
JavaScript
70 lines
2.1 KiB
JavaScript
function initBuffers(gl)
|
|
{
|
|
const vertexBuffer = initVertexBuffer(gl);
|
|
const indexBuffer = initIndexBuffer(gl);
|
|
const normalBuffer = initNormalBuffer(gl);
|
|
return { vertex: vertexBuffer, index : indexBuffer, normal: normalBuffer };
|
|
}
|
|
|
|
function initVertexBuffer(gl)
|
|
{
|
|
const positionBuffer = gl.createBuffer();
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
|
|
const positions = [
|
|
// Front face
|
|
-1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0,
|
|
// Back face
|
|
-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0,
|
|
// Top face
|
|
-1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0,
|
|
// Bottom face
|
|
-1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0,
|
|
// Right face
|
|
1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0,
|
|
// Left face
|
|
-1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0
|
|
];
|
|
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
|
|
return positionBuffer;
|
|
}
|
|
|
|
function initNormalBuffer(gl)
|
|
{
|
|
const normalBuffer = gl.createBuffer();
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
|
|
const vertexNormals = [
|
|
// Front
|
|
0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
|
|
// Back
|
|
0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0,
|
|
// Top
|
|
0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
|
|
// Bottom
|
|
0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0,
|
|
// Right
|
|
1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
|
|
// Left
|
|
-1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0,
|
|
];
|
|
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertexNormals), gl.STATIC_DRAW);
|
|
return normalBuffer;
|
|
}
|
|
|
|
function initIndexBuffer(gl)
|
|
{
|
|
const indexBuffer = gl.createBuffer();
|
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
|
|
const indices = [
|
|
0, 1, 2, 0, 2, 3, // front
|
|
4, 5, 6, 4, 6, 7, // back
|
|
8, 9, 10, 8, 10, 11, // top
|
|
12, 13, 14, 12, 14, 15, // bottom
|
|
16, 17, 18, 16, 18, 19, // right
|
|
20, 21, 22, 20, 22, 23, // left
|
|
];
|
|
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
|
|
return indexBuffer;
|
|
}
|
|
|
|
export { initBuffers };
|