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,C,D,E,F,G,H ] = [
[ -1.0, -1.0, 1.0 ],
[ 1.0, -1.0, 1.0 ],
[ 0.0, 1.0, 0.0 ],
[ 0.0, 1.0, 0.0 ],
[ -1.0, -1.0, -1.0 ],
[ 1.0, -1.0, -1.0 ],
[ 0.0, 1.0, 0.0 ],
[ 0.0, 1.0, 0.0 ],
];
const position = new Float32Array([
...A, ...B, ...C, ...D,
...B, ...F, ...G, ...C,
...F, ...E, ...H, ...G,
...E, ...A, ...D, ...H,
...D, ...C, ...G, ...H,
...E, ...F, ...B, ...A
]);
const normal = new Float32Array([
...calculaNormal(A,B,C),...calculaNormal(A,B,C),...calculaNormal(A,B,C),...calculaNormal(A,B,C),
...calculaNormal(B,F,G),...calculaNormal(B,F,G),...calculaNormal(B,F,G),...calculaNormal(B,F,G),
...calculaNormal(F,E,H),...calculaNormal(F,E,H),...calculaNormal(F,E,H),...calculaNormal(F,E,H),
...calculaNormal(E,A,D),...calculaNormal(E,A,D),...calculaNormal(E,A,D),...calculaNormal(E,A,D),
...calculaNormal(D,C,G),...calculaNormal(D,C,G),...calculaNormal(D,C,G),...calculaNormal(D,C,G),
...calculaNormal(E,F,B),...calculaNormal(E,F,B),...calculaNormal(E,F,B),...calculaNormal(E,F,B),
]);
const color = new Float32Array([
...RED, ...RED, ...RED, ...RED,
...BLUE, ...BLUE, ...BLUE, ...BLUE,
...GREEN, ...GREEN, ...GREEN, ...GREEN,
...YELLOW, ...YELLOW, ...YELLOW, ...YELLOW,
...CYAN, ...CYAN, ...CYAN, ...CYAN,
...PURPLE, ...PURPLE, ...PURPLE, ...PURPLE,
]);
const index = new Uint16Array([
0, 1, 2, 0, 2, 3,
4, 5, 6, 4, 6, 7,
8, 9, 10, 8, 10, 11,
12, 13, 14, 12, 14, 15,
16, 17, 18, 16, 18, 19,
20, 21, 22, 20, 22, 23
]);
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 a_normal = gl.getAttribLocation(prog,"a_normal");
let vao = gl.createVertexArray()
gl.bindVertexArray(vao)
gl.enableVertexAttribArray(a_position);
gl.enableVertexAttribArray(a_color);
gl.enableVertexAttribArray(a_normal);
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 normal_vbo = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER,normal_vbo);
gl.bufferData(gl.ARRAY_BUFFER,normal,gl.STATIC_DRAW);
gl.vertexAttribPointer(a_normal, 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);
}
}