Tin Benjamin Matuka
HomeBlogProjectsAbout me

Blog

wxLua + LuaGL = Win!
2011-01-31

A few weeks ago I found wxLua in AUR and decided to give it a try. Documentation wasn't great, but at least it had a bunch of simple samples which are enough to figure out how stuff works. One of the things wxLua supports is wxGLCanvas and I wanted to learn OpenGL, so I figured it would be easy to do. First I had to find a way to work with OpenGL in Lua, so after going through a bunch of dead projects I found LuaGL. Unfortunately, there was no ArchLinux package and I didn't feel like doing it the dirty way, so I made a binary (it was simpler and faster) package myself. Now I had both and only had to figure out how to make them work together. After a lot of trial and error, I finally got everything to work and made a demo.

Here is an excerpt of code that creates the canvas and preps it for rendering:

-- This is where the GLCanvas is created
canvas = wx.wxGLCanvas(frame, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxEXPAND)

-- A GLContext is created
context = wx.wxGLContext(canvas)

-- Connect the PAINT event to the render function
canvas:Connect(wx.wxEVT_PAINT, render)

-- Refresh the canvas so that it gets properly resized and rendered.
canvas:Refresh()

And here is the render function that actually uses LuaGL and that we linked to the PAINT event:

function render()
    context:SetCurrent(canvas)

    gl.ClearColor(0, 0, 0, 0)
    gl.Clear(gl.COLOR_BUFFER_BIT)

    gl.Begin('TRIANGLES')
        gl.Vertex( 0,  0.75, 0)
        gl.Vertex(-0.75, -0.75, 0)
        gl.Vertex( 0.75, -0.75, 0)
    gl.End()

    canvas:SwapBuffers()
end

You can download the entire demo here (make sure to run it with wxlua instead of lua, since wxlua library segfaults with my current lua package). I hope this will be enough to get your OpenGL learning and coding painlessly started. Good luck!

Demo screenshot