four.gl

local ffi = require( "ffi" ) local libs = { OSX = { x86 = "OpenGL.framework/OpenGL", x64 = "OpenGL.framework/OpenGL" }, Windows = { x86 = "OPENGL32.DLL", x64 = "OPENGL32.DLL" }, Linux = { x86 = "libGL.so", x64 = "libGL.so", arm = "libGL.so" }, BSD = { x86 = "libGL.so", x64 = "libGL.so" }, POSIX = { x86 = "libGL.so", x64 = "libGL.so" }, Other = { x86 = "libGL.so", x64 = "libGL.so" }}

local lib = libs[ffi.os][ffi.arch] local lo = ffi.load(lib) local hi = {}

four.gl = { lo = lo, hi = hi }

-- hi, sligthly higher level interface to gl for some calls.

function hi.glGetBooleanv(pname) local param = ffi.new ("GLboolean[1]", 0) lo.glGetBooleanv(pname, param) return (param[0] == 1) end

function hi.glGetDoublev(pname) local param = ffi.new ("GLdouble[1]", 0) lo.glGetDoublev(pname, param) return param[0] end

function hi.glGetFloatv(pname) local param = ffi.new ("GLfloat[1]", 0) lo.glGetDoublev(pname, param) return param[0] end

function hi.glGetIntegerv(pname) local param = ffi.new ("GLint[1]", 0) lo.glGetIntegerv(pname, param) return param[0] end

function hi.glGetString(pname) local ptr = lo.glGetString(pname) if ptr == 0 then return "" else return ffi.string(ptr) end end

function hi.glGetShaderiv (s, flag) local flag_v = ffi.new ("GLint[1]", 0) lo.glGetShaderiv (s, flag, flag_v); return flag_v[0] end

function hi.glShaderSource (s, src) -- TODO check ffi.cast and C null terminated string stuff local arr = ffi.new ("const GLchar [1]", ffi.cast ("GLchar ", src)) lo.glShaderSource (s, 1, arr, nil) end

function hi.glGetShaderInfoLog (s) local len = hi.glGetShaderiv (s, lo.GL_INFO_LOG_LENGTH) if (len == 0) then return "" else local log = ffi.new("GLchar [?]", len) lo.glGetShaderInfoLog (s, len, nil, log) return ffi.string (log, len - 1) end end

function hi.glGetProgramiv (s, flag) local flag_v = ffi.new ("GLint[1]", 0) lo.glGetProgramiv (s, flag, flag_v); return flag_v[0] end

function hi.glGetProgramInfoLog (s) local len = hi.glGetProgramiv (s, lo.GL_INFO_LOG_LENGTH) if (len == 0) then return "" else local log = ffi.new("GLchar [?]", len) lo.glGetProgramInfoLog (s, len, nil, log) return ffi.string (log, len - 1) end end

function hi.glGenBuffer() local id = ffi.new ("GLuint[1]", 0) lo.glGenBuffers(1, id) return id[0] end

function hi.glDeleteBuffer(id) local idptr = ffi.new("GLuint[1]", id) lo.glDeleteBuffers(1, idptr) end

function hi.glGenVertexArray() local id = ffi.new ("GLuint[1]", 0) lo.glGenVertexArrays(1, id) return id[0] end

function hi.glDeleteVertexArray(id) local idptr = ffi.new("GLuint[1]", id) lo.glDeleteVertexArrays(1, idptr) end

function hi.glGenTexture() local id = ffi.new("GLuint[1]", 0) lo.glGenTextures(1, id) return id[0] end

function hi.glDeleteTexture(id) local idptr = ffi.new("GLuint[1]", id) lo.glDeleteTextures(1, idptr) end

-- lo, raw bindings to OpenGL

--[[-- This was obtained from glcorearb.h version 2012/08/06, mostly via :

s/^#ifndef.$// s/^#ifdef.$// s/^#endif.$// s/^#define +\([^ ]+\) +\([^ ]+\) $/static const GLenum \1 = \2;/ s/GLAPI //s s/APIENTRY //s s/.APIENTRYP.\n//s

and a few manual ajdustements (reorder and manual adjustements).