I made this project on Qt and I have to use OpenGL to model a building. I have this code bellow that open a window and draw a cube on the screen. I Would like to know how can I walk through this scene using the keyboard? For example, I press right arrow and the "camera" will point to right direction, left arrow for left direction, up arrow will "get in" (-z plan) and down arrow will "come back" (z plan).
Can anyone explain to me how this works? Some tutorial for this? Any help? I was trying to use it glut lib, but if Qt have some simple function to do this, please, let me know.
glwidget.cpp:
#include "glwidget.h"
#include <GLUT/glut.h>
GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) {
}
void GLWidget::initializeGL() {
glClearColor(0.2, 0.3, 0.4, 0.1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
}
void GLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutWireCube(0.6);
}
void GLWidget::resizeGL(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (float)w/h, 0.01, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,5, 0,0,0, 0,1,0);
}
main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include <GLUT/glut.h>
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
There are any easy way to do this? This is my first time using these tools. (Explain like I'm a 5yo hehe)