lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 3 May 2012 22:42, Dimiter 'malkia' Stanev <malkia@gmail.com> wrote:
> I think someone posted OpenGL ffi bindings that took care of this, have to
> dig the archives to find them though.

I know I emailed you a set of bindings I was working on a while ago,
is that what you remember? ;)

I've got bindings I've been using for a little while for accessing the
new gl3 api, significantly generated from gl3.h, but they're not
extensively tested. Also, they're linux-only, but as far as I know
porting it should be as easy as swapping out glXGetProcAddress for
wgl* and such, and maybe changing the ffi.load line. It's my first
nontrivial binding written with the luajit ffi, so I'd love to hear
about errors/problems/other comments.

The full binding is at https://gist.github.com/7321caacf90d6ba8cc09
and I've put a few relevant sections below.

    henk

--------

local ffi = require 'ffi'

local gl = {
  GL_DEPTH_BUFFER_BIT =               0x00000100,
  GL_STENCIL_BUFFER_BIT =             0x00000400,
  GL_COLOR_BUFFER_BIT =               0x00004000,
  GL_FALSE =                          0,
  GL_TRUE =                           1,
  -- ... many more constants ...
}

local libgl = ffi.load('libGL.so.1')

ffi.cdef[[
typedef unsigned int GLenum;
typedef unsigned char GLboolean;
typedef unsigned int GLbitfield;
typedef signed char GLbyte;
typedef short GLshort;
typedef int GLint;
// ... more types ...

typedef void (*GLDEBUGPROCARB)(GLenum source,GLenum type,GLuint
id,GLenum severity,GLsizei length,const GLchar *message,GLvoid
*userParam);
typedef void (*GLDEBUGPROCAMD)(GLuint id,GLenum category,GLenum
severity,GLsizei length,const GLchar *message,GLvoid *userParam);
typedef GLintptr GLvdpauSurfaceNV;
typedef void (*PFNGLCULLFACEPROC) (GLenum mode);
typedef void (*PFNGLFRONTFACEPROC) (GLenum mode);
typedef void (*PFNGLHINTPROC) (GLenum target, GLenum mode);
typedef void (*PFNGLLINEWIDTHPROC) (GLfloat width);
typedef void (*PFNGLPOINTSIZEPROC) (GLfloat size);
typedef void (*PFNGLPOLYGONMODEPROC) (GLenum face, GLenum mode);
typedef void (*PFNGLSCISSORPROC) (GLint x, GLint y, GLsizei width,
GLsizei height);
typedef void (*PFNGLTEXPARAMETERFPROC) (GLenum target, GLenum pname,
GLfloat param);
// ... many more function type declarations ...
]]

ffi.cdef[[
extern void (*glXGetProcAddress(const GLubyte *procname))( void );
]]

local debugging = true

setmetatable(gl, {
  __index = function (t, k)
    local fn = libgl.glXGetProcAddress(k)
    if fn ~= nil then
      t[k] = ffi.cast('PFN' .. k:upper() .. 'PROC', fn)

      if debugging and k ~= 'glGetError' then
        local fn = t[k]
        t[k] = function (...)
          local ret = fn(...)
          local err = gl.glGetError()
          if err ~= gl.GL_NO_ERROR then
            error('gl error ' .. err)
          end
          return ret
        end
      end

      return t[k]
    else
      return nil
    end
  end
})

return gl