1 module gfm.opengl.vao; 2 3 import std..string; 4 5 import derelict.opengl3.gl3; 6 7 import gfm.core.log, 8 gfm.opengl.opengl; 9 10 /// OpenGL Vertex Array Object wrapper. 11 final class VAO 12 { 13 public 14 { 15 /// Creates a VAO. 16 /// Throws: $(D OpenGLException) on error. 17 this(OpenGL gl, GLuint target, GLuint storage, GLuint usage) 18 { 19 _gl = gl; 20 glGenVertexArrays(1, &_handle); 21 gl.runtimeCheck(); 22 _initialized = true; 23 } 24 25 ~this() 26 { 27 close(); 28 } 29 30 /// Releases the OpenGL VAO resource. 31 void close() 32 { 33 if (_initialized) 34 { 35 glDeleteVertexArrays(1, &_handle); 36 _initialized = false; 37 } 38 } 39 40 /// Uses this VAO. 41 /// Throws: $(D OpenGLException) on error. 42 void bind() 43 { 44 glBindVertexArray(_handle); 45 _gl.runtimeCheck(); 46 } 47 48 /// Unuses this VAO. 49 /// Throws: $(D OpenGLException) on error. 50 void unbind() 51 { 52 glBindVertexArray(0); 53 _gl.runtimeCheck(); 54 } 55 56 /// Returns: Wrapped OpenGL resource handle. 57 GLuint handle() pure const nothrow 58 { 59 return _handle; 60 } 61 } 62 63 private 64 { 65 OpenGL _gl; 66 GLuint _handle; 67 bool _initialized; 68 } 69 }