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;
/**
* 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;
}

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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--;