📜 ⬆️ ⬇️

Qt + OpenGl Basics. Part 1

This article is introductory, designed for familiarity with Qt + OpenGL for beginners who plan to learn Qt (as a cross-platform software development toolkit in the C ++ programming language) + OpenGL (as a graphic library).

What a newbie needs:
1) Qt Creator (has good on-line documentation and hints while typing). Skchaet
2) doc.qt.nokia.com - official documentation in English
3) doc.crossplatform.ru - documentation in Russian
4) Be sure to read about Qt and OpenGL
5) Excellent article to start exploring.

What do we do
Since this article focuses specifically on the basics, our task will be the following:
1) Disassemble how the application is created
2) How to draw objects
3) How to work with the mouse pointer and events (pressing the keys on the keyboard and mouse)
4) Work with a timer
5) Let's create our first banal game. We will use the timer to randomly move the square. After hovering over the square of the pointer and clicking on it with the left mouse button, in the case of hitting the square, we will add +1 to the points.
')


Create a project

When you open Qt Creator, we start creating a new project.
Choosing a Qt Project Widget -> GUI Qt Application
The Class Information section unchecks to create a form.
As a result of actions we will get a project with files:
opengl.pro - required to compile our project
mainwindow.h - to declare all global data
main.cpp
mainwindow.cpp - methods of our program

Connecting libraries

In the * .pro file of your project in the Qt + = line, you need to add opengl in order to enable the use of the opengl library. Other libraries are connected in the same way.

In the mainwindow.h file - if your name is selected by default, you need to connect:
#include <QGLWidget> #include <QtOpenGL> #include <QTimer> 


Predestination for us the necessary methods and variables

Open mainwindow.h
First we change:
class MainWindow: public QMainWindow
on
class MainWindow: public QGLWidget
This is because QMainWindow is a class for displaying a simple window, and since we will work with opengl, we will need a QGLWidget - this is a class for displaying graphics that implements the functions of the OpenGL library.

Now we will predetermine variables and methods.
 protected: int geese_size; //   int point; //   int gdx, gdy; //   int cax, cay, cbx, cby; //   (  (   )   ) int wax ,way; //     bool singling; //   ,  true      cax, cay, cbx, cby void self_cursor(); //      void initializeGL(); //    opengl void resizeGL(int nWidth, int nHeight); //        void paintGL(); //       void keyPressEvent(QKeyEvent *ke); //       void mouseMoveEvent(QMouseEvent *me); //     ,    setMouseTracking(false) void mousePressEvent(QMouseEvent *me); //      void mouseReleaseEvent(QMouseEvent *me); //    ""   void singling_lb(); //     void geese(); //         


We also have one slot in order to recalculate the new coordinates of the square on which to click on the timer.

 protected slots: void geese_coord(); //    


Image Building Principle

QGLWidget is so arranged that when the class is first initialized, it automatically calls methods in the following order:
At startup: initializeGL () -> resizeGL () -> paintGL ()
When resizing a window: resizeGL () -> paintGL ()
updateGL () calls paintGL ()

initializeGL - must be used for global image building, which is not necessary to specify when building a frame.
resizeGL - used to build the size of the window. If during the work the window size is changed, but not to change the viewing area, then when you increase the size, you can observe unpredictable phenomena.
paintGL - this method will build our every frame for display.
 glClear(GL_COLOR_BUFFER_BIT); //   glMatrixMode(GL_PROJECTION); //   glLoadIdentity(); //   glOrtho(0,500,500,0,1,0); //     // BlendFunc     ,       // glEnable(GL_BLEND); // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); qglColor(Qt::white); //     // renderText     ,       ( QFont) renderText(10, 10 , 0, QString::fromUtf8("  %1 :").arg(17), QFont() , 2000); // glBegin  glEnd -     (  ), glBegin   ,   . glBegin(GL_POLYGON); glColor4f(0,1,0, 0.25);//    glVertex2f(200, 300); //  1  4 -     glVertex2f(300, 300); glVertex2f(300, 400); glVertex2f(200, 400); glEnd(); swapBuffers(); 


Why double buffering

PaintGL does not immediately draw the picture on the screen, but enters it into the buffer, and on request swapBuffers () replaces the current image with what appears in the buffer. By itself, buffering allows you to more correctly replace the image so that there are no jumps on the screen.

Mouse Click Events

mousePressEvent () - the method is automatically called when you press the mouse buttons. In the transmitted parameters, you can get various information, for example, which button was pressed and which point by coordinates.
-This event in our example is used to determine where the mouse was clicked, then if our coordinates are in the square field, then add +1 to our points and rebuild our frame.
- Also, we use to determine the initial coordinates to highlight the area on the screen, while clamping and moving the pointer.

Mouse Move Event

mouseMoveEvent () - automatically called when you change the coordinates of the mouse pointer. But there is one. But, the default is setMouseTracking (false), so the event is triggered only when the mouse is pressed, in order for the method to be called even without clicking, you must set setMouseTracking (true).
- We use this method to get the current position of the pointer in order to restructure the selection of the area or draw our own cursor.

The event when the "mouse button" is pressed

mouseReleaseEvent () is automatically called if the mouse button is “unwound”. It also takes various parameters.
- In this case, we use the method to erase the selected area from the screen.

Event keystroke on the keyboard

keyPressEvent () - the method is called at the event when the button on the keyboard is pressed.
- In our example, we use this method in order to redefine the coordinates of our square and move it to a new place.

Timer

QTimer - allows us to create a stream that will listen to signals and run the corresponding slots.
- In this case, we create a timer that will wait for 750ms after which it finishes its work by sending us a timeout () signal, but at the end of the signal we will not stop working, but will again start the slot to redefine the coordinates of the square that we need to click for order to score points.
 QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(geese_coord())); timer->start(750); 


Assignment on this material for learning.

The code I have laid out works, but my homework will modify the code so that when you start the game there is a greeting, when you click on it, you are given a minute to receive points. After a minute, the number of points scored was displayed and it was offered to play again.

Conclusion!

Most of the little written here in our first primitive game is simply not necessary. But I want to note again: "The article is introductory, designed for familiarity with Qt + OpenGL ". Also, if you notice programs written in this way, you can compile for any operating environment.

The finished version of the working code can be found here:

View and download the source
Download game for windows
Download game for Linux

PS In the future, if not against it, I will continue to write the lessons. For example, in the following lesson, we will create a game with shooting for example ducks.

If you have questions - write in the comments or in a personal.

Source: https://habr.com/ru/post/132674/


All Articles