85 lines
1.9 KiB
TypeScript
85 lines
1.9 KiB
TypeScript
|
export interface GlCanvasBuffers {
|
||
|
position: WebGLBuffer,
|
||
|
normal: WebGLBuffer,
|
||
|
textureCoord: WebGLBuffer,
|
||
|
indices: WebGLBuffer,
|
||
|
};
|
||
|
|
||
|
export function initBuffers(gl: WebGLRenderingContext): GlCanvasBuffers {
|
||
|
const positionBuffer = initPositionBuffer(gl);
|
||
|
const textureCoordBuffer = initTextureBuffer(gl);
|
||
|
const indexBuffer = initIndexBuffer(gl);
|
||
|
const normalBuffer = initNormalBuffer(gl);
|
||
|
|
||
|
return {
|
||
|
position: positionBuffer,
|
||
|
normal: normalBuffer,
|
||
|
textureCoord: textureCoordBuffer,
|
||
|
indices: indexBuffer,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
function initPositionBuffer(gl: WebGLRenderingContext) {
|
||
|
const positionBuffer = gl.createBuffer();
|
||
|
|
||
|
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
|
||
|
const positions = [
|
||
|
-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 initIndexBuffer(gl: WebGLRenderingContext) {
|
||
|
const indexBuffer = gl.createBuffer();
|
||
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
|
||
|
|
||
|
const indices = [
|
||
|
0, 1, 2,
|
||
|
0, 2, 3,
|
||
|
];
|
||
|
|
||
|
gl.bufferData(
|
||
|
gl.ELEMENT_ARRAY_BUFFER,
|
||
|
new Uint16Array(indices),
|
||
|
gl.STATIC_DRAW
|
||
|
);
|
||
|
|
||
|
return indexBuffer;
|
||
|
}
|
||
|
|
||
|
function initTextureBuffer(gl: WebGLRenderingContext) {
|
||
|
const textureCoordBuffer = gl.createBuffer();
|
||
|
gl.bindBuffer(gl.ARRAY_BUFFER, textureCoordBuffer);
|
||
|
|
||
|
const textureCoordinates = [
|
||
|
0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
|
||
|
];
|
||
|
|
||
|
gl.bufferData(
|
||
|
gl.ARRAY_BUFFER,
|
||
|
new Float32Array(textureCoordinates),
|
||
|
gl.STATIC_DRAW
|
||
|
);
|
||
|
|
||
|
return textureCoordBuffer;
|
||
|
}
|
||
|
|
||
|
function initNormalBuffer(gl: WebGLRenderingContext) {
|
||
|
const normalBuffer = gl.createBuffer();
|
||
|
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
|
||
|
|
||
|
const vertexNormals = [
|
||
|
0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
|
||
|
];
|
||
|
|
||
|
gl.bufferData(
|
||
|
gl.ARRAY_BUFFER,
|
||
|
new Float32Array(vertexNormals),
|
||
|
gl.STATIC_DRAW
|
||
|
);
|
||
|
|
||
|
return normalBuffer;
|
||
|
}
|