-
Notifications
You must be signed in to change notification settings - Fork 12
/
vertex-array.js
52 lines (42 loc) · 1.03 KB
/
vertex-array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { enableVertexData } from "./utils.js";
/**
* @typedef {import("./types.js").PexResource} VertexArrayOptions
* @property {object} vertexLayout
* @property {object} [attributes]
* @property {object} [indices]
*/
function createVertexArray(ctx, opts) {
const gl = ctx.gl;
const vertexArray = {
class: "vertexArray",
handle: gl.createVertexArray(),
_update: updateVertexArray,
...opts,
};
updateVertexArray(ctx, vertexArray, opts);
return vertexArray;
}
const TYPE_TO_SIZE = {
float: 1,
vec2: 2,
vec3: 3,
vec4: 4,
mat3: 12,
mat4: 16,
};
// TODO: can't update attributes/indices as they're in vertexArray
function updateVertexArray(ctx, vertexArray, { vertexLayout }) {
const gl = ctx.gl;
gl.bindVertexArray(vertexArray.handle);
enableVertexData(
ctx,
Object.entries(vertexLayout).map(([name, { location, type, size }]) => [
name,
location,
size ?? TYPE_TO_SIZE[type],
]),
vertexArray,
);
gl.bindVertexArray(null);
}
export default createVertexArray;