1 module opengl.d.vertexarray;
2 
3 import opengl.gl4;
4 
5 ///
6 enum VertexAttribType
7 {
8 	byte_ = GL_BYTE,
9 	unsignedByte = GL_UNSIGNED_BYTE,
10 	short_ = GL_SHORT,
11 	unsigned_short = GL_UNSIGNED_SHORT,
12 	fixed = GL_FIXED,
13 	float_ = GL_FLOAT
14 }
15 
16 ///
17 struct VertexArray
18 {
19 	/// Empty constructor that needs to be called explicitly
20 	static VertexArray opCall()
21 	{
22 		VertexArray ret;
23 		glGenVertexArrays(1, &ret.id);
24 		return ret;
25 	}
26 
27 	~this()
28 	{
29 		if (id)
30 		{
31 			glDeleteVertexArrays(1, &id);
32 		}
33 	}
34 
35 	///
36 	void bind()
37 	{
38 		if (boundVAO == id)
39 			return;
40 		glBindVertexArray(id);
41 		boundVAO = id;
42 	}
43 
44 	///
45 	void enable(uint index)
46 	{
47 		bind();
48 		glEnableVertexAttribArray(index);
49 	}
50 
51 	///
52 	void disable(uint index)
53 	{
54 		bind();
55 		glDisableVertexAttribArray(index);
56 	}
57 
58 	///
59 	void draw(PrimitiveType mode, int first, uint count)
60 	{
61 		bind();
62 		glDrawArrays(mode, first, count);
63 	}
64 
65 	///
66 	void drawIndexed(PrimitiveType mode, ubyte[] indices)
67 	{
68 		bind();
69 		glDrawElements(mode, cast(GLsizei) indices.length, GL_UNSIGNED_BYTE, indices.ptr);
70 	}
71 
72 	///
73 	void drawIndexed(PrimitiveType mode, uint count, ubyte[] indices)
74 	{
75 		bind();
76 		glDrawElements(mode, count, GL_UNSIGNED_BYTE, indices.ptr);
77 	}
78 
79 	///
80 	void drawIndexed(PrimitiveType mode, ushort[] indices)
81 	{
82 		bind();
83 		glDrawElements(mode, cast(GLsizei) indices.length, GL_UNSIGNED_SHORT, indices.ptr);
84 	}
85 
86 	///
87 	void drawIndexed(PrimitiveType mode, uint count, ushort[] indices)
88 	{
89 		bind();
90 		glDrawElements(mode, count, GL_UNSIGNED_SHORT, indices.ptr);
91 	}
92 
93 	///
94 	void drawIndexed(PrimitiveType mode, uint[] indices)
95 	{
96 		bind();
97 		glDrawElements(mode, cast(GLsizei) indices.length, GL_UNSIGNED_INT, indices.ptr);
98 	}
99 
100 	///
101 	void drawIndexed(PrimitiveType mode, uint count, uint[] indices)
102 	{
103 		bind();
104 		glDrawElements(mode, count, GL_UNSIGNED_INT, indices.ptr);
105 	}
106 
107 	///
108 	void pointer(uint index, int size = 4, VertexAttribType type = VertexAttribType.float_,
109 			bool normalized = false, uint stride = 0, void* pointer = null)
110 	{
111 		glVertexAttribPointer(index, size, type, normalized, stride, pointer);
112 	}
113 
114 	///
115 	uint id;
116 }
117 
118 private __gshared uint boundVAO;