From c52e1b30391d730cd6d20f65ec75d61884355c5c Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 23:58:00 -0500 Subject: refactor: rename opengl functions to match their C counterparts --- demo/honey.lua | 169 ++++++++++++++++++++++++++----------------------------- src/gl/data.c | 47 ++++++++-------- src/gl/drawing.c | 39 +++++++------ src/gl/gl.c | 44 +++++++-------- src/gl/shader.c | 45 +++++++-------- src/gl/texture.c | 37 ++++++------ 6 files changed, 185 insertions(+), 196 deletions(-) diff --git a/demo/honey.lua b/demo/honey.lua index 6d2cdfc..b0b3ec4 100644 --- a/demo/honey.lua +++ b/demo/honey.lua @@ -1,21 +1,20 @@ local gl = honey.gl local window = honey.window -gl.errorName = function(errorCode) - for name, code in pairs(gl.errorType) do - if code == errorCode then return name end - end - return 'unknown' -end -gl.init() +--====== initialize opengl ======-- + +gl.Init() window.setHint(window.hintType.contextVersionMajor, 3) window.setHint(window.hintType.contextVersionMinor, 3) window.setHint(window.hintType.openGlProfile, window.profileType.openGlCoreProfile) + +--====== create window ======-- + local w = window.create(640, 480, 'hello, world!') window.makeContextCurrent(w) -gl.initGlad() +gl.InitGlad() window.setFramebufferSizeCallback(w, function(_, width, height) print(string.format("resize: (%d, %d)", width, height)) @@ -23,8 +22,9 @@ window.setFramebufferSizeCallback(w, function(_, width, height) end) -local vertexShaderSource = [[ +--====== compile shaders ======-- +local vertexShaderSource = [[ #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; @@ -56,108 +56,101 @@ void main() } ]] -local vertexShader = gl.shader.create(gl.shader.type.vertexShader) -gl.shader.setSource(vertexShader, vertexShaderSource) -gl.shader.compile(vertexShader) -local fragmentShader = gl.shader.create(gl.shader.type.fragmentShader) -gl.shader.setSource(fragmentShader, fragmentShaderSource) -gl.shader.compile(fragmentShader) +-- vertex shader +local vertexShader = gl.CreateShader(gl.VERTEX_SHADER) +gl.ShaderSource(vertexShader, vertexShaderSource) +gl.CompileShader(vertexShader) -local shader = gl.shader.createProgram() -gl.shader.attachShader(shader, vertexShader) -gl.shader.attachShader(shader, fragmentShader) -gl.shader.link(shader) -gl.shader.delete(vertexShader) -gl.shader.delete(fragmentShader) +-- fragment shader +local fragmentShader = gl.CreateShader(gl.FRAGMENT_SHADER) +gl.ShaderSource(fragmentShader, fragmentShaderSource) +gl.CompileShader(fragmentShader) +-- link +local shader = gl.CreateProgram() +gl.AttachShader(shader, vertexShader) +gl.AttachShader(shader, fragmentShader) +gl.LinkProgram(shader) +-- clean up +gl.DeleteShader(vertexShader) +gl.DeleteShader(fragmentShader) ----------------------------------------------------------------- - -local texture = gl.texture.create() -gl.texture.bind(gl.texture.bindTarget.texture2d, texture) -err = gl.getError() -if err ~= gl.errorType.noError then error(gl.errorName(err)) end - - -local image, width, height, channels = honey.image.load('container.jpg', 0) -gl.texture.bufferImage2d( - gl.texture.bindTarget.texture2d, 0, - gl.texture.format.rgb, - width, height, - gl.texture.format.rgb, gl.dataType.uchar, - image -) -gl.texture.generateMipmaps(gl.texture.bindTarget.texture2d) -err = gl.getError() -if err ~= gl.errorType.noError then error(gl.errorName(err)) end - -honey.image.destroy(image) +--====== set up vertex data ======-- local vertices = { - -- position color uvs - 0.5, 0.5, 0.0, 0, 0, 0, 1, 1, - 0.5, -0.5, 0.0, 1, 0, 0, 1, 0, - -0.5, -0.5, 0.0, 0, 1, 0, 0, 0, - -0.5, 0.5, 0.0, 0, 0, 1, 0, 1 +-- positions colors uvs + 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, -- top right + 0.5, -0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, -- bottom right + -0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, -- bottom let + -0.5, 0.5, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0 -- top let } local indices = { - 0, 1, 3, - 1, 2, 3 + 0, 1, 3, -- first triangle + 1, 2, 3 -- second triangle } -local vertexArray = gl.data.createVertexArray() +-- buffers +local vertexArray = gl.GenVertexArrays() +local vertexBuffer = gl.GenBuffers() +local elementBuffer = gl.GenBuffers() -local vertexBuffer = gl.data.createBuffer() -local elementBuffer = gl.data.createBuffer() -gl.data.bindVertexArray(vertexArray) +gl.BindVertexArray(vertexArray) -gl.data.bindBuffer(gl.data.bufferTarget.arrayBuffer, vertexBuffer) -local err = gl.getError() -if err ~= gl.errorType.noError then error(gl.errorName(err)) end -gl.data.bufferData(gl.data.bufferTarget.arrayBuffer, gl.dataType.float, vertices, gl.data.bufferUsage.staticDraw) -if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end +gl.BindBuffer(gl.ARRAY_BUFFER, vertexBuffer) +gl.BufferData(gl.ARRAY_BUFFER, gl.FLOAT, vertices, gl.STATIC_DRAW) -gl.data.bindBuffer(gl.data.bufferTarget.elementArrayBuffer, elementBuffer) -gl.data.bufferData(gl.data.bufferTarget.elementArrayBuffer, gl.dataType.uint, indices, gl.data.bufferUsage.staticDraw) +gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementBuffer) +gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, gl.UNSIGNED_INT, indices, gl.STATIC_DRAW) -gl.data.vertexAttribPointer(0, 3, false, 8, 0) -gl.data.vertexArrayEnableAttrib(0) -gl.data.vertexAttribPointer(1, 3, false, 8, 3) -gl.data.vertexArrayEnableAttrib(1) -gl.data.vertexAttribPointer(2, 2, false, 8, 6) -gl.data.vertexArrayEnableAttrib(2) +-- position +gl.VertexAttribPointer(0, 3, false, 8, 0) +gl.EnableVertexAttribArray(0) -gl.data.bindBuffer(gl.data.bufferTarget.arrayBuffer, 0) -if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end +-- color +gl.VertexAttribPointer(1, 3, false, 8, 3) +gl.EnableVertexAttribArray(1) -gl.shader.use(shader) -local textureLocation = gl.shader.getUniformLocation(shader, "ourTexture") -err = gl.getError() -if err ~= gl.errorType.noError then error(gl.errorName(err)) end -gl.shader.uniform1i(textureLocation, 0) -err = gl.getError() -if err ~= gl.errorType.noError then error(gl.errorName(err)) end +-- uv +gl.VertexAttribPointer(2, 2, false, 8, 6) +gl.EnableVertexAttribArray(2) -while not window.shouldClose(w) do - gl.draw.setClearColor(0.2, 0.3, 0.3, 1.0) - gl.draw.clear(gl.draw.bufferMask.colorBuffer); +--====== load texture ======-- + +local texture = gl.GenTextures() +gl.BindTexture(gl.TEXTURE_2D, texture) - gl.texture.setActiveUnit(0) - err = gl.getError() - if err ~= gl.errorType.noError then error(gl.errorName(err)) end - gl.texture.bind(gl.texture.bindTarget.texture2d, texture) - err = gl.getError() - if err ~= gl.errorType.noError then error(gl.errorName(err)) end +local image, width, height = honey.image.load('container.jpg', 3) +gl.TexImage2D( + gl.TEXTURE_2D, 0, + gl.RGB, width, height, + gl.RGB, gl.UNSIGNED_BYTE, + image +) +gl.GenerateMipmap(gl.TEXTURE_2D) +honey.image.destroy(image) + +-- connect shader samplers to texture units +gl.UseProgram(shader) +gl.Uniform1i(gl.GetUniformLocation(shader, 'ourTexture'), 0) + + +--====== main loop ======-- + +while not window.shouldClose(w) do + gl.ClearColor(0.2, 0.3, 0.3, 1.0) + gl.Clear(gl.COLOR_BUFFER_BIT) + gl.ActiveTexture(0) + gl.BindTexture(gl.TEXTURE_2D, texture) - gl.shader.use(shader) - gl.data.bindVertexArray(vertexArray) - gl.draw.drawElements(gl.draw.primitiveType.triangles, 6, gl.dataType.uint, 0) + gl.UseProgram(shader) + gl.BindVertexArray(vertexArray) + gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0) window.swapBuffers(w) window.pollEvents() end window.destroy(w) -gl.terminate() +gl.Terminate() diff --git a/src/gl/data.c b/src/gl/data.c index 777689a..a9d0324 100644 --- a/src/gl/data.c +++ b/src/gl/data.c @@ -4,6 +4,7 @@ #include #include #include +#include "util/util.h" #include "gl.h" int gl_create_buffer(lua_State *L); @@ -18,32 +19,30 @@ int gl_vertex_array_enable_attrib(lua_State *L); void setup_data(lua_State *L, int gl_index) { - int buffer_binding_targets = hs_create_table(L, - hs_str_int("arrayBuffer", GL_ARRAY_BUFFER), - hs_str_int("elementArrayBuffer", GL_ELEMENT_ARRAY_BUFFER), - ); - - int buffer_usage_patterns = hs_create_table(L, - hs_str_int("streamDraw", GL_STREAM_DRAW), - hs_str_int("staticDraw", GL_STATIC_DRAW), - hs_str_int("dynamicDraw", GL_DYNAMIC_DRAW), - ); - - hs_create_table(L, - hs_str_cfunc("createBuffer", gl_create_buffer), - hs_str_cfunc("bindBuffer", gl_bind_buffer), - hs_str_cfunc("bufferData", gl_buffer_data), - - hs_str_cfunc("createVertexArray", gl_vertex_array_create), - hs_str_cfunc("bindVertexArray", gl_vertex_array_bind), - hs_str_cfunc("vertexAttribPointer", gl_vertex_attrib_pointer), - hs_str_cfunc("vertexArrayEnableAttrib", gl_vertex_array_enable_attrib), - - hs_str_tbl("bufferTarget", buffer_binding_targets), - hs_str_tbl("bufferUsage", buffer_usage_patterns), + int tbl = hs_create_table(L, + /* functions */ + hs_str_cfunc("GenBuffers", gl_create_buffer), + hs_str_cfunc("BindBuffer", gl_bind_buffer), + hs_str_cfunc("BufferData", gl_buffer_data), + + hs_str_cfunc("GenVertexArrays", gl_vertex_array_create), + hs_str_cfunc("BindVertexArray", gl_vertex_array_bind), + hs_str_cfunc("VertexAttribPointer", gl_vertex_attrib_pointer), + hs_str_cfunc("EnableVertexAttribArray", gl_vertex_array_enable_attrib), + + /******** enums ********/ + /* buffer bind targets */ + hs_str_int("ARRAY_BUFFER", GL_ARRAY_BUFFER), + hs_str_int("ELEMENT_ARRAY_BUFFER", GL_ELEMENT_ARRAY_BUFFER), + + /* buffer usage patters */ + hs_str_int("STREAM_DRAW", GL_STREAM_DRAW), + hs_str_int("STATIC_DRAW", GL_STATIC_DRAW), + hs_str_int("DYNAMIC_DRAW", GL_DYNAMIC_DRAW), ); - lua_setfield(L, gl_index, "data"); + append_table(L, gl_index, tbl); + lua_pop(L, 1); } diff --git a/src/gl/drawing.c b/src/gl/drawing.c index ca914e0..e6b1e13 100644 --- a/src/gl/drawing.c +++ b/src/gl/drawing.c @@ -2,6 +2,7 @@ #include #include #include +#include "util/util.h" int gl_set_viewport(lua_State *L); int gl_draw_arrays(lua_State *L); @@ -11,30 +12,28 @@ int gl_clear(lua_State *L); void setup_drawing(lua_State *L, int gl_index) { - int primitive_types = hs_create_table(L, - hs_str_int("points", GL_POINTS), - hs_str_int("lines", GL_LINES), - hs_str_int("triangles", GL_TRIANGLES), - ); - - int buffer_masks = hs_create_table(L, - hs_str_int("colorBuffer", GL_COLOR_BUFFER_BIT), - hs_str_int("depthBuffer", GL_DEPTH_BUFFER_BIT), - hs_str_int("stencilBuffer", GL_STENCIL_BUFFER_BIT), - ); + int tbl = hs_create_table(L, + /* functions */ + hs_str_cfunc("DrawArrays", gl_draw_arrays), + hs_str_cfunc("DrawElements", gl_draw_elements), + hs_str_cfunc("ClearColor", gl_set_clear_color), + hs_str_cfunc("Clear", gl_clear), + hs_str_cfunc("Viewport", gl_set_viewport), - hs_create_table(L, - hs_str_cfunc("drawArrays", gl_draw_arrays), - hs_str_cfunc("drawElements", gl_draw_elements), - hs_str_cfunc("setClearColor", gl_set_clear_color), - hs_str_cfunc("clear", gl_clear), - hs_str_cfunc("setViewport", gl_set_viewport), + /******** enums ********/ + /* rendering primitives */ + hs_str_int("POINTS", GL_POINTS), + hs_str_int("LINES", GL_LINES), + hs_str_int("TRIANGLES", GL_TRIANGLES), - hs_str_tbl("primitiveType", primitive_types), - hs_str_tbl("bufferMask", buffer_masks), + /* clear bitmasks */ + hs_str_int("COLOR_BUFFER_BIT", GL_COLOR_BUFFER_BIT), + hs_str_int("DEPTH_BUFFER_BIT", GL_DEPTH_BUFFER_BIT), + hs_str_int("STENCIL_BUFFER_BIT", GL_STENCIL_BUFFER_BIT), ); - lua_setfield(L, gl_index, "draw"); + append_table(L, gl_index, tbl); + lua_pop(L, 1); } int gl_set_clear_color(lua_State *L) diff --git a/src/gl/gl.c b/src/gl/gl.c index c4502dc..4639d48 100644 --- a/src/gl/gl.c +++ b/src/gl/gl.c @@ -20,36 +20,34 @@ int gl_get_error(lua_State *L); void setup_gl(lua_State *L, int honey_index) { - int data_types = hs_create_table(L, - hs_str_int("uchar", GL_UNSIGNED_BYTE), - hs_str_int("uint", GL_UNSIGNED_INT), - hs_str_int("int", GL_INT), - hs_str_int("float", GL_FLOAT), - ); - - int error_types = hs_create_table(L, - hs_str_int("noError", GL_NO_ERROR), - hs_str_int("invalidEnum", GL_INVALID_ENUM), - hs_str_int("invalidValue", GL_INVALID_VALUE), - hs_str_int("invalidOperation", GL_INVALID_OPERATION), - hs_str_int("invalidFramebufferOperation", GL_INVALID_FRAMEBUFFER_OPERATION), - hs_str_int("outOfMemory", GL_OUT_OF_MEMORY), - ); - int gl_index = hs_create_table(L, - hs_str_cfunc("init", gl_init), - hs_str_cfunc("initGlad", glad_init), - hs_str_cfunc("terminate", gl_terminate), - hs_str_cfunc("getError", gl_get_error), - - hs_str_tbl("dataType", data_types), - hs_str_tbl("errorType", error_types), + /* functions */ + hs_str_cfunc("Init", gl_init), + hs_str_cfunc("InitGlad", glad_init), + hs_str_cfunc("Terminate", gl_terminate), + hs_str_cfunc("GetError", gl_get_error), + + /******** enums ********/ + /* data types */ + hs_str_int("UNSIGNED_BYTE", GL_UNSIGNED_BYTE), + hs_str_int("UNSIGNED_INT", GL_UNSIGNED_INT), + hs_str_int("INT", GL_INT), + hs_str_int("FLOAT", GL_FLOAT), + + /* error types */ + hs_str_int("NO_ERROR", GL_NO_ERROR), + hs_str_int("INVALID_ENUM", GL_INVALID_ENUM), + hs_str_int("INVALID_VALUE", GL_INVALID_VALUE), + hs_str_int("INVALID_OPERATION", GL_INVALID_OPERATION), + hs_str_int("INVALID_FRAMEBUFFER_OPERATION", GL_INVALID_FRAMEBUFFER_OPERATION), + hs_str_int("OUT_OF_MEMORY", GL_OUT_OF_MEMORY), ); setup_shader(L, gl_index); setup_drawing(L, gl_index); setup_data(L, gl_index); setup_texture(L, gl_index); + lua_setfield(L, honey_index, "gl"); } diff --git a/src/gl/shader.c b/src/gl/shader.c index c3b409c..b4d03e6 100644 --- a/src/gl/shader.c +++ b/src/gl/shader.c @@ -2,6 +2,7 @@ #include #include #include +#include "util/util.h" int gl_create_shader(lua_State *L); int gl_shader_set_source(lua_State *L); @@ -20,30 +21,30 @@ int gl_uniform_4f(lua_State *L); void setup_shader(lua_State *L, int gl_index) { - int shader_types = hs_create_table(L, - hs_str_int("vertexShader", GL_VERTEX_SHADER), - hs_str_int("fragmentShader", GL_FRAGMENT_SHADER), + int tbl = hs_create_table(L, + /* functions */ + hs_str_cfunc("CreateShader", gl_create_shader), + hs_str_cfunc("ShaderSource", gl_shader_set_source), + hs_str_cfunc("CompileShader", gl_shader_compile), + hs_str_cfunc("DeleteShader", gl_shader_delete), + + hs_str_cfunc("CreateProgram", gl_program_create), + hs_str_cfunc("AttachShader", gl_program_attach_shader), + hs_str_cfunc("LinkProgram", gl_program_link), + hs_str_cfunc("UseProgram", gl_program_use), + + hs_str_cfunc("GetUniformLocation", gl_uniform_get_location), + hs_str_cfunc("Uniform1i", gl_uniform_1i), + hs_str_cfunc("Uniform4f", gl_uniform_4f), + + /******** enums ********/ + /* shader types */ + hs_str_int("VERTEX_SHADER", GL_VERTEX_SHADER), + hs_str_int("FRAGMENT_SHADER", GL_FRAGMENT_SHADER), ); - hs_create_table(L, - hs_str_cfunc("create", gl_create_shader), - hs_str_cfunc("setSource", gl_shader_set_source), - hs_str_cfunc("compile", gl_shader_compile), - hs_str_cfunc("delete", gl_shader_delete), - - hs_str_cfunc("createProgram", gl_program_create), - hs_str_cfunc("attachShader", gl_program_attach_shader), - hs_str_cfunc("link", gl_program_link), - hs_str_cfunc("use", gl_program_use), - - hs_str_cfunc("getUniformLocation", gl_uniform_get_location), - hs_str_cfunc("uniform1i", gl_uniform_1i), - hs_str_cfunc("uniform4f", gl_uniform_4f), - - hs_str_tbl("type", shader_types), - ); - - lua_setfield(L, gl_index, "shader"); + append_table(L, gl_index, tbl); + lua_pop(L, 1); } diff --git a/src/gl/texture.c b/src/gl/texture.c index d0c3a0a..739dd13 100644 --- a/src/gl/texture.c +++ b/src/gl/texture.c @@ -2,6 +2,7 @@ #include #include #include +#include "util/util.h" int gl_texture_create(lua_State *L); @@ -13,27 +14,25 @@ int gl_texture_set_active(lua_State *L); void setup_texture(lua_State *L, int gl_index) { - int bind_targets = hs_create_table(L, - hs_str_int("texture2d", GL_TEXTURE_2D), + int tbl = hs_create_table(L, + /* functions */ + hs_str_cfunc("GenTextures", gl_texture_create), + hs_str_cfunc("BindTexture", gl_texture_bind), + hs_str_cfunc("TexImage2D", gl_texture_image_2d), + hs_str_cfunc("GenerateMipmap", gl_texture_generate_mipmaps), + hs_str_cfunc("ActiveTexture", gl_texture_set_active), + + /******** enums ********/ + /* texture binding targets */ + hs_str_int("TEXTURE_2D", GL_TEXTURE_2D), + + /* texture data formats */ + hs_str_int("RGB", GL_RGB), + hs_str_int("RGBA", GL_RGBA), ); - int formats = hs_create_table(L, - hs_str_int("rgb", GL_RGB), - hs_str_int("rgba", GL_RGBA), - ); - - hs_create_table(L, - hs_str_cfunc("create", gl_texture_create), - hs_str_cfunc("bind", gl_texture_bind), - hs_str_cfunc("bufferImage2d", gl_texture_image_2d), - hs_str_cfunc("generateMipmaps", gl_texture_generate_mipmaps), - hs_str_cfunc("setActiveUnit", gl_texture_set_active), - - hs_str_tbl("bindTarget", bind_targets), - hs_str_tbl("format", formats), - ); - - lua_setfield(L, gl_index, "texture"); + append_table(L, gl_index, tbl); + lua_pop(L, 1); } -- cgit v1.2.1