Skip to content

Instantly share code, notes, and snippets.

@anden3
Created January 3, 2018 22:49
Show Gist options
  • Select an option

  • Save anden3/1b84d766aef0b92555d18b7c2fb7a511 to your computer and use it in GitHub Desktop.

Select an option

Save anden3/1b84d766aef0b92555d18b7c2fb7a511 to your computer and use it in GitHub Desktop.
CreateHouse2
function createHouse2() {
const vertices = [
[-10, -5, 10], // 0
[10, -5, 10], // 1
[-10, 5, 10], // 2
[10, 5, 10], // 3
[0, 15, 10], // 4
[-10, -5, -10], // 5
[-10, 5, -10], // 6
[0, 15, -10], // 7
[10, -5, -10], // 8
[10, 5, -10] // 9
];
const cornerColors = new Map([
["-10,-5,10", [1, 0, 0, 1]],
["10,-5,10", [0, 1, 0, 1]],
["-10,5,10", [0, 0, 1, 1]],
["10,5,10", [1, 1, 0, 1]],
["0,15,10", [0, 1, 1, 1]],
["-10,-5,-10", [1, 0, 1, 1]],
["-10,5,-10", [1, 1, 1, 1]],
["0,15,-10", [0.5, 0.5, 0, 1]],
["10,-5,-10", [0, 0.5, 0.5, 1]],
["10,5,-10", [0.5, 0, 0.5, 1]]
]);
const indexes = [
0, 1, 2, 1, 3, 2, // Front side wall
2, 3, 4, // Front side roof
5, 0, 6, 0, 2, 6, // Left side wall
6, 2, 7, 2, 4, 7, // Left side roof
8, 5, 9, 5, 6, 9, // Back side wall
9, 6, 7, // Back side roof
1, 8, 3, 8, 9, 3, // Right side wall
3, 9, 4, 9, 7, 4, // Right side roof
5, 8, 0, 8, 1, 0 // Floor
];
let data = [];
for (let vertex of vertices) {
data.extend(vertex);
data.extend(cornerColors.get(vertex.toString()));
}
const indexBuffer = gl.createBuffer();
const vertexBuffer = gl.createBuffer();
shared.house2.vertexCount = indexes.length;
shared.house2.vertexArray = gl.createVertexArray();
gl.bindVertexArray(shared.house2.vertexArray);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint8Array(indexes), gl.STATIC_DRAW);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 28, 0);
gl.enableVertexAttribArray(1);
gl.vertexAttribPointer(1, 4, gl.FLOAT, false, 28, 12);
gl.bindVertexArray(null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment