function cubeGeometry(gl) {
const RED = [ 1.0, 0.0, 0.0 ];
const YELLOW = [ 1.0, 1.0, 0.0 ];
const GREEN = [ 0.0, 1.0, 0.0 ];
const CYAN = [ 0.0, 1.0, 1.0 ];
const BLUE = [ 0.0, 0.0, 1.0 ];
const PURPLE = [ 1.0, 0.0, 1.0 ];
const [ A,B,D,C,E ] = [
[ -1.0, 1.0, -1.0 ],
[ 1.0, 1.0, -1.0 ],
[ -1.0, -1.0, -1.0 ],
[ 1.0, -1.0, -1.0 ],
[ 0.0, 0.0, 1.0 ],
];
const position = new Float32Array([
...A, ...B, ...C, ...D, ...E
]);
const color = new Float32Array([
...RED, ...RED, ...RED, ...RED,
...GREEN, ...GREEN, ...GREEN,
...BLUE, ...BLUE, ...BLUE,
...PURPLE, ...PURPLE, ...PURPLE,
...CYAN, ...CYAN, ...CYAN
]);
const index = new Uint16Array([
0, 1, 4,
1, 2, 4,
2, 3, 4,
3, 0, 4,
0, 1, 2,
0, 2, 3
]);
const prog = createProgram(gl, [
[ gl.VERTEX_SHADER, vertexShader ],
[ gl.FRAGMENT_SHADER, fragmentShader ]
]);
let a_position = gl.getAttribLocation(prog,"a_position");
let a_color = gl.getAttribLocation(prog,"a_color");
let vao = gl.createVertexArray()
gl.bindVertexArray(vao)
gl.enableVertexAttribArray(a_position);
gl.enableVertexAttribArray(a_color);
let position_vbo = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER,position_vbo);
gl.bufferData(gl.ARRAY_BUFFER,position,gl.STATIC_DRAW);
gl.vertexAttribPointer(a_position, 3, gl.FLOAT, false, 0, 0);
let color_vbo = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER,color_vbo);
gl.bufferData(gl.ARRAY_BUFFER,color,gl.STATIC_DRAW);
gl.vertexAttribPointer(a_color, 3, gl.FLOAT, false, 0, 0);
let index_vbo = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,index_vbo);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,index,gl.STATIC_DRAW);
return (uniforms) => {
gl.bindVertexArray(vao);
gl.useProgram(prog);
gl.uniformMatrix4fv(gl.getUniformLocation(prog,"model"),false,uniforms.model);
gl.uniformMatrix4fv(gl.getUniformLocation(prog,"view"),false,uniforms.view);
gl.uniformMatrix4fv(gl.getUniformLocation(prog,"projection"),false,uniforms.projection);
gl.drawElements(gl.TRIANGLES,index.length,gl.UNSIGNED_SHORT,0);
}
}