Drawing with Cairo on an OpenGL texture

I’ve been looking at the experimental OpenVG backend for Cairo using ShivaVG. One thing I wanted to try was using Cairo to directly generate OpenGL textures without having to use any copying, but this has proven more difficult than I first thought:

  • ShivaVG does not yet support EGL, the platform independent library that the Khronos group pitches to replace GLUT, WGL and other libraries that connect OpenGL to the underlying platform. EGL has support for creating independent framebuffers and rendering to them, while GLUT has no provision for this.
  • ShivaVG initializes itself on a rendering context and uses it all the time, making it difficult to use on different graphics contexts.

Finally, using the framebuffer object extension I’ve been able to make a simple demo of a rotating cube with the famous Cairo Clock as its texture:

OpenGL cube with a Cairo texture.

At first the drawing was completely skewed, until I remembered that OpenGL’s state does not change when the rendering context does. So, for each frame, you have to set up the rendering state to the one ShivaVG needs to draw, and then change it back. It’d be cool if ShivaVG provided functionality to do this, but the code needed to set up the correct rendering environment is not so complex anyway:

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();  // To keep correct perspective for 3D scene
	glLoadIdentity();
	gluOrtho2D(0,width,0,height);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

The demo code is based on an article by Rob “phantom” Jones on GameDev, and it’s a complete hack that should only be used for demo purposes. In fact, it’ll crash and burn a lot if your OpenGL implementation does not support framebufffer objects. You can find the complete code here, cairo_openvg_cube.c.

Leave a Reply

Your email address will not be published. Required fields are marked *