Preface
It is assumed that the reader is familiar with both OpenGL and the Lua programming language.
For convenience of reference, this document contains external (deep) links to the Lua Reference Manual and the OpenGL Documentation.
Getting and installing
For installation intructions, refer to the README file in the MoonGL official repository on GitHub.
Module organization
The MoonGL module is loaded using Lua’s require() and returns a table containing the functions it provides (as usual with Lua modules). This manual assumes that such table is named gl, i.e. that it is loaded with:
gl = require("moongl")
but nothing forbids the use of a different name.
Examples
This manual contains a code snippets section where short incomplete examples show how to use MoonGL functions.
Complete examples can be found in the examples/ directory of the release package.
License
MoonGL is released under the MIT/X11 license (same as Lua, and with the same only requirement to give proper credits to the original author). The copyright notice is in the LICENSE file in the base directory of the official repository on GitHub.
See also
MoonGL is part of MoonLibs, a collection of Lua libraries for graphics and audio programming.
Introduction
MoonGL is an almost-one-to-one binding library to OpenGL, and it is intended to be used in the same way one would use OpenGL, except, of course, for coding in Lua instead of C or C++. In particular, this means that tasks like creating a GL context and handling input are not addressed by MoonGL, and one needs to resort to external modules (some of them are needed to run the examples).
This document purposedly reflects the structure of the OpenGL 4.5 Reference card, so that one can easily find the correspondence between MoonGL and OpenGL API by comparing the two documents. Links to corresponding sections of the OpenGL Wiki are also provided for this purpose.
As a general rule, each OpenGL function is bound to by a MoonGL function whose name is a snake_case version of the original. For example, glClear( ) is bound to by gl.clear( ), glClearBuffer( ) by gl.clear_buffer( ), and so on.
The mapping between OpenGL and MoonGL functions is not exactly one-to-one, however. This is mainly because MoonGL leverages Lua’s function overloading capabilities, whereas OpenGL, being a C API, has a lot of functions with slightly different names that are logically the same overloaded function.
MoonGL also adds a few functions (tagged with NONGL in this manual) that are not present in the OpenGL API. Among these:
-
new_xxx( ) functions that combine generation and binding of a single object;
-
unbind_xxx( ) functions to unbind objects (these are just wrappers of the corresponding bind_xxx( ) which have the same effect if they receive a nil or 0 as object name);
-
data handling functions, to convert data from Lua arrays to binary strings and viceversa;
-
utilities for compiling shader programs.
Parameter values that in OpenGL are #defined as integer codes become strings in MoonGL (as is customary in Lua). There is usually an easily recognizable pattern to derive the correct MoonGL string from the OpenGL #define name. For example, the buffer target codes GL_ARRAY_BUFFER and GL_ELEMENT_ARRAY_BUFFER become 'array' and 'element array', respectively (the string is typically obtained by removing the GL_ prefix and possibly some redundant part from the #define name, replacing underscores with spaces, and going from upper-case to lower-case letters).
Initialization
-
init( )
Binding to glewInit(). This function must be called as soon as a GL context is obtained and made current, and before calling any other MoonGL function (since it initializes OpenGL’s function pointers, failing to do so would likely cause a segmentation fault).
See example. -
boolean = is_supported(string)
Binding to glewIsSupported() (accepts the same strings). See example. -
string = version( )
Returns a string describing the latest OpenGL version supported by the combination of MoonGL, GLEW, and the OpenGL implementation being used (e.g. 'OpenGL 4.5'). This function can be used only after initialization.
(Note that the get() function returns the version supported by the OpenGL implementation being used, which may differ from the version returned by this function). -
The gl table contains the following fields for version information:
- gl._VERSION: a string describing the MoonGL version (e.g. 'MoonGL 0.4'), and
- gl._GLEW_VERSION: a string describing the GLEW version (e.g. 'GLEW 1.13.0').
Command Execution
-
status = get_graphics_reset_status( )
status: 'no error', 'guilty context reset', 'innocent context reset', 'unknown context reset'.
Rfr: glGetGraphicsResetStatus.
Note
|
The glGetError( ) function is not exposed. It is used internally by MoonGL, that checks for errors each time it executes an OpenGL command and raises an error if the command did not succeed. |
Synchronization
-
sync = fence_sync(condition)
delete_sync(sync)
condition: 'gpu commands complete'.
Rfr: glFenceSync - glDeleteSync.
-
wait_sync(sync)
Rfr: glWaitSync.
-
status = client_wait_sync(sync, timeout, [flag, … ])
status: 'already signaled', 'timeout expired', 'condition satisfied', 'wait failed'.
glClientWaitSync.
-
value, … = get_sync(sync, pname)
pname: 'type', 'condition', 'status', 'flags'.
Rfr: glGetSync.
-
lightuserdata = raw_sync(sync) NONGL
Returns the raw sync object as a lightuserdata (GLsync*).
Asynchronous Queries and Timers
-
id1, id2, … = gen_queries([n = 1])
id1, id2, … = create_queries(target, [n = 1])
delete_queries(id1, [id2, …])
Rfr: glGenQueries - glDeleteQueries.
-
begin_query(target, id, [index])
end_query(target, [index])
Rfr: glBeginQuery.
-
value = get_query(target, pname, [index])
pname: 'current query', 'query counter bits'.
Rfr: glGetQuery.
-
query_counter(id)
Rfr: glQueryCounter.
-
time = get_timestamp( )
Rfr: glGetTimestamp.
Buffer Objects
-
buffer = new_buffer(target) NONGL
buffer1, buffer2, … = gen_buffers([n = 1])
buffer1, buffer2, … = create_buffers([n = 1])
bind_buffer(target, [buffer])
unbind_buffer(target) NONGL
delete_buffers(buffer1, [buffer2, …])
Rfr: glGenBuffers - glBindBuffers - glDeleteBuffers.
See example.
Create/Modify Buffer Object Data:
-
size = buffer_storage(target|buffer, [data|dataptr], [flag, flag, …])
flag: 'dynamic storage', 'map read', 'map write', 'map persistent', 'map coherent', 'client storage'.
Rfr: glBufferStorage.
-
size = buffer_data(target|buffer, data|dataptr|size, usage)
size = buffer_sub_data(target|buffer, offset, data|dataptr)
size: size of memory to allocate for the buffer (in bytes).
Rfr: glBufferData - glBufferSubData.
See example and 'Data handling utilities'.
-
clear_buffer_data(target|buffer, intformat, format, type, [data|dataptr])
clear_buffer_sub_data(target|buffer, intformat), offset, size, format, type, [data|dataptr])
Rfr: glClearBufferData - glClearBufferSubData.
See also 'Data handling utilities'.
-
bind_buffer_range(target, index, buffer, offset, size)
bind_buffer_base(target, index, buffer)
Rfr: glBindBufferRange - glBindBufferBase.
Map/Unmap Buffer Data:
-
ptr = map_buffer(target|buffer, access)
ptr = map_buffer_range(target|buffer, offset, length, [accessbit1, …])
unmap_buffer(target|buffer)
access: 'read only', 'write only', 'read write',
accessbit: 'read', 'write', 'persistent', 'coherent', 'invalidate buffer', 'invalidate range', 'flush explicit', 'unsynchronized'.
The returned ptr value is a lightuserdata containing the raw pointer to the mapped memory (use at your peril).
Rfr: glMapBuffer - glMapBufferRange.
-
flush_mapped_buffer_range(target|buffer, offset, length)
Rfr: glFlushMappedBufferRange.
-
mapped_buffer_copy_from(target|buffer, offset, length, srcptr, [srcoffset=0]) NONGL
mapped_buffer_copy_to(target|buffer, offset, length, dstptr, [dstoffset=0]) NONGL
Copy data from/to raw memory to/from a mapped buffer (non-OpenGL functions).
srcptr, dstptr: lightuserdata containing a pointer to raw memory.
(Note that MoonGL has no means to perform boundary checks on the passed raw memory, so use with caution.)
Invalidate Buffer Data:
-
invalidate_buffer_data(buffer)
invalidate_buffer_sub_data(buffer, offset, length)
Rfr: glInvalidateBufferData - glInvalidateBufferSubData.
Buffer Object Queries:
-
data = get_buffer_sub_data(target|buffer, offset, size)
Rfr: glGetBufferSubData.
-
value, … = get_buffer_parameter(target|buffer, pname)
pname: 'size', 'usage', 'access', 'access flags', 'immutable storage', 'mapped', 'map pointer', 'map offset', 'map length', 'storage flags'.
Rfr: glGetBufferParameter.
Copy Between Buffers:
-
copy_buffer_sub_data(readtarget|readbuffer, writetarget|writebuffer, readoffset, writeoffset, size)
Source and destination must be specified both as targets or both as buffer names.
Rfr: glCopyBufferSubData.
Shaders and Programs
The following functions are MoonGL additions to ease the creation of shading programs:
-
program, shader1, shader2, … = make_program(shadertype1, filename1, [shadertype2, filename2, …]) NONGL
program, shader1, shader2, … = make_program({shadertype = filename}) NONGL
Creates, compiles and links a program with the given shaders, checking for errors at each compilation step.
Returns the OpenGL names assigned to the program and to the shaders.
-
program, shader1, shader2, … = make_program_s(shadertype1, sourcestring1, [shadertype2, sourcestring2, …]) NONGL
program, shader1, shader2, … = make_program_s({shadertype, sourcestring}) NONGL
Same as gl.make_program( ), but loads the shaders' code from strings instead of from files. -
clean_program(program, shader1, shader2, …) NONGL
Given the names of a program and of the shaders attached to it, detaches the shaders, and deletes them and the program.
Shader Objects
-
shader = create_shader(shadertype)
delete_shaders(shader1, …)
delete_shader(shader) NONGL
Rfr: glCreateShader - glDeleteShader.
-
shader_source(shader, sourcestring)
Rfr: glShaderSource.
-
compile_shader(shader, [checkstatus=false])
If checkstatus=true, also checks the compile status and possibly raises an error with the info log.
Rfr: glCompileShader.
-
release_shader_compiler( )
Rfr: glReleaseShaderCompiler.
-
shader_binary({shader1, …}, binaryformat, binary)
Rfr: glShaderBinary.
-
specialize_shader(shader, entrypoint, [{index}, {value}])
Rfr: glSpecializeShader.
Program Objects
-
program = create_program( )
delete_programs(program1, …)
delete_program(program) NONGL
Rfr: glCreateProgram - glDeleteProgram.
-
attach_shader(program, shader)
detach_shader(program, shader)
Rfr: glAttachShader - glDetachShader.
-
link_program(program, [checkstatus=false])
If checkstatus=true, also checks the link status and possibly raises an error with the info log.
Rfr: glLinkProgram.
-
use_program(program)
Rfr: glUseProgram.
-
program_parameter(program, pname, boolean)
pname: 'separable', 'binary retriavable hint'.
Rfr: glProgramParameter.
-
program = create_shader_program(shadertype, sourcestring)
Rfr: glCreateShaderProgram.
-
format, binary = get_program_binary(program)
program_binary(program, format, binary)
format: integer,
binary: binary string
Rfr: glGetProgramBinary - glProgramBinary.
Program Pipeline Objects
-
pipeline = new_program_pipeline( ) NONGL
pipeline1, pipeline2, … = gen_program_pipelines([n = 1])
pipeline1, pipeline2, … = create_program_pipelines([n = 1])
bind_program_pipeline(pipeline)
unbind_program_pipeline( ) NONGL
delete_program_pipelines(pipeline1, [pipeline2, …])
Rfr: glGenProgramPipelines - glBindProgramPipeline - glDeleteProgramPipelines.
-
use_program_stages(pipeline, program, [stage1, …])
stage: 'vertex', 'fragment', 'geometry', 'tess control', 'tess evaluation', 'compute', 'all'.
Rfr: glUseProgramStages.
-
active_shader_program(pipeline, program)
Rfr: glActiveShaderProgram.
Uniform Variables Queries:
-
location = get_uniform_location(program, name)
name = get_active_uniform_name(program, index)
index1, … = get_uniform_indices(program, name1, [name2, …])
Rfr: glGetUniformLocation - glGetActiveUniformName - glGetUniformIndices.
-
name, type, size = get_active_uniform(program, index)
Rfr: glGetActiveUniform.
-
value1, … = get_active_uniforms(program, pname, index1, [index2, …])
pname: 'type', 'size', 'name length', 'block index', 'offset', 'array stride', 'matrix stride', 'is row major', 'atomic counter buffer index'.
Rfr: glGetActiveUniforms.
-
index = get_uniform_block_index(program, name)
name = get_active_uniform_block_name(program, index)
Rfr: glGetUniformBlockIndex - glGetActiveUniformBlockName.
-
value = get_active_uniform_block(program, index, pname)
pname: 'binding', 'data size', 'name length', 'active uniforms', 'active uniform indices', 'referenced by vertex shader', 'referenced by tess control shader', 'referenced by tess evaluation shader', 'referenced by geometry shader', 'referenced by fragment shader', 'referenced by compute shader'.
Rfr: glGetActiveUniformBlock.
-
value = get_active_atomic_counter_buffer(program, index, pname)
pname: 'binding', 'data size', 'active atomic counters', 'active atomic counter indices', 'referenced by vertex shader', 'referenced by tess control shader', 'referenced by tess evaluation shader', 'referenced by geometry shader', 'referenced by fragment shader', 'referenced by compute shader'.
Rfr: glGetActiveAtomicCounterBuffer.
Load Uniform Variables:
-
uniform(location, type, val1, [val2, val3, val4])
uniformv(location, count, type, val1, [val2, … ])
program_uniform(program, location, type, val1, [val2, val3, val4])
program_uniformv(program, location, count, type, val1, [val2, … ])
type: 'bool', 'int', 'uint', 'float' or 'double'.
count: number of vectors (or variables) in the array.
Rfr: glUniform - glProgramUniform. See example.
-
uniform_matrix(location, type, size, transpose, val1, val2, …, valN)
uniform_matrixv(location, count, type, size, transpose, val1, val2, …, valN)
program_uniform_matrix(program, location, type, size, transpose, val1, val2, …, valN)
program_uniform_matrixv(program, location, count, type, size, transpose, val1, val2, …, valN)
type: 'bool', 'int', 'uint', 'float' or 'double'.
size: '2x2', '3x3', '4x4', '2x3', '3x2', '2x4', '4x2'', '3x4', '4x3'.
transpose: boolean (= true if the value are passed in row-major order).
count: number of matrices in the array (N = count x no. of elements in a matrix).
Rfr: glUniformMatrix - glProgramUniformMatrix. See example.
Uniform Buffer Objects Bindings:
-
uniform_block_binding(program, index, binding)
Rfr: glUniformBlockBinding.
Shader Buffer Variables:
-
shader_storage_block_binding(program, index, binding)
Rfr: glShaderStorageBlockBinding.
Subroutine Uniform Variables:
-
location = get_subroutine_uniform_location(program, shadertype, name)
index = get_subroutine_index(program, shadertype, name)
name = get_active_subroutine_name(program, shadertype, index)
name = get_active_subroutine_uniform_name(program, shadertype, index)
Rfr: glGetSubroutineUniformLocation - glGetSubroutineIndex - glGetActiveSubroutineName - glGetActiveSubroutineUniformName.
-
value, … = get_active_subroutine_uniform(program, shadertype, index, pname)
pname: 'num compatible subroutines', 'compatible subroutines', 'uniform size', 'uniform name length'.
Rfr: glGetActiveSubroutineUniform.
-
uniform_subroutines(shadertype, index1, [index2, …])
Rfr: glUniformSubroutines.
Shared Memory Access
-
memory_barrier(bit1, bit2, …)
memory_barrier_by_region(bit1, bit2, …)
bitk: 'all', 'vertex attrib array', 'element array', 'uniform', 'texture fetch', 'shader image access', 'command', 'pixel buffer', 'texture update', 'buffer update', 'client mapped buffer', 'query buffer', 'framebuffer', 'transform feedback', 'atomic counter', 'shader storage'.
Rfr: glMemoryBarrier.
Program Interfaces:
-
value = get_program_interface(program, interface, pname)
index = get_program_resource_index(program, interface, name)
name = get_program_resource_name(program, interface, index)
location = get_program_resource_location(program, interface, name)
index = get_program_resource_location_index(program, interface, name)
value, … = get_program_resource(program, interface, index, property)
interface: 'uniform' for GL_UNIFORM, 'uniform block' for GL_UNIFORM_BLOCK, etc.
pname: 'active resources', 'max name length', 'max num active variables', 'max num compatible subroutines'.
property: 'array size' for GL_ARRAY_SIZE, 'array stride' for GL_ARRAY_STRIDE, etc.
Rfr: Shader Program Query.
Shader and Program Queries
-
value = get_shader(shader, pname)
pname: 'type', 'delete status', 'compile status', 'info log length', 'source length'.
Rfr: glGetShader.
-
value, … = get_program(program, pname)
pname: 'active atomic counter buffers' for GL_ACTIVE_ATOMIC_COUNTER_BUFFERS, etc.
Rfr: glGetProgram.
-
value, … = get_program_pipeline(pipeline, pname)
pname: 'active program' for GL_ACTIVE_PROGRAM, 'validate status' for GL_VALIDATE_STATUS, etc.
Rfr: glGetProgramPipeline.
-
shader1, … = get_attached_shaders(program)
Returns nil if no shaders are attached to program.
Rfr: glGetAttachedShaders.
-
string = get_shader_info_log(shader)
string = get_program_info_log(program)
string = get_program_pipeline_info_log(pipeline)
Rfr: glGetShaderInfoLog - glGetProgramInfoLog - glGetProgramPipelineInfoLog.
-
string = get_shader_source(shader)
Rfr: glGetShaderSource.
-
range0, range1, precision = get_shader_precision_format(shadertype, precisiontype)
precisiontype: 'low int' for GL_LOW_INT, 'medium int' for GL_MEDIUM_INT, etc.
Rfr: glGetShaderPrecisionFormat.
-
val1, … = get_uniform(program, location, type, size)
type: 'bool', 'int', 'uint', 'float' or 'double'.
size: number of elements in the uniform variable, vector or matrix (1 … 16).
Rfr: glGetUniform.
-
value = get_uniform_subroutine(shadertype, location)
Rfr: glGetUniformSubroutine.
-
value, … = get_program_stage(program, shadertype, pname)
pname: 'active subroutines' for GL_ACTIVE_SUBROUTINES, etc.
Rfr: glGetProgramStage.
Textures and Samplers
Texture units:
-
active_texture(i)
i: 0 for GL_TEXTURE0, 1 for GL_TEXTURE1, etc.
Rfr: glActiveTexture. See example.
-
bind_texture_unit(unit, texture)
glBindTextureUnit.
Texture Objects:
-
texture = new_texture(target) NONGL
texture1, texture2, … = gen_textures([n = 1])
texture1, texture2, … = create_textures(target, [n = 1])
bind_texture(target, [texture])
bind_textures(first, texture1, [texture2, … ])
unbind_texture(target) NONGL
delete_textures(texture1, [texture2, …])
Rfr: glGenTextures - glBindTexture - glBindTextures - glDeleteTextures. See example.
Sampler Objects
-
sampler = new_sampler(unit) NONGL
sampler1, sampler2, … = gen_samplers([n = 1])
sampler1, sampler2, … = create_samplers([n = 1])
bind_sampler(unit, [sampler])
bind_samplers(first, sampler1, [sampler2, …])
unbind_sampler(unit) NONGL
delete_samplers(sampler1, [sampler2, …])
Rfr: glGenSamplers - glBindSampler - glBindSamplers - glDeleteSamplers.
-
sampler_parameter(sampler, pname, val1, [val2, …])
value, … = get_sampler_parameter(sampler, pname)
pname: 'wrap s', 'wrap t', 'wrap r', 'max anisotropy', 'min filter', 'mag filter', 'border color', 'min lod', 'max lod', 'lod bias', 'compare mode', 'compare func'.
Rfr: glSamplerParameter - glGetSamplerParameter.
Texture Image Spec.:
-
texture_image(target, level, intformat, format, type, [data|dataptr|offset], width) 1D
texture_image(target, level, intformat, format, type, [data|dataptr|offset], width, height) 2D
texture_image(target, level, intformat, format, type, [data|dataptr|offset], width, height, depth) 3D
If a buffer is bound to the 'pixel unpack' target, then an integer offset parameter must be passed (in this case OpenGL gets the data from that buffer at the given offset). Otherwise the data may be passed either as a binary string or a pointer, or as nil for no pixel transfer.
Rfr: glTexImage1D - glTexImage2D - glTexImage3D. See example.
Alternate Texture Image Spec.:
-
copy_texture_image(target, level, intformat, x, y, width) 1D
copy_texture_image(target, level, intformat, x, y, width, height) 2D
Rfr: glCopyTexImage1D - glCopyTexImage2D.
-
texture_sub_image(target|texture, level, format, type, [data|dataptr|offset], xofs, width) 1D
texture_sub_image(target|texture, level, format, type, [data|dataptr|offset], xofs, yofs, width, height) 2D
texture_sub_image(target|texture, level, format, type, [data|dataptr|offset], xofs, yofs, zofs, width, height, depth) 3D
data|dataptr|offset: see gl.texture_image( ).
Rfr: glTexSubImage1D - glTexSubImage2D - glTexSubImage3D.
-
copy_texture_sub_image(target|texture, level, xofs, x, y, width) 1D
copy_texture_sub_image(target|texture, level, xofs, yofs, x, y, width, height) 2D
copy_texture_sub_image(target|texture, level, xofs, yofs, zofs, x, y, width, height) 3D
Rfr: glCopyTexSubImage1D - glCopyTexSubImage2D - glCopyTexSubImage3D.
Compressed Texture Images:
-
compressed_texture_image(target, level, intformat, data|dataptr, width) 1D
compressed_texture_image(target, level, intformat, data|dataptr, width, height) 2D
compressed_texture_image(target, level, intformat, data|dataptr, width, height, depth) 3D
Rfr: glCompressedTexImage1D - glCompressedTexImage2D - glCompressedTexImage3D.
-
compressed_texture_sub_image(target|texture, level, intformat, data|dataptr, xofs, width) 1D
compressed_texture_sub_image(target|texture, level, intformat, data|dataptr, xofs, yofs, width, height) 2D
compressed_texture_sub_image(target|texture, level, intformat, data|dataptr, xofs, yofs, zofs, width, height, depth) 3D
Rfr: glCompressedTexSubImage1D - glCompressedTexSubImage2D - glCompressedTexSubImage3D.
Multisample Textures:
-
texture_image_multisample(target, samples, intformat, fixedsamplelocs, width, height, depth) 2D
texture_image_multisample(target, samples, intformat, fixedsamplelocs, width, height, depth, height) 3D
Rfr: glTexImage2DMultisample - glTexImage3DMultisample.
Buffer Textures:
-
texture_buffer_range(target|texture, intformat, buffer, offset, size)
Rfr: glTexBufferRange.
-
texture_buffer(target|texture, intformat, buffer)
Rfr: glTexBuffer.
Invalidate/Clear Texture Image Data:
-
invalidate_texture_image(texture, level)
invalidate_texture_sub_image(texture, level, xofs, yofs, zofs, width, height, depth)
Rfr: glInvalidateTexImage - glInvalidateTexSubImage.
-
clear_texture_image(texture, level, format, type, [data|dataptr])
clear_texture_sub_image(texture, level, format, type, [data|dataptr], xofs, yofs, zofs, width, height, depth)
Rfr: glClearTexImage - glClearTexSubImage.
Texture Parameters
-
texture_parameter(target|texture, pname, val1, …)
pname: 'base level', 'wrap s', 'wrap t', 'wrap r', 'border color', 'depth stencil mode', 'compare func', 'compare mode', 'lod bias', 'min filter', 'mag filter', 'min lod', 'max lod', 'max level', 'swizzle r', 'swizzle g', 'swizzle b', 'swizzle a', 'swizzle rgba', 'max anisotropy'.
Rfr: glTexParameter. See example.
Texture Queries
-
value, … = get_texture_parameter(target|texture, pname)
pname: 'depth stencil texture mode', 'base level' for GL_TEXTURE_BASE_LEVEL, 'compare func' for GL_TEXTURE_COMPARE_FUNC, etc.
Rfr: glGetTexParameter.
-
value = get_texture_level_parameter(target|texture, level, pname)
pname: 'width' for GL_TEXTURE_WIDTH, 'fixed sample locations' for GL_TEXTURE_FIXED_SAMPLE_LOCATIONS, etc.
'width', 'height', 'depth', 'fixed sample locations', 'internal format', 'shared size', 'compressed', 'compressed image size', 'samples', 'buffer offset', 'buffer size', 'red size', 'green size', 'blue size', 'alpha size', 'depth size', 'red type', 'green type', 'blue type', 'alpha type', 'depth type'.
Rfr: glGetTexLevelParameter.
-
data = get_texture_image(target|texture, level, format, type, n)
n: data size or offset in buffer.
If a buffer is bound to the 'pixel pack' target, then n is interpreted as an offset and this function returns nil (in this case OpenGL copies the data to that buffer at the given offset). Otherwise n must be the size in bytes of the expected data, which this function returns as a binary string.
Rfr: glGetTexImage.
-
data = get_texture_sub_image(texture, level, format, type, xofs, yofs, zofs, width, height, depth, n)
n: data size.
Rfr: glGetTextureSubImage.
-
data = get_compressed_texture_image(target|texture, level, n)
data = get_compressed_texture_sub_image(texture, level, xofs, yofs, zofs, width, height, depth, n)
n: data size.
Rfr: glGetCompressedTexImage - glGetCompressedTextureSubImage.
Pixel Storage Modes:
-
pixel_store(pname, val)
pname: 'unpack swap bytes' for GL_UNPACK_SWAP_BYTES, 'unpack lsb first' for GL_UNPACK_LSB_FIRST, etc.
Rfr: glPixelStore.
Cube Map Texture Select:
Manual Mipmap Generation:
-
generate_mipmap(target|texture)
Rfr: glGenerateMipmap. See example.
Texture Views:
-
texture_view(texture, target, origtexture, intformat, minlevel, numlevels, minlayer, numlayers)
Rfr: glTextureView.
Immutable-Format Tex. Images:
-
texture_storage(target|texture, levels, intformat, width) 1D
texture_storage(target|texture, levels, intformat, width, height) 2D
texture_storage(target|texture, levels, intformat, width, height, depth) 3D
Rfr: glTexStorage1D - glTexStorage2D - glTexStorage3D.
-
texture_storage_multisample(target|texture, samples, intformat, fixedsamplelocs, width, height) 2D
texture_storage_multisample(target|texture, samples, intformat, fixedsamplelocs, width, height, depth) 3D
Rfr: glTexStorage2DMultisample - glTexStorage3DMultisample.
Texture Image Loads/Stores:
-
bind_image_texture(unit, texture, level, layered, layer, access, intformat)
access: 'read only', 'write only', 'read write'.
Rfr: glBindImageTexture.
-
bind_image_textures(first, texture1, [texture2, …])
Rfr: glBindImageTextures.
Framebuffer Objects
Rfr: Framebuffer Objects.
-
framebuffer = new_framebuffer(target) NONGL
framebuffer1, framebuffer2, … = gen_framebuffers([n = 1])
framebuffer1, framebuffer2, … = create_framebuffers([n = 1])
bind_framebuffer(target, [framebuffer])
unbind_framebuffer(target) NONGL
delete_framebuffers(framebuffer1, [framebuffer2, …])
Rfr: glGenFramebuffers - glBindFramebuffer - glDeleteFramebuffers.
-
framebuffer_parameter(target|framebuffer, pname, value)
pname: 'default width', 'default height', 'default layers', 'default samples', 'default fixed sample locations'.
Rfr: glFramebufferParameter.
-
value, … = get_framebuffer_parameter(target|framebuffer, pname)
pname: 'default width', 'default height', 'default layers', 'default samples', 'default fixed sample locations', 'doublebuffer', 'implementation color read format', 'implementation color read type', 'samples', 'sample buffers', 'stereo'.
Rfr: glGetFramebufferParameter.
-
value, … = get_framebuffer_attachment_parameter(target|framebuffer, attachment, pname)
pname: 'object type', 'object name', 'component type', 'red size', 'green size', 'blue size', 'alpha size', 'depth size', 'stencil size', 'color encoding', 'texture layer', 'texture level', 'layered', 'texture cube map face'.
Rfr: glGetFramebufferAttachmentParameter.
Renderbuffer Objects:
-
renderbuffer = new_renderbuffer(target) NONGL
renderbuffer1, renderbuffer2, … = gen_renderbuffers([n = 1])
renderbuffer1, renderbuffer2, … = create_renderbuffers([n = 1])
bind_renderbuffer(target, [renderbuffer])
unbind_renderbuffer(target) NONGL
delete_renderbuffers(renderbuffer1, [renderbuffer2, …])
Rfr: glGenRenderbuffers - glBindRenderbuffer - glDeleteRenderbuffers.
-
renderbuffer_storage(target|renderbuffer, internalformat, width, height)
renderbuffer_storage_multisample(target|renderbuffer, samples, internalformat, width, height)
Rfr: glRenderbufferStorage - glRenderbufferStorageMultisample.
-
value, … = get_renderbuffer_parameter(target|renderbuffer, pname)
pname: 'width', 'height', 'internal format', 'samples', 'red size', 'green size', 'blue size', 'alpha size', 'depth size', 'stencil size'.
Rfr: glGetRenderbufferParameter.
Attaching Renderbuffer Images:
-
framebuffer_renderbuffer(target|framebuffer, attachment, renderbuffertarget, renderbuffer)
Rfr: glFramebufferRenderbuffer.
Attaching Texture Images:
-
framebuffer_texture(target|framebuffer, attachment, texture, level)
framebuffer_texture_1d(target, attachment, textarget, texture, level)
framebuffer_texture_2d(target, attachment, textarget, texture, level)
framebuffer_texture_3d(target, attachment, textarget, texture, level, layer)
Rfr: glFramebufferTexture.
-
framebuffer_texture_layer(target|framebuffer, attachment, texture, level, layer)
Rfr: glFramebufferTextureLayer.
Feedback Loops:
-
texture_barrier( )
Rfr: glTextureBarrier.
Framebuffer Completeness:
-
status = check_framebuffer_status(target, [framebuffer])
status: 'complete', 'undefined', 'incomplete attachment', 'incomplete missing attachment', 'incomplete draw buffer', 'incomplete read buffer', 'unsupported', 'incomplete multisample', 'incomplete layer targets'.
Rfr: glCheckFramebufferStatus.
Vertices
Separate Patches :
-
patch_parameter(pname, val, [val, …])
pname: 'vertices', 'default outer level', 'default inner level'.
Rfr: glPatchParameter.
Current Vertex Attribute Values:
-
vertex_attrib(index, type, val1, [val2, val3, val4])
type: 'byte', 'ubyte', 'short', 'ushort', 'int', 'uint', 'float', 'double'.
Rfr: glVertexAttrib.
-
vertex_attrib_n(index, type, val1, val2, val3, val4)
type: 'byte', 'ubyte', 'short', 'ushort', 'int', 'uint'.
Rfr: glVertexAttrib.
-
vertex_attrib_p(index, type, normalized, val1, val2, val3, val4)
type: 'int 2 10 10 10 rev', 'uint 2 10 10 10 rev', 'uint 10f 11f 11f rev'.
Rfr: glVertexAttrib.
Vertex Arrays
Vertex Array Objects:
-
array = new_vertex_array( ) NONGL
array1, array2, … = gen_vertex_arrays([n = 1])
array1, array2, … = create_vertex_arrays([n = 1])
bind_vertex_array([array])
unbind_vertex_array( ) NONGL
delete_vertex_arrays(array1, [array2, …])
Rfr: glGenVertexArrays - glBindVertexArray - glDeleteVertexArrays - See example.
-
vertex_array_element_buffer(array, buffer)
Generic Vertex Attribute Arrays:
-
vertex_attrib_format(index, size, type, normalized, offset)
vertex_attrib_format(array, index, size, type, normalized, offset)
type: 'byte', 'ubyte', 'short', 'ushort', 'int', 'uint', 'float', 'double', 'half float', 'fixed', 'int 2 10 10 10 rev', 'uint 2 10 10 10 rev', 'uint 10f 11f 11f rev'.
Rfr: glVertexAttribFormat.
-
bind_vertex_buffer(index, buffer, offset, stride)
bind_vertex_buffer(array, index, buffer, offset, stride)
bind_vertex_buffers(index, buffer1, offset1, stride1, [buffer2, offset2, stride2, …])
bind_vertex_buffers(array, index, buffer1, offset1, stride1, [buffer2, offset2, stride2, …])
Rfr: glBindVertexBuffer - glBindVertexBuffers.
-
vertex_attrib_binding(attribindex, bindingindex)
vertex_attrib_binding(array, attribindex, bindingindex)
Rfr: glVertexAttribBinding.
-
vertex_attrib_pointer(index, size, type, normalized, stride, pointer)
vertex_attrib_i_pointer(index, size, type, stride, pointer)
size: 1, 2, 3, 4, or 0 for GL_BGRA.
type: 'byte', 'ubyte', 'short', 'ushort', 'int', 'uint', 'float', 'double', 'half float', 'fixed', 'int 2 10 10 10 rev', 'uint 2 10 10 10 rev', 'uint 10f 11f 11f rev'.
Rfr: glVertexAttribPointer. See example.
-
enable_vertex_attrib_array(index)
enable_vertex_attrib_array(array, index)
disable_vertex_attrib_array(index)
disable_vertex_attrib_array(array, index)
Rfr: glEnableVertexAttribArray - glDisableVertexAttribArray.
Vertex Attribute Divisors
-
vertex_binding_divisor(index, divisor)
vertex_binding_divisor(array, index, divisor)
vertex_attrib_divisor(index, divisor)
Rfr: glVertexBindingDivisor - glVertexAttribDivisor.
Primitive Restart:
-
primitive_restart_index(index)
Rfr: glPrimitiveRestartIndex.
Drawing Commands:
-
draw_arrays(mode, first, count, [instancecount], [baseinstance])
draw_arrays_indirect(mode, indirect)
multi_draw_arrays(mode, first1, count1, [first2, count2 …])
multi_draw_arrays_indirect(mode, indirect, drawcount, stride)
multi_draw_arrays_indirect_count(mode, indirect, drawcount, maxdrawcount, stride)
draw_elements(mode, count, type, indices, [instancecount], [baseinstance])
draw_elements_indirect(mode, type, indirect)
draw_elements_base_vertex(mode, count, type, indices, basevertex , [instancecount], [baseinstance])
multi_draw_elements(mode, type, count1, indices1, [count2, indices2, …])
multi_draw_elements_indirect(mode, type, indirect, drawcount, stride)
multi_draw_elements_indirect_count(mode, type, indirect, drawcount, maxdrawcount, stride)
multi_draw_elements_base_vertex(mode, type, count1, indices1, basevertex1, [count2, indices2, basevertex2, …])
draw_range_elements(mode, start, end, count, type, indices)
draw_range_elements_base_vertex(mode, start, end, count, type, indices, basevertex)
mode: 'points', 'patches', 'line strip', 'line loop', 'triangle strip', 'triangle fan', 'lines', 'lines adjacency', 'triangles', 'triangles adjacency', 'line strip adjacency', 'triangle strip adjacency'.
type: 'ubyte', 'ushort', 'uint'.
Rfr: glDrawArrays - glDrawElements.
Vertex Array Queries:
-
value = get_vertex_array(array, pname, [index])
pname: 'size' for GL_VERTEX_ATTRIB_ARRAY_SIZE, etc (and 'binding offset' for GL_VERTEX_BINDING_OFFSET, 'element array buffer binding' for GL_ELEMENT_ARRAY_BUFFER_BINDING, 'current attrib' for GL_CURRENT_VERTEX_ATTRIB).
Rfr: glGetVertexArrayiv, glGetVertexArrayIndexed.
-
value, … = get_vertex_attrib(index, pname)
va1, val2, val3, val4 = get_current_vertex_attrib(index, type)
pname: see get_vertex_array.
type: 'byte', ubyte', 'short', 'ushort', 'int', 'uint', 'float', 'double'.
Rfr: glGetVertexAttrib.
-
pointer = get_vertex_attrib_pointer(index)
Rfr: glGetVertexAttribPointerv.
Conditional Rendering:
-
begin_conditional_render(id, mode )
end_conditional_render( )
mode: 'query wait' for GL_QUERY_WAIT, 'query no wait' for GL_QUERY_NO_WAIT, etc.
Rfr: glBeginConditionalRender.
Vertex Attributes
Attribute Location:
-
bind_attrib_location(program, index, name)
Rfr: glBindAttribLocation.
-
name, glsltype, size = get_active_attrib(program, index)
Rfr: glGetActiveAttrib.
-
location = get_attrib_location(program, name)
Don’t use this function, use bind_attrib_location( ) instead.
Rfr: glGetAttribLocation.
Transform Feedback Variables:
-
transform_feedback_varyings(program, mode, varying1, [varying2, … ])
mode: 'interleaved attribs', 'separate attribs'.
Rfr: glTransformFeedbackVaryings.
-
name, glsltype, size = get_transform_feedback_varying(program, index)
mode: 'interleaved attribs', 'separate attribs'.
Rfr: glGetTransformFeedbackVarying.
Shader Execution:
-
validate_program(program)
validate_program_pipeline(pipeline)
Rfr: glValidateProgram - glValidateProgramPipeline.
Vertex Post-Processing
Transform Feedback:
-
xfb= new_transform_feedback(target ) NONGL
xfb1, xfb2, … = gen_transform_feedbacks([n = 1])
xfb1, xfb2, … = create_transform_feedbacks([n = 1])
bind_transform_feedback(target, [xfb])
unbind_transform_feedback(target) NONGL
delete_transform_feedbacks(xfb1, [xfb2, …])
target: 'transform feedback'.
Rfr: glGenTransformFeedbacks - glBindTransformFeedback - glDeleteTransformFeedbacks.
-
begin_transform_feedback(primitivemode)
end_transform_feedback( )
pause_transform_feedback( )
primitivemode: 'triangles', 'lines', 'points'.
Rfr: glBeginTransformFeedback - glEndTransformFeedback - glPauseTransformFeedback.
-
transform_feedback_buffer_range(xfb, index, buffer, offset, size)
transform_feedback_buffer_base(xfb, index, buffer)
-
draw_transform_feedback(mode, xfb, [instancecount])
draw_transform_feedback_stream(mode, xfb, stream, [instancecount])
mode: 'points', 'patches', 'line strip', 'line loop', 'triangle strip', 'triangle fan', 'lines', 'lines adjacency', 'triangles', 'triangles adjacency', 'line strip adjacency', 'triangle strip adjacency'.
Rfr: glDrawTransformFeedback - glDrawTransformFeedbackStream.
Flatshading:
-
provoking_vertex(provokemode)
provokemode: 'first vertex convention', 'last vertex convention'.
Rfr: glProvokingVertex.
Primitive Clipping:
-
clip_control(origin, depth)
origin: 'lower left', 'upper left'.
depth: 'negative one to one', 'zero to one'.
Rfr: glClipControl.
Controlling Viewport:
-
depth_range(near, far, [index])
depth_range_array(first, near1, far1, [near2, far2, …])
Rfr: glDepthRange - glDepthRangeIndexed - glDepthRangeArray.
-
viewport(x, y, w, h, [index])
viewport_array(first, x1, y1, w1, h1, [x2, y2, w2, h2])
Rfr: glViewport - glViewportIndexed - glViewportArray.
Rasterization
Multisampling:
-
value, … = get_multisample(pname)
pname: 'sample position'.
Rfr: glGetMultisample.
-
min_sample_shading(value)
Rfr: glMinSampleShading.
Points:
-
point_size(size)
Rfr: glPointSize.
-
point_parameter(pname, value)
pname: 'fade threshold size', 'point sprite coord origin'.
Rfr: glPointParameter.
Line Segments:
-
line_width(width)
Rfr: glLineWidth.
Polygons:
-
front_face(dir)
dir: 'ccw', 'cw'.
Rfr: glFrontFace.
-
cull_face(mode)
mode: 'front', 'back', 'front and back'.
Rfr: glCullFace.
Polygon Rast. & Depth Offset:
-
polygon_mode(face, mode)
face: 'front and back'.
mode: 'point', 'line', 'fill'.
Rfr: glPolygonMode.
-
polygon_offset(factor, units)
polygon_offset_clamp(factor, units, clamp)
Rfr: glPolygonOffset.
Fragment Shaders
-
bind_frag_data_location(program, color, name, [index])
Rfr: glBindFragDataLocation, glBindFragDataLocationIndexed.
-
location = get_frag_data_location(program, name)
Rfr: glGetFragDataLocation.
-
index = get_frag_data_index(program, name)
Rfr: glGetFragDataIndex.
Compute Shaders
-
dispatch_compute(numx, numy, numz)
Rfr: glDispatchCompute.
-
dispatch_compute_indirect(indirect)
Rfr: glDispatchComputeIndirect.
Per-Fragment Operations
Scissor Test:
-
scissor_array(first, x1, y1, w1, h1, [x2, y2, w2, h2, …])
scissor(left, bottom, width, height, [index])
Rfr: glScissor - glScissorArray.
Multisample Fragment Ops.:
-
sample_coverage(value, invert)
Rfr: glSampleCoverage.
-
sample_mask(masknumber, mask)
Rfr: glSampleMask.
Stencil Test:
-
stencil_func(func, ref, mask, [face])
func: 'never' for GL_NEVER, 'always' for GL_ALWAYS, etc.
face: 'front', 'back', 'front and back'.
Rfr: glStencilFunc - glStencilFuncSeparate.
-
stencil_op(sfail, dpfail, dppass, [face])
sfail, dpfail, dppass: 'keep' for GL_KEEP, 'incr wrap' for GL_INCR_WRAP, etc.
face: 'front', 'back', 'front and back'.
Rfr: glStencilOp - glStencilOpSeparate.
Depth Buffer Test:
-
depth_func(func)
func: 'never' for GL_NEVER, 'always' for GL_ALWAYS, etc.
Rfr: glDepthFunc.
Occlusion Queries:
Blending:
-
blend_equation(mode)
blend_equation(buf, mode)
blend_equation(rgb, alpha)
blend_equation(buf, rgb, alpha)
mode, rgb, aplha: 'min', 'max', 'add', 'subtract', 'reverse subtract'.
Rfr: glBlendEquation.
-
blend_func(src, dst)
blend_func(buf, src, dst)
blend_func(srcrgb, dstrgb, srcalpha, dstalpha)
blend_func(buf, srcrgb, dstrgb, srcalpha, dstalpha)
src, dst: 'zero', 'one', 'src color', 'one minus src color', etc.
Rfr: glBlendFunc.
-
blend_color(red, green, blue, alpha)
Rfr: glBlendColor.
Dithering:
Logical Operations:
-
logic_op(op, green, blue, alpha)
op: 'clear', 'and', 'and reverse', etc.
Rfr: glLogicOp.
Hints
-
hint(target, hint)
target: 'line smooth', 'polygon smooth', 'texture compression', 'fragment shader derivative'.
hint: 'fastest', 'nicest', 'don’t care'.
Rfr: glHint.
Whole Framebuffer
Selecting Buffers for Writing:
-
draw_buffer(buf)
draw_buffer(framebuffer, buf)
draw_buffers(buf1, [buf2, …])
draw_buffers({buf1, [buf2, …]})
draw_buffers(framebuffer, buf1, [buf2, …])
draw_buffers(framebuffer, {buf1, [buf2, …]})
bufk: attachment.
Rfr: glDrawBuffer, glDrawBuffers.
Fine Control of Buffer Updates:
-
color_mask(r, g, b, a)
color_mask(drawbuffer, r, g, b, a)
r, g, b, a: booleans.
Rfr: glColorMask.
-
depth_mask(boolean)
Rfr: glDepthMask.
-
stencil_mask(mask, [face])
face: 'front', 'back', 'front and back'.
Rfr: glStencilMask.
Clearing the Buffers:
-
clear_color(r, g, b, a)
clear_color(color)
Rfr: glClearColor. See example.
-
clear_depth(d)
Rfr: glClearDepth. See example.
-
clear_stencil(s)
Rfr: glClearStencil.
-
clear_buffer('color', drawbuffer, r, g, b, a)
clear_buffer('color', drawbuffer, color)
clear_buffer('depth', value)
clear_buffer('stencil', value)
clear_buffer(framebuffer, 'color', drawbuffer, r, g, b, a)
clear_buffer(framebuffer, 'color', drawbuffer, color)
clear_buffer(framebuffer, 'depth', value)
clear_buffer(framebuffer, 'stencil', value)
Rfr: glClearBuffer.
-
clear_bufferfi(depth, stencil)
clear_bufferfi(framebuffer, depth, stencil)
Rfr: glClearBufferfi.
Invalidating Framebuffers:
-
invalidate_framebuffer(target|framebuffer, attachment1, [attachment2, … ])
invalidate_sub_framebuffer(target|framebuffer, x, y, width, height, attachment1, [attachment2, … ])
Rfr: glInvalidateFramebuffer, glInvalidateSubFramebuffer.
Reading and Copying Pixels
Reading Pixels:
-
read_buffer(src)
read_buffer(framebuffer, src)
src: attachment.
Rfr: glReadBuffer.
-
data = read_pixels(x, y, width, height, format, type)
format: 'stencil index' for GL_STENCIL_INDEX, 'red' for GL_RED, etc.
type: 'half float' for GL_HALF_FLOAT, 'float' for GL_FLOAT, etc.
Returns data as a binary string.
Rfr: glReadPixels.
Final Conversion:
-
clamp_color(target, clamp)
target: 'clamp read color'.
clamp: 'true', 'false', 'fixed only' (Lua booleans true and false are accepted as well).
Rfr: glClampColor.
-
blit_framebuffer(srcx0, srcy0, srcx1, srcy1, dstx0, dsty0, dstx1, dsty1, filter, [maskbit1, …])
blit_framebuffer(readframebuffer, drawframebuffer, ..ditto..)
filter: 'linear', 'nearest'.
maskbit(s): 'color', 'depth', 'stencil'.
Rfr: glBlitFramebuffer.
Copying Pixels:
-
copy_image_sub_data(srcname, srctarget, srclevel, srcx, srcy, srcz, dstname, dsttarget, dstlevel, dstx, dsty, dstz, width, height, depth)
Rfr: glCopyImageSubData.
Debug Output
Debug Message Callbacks:
-
debug_message_callback(func)
The func callback is executed as func(source, type, id, severity, message).
source: 'api' for GL_DEBUG_SOURCE_API, 'shader compiler' for GL_DEBUG_SOURCE_SHADER_COMPILER, etc.
type: 'error' for GL_DEBUG_TYPE_ERROR, 'deprecated behavior' for GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR, etc.
severity: 'high', 'medium', 'low', 'notification'.
Rfr: glDebugMessageCallback.
Controlling Debug Messages:
-
debug_message_control(source, type, severity, enabled, [id1, …])
Rfr: glDebugMessageControl.
Externally Generated Messages:
-
debug_message_insert(source, type, severity, id, message)
Rfr: glDebugMessageInsert.
Debug Groups:
-
push_debug_group(source, id, message)
pop_debug_group( )
Rfr: glPushDebugGroup - glPopDebugGroup.
Debug Labels:
-
object_label(identifier, name, label)
identifier: 'buffer' for GL_BUFFER, 'vertex array' for GL_VERTEX_ARRAY, etc.
Rfr: glObjectLabel.
Synchronous Debug Output:
DebugOutput Queries:
-
label = get_object_label(identifier, name)
Rfr: glGetObjectLabel.
State and State Requests
Simple Queries:
-
value, … = get(pname, [index])
pname: 'context flags' for GL_CONTEXT_FLAGS, 'major version' for GL_MAJOR_VERSION, etc.
The index argument may be mandatory or optional, depending on pname.
Rfr: glGet.
Note
|
An 'invalid enumerant' error raised by a get( ) call means that the pname is not supported by the OpenGL version being used (a mis-spelled pname causes a 'bad argument …' error). |
-
enable(capability, [index])
disable(capability, [index])
boolean = is_enabled(capability, [index])
Rfr: glEnable - glIsEnabled.
String Queries:
-
value, … = get_string(pname, [index])
pname: 'renderer', 'vendor', 'version', 'extensions', 'shading language version'.
The index argument may be mandatory or optional, depending on pname.
Rfr: glGetString.
-
{extension} = get_extensions([byname]) NONGL
{spir_v_extension} = get_spir_v_extensions([byname]) NONGL
If byname is false (default), returns an array with the names (strings) of all the supported extensions.
If byname is true, returns a table of true values indexed with the names of the supported extensions.
Rfr: glGetString.
Internal Format Queries:
-
value, … = get_internalformat(texturetarget, intformat, pname)
pname: 'num sample counts' for GL_NUM_SAMPLE_COUNTS, 'samples' for GL_SAMPLES, etc.
Rfr: glGetInternalformat.
Transform Feedback Queries:
-
value = get_transform_feedback(xfb, pname)
pname: 'paused', 'active', 'buffer binding', 'buffer start', 'buffer size'.
Rfr: glGetTransformFeedback.
Data handling utilities
The functions described in this section are MoonGL-specific utilities (they have no equivalent in OpenGL) that can be used to encode data from Lua variables to binary strings, and to decode data the other way round. They are useful to deal with the binary string format expected by GL functions like, for example, buffer_data( ) and buffer_sub_data( ).
-
val1, …, valN = flatten(table) NONGL
Flattens out the given table and returns the terminal elements in the order they are found. Similar to Lua’s table.unpack( ), it also unpacks any nested table. Only the array part of the table(s) is considered.
-
size = sizeof(type) NONGL
Returns the size, in bytes, of the GL type.
-
data = pack(type, val1, …, valN) NONGL
data = pack(type, table) NONGL
Packs the numbers val1, …, valN in a binary string, encoding them according to type. The values may be passed also in a (possibly nested) table t. -
{val1, …, valN} = unpack(type, data) NONGL
Unpacks the binary string data, interpreting it as a sequence of values enncoded according to type, and returns the extracted values in a flat table. The length of the binary string must be a multiple of gl.sizeof(type).
Common data types
color: {float}[4] (normalized rgba components).
data: binary string.
dataptr: {ptr, size}, where ptr is a lightuserdata containing a raw pointer to size bytes of memory.
Enumerations
attachment: 'none', 'front left', 'front right', 'back left', 'back right', 'front', 'back', 'left', 'right', 'front and back', 'color attachment 0', 'color attachment 1', …, 'color attachment 15', 'color', 'depth', 'stencil', 'depth attachment', 'stencil attachment', 'depth stencil attachment'.
buffer target: 'array', 'uniform', 'atomic counter', 'query', 'copy read', 'copy write', 'dispatch indirect', 'draw indirect', 'element array', 'texture', 'pixel pack', 'pixel unpack', 'shader storage', 'transform feedback'.
buffer usage: 'static draw', 'static read', 'static copy', 'dynamic draw', 'dynamic read', 'dynamic copy', 'stream draw', 'stream read', 'stream copy'.
capability: 'blend', 'clip distance 0' .. 'clip distance 7', 'color logic op', 'cull face', 'debug output', 'debug output synchronous', 'depth clamp', 'depth test', 'dither', 'framebuffer srgb', 'line smooth', 'multisample', 'polygon offset fill', 'polygon offset line', 'polygon offset point', 'polygon smooth', 'primitive restart', 'primitive restart fixed index', 'rasterizer discard', 'sample alpha to coverage', 'sample alpha to one', 'sample coverage', 'sample shading', 'sample mask', 'scissor test', 'stencil test', 'texture cube map seamless', 'program point size'.
format: 'rgb' for GL_RGB, 'depth component' for GL_DEPTH_COMPONENT, etc.
framebuffer target: 'draw', 'read', or 'draw read' (for GL_FRAMEBUFFER).
glsltype: 'float', 'vec2', 'vec3', 'vec4', 'double', 'dvec2', 'dvec3', 'dvec4', 'int', 'ivec2', 'ivec3', 'ivec4', 'uint', 'uvec2', 'uvec3', 'uvec4', 'bool', 'bvec2', 'bvec3', 'bvec4', 'mat2', 'mat3', 'mat4', 'mat2x3', 'mat2x4', 'mat3x2', 'mat3x4', 'mat4x2', 'mat4x3', 'dmat2', 'dmat3', 'dmat4', 'dmat2x3', 'dmat2x4', 'dmat3x2', 'dmat3x4', 'dmat4x2', 'dmat4x3', 'sampler1D', 'sampler2D', 'sampler3D', 'samplerCube', 'sampler1DShadow', 'sampler2DShadow', 'sampler1DArray', 'sampler2DArray', 'sampler1DArrayShadow', 'sampler2DArrayShadow', 'sampler2DMS', 'sampler2DMSArray', 'samplerCubeShadow', 'samplerBuffer', 'sampler2DRect', 'sampler2DRectShadow', 'isampler1D', 'isampler2D', 'isampler3D', 'isamplerCube', 'isampler1DArray', 'isampler2DArray', 'isampler2DMS', 'isampler2DMSArray', 'isamplerBuffer', 'isampler2DRect', 'usampler1D', 'usampler2D', 'usampler3D', 'usamplerCube', 'usampler2DArray', 'usampler2DArray', 'usampler2DMS', 'usampler2DMSArray', 'usamplerBuffer', 'usampler2DRect', 'image1D', 'image2D', 'image3D', 'image2DRect', 'imageCube', 'imageBuffer', 'image1DArray', 'image2DArray', 'image2DMS', 'image2DMSArray', 'iimage1D', 'iimage2D', 'iimage3D', 'iimage2DRect', 'iimageCube', 'iimageBuffer', 'iimage1DArray', 'iimage2DArray', 'iimage2DMS', 'iimage2DMSArray', 'uimage1D', 'uimage2D', 'uimage3D', 'uimage2DRect', 'uimageCube', 'uimageBuffer', 'uimage1DArray', 'uimage2DArray', 'uimage2DMS', 'uimage2DMSArray', 'atomic_uint'.
intformat: 'rgb' for GL_RGB, 'stencil index' for GL_STENCIL_INDEX, 'r8 snorm' for GL_R8_SNORM, etc.
Values: 'none', 'stencil index', 'depth component', 'depth stencil', 'red', 'rg', 'rgb', 'rgba', 'r8', 'r8 snorm', 'r16', 'r16 snorm', 'rg8', 'rg8 snorm', 'rg16', 'rg16 snorm', 'r3 g3 b2', 'rgb4', 'rgb5', 'rgb8', 'rgb8 snorm', 'rgb10', 'rgb12', 'rgb16 snorm', 'rgba2', 'rgba4', 'rgb5 a1', 'rgba8', 'rgba8 snorm', 'rgb10 a2', 'rgb10 a2ui', 'rgba12', 'rgba16', 'srgb8', 'srgb8 alpha8', 'srgb', 'srgb alpha', 'r16f', 'rg16f', 'rgb16f', 'rgba16f', 'r32f', 'rg32f', 'rgb32f', 'rgba32f', 'r11f g11f b10f', 'rgb9 e5', 'r8i', 'r8ui', 'r16i', 'r16ui', 'r32i', 'r32ui', 'rg8i', 'rg8ui', 'rg16i', 'rg16ui', 'rg32i', 'rg32ui', 'rgb8i', 'rgb8ui', 'rgb16i', 'rgb16ui', 'rgb32i', 'rgb32ui', 'rgba8i', 'rgba8ui', 'rgba16i', 'rgba16ui', 'rgba32i', 'rgba32ui', 'depth component16', 'depth component24', 'depth component32', 'depth component32f', 'depth24 stencil8', 'depth32f stencil8', 'stencil index8', 'compressed red', 'compressed rg', 'compressed rgb', 'compressed rgba', 'compressed srgb', 'compressed srgb alpha', 'compressed red rgtc1', 'compressed signed red rgtc1', 'compressed rg rgtc2', 'compressed signed rg rgtc2', 'compressed rgba bptc unorm', 'compressed srgb alpha bptc unorm', 'compressed rgb bptc signed float', 'compressed rgb bptc unsigned float', 'compressed rgb s3tc dxt1 ext', 'compressed srgb s3tc dxt1 ext', 'compressed rgba s3tc dxt1 ext', 'compressed srgb alpha s3tc dxt1 ext', 'compressed rgba s3tc dxt3 ext', 'compressed srgb alpha s3tc dxt3 ext', 'compressed rgba s3tc dxt5 ext', 'compressed srgb alpha s3tc dxt5 ext'.
query target: 'timestamp', 'any samples passed', 'any samples passed conservative', 'primitives generated', 'samples passed', 'time elapsed', 'transform feedback primitives written', 'vertices submitted', 'primitives submitted', 'vertex shader invocations', 'tess control shader patches', 'tess evaluation shader invocations', 'geometry shader primitives emitted', 'fragment shader invocations', 'compute shader invocations', 'geometry shader invocations', 'clipping input primitives', 'clipping output primitives', 'transform feedback overflow', 'transform feedback stream overflow'.
renderbuffer target: 'renderbuffer'.
shaderbinaryformat: 'spir v'.
shadertype: 'vertex', 'fragment', 'geometry', 'tess evaluation', 'tess control', 'compute'.
texture target: '1d', '2d', '3d', '1d array', '2d array', 'rectangle', 'cube map', 'cube map array', '2d multisample', '2d multisample array', 'cube map positive x', 'cube map positive y', 'cube map positive z', 'cube map negative x', 'cube map negative y', 'cube map negative z', 'proxy 1d', 'proxy 2d', 'proxy 3d', 'proxy 1d array', 'proxy 2d array', 'proxy rectangle', 'proxy cube map', 'proxy cube map array', 'proxy 2d multisample', 'proxy 2d multisample array', 'buffer', 'renderbuffer'.
type: 'none', 'ubyte', 'byte', 'ushort', 'short', 'uint', 'int', 'fixed', 'half float', 'float', 'double', 'ubyte 3 3 2', 'ubyte 2 3 3 rev', 'ushort 5 6 5', 'ushort 5 6 5 rev', 'ushort 4 4 4 4', 'ushort 4 4 4 4 rev', 'ushort 5 5 5 1', 'ushort 1 5 5 5 rev', 'uint 8 8 8 8', 'uint 8 8 8 8 rev', 'uint 10 10 10 2', 'uint 2 10 10 10 rev', 'uint 24 8', 'uint 10f 11f 11f rev', 'uint 5 9 9 9 rev', 'float 32 uint 24 8 rev', 'int 2 10 10 10 rev'.
Code Snippets
This section contains code snippets (i.e. incomplete examples) that show the usage of MoonGL functionalities in practice.
gl = require("moongl")
glfw = require("moonglfw")
-- Create a window and make its GL context current:
window = glfw.create_window(600, 400, "Hello, MoonGL!")
glfw.make_context_current(window)
-- This is actually glewInit():
gl.init()
-- ... now it is safe to call MoonGL functions ...
if not gl.is_supported("GL_VERSION_4_5") then
error("Sorry, OpenGL 4.5 is not supported by your system")
end
-- specify the clear value for the color buffer:
gl.clear_color(1.0, 0.0, 0.0, 1.0)
-- specify the clear value for the depth buffer:
gl.clear_depth(0.6)
-- clear the color buffer and the depth buffer:
gl.clear('color', 'depth')
vertices = {
-- position: color: texture coords: 0 ____ 1
-0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, -- 0 | /|
0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, -- 1 | / |
0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, -- 2 |/___|
-0.5, -0.5, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, -- 3 3 2
}
indices = {
0, 1, 3, -- first triangle
1, 2, 3, -- second triangle
}
floatsz = gl.sizeof('float')
vao = gl.new_vertex_array() -- vertex array object
vbo = gl.new_buffer('array') -- vertex buffer object
gl.buffer_data('array', gl.pack('float', vertices), 'static draw')
-- position attribute
gl.vertex_attrib_pointer(0, 3, 'float', false, 8*floatsz, 0)
gl.enable_vertex_attrib_array(0)
-- color attribute
gl.vertex_attrib_pointer(1, 3, 'float', false, 8*floatsz, 3*floatsz)
gl.enable_vertex_attrib_array(1)
-- texture coords attribute
gl.vertex_attrib_pointer(2, 2, 'float', false, 8*floatsz, 6*floatsz)
gl.enable_vertex_attrib_array(2)
ebo = gl.new_buffer('element array') -- element buffer object
gl.buffer_data('element array', gl.pack('uint', indices), 'static draw')
gl.unbind_vertex_array()
gl.uniform(0, 'uint', 123)
gl.uniform(1, 'float', 1.234)
gl.uniform(2, 'bool', true)
gl.uniform(3, 'int', 1, 2) -- 2D vector
v = { 1.0, 2.0, 3.0 }
gl.uniform(4, 'float', gl.flatten(v)) -- table.unpack(v) would do also
M = { 1.0, 2.0, 3.0 -- row-major order
4.0, 5.0, 6.0
7.0, 8.0, 9.0 }
gl.uniform_matrix(0, 'float', '3x3', true, gl.flatten(M))
-- 1) Creating the texture:
tex_id = gl.new_texture('2d')
-- configure it:
gl.texture_parameter('2d', 'wrap s', 'repeat')
gl.texture_parameter('2d', 'wrap t', 'repeat')
gl.texture_parameter('2d', 'min filter', 'linear')
gl.texture_parameter('2d', 'mag filter', 'linear')
-- load the texture image using MoonImage:
image, w, h = mi.load('myimage.png', 'rgb')
gl.texture_image('2d', 0, 'rgb', 'rgb', 'ubyte', image, w, h)
image, w, h = nil, nil, nil -- let the GC collect them
-- generate the mipmap:
gl.generate_mipmap('2d')
gl.unbind_texture('2d')
-- ...
-- 2) Using the texture in the game loop:
gl.use_program(prog_id)
gl.active_texture(0) -- activate texture unit 0
gl.bind_texture('2d', tex_id)
-- Assuming our program has a sampler2D uniform named mytexture:
gl.uniform(gl.uniform_location(prog_id, 'mytexture'), 'int', 0)
-- ... bind geometry and draw ...