How it works...

We must set the OpenGL version to 3.x and the surface format to core profile so that we can access the newer shader pipeline, which is completely different from the older, deprecated compatibility profile. OpenGL 2.x still exists in the compatibility profile, solely for the sake of allowing OpenGL programs to run on old hardware. The profile that's created must be applied to the OpenGL context before it will work.

In OpenGL 3 and the later versions, most of the calculations are done in the GPU through shader programs, since all of the common fixed functions have now been completely deprecated. Therefore, we created a very simple vertex shader and fragment shader in the preceding example.

A shader program consists of three different parts: geometry shader (optional), vertex shader, and fragment shader. The geometry shader calculates the creation of geometry before passing the data to vertex shader; the vertex shader handles the position and motion of the vertices before passing the data to the fragment shader; and finally, the fragment shader calculates and displays the resulting pixels on screen.

In the preceding example, we only used vertex and fragment shaders, and excluded the geometry shader since it is optional. You can save the GLSL code in a text file and load it in to your Qt 5 program by calling addShaderFromFile(), but since our shaders are very simple and short, we just define it directly in our C++ source code.

After that, we use something called the Vertex Buffer Object (VBO) to store the vertex positions in bulk before sending it to the GPU. We can also use the VBO to store other information such as normals, texture coordinates, and vertex colors. You can send anything you want to the GPU as long as it matches the input inside your shader code. Then, we add the VBO into a Vertex Array Object (VAO) and send the whole VAO to the GPU for processing. You can add many different VBOs into the VAO, since the VAO is just like any ordinary C++ array.

Just like what we have learned in the previous chapters, all drawings happen within the paintEvent() function, and it will only be called by Qt when it thinks it is necessary to refresh the screen. To force Qt to update the screen, call update() manually. Also, we must update the viewport whenever the window screen has been resized, by calling glViewport(x, y ,width, height).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset