gltk: comment more

This commit is contained in:
Josh Holtrop 2024-01-15 22:16:12 -05:00
parent f6cfbcc406
commit 1aa3650ee3
4 changed files with 110 additions and 30 deletions

View File

@ -2,10 +2,17 @@ module gltk.array;
import gl; import gl;
/**
* OpenGL array object.
*/
class Array class Array
{ {
/** Array ID. */
private GLuint m_id; private GLuint m_id;
/**
* Construct array.
*/
this() this()
{ {
m_id = 0u; m_id = 0u;
@ -16,17 +23,26 @@ class Array
} }
} }
/**
* Destruct array.
*/
~this() ~this()
{ {
glDeleteVertexArrays(1, &m_id); glDeleteVertexArrays(1, &m_id);
} }
void bind() /**
* Bind array.
*/
void bind() const
{ {
glBindVertexArray(m_id); glBindVertexArray(m_id);
} }
@property GLuint id() /**
* Get array ID.
*/
@property GLuint id() const
{ {
return m_id; return m_id;
} }

View File

@ -2,12 +2,23 @@ module gltk.buffer;
import gl; import gl;
/**
* OpenGL buffer object.
*/
class Buffer class Buffer
{ {
/** Buffer ID. */
private GLuint m_id; private GLuint m_id;
/** Buffer target. */
private GLenum m_target; private GLenum m_target;
/**
* Construct buffer.
*
* @param target
* OpenGL buffer target (e.g. GL_ARRAY_BUFFER, GL_ELEMENT_BUFFER)
*/
this(GLenum target) this(GLenum target)
{ {
m_id = 0u; m_id = 0u;
@ -19,30 +30,55 @@ class Buffer
m_target = target; m_target = target;
} }
/**
* Destruct buffer.
*/
~this() ~this()
{ {
glDeleteBuffers(1, &m_id); glDeleteBuffers(1, &m_id);
} }
void bind() /**
* Bind buffer.
*/
void bind() const
{ {
glBindBuffer(m_target, m_id); glBindBuffer(m_target, m_id);
} }
@property GLuint id() /**
* Get buffer ID.
*/
@property GLuint id() const
{ {
return m_id; 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) void set_buffer_data(GLenum usage, const void * ptr, size_t size)
{ {
bind(); glNamedBufferData(m_id, size, ptr, usage);
glBufferData(m_target, 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) void set_buffer_data(T)(GLenum usage, T[] arr)
{ {
bind(); glNamedBufferData(m_id, arr.length * arr[0].sizeof, arr.ptr, usage);
glBufferData(m_target, arr.length * arr[0].sizeof, arr.ptr, usage);
} }
} }

View File

@ -1,35 +1,36 @@
module gltk.shader; module gltk.shader;
import gl; import gl;
static import std.file;
/**
* OpenGL shader object.
*/
class Shader class Shader
{ {
/** Shader ID. */
private GLuint m_id; private GLuint m_id;
private GLenum m_shader_type; /**
* Construct shader.
this(GLenum shader_type) *
* @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); m_id = glCreateShader(shader_type);
if (m_id == 0u) 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 status;
GLint gllength = length; const(GLchar) * source_string = source.ptr;
glShaderSource(m_id, 1, &source, &gllength); GLint source_length = cast(GLint)source.length;
glShaderSource(m_id, 1, &source_string, &source_length);
glCompileShader(m_id); glCompileShader(m_id);
@ -40,7 +41,7 @@ class Shader
} }
string message = "Error compiling "; string message = "Error compiling ";
switch (m_shader_type) switch (shader_type)
{ {
case GL_VERTEX_SHADER: case GL_VERTEX_SHADER:
message ~= "vertex"; message ~= "vertex";
@ -68,13 +69,18 @@ class Shader
throw new Exception(message); 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); glDeleteShader(m_id);
set_source(file_data.ptr, cast(int)file_data.length);
} }
@property GLuint id() /**
* Get shader ID.
*/
@property GLuint id() const
{ {
return m_id; return m_id;
} }

View File

@ -2,10 +2,17 @@ module gltk.texture;
import gl; import gl;
/**
* OpenGL texture object.
*/
class Texture class Texture
{ {
/** Texture ID. */
private GLuint m_id; private GLuint m_id;
/**
* Construct texture.
*/
this() this()
{ {
m_id = 0u; m_id = 0u;
@ -16,21 +23,36 @@ class Texture
} }
} }
/**
* Destruct texture.
*/
~this() ~this()
{ {
glDeleteTextures(1, &m_id); 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 void bind(GLenum target) const
{ {
glBindTexture(target, m_id); glBindTexture(target, m_id);
} }
@property GLuint id() /**
* Get texture ID.
*/
@property GLuint id() const
{ {
return m_id; 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) static uint next_power_of_2(uint n)
{ {
n--; n--;