In this section, we will be looking at how to move an object in OpenGL using keyboard controls. Qt provides an easy way to detect keyboard events using virtual functions, namely keyPressEvent() and keyReleaseEvent(). We will be using the previous example and adding to it.
Moving an object using keyboard controls
How to do it...
To move an object using keyboard controls, follow these steps:
- Open up renderwindow.h and declare two floating-point numbers called moveX and moveZ. Then, declare a QVector3D variable called movement:
QTime* time;
int currentTime = 0;
int oldTime = 0;
float deltaTime = 0;
float rotation = 0;
float moveX = 0;
float moveZ = 0;
QVector3D movement = QVector3D(0, 0, 0);
- We will also declare two functions called...