1 /*
2 *******************************************************************************************
3 * Dgame (a D game framework) - Copyright (c) Randy Schütt
4 * 
5 * This software is provided 'as-is', without any express or implied warranty.
6 * In no event will the authors be held liable for any damages arising from
7 * the use of this software.
8 * 
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 * 
13 * 1. The origin of this software must not be misrepresented; you must not claim
14 *    that you wrote the original software. If you use this software in a product,
15 *    an acknowledgment in the product documentation would be appreciated but is
16 *    not required.
17 * 
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 *    misrepresented as being the original software.
20 * 
21 * 3. This notice may not be removed or altered from any source distribution.
22 *******************************************************************************************
23 */
24 module Dgame.System.VertexArrayObject;
25 
26 private import derelict.opengl3.gl;
27 
28 /**
29  * A Vertex Array stores informations of a Buffer object and restore them if you bind this.
30  * This decrease the calls of the Buffer, because the last state is always stored and can be restored easily.
31  *
32  * Author: rschuett
33  */
34 class VertexArrayObject {
35 private:
36 	GLuint _vid;
37 	
38 public:
39 final:
40 	/**
41 	 * CTor
42 	 */
43 	this() {
44 		glGenVertexArrays(1, &this._vid);
45 		
46 		this.bind();
47 		this.unbind();
48 	}
49 	
50 	/**
51 	 * DTor
52 	 */
53 	~this() {
54 		glDeleteVertexArrays(1, &this._vid);
55 	}
56 	
57 	/**
58 	 * Bind the Vertex Array
59 	 */
60 	void bind() const {
61 		glBindVertexArray(this._vid);
62 	}
63 	
64 	/**
65 	 * Unbind the Vertex array
66 	 */
67 	void unbind() const {
68 		glBindVertexArray(0);
69 	}
70 	
71 	/**
72 	 * Enable the usage of the Vertex array on the specific index
73 	 */
74 	void enable(ubyte index) const {
75 		glEnableVertexAttribArray(index);
76 	}
77 	
78 	/**
79 	 * Disable the usage
80 	 * 
81 	 * See: enable
82 	 */
83 	void disable(ubyte index) const {
84 		glDisableVertexAttribArray(index);
85 	}
86 	
87 	/**
88 	 * Bind, Enable and Point to the Vertex array on the specific index
89 	 * 
90 	 */
91 	void pointTo(ubyte index) const {
92 		this.bind();
93 		
94 		this.enable(index);
95 		scope(exit) this.disable(index);
96 		
97 		glVertexAttribPointer(index, 3, GL_FLOAT, GL_FALSE, 0, null);
98 	}
99 	
100 	/**
101 	 * Returns if this Vertex array is valid
102 	 */
103 	bool isValid() const {
104 		return glIsVertexArray(this._vid) == GL_TRUE;
105 	}
106 }