1 module glwt.vertex_array;
2 
3 import cst_	;
4 
5 import derelict.opengl	;
6 
7 import glwt.buffer;
8 public import glwt.types;
9 
10 
11 /**	Unittest to ensure Compiler works as expected*/
12 static this(){
13 	VertexArray	aa;
14 	uint	au = *(cast(uint*)cast(void*)&aa);
15 	assert(aa.id==au, "Error, byte data for Struct is not as expected.");
16 	uint	bu;
17 	ArrayBuffer	ba = *(cast(ArrayBuffer*)cast(void*)&bu);
18 	assert(ba.id==bu, "Error, byte data for Struct is not as expected.");
19 	ArrayBuffer	[2]	ca;
20 	uint	[2]	cu = cast(uint[])cast(void[])ca;
21 	assert(ca[0].id==cu[0], "Error, byte data for Struct is not as expected.");
22 	assert(ca[1].id==cu[1], "Error, byte data for Struct is not as expected.");
23 }
24 
25 
26 /**	A wrapper for VertexArrays.*/
27 struct VertexArray {
28 	uint id = 0;
29 }
30 
31 
32 
33 
34 
35 
36 /**	glGenBuffers() wrapper.
37 */
38 void
39 gen(ref VertexArray vertexArray)
40 {
41 	glGenVertexArrays(1, &vertexArray.id);
42 }
43 /**	glGenBuffers() wrapper.
44 */
45 void
46 gen(ref VertexArray[] vertexArrays)
47 {
48 	// Works because a struct with a single var has the same byte data and size as its only var.
49 	glGenVertexArrays(vertexArrays.length, vertexArrays.ptr.cst!(uint*));
50 }
51 
52 
53 /**	glDeleteBuffers() wrapper.
54 */
55 void
56 del(ref VertexArray vertexArray)
57 {
58 	glDeleteVertexArrays(1, &vertexArray.id);
59 }
60 
61 
62 
63 
64 
65 
66 
67 
68 enum string array_ensureGen_m = "version(assert){assert(vertexArray.id!=0, \"Vertex Array not generated.  Call `vertexArray.gen` before using this function.\");}";
69 enum string array_ensureBound_m = array_ensureGen_m~"version(assert){
70 uint ba;//bound array
71 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, cast(int*)&ba);
72 assert(vertexArray.id==ba, \"Vertex Array not bound.  Call `vertexArray.bind` before using this function.\");
73 }";
74 
75 
76 
77 
78 
79 
80 /**	glBindBuffer wrapper.  
81 Binds buffer to be used by future calls.
82 */
83 void 
84 bind(VertexArray vertexArray)
85 {
86 	mixin(array_ensureGen_m);
87 	glBindVertexArray(vertexArray.id);
88 }
89 /**ditto*/
90 alias use=bind;
91 
92 
93 
94 
95 
96 
97 /**	glEnableVertexAttribArray wrapper
98 */
99 void
100 enableAttribute(VertexArray vertexArray, uint attribute) {
101 	mixin(array_ensureBound_m);
102 	glEnableVertexAttribArray(attribute);
103 }
104 ///ditto
105 alias enableVertexArribArray = enableAttribute;
106 ///ditto
107 alias enableAttrib = enableAttribute;
108 
109 /**	glDisableVertexAttribArray wrapper
110 */
111 void
112 disableAttribute(VertexArray vertexArray, uint attribute) {
113 	mixin(array_ensureBound_m);
114 	glDisableVertexAttribArray(attribute);
115 }
116 ///ditto
117 alias disableVertexArribArray = disableAttribute;
118 ///ditto
119 alias disableAttrib = disableAttribute;
120 
121 
122 
123 
124 /**	glVertexAttribPointer wrapper.
125 */
126 void 
127 attributePointer(VertexArray vertexArray, uint attribute, ArrayBuffer buffer, uint count, DataType type, bool normalized=false, uint stride=0, int* offset=null) {
128 	mixin(buffer_ensureBound_m);
129 	vertexArray.attributePointer(attribute, count, type, normalized, stride, offset);
130 }
131 ///ditto
132 void 
133 attributePointer(VertexArray vertexArray, uint attribute, uint count, DataType type, bool normalized=false, uint stride=0, int* offset=null) {
134 	mixin(array_ensureBound_m);
135 	version(assert){
136 		bool ae;//attribute enabled
137 		glGetVertexAttribiv(attribute, GL_VERTEX_ATTRIB_ARRAY_ENABLED, cast(int*)&ae);
138 		assert(ae, "Vertex Attribute not enabled.  Call `vertexArray.enableAttribute(attribute)` before assigning value.");
139 	}
140 	glVertexAttribPointer(attribute, count, type, normalized?GL_TRUE:GL_FALSE, stride, offset);
141 }
142 ///ditto
143 alias bindAttribute = attributePointer;
144 ///ditto
145 alias attribPointer = attributePointer;
146 
147 ////void bindElements(ref ArrayBuffer elements) {
148 ////   bindContext;
149 ////   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elements.id);
150 ////}