Michał Kazimierz Kowalczyk


Necessitas: using OpenGL ES 2.0

Once, I had to work with a QGLWidget. I needed to display something on my tabet with a high speed. To understand how to obtain hardware acceleration I thought that probably I have to use OpenGL with which I wasn't familiarised.

Necessitas is supporting OpenGL (as announced Bogdan Vatra here). But we need to make it more specific. Necessitas currently (vesion 0.3.4) supports OpenGL ES 2.0. This fact really narrows all the knowledge: articles, tutorials, examples about OpenGL that you can find on Internet. Someone found the way of using OpenGL ES 1.0 (more information here) but he claims that is not the most optimal solution. That's why I decided to use OpenGL ES 2.0.

After long time of searching of good example how to use OpenGL ES 2.0 in Qt projects I found this example. To make it easier for you, I provide it in Necessitas project. I have to admit that in my opinion it is not the best example to learn anything but still it's good presentation of technology.

The problem with this example (besides that its quite complicated) is that is not fully compatible with Necessitas. I can't say that it doesn't work perfectly everywhere but in my case there were some problems. The biggest one was displaying user interface elements with rendered image at the same time. They are just invisible.

After reading lot of articles to understand how does it work, I created my own, light example. You can see that this is typical Necessitas project, previous one was created just for Qt.

At the form you can find QDial and QWidget promoted to QGLCanvas. In MainWindow's constructor you can find that signal valueChanged(int) of QDial will be send to setAngle(int) - slot of QGLCanvas. And that's the end of non-GL things.

My goal is not making tutorial about OpenGL ES 2.0 but give you some knowledge to start with it. So, let's start with important notions:

Vertices are describing how our 3D object is created. In general, each vertex is a triple (coordinates: x, y, z). Sequences of vertices are creating an object. In OpenGL ES 2.0 we are limited to use maximally 3 vertices per plane (so we can create triangles but we cannot create squares). Read more about vertices.

In our case, we create a plane of shape of rectangle. We need 2 triangles, each of them consists of 3 vertices and each vertex contains 3 coordinates, so we need 18 coordinates to describe it.

Next thing are texture coordinates. They say us how object will be textured. Read more...

In some situations, we need normals (normal vectors). They are special vectors which are perpendicular to planes. Read more...

Now, let's take a look on shaders. Shaders are special programs that are used for doing calculations for rendering processes. Read more...

In case of OpenGL ES 2.0, there exists special shading language. You can find its specification here.

And, the last but not least, uniforms. Uniforms are special objects which are used to communicate your OpenGL application and shader programs. Read more...

In our case, an uniform is for example QMatrix4x4 object. We use it as a parameter for a shader. By a matrix transformations we rotate rendered object.

How typical QGLWidget is made? The most important methods that we are using are initializeGL () and paintGL (). As you can suppose, in first one we do all the things that we need to do before we draw anything (for instance we define shaders). In second one we describe what and how we will draw.

Each OpenGL statement used for drawing should be placed between beginNativePainting () and endNativePainting () - methods of QPainter.

Before drawing the rectangle, we call:


In this way we clear previous rectangle that we drawed.

To execute paintGL () method we need to call:

updateGL ();

You will find quite important this line in constructor:

setAutoBufferSwap (false);

Without it display is blinking.

For now it's all. I hope it's sufficient to understand my example. If you have some questions, comment this article or contact me. If you would found some better sources of knowledge about notions that I presented here, please, share it.


--- update ---

Claude LRV found that to use OpenGL at your project, you need update Package Configuration settings. You need check QtOpenGL library at Project -> Run Settings -> Package Configuration -> Libraries. What is interesting in my case it happens automatically all the time (I'm using Necessitas for MacOS and I guess this is the reason). To learn more, see comments.


Hosted on eKK.pl.