File naming convention for GLSL shaders

Vertex, fragment and geometry shader programs can be written in GLSL. These are usually written in separate files. Any filename and extension can be used for GLSL shader files since the language standard does not enforce anything.

I found it convenient to name the shader programs as foo.vs.glsl, foo.fs.glsl and foo.gs.glsl. I found this convention used in the source code from the book The OpenGL Superbible. The .glsl is recognized by many editors and GLSL syntax highlighting is automatically applied when you open those files.

About these ads

How to convert PDF to image

You may sometimes want to convert a PDF file into an image format like PNG or JPG. Doing this is easy using the convert application from ImageMagick.

If you do not have it, first install ImageMagick:

$ sudo apt install imagemagick

To convert a PDF with a single page into a JPG:

$ convert foo.pdf foo.jpg
$ ls
foo.pdf
foo.jpg

If the PDF has multiple pages, one image file is produced per page:

$ convert foo.pdf foo.png
$ ls
foo.pdf
foo-1.png
foo-2.png
foo-3.png

By default, the image file is produced at 96 DPI resolution of the PDF. If you need higher DPI, use the density option. For example, to generate at 300 DPI:

$ convert -density 300 foo.pdf foo.png

Tried with: ImageMagick 6.7.7-10 and Ubuntu 14.04

wxWidgets Examples

Starting off with a GUI library can be quite daunting. Lots of new code and classes need to be written to get even the most basic program working. I have found that the best way to get started with wxWidgets is to look at its examples. Pick the example that implements most of what you need and start off from there.

Doing this is easy:

  • Install the wxWidgets examples:
$ sudo apt-get install wx3.0-examples
  • The examples are zipped up and need to be uncompressed before using them:
$ cd /usr/share/doc/wx3.0-examples/examples
$ ./unpack_examples.sh ~/wx-examples
  • You can build all the examples in one command:
$ cd ~/wx-examples/samples
$ make
  • Or you can go to a specific directory and build a specific example:
$ cd ~/wx-examples/samples/opengl/penguin
$ make

Tried with: wxWidgets 3.0 and Ubuntu 14.04

How to get mouse position in wxWidgets

To get mouse position relative to top-left corner of display, add this in a mouse event handler:

const wxPoint pt = wxGetMousePosition();
int mouseX = pt.x;
int mouseY = pt.y;

To get mouse position relative to top-left corner of current window or canvas, add this in a mouse event handler:

const wxPoint pt = wxGetMousePosition();
int mouseX = pt.x - this->GetScreenPosition().x;
int mouseY = pt.y - this->GetScreenPosition().y;

Tried with: wxWidgets 3.0 and Ubuntu 14.04

Segmentation fault at glCreateShader in wxWidgets

Problem

I had a simple graphics program written using OpenGL and using GLUT for windowing. To get more windowing functionality, I wanted to move it to wxWidgets. I picked an example program that uses OpenGL in wxWidgets. It compiled and worked correctly. When I placed my OpenGL code into this example, I got a segmentation fault at the glCreateShader call in my code.

Solution

I had a glewInit in my original code. I had put this in the application class constructor. Apparently, wxWidgets requires that the first OpenGL or GLEW call be placed inside the OnPaint event handler function. Look for EVT_PAINT to find this function. Once I did that, the error disappeared and OpenGL worked.