0

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)

U23r
  • 1,653
  • 10
  • 28
  • 44
  • Please, have a look at [gluLookAt()](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluLookAt.xml). This function multiplies a matrix to your `GL_MODELVIEW` stack to define view (transformation). You could make these 3 vectors variables which you modify in signal handlers for keyboard. E.g. if you intend to move into x-direction just add a value to `eyeX` and `centerX`. If you intend to turn just rotate center vector about up axis at eye origin. A `QTimer` might be necessary also to do this periodically from key-down to key-up. – Scheff's Cat May 07 '18 at 09:49
  • ...This requires some basic understanding of linear [vector algebra](https://en.wikipedia.org/wiki/Vector_algebra) or at least experience with `sin()` and `cos()`. How to explain this to a _5yo_? – Scheff's Cat May 07 '18 at 09:51
  • I just tried google "opengl navigation" and found a variety of hits with tutorials, forum Q/As, and even a link to the Red Book chapter 3. So, this may worth a research on your own. – Scheff's Cat May 07 '18 at 09:57
  • see [Understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) and then the last 3 links in there should be interesting for you – Spektre May 07 '18 at 10:00

0 Answers0