1 module hoekjed.opengl.vao;
2 
3 import bindbc.opengl;
4 import hoekjed;
5 
6 class VAO {
7 	private uint vao;
8 	private VBO[uint] vbos;
9 	private EBO ebo;
10 	private uint hoekAantal;
11 
12 	public this() {
13 		glGenVertexArrays(1, &vao);
14 	}
15 
16 	public ~this() {
17 		import std.stdio;
18 
19 		glDeleteVertexArrays(1, &vao);
20 		stderr.write("VAO ");
21 		stderr.write(vao);
22 		stderr.writeln(" is verwijderd.");
23 	}
24 
25 	public void teken() {
26 		zetVAO();
27 		tekenVAO();
28 	}
29 
30 	public void zetInhoud(M : Mat!(L, 1, S), uint L, S)(uint plek, M[] inhoud)
31 			if (L > 0 && L <= 4) {
32 		assert(plek !in vbos);
33 		this.zetVAO();
34 		this.vbos[plek] = new VBO(inhoud);
35 		glVertexAttribPointer(plek, L, ctGLenum!(S)(), false, M.sizeof, null);
36 		glEnableVertexAttribArray(plek);
37 	}
38 
39 	public void zetVolgorde(Vec!(3, uint)[] volgorde) {
40 		this.zetVAO();
41 		this.ebo = new EBO(volgorde);
42 		this.hoekAantal = 3 * cast(uint) volgorde.length;
43 	}
44 
45 	public void tekenVAO() {
46 		glDrawElements(GL_TRIANGLES, this.hoekAantal, GL_UNSIGNED_INT, null);
47 	}
48 
49 	public void zetVAO() {
50 		glBindVertexArray(vao);
51 	}
52 }
53 
54 private GLenum ctGLenum(S)() {
55 	static if (is(S == byte))
56 		return GL_BYTE;
57 	else static if (is(S == ubyte))
58 		return GL_UNSIGNED_BYTE;
59 	else static if (is(S == short))
60 		return GL_SHORT;
61 	else static if (is(S == ushort))
62 		return GL_UNSIGNED_SHORT;
63 	else static if (is(S == int))
64 		return GL_INT;
65 	else static if (is(S == uint))
66 		return GL_UNSIGNED_INT;
67 	else static if (is(S == float))
68 		return GL_FLOAT;
69 	else static if (is(S == double))
70 		return GL_DOUBLE;
71 	else
72 		static assert(0, "Soort " ~ S.stringof ~ " niet ondersteund.");
73 }