From 1aa3650ee3b921836085269d04bfc66f4febdfe3 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 15 Jan 2024 22:16:12 -0500 Subject: [PATCH] gltk: comment more --- libs/gltk/gltk/array.d | 20 +++++++++++++++-- libs/gltk/gltk/buffer.d | 48 +++++++++++++++++++++++++++++++++++----- libs/gltk/gltk/shader.d | 48 ++++++++++++++++++++++------------------ libs/gltk/gltk/texture.d | 24 +++++++++++++++++++- 4 files changed, 110 insertions(+), 30 deletions(-) diff --git a/libs/gltk/gltk/array.d b/libs/gltk/gltk/array.d index be36f1d..d86bf8d 100644 --- a/libs/gltk/gltk/array.d +++ b/libs/gltk/gltk/array.d @@ -2,10 +2,17 @@ module gltk.array; import gl; +/** + * OpenGL array object. + */ class Array { + /** Array ID. */ private GLuint m_id; + /** + * Construct array. + */ this() { m_id = 0u; @@ -16,17 +23,26 @@ class Array } } + /** + * Destruct array. + */ ~this() { glDeleteVertexArrays(1, &m_id); } - void bind() + /** + * Bind array. + */ + void bind() const { glBindVertexArray(m_id); } - @property GLuint id() + /** + * Get array ID. + */ + @property GLuint id() const { return m_id; } diff --git a/libs/gltk/gltk/buffer.d b/libs/gltk/gltk/buffer.d index 83d4cfb..f88caaa 100644 --- a/libs/gltk/gltk/buffer.d +++ b/libs/gltk/gltk/buffer.d @@ -2,12 +2,23 @@ module gltk.buffer; import gl; +/** + * OpenGL buffer object. + */ class Buffer { + /** Buffer ID. */ private GLuint m_id; + /** Buffer target. */ private GLenum m_target; + /** + * Construct buffer. + * + * @param target + * OpenGL buffer target (e.g. GL_ARRAY_BUFFER, GL_ELEMENT_BUFFER) + */ this(GLenum target) { m_id = 0u; @@ -19,30 +30,55 @@ class Buffer m_target = target; } + /** + * Destruct buffer. + */ ~this() { glDeleteBuffers(1, &m_id); } - void bind() + /** + * Bind buffer. + */ + void bind() const { glBindBuffer(m_target, m_id); } - @property GLuint id() + /** + * Get buffer ID. + */ + @property GLuint id() const { return m_id; } + /** + * Set buffer data. + * + * @param usage + * OpenGL buffer usage (e.g. GL_STATIC_DRAW, ...) + * @param ptr + * Pointer to buffer data. + * @param size + * Buffer data size. + */ void set_buffer_data(GLenum usage, const void * ptr, size_t size) { - bind(); - glBufferData(m_target, size, ptr, usage); + glNamedBufferData(m_id, size, ptr, usage); } + /** + * Set buffer data. + * + * @param usage + * OpenGL buffer usage (e.g. GL_STATIC_DRAW, ...) + * @param array + * Buffer data. + */ void set_buffer_data(T)(GLenum usage, T[] arr) { - bind(); - glBufferData(m_target, arr.length * arr[0].sizeof, arr.ptr, usage); + glNamedBufferData(m_id, arr.length * arr[0].sizeof, arr.ptr, usage); } } diff --git a/libs/gltk/gltk/shader.d b/libs/gltk/gltk/shader.d index 30fc62d..6db2e29 100644 --- a/libs/gltk/gltk/shader.d +++ b/libs/gltk/gltk/shader.d @@ -1,35 +1,36 @@ module gltk.shader; import gl; -static import std.file; +/** + * OpenGL shader object. + */ class Shader { + /** Shader ID. */ private GLuint m_id; - private GLenum m_shader_type; - - this(GLenum shader_type) + /** + * Construct shader. + * + * @param shader_type + * OpenGL shader type (e.g. GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, ...). + * @param source + * Shader source. + */ + this(GLenum shader_type, string source) { - m_shader_type = shader_type; m_id = glCreateShader(shader_type); if (m_id == 0u) { - throw new Exception("Failed to allocate an OpenGL shader"); + throw new Exception("Failed to allocate an OpenGL shader object"); } - } - ~this() - { - glDeleteShader(m_id); - } - - void set_source(const char * source, int length = -1) - { GLint status; - GLint gllength = length; - glShaderSource(m_id, 1, &source, &gllength); + const(GLchar) * source_string = source.ptr; + GLint source_length = cast(GLint)source.length; + glShaderSource(m_id, 1, &source_string, &source_length); glCompileShader(m_id); @@ -40,7 +41,7 @@ class Shader } string message = "Error compiling "; - switch (m_shader_type) + switch (shader_type) { case GL_VERTEX_SHADER: message ~= "vertex"; @@ -68,13 +69,18 @@ class Shader throw new Exception(message); } - void set_source_from_file(string filename) + /** + * Destruct shader object. + */ + ~this() { - const(char)[] file_data = cast(const(char)[])std.file.read(filename); - set_source(file_data.ptr, cast(int)file_data.length); + glDeleteShader(m_id); } - @property GLuint id() + /** + * Get shader ID. + */ + @property GLuint id() const { return m_id; } diff --git a/libs/gltk/gltk/texture.d b/libs/gltk/gltk/texture.d index 73a9b4d..07517f1 100644 --- a/libs/gltk/gltk/texture.d +++ b/libs/gltk/gltk/texture.d @@ -2,10 +2,17 @@ module gltk.texture; import gl; +/** + * OpenGL texture object. + */ class Texture { + /** Texture ID. */ private GLuint m_id; + /** + * Construct texture. + */ this() { m_id = 0u; @@ -16,21 +23,36 @@ class Texture } } + /** + * Destruct texture. + */ ~this() { glDeleteTextures(1, &m_id); } + /** + * Bind the texture to a texture target. + * + * @param target + * Shader target (e.g. GL_TEXTURE_2D, ...) + */ void bind(GLenum target) const { glBindTexture(target, m_id); } - @property GLuint id() + /** + * Get texture ID. + */ + @property GLuint id() const { return m_id; } + /** + * Get the smallest power of 2 that is greater than or equal to n. + */ static uint next_power_of_2(uint n) { n--;