Want to write a Perl program that uses OpenGL for graphical output? Running Ubuntu? Can’t work out how to install what you need to get even a basic program working? This is your lucky day. Read on.
Perl is the duct tape of the Internet. Most folk think you’re restricted to using Perl on the command line — and don’t know that it can quite happily drive a graphical user interface. Even fewer realise that you can build high FPS OpenGL applications using it. Better yet, you can develop it all on Ubuntu (or any Linux distro for that matter) and fully embrace Free and Open Source Software (FOSS) in the process.
For someone new to the scene, however, setting your system up so that you have the right toolchain in place can be a bit daunting. The same applies to those who have been away from the scene for a while and are a bit rusty. This post is for you.
What follows is a step-by-step guide for installing everything you need onto a regular Ubuntu system so that you can start developing OpenGL applications with Perl.
I can verify that this process works on Ubuntu 16.04 (which includes Perl 5.22).
Let’s get cracking…
Mesa implements a translation layer between a graphics API such as OpenGL and the graphics hardware drivers in the operating system kernel.
$ sudo apt-get install build-essential libgl1-mesa-dev
At least one of these — GLEW, SDL2, GLM, FreeType — will come in handy down the line, so may as well install them now.
$ sudo apt-get install libglew-dev libsdl2-dev libsdl2-image-dev libglm-dev libfreetype6-dev
cpanminus is a standalone, minimalist, dependency-free, zero-config script to get, unpack, build and install modules from CPAN.
$ sudo cpan App:cpanminus
OpenGL::Modern provides Perl bindings to the OpenGL graphics APIs using the OpenGL Extension Wrangler (GLEW) library.
$ sudo cpanm OpenGL::Modern
Note: All of the remaining steps can be completed with a single, convoluted, one-line command. That command will be given at the end. You’ll need to verify and update it though, so keep reading.
The Graphics Library Framework (GLFW) is a lightweight utility library that gives programmers the ability to create and manage windows and OpenGL contexts, as well as handle joystick, keyboard and mouse input. Hover over the “Source Package” link on the GLFW download site and make sure that the version number and URI are up-to-date before executing the following commands.
$ wget https://github.com/glfw/glfw/releases/download/3.2.1/glfw-3.2.1.zip
Unpack the archive and enter the directory that gets created.
$ unzip glfw-3.2.1.zip $ cd glfw-3.2.1
The GLFW author uses CMake because it makes his job easier, so we need to install that and some other packages to proceed.
$ sudo apt-get install cmake xorg-dev libglu1-mesa-dev
Use CMake and make to build and install the GLFW.
$ sudo cmake -G "Unix Makefiles" $ sudo make $ sudo make install
That’s it!
Now, you’ll notice that you have to type in the version number a few different times. We can set the number in a variable and then reduce the last 7 steps into a one-liner. Here it is for your copying and pasting pleasure (but remember to check the version and URI first):
version="3.2.1" && wget "https://github.com/glfw/glfw/releases/download/${version}/glfw-${version}.zip" && unzip glfw-${version}.zip && cd glfw-${version} && sudo apt-get install cmake xorg-dev libglu1-mesa-dev && sudo cmake -G "Unix Makefiles" && sudo make && sudo make install
Assuming everything went according to plan, right towards the end a small window titled “Simple Example” should have popped up briefly on your screen with a rotating OpenGL triangle in it. That’s the sign that everything is working.
/cheer
Finally, the purpose of this post is not to try teach you how to program in Perl or OpenGL. There are millions of other web pages out there that can help you do that. I personally, however, appreciate having a simple little skeleton of a program to get me started. Figuring that you might appreciate one as well, I prepared one for you.
#!/usr/bin/perl use OpenGL::GLFW qw(:all); use OpenGL::Modern qw(:all); my $errorCallback = sub { my ( $error, $description ) = @_; print STDERR "Error $error: $description\n"; }; glfwSetErrorCallback( $errorCallback ); glfwInit() or die "glfwInit failed, $!\n"; my $window = glfwCreateWindow(960,540,"Blank Slate",NULL,NULL); unless ( defined $window ) { glfwTerminate(); die "glfwCreateWindow failed, $!\n"; } glfwMakeContextCurrent( $window ); my $keyCallback = sub { my ( $window, $key, $scancode, $action, $mods ) = @_; if ( $key == GLFW_KEY_ESCAPE && $action == GLFW_PRESS ) { glfwSetWindowShouldClose( $window, GLFW_TRUE ); } }; glfwSetKeyCallback( $window, $keyCallback ); glewInit() == GLEW_OK or die "glewInit failed, $!\n"; glfwSwapInterval( 1 ); glClearColor( 0, 0.5, 0, 1); while ( !glfwWindowShouldClose( $window ) ) { glClear( GL_COLOR_BUFFER_BIT ); # draw stuff here glfwSwapBuffers( $window ); glfwPollEvents(); } glfwDestroyWindow( $window ); glfwTerminate();
Copy and paste it into a file using your favourite editor, save and close it, chmod+x it and then run it.
All that the program basically does is:
- set an error handler so that if anything goes wrong you get some feedback
- initialise a bunch of stuff
- create a window that can be moved, resized, maximised and minimised
- bind a callback to the window to trap key presses
- enter a loop that just continuously clears the (double-buffered) window until either the ESC key is pressed or the window is closed
- clean up
It took me about nine hours to go from zero to having a basic working program. There seemed to be no “Perl and OpenGL on Ubuntu” quick start guide on the Web for complete noobs like me. Working out the steps required was a frustrating process. Hopefully this guide will make your life a little easier.
Enjoy!
PS: Getting up to speed would have been a lot harder without the help of Chris Marshall on the Perl OpenGL mailing list on SourceForge, and the great folks on StackOverflow. Cheers!
Cool it works 🙂
GLFW is now 3.3.8
Thanks 🙂