Domanda

I'm trying to port my height map visualization program written on c++, from SFML to Qt, so it can be shown on widget and controlled by the GUI elements.

The problem is that when I start an application, a camera starts to roll around its center very fast(actually, it looks like a terrain mesh flying around a camera, like an Earth around the Sun :), without any actions from my side(e.g moving mouse, pressing buttons).

Camera should move forward, back, left, right when I press w,a,s,d and look around when I move the mouse(Just typical FPS camera behavior).

I think that problem are in the program's main loop, because it's no standard while(true){ //do something// } approach in qt, and it's a little confusing.

Here's my code:

OGLWidget class(here I'm drawing stuff. Problem somewhere here I think) :

class OGLWidget :
    public QGLWidget
{
    Q_OBJECT

public:
    OGLWidget(QWidget *parent = 0);
    ~OGLWidget(void);

public:
    void paintGL();
    void initializeGL();
    void resizeGL();

public:
    void updateCamera();

public slots:
    void mainLoop();

protected:
    void keyPressEvent(QKeyEvent *e);
    void keyReleaseEvent(QKeyEvent *e);

private:
    Terrain _terrain;
    Camera  _camera;

private:
    int           _keyPressed;
    QTimer        _timer;
    QElapsedTimer _elapsedTimer;
    float         _simulationTime;
    float         _fps;
};


OGLWidget::OGLWidget(QWidget *parent)  : QGLWidget(parent)
{
    _terrain.loadHeightMap("normalHeightMap256_2.png");

    _camera.setScreenDimension(this->width(), this->height());

    //setting vertical sync
    QGLFormat frmt;
    frmt.setSwapInterval(1);
    setFormat(frmt);

    setMouseTracking(true);

    setFocus();

    _simulationTime = 0;

    _fps = 1.f / 60.f;

    connect(&_timer, SIGNAL(timeout()), this, SLOT(mainLoop()));
    _timer.start();

    _elapsedTimer.start();
}


OGLWidget::~OGLWidget(void)
{
}

void OGLWidget::mainLoop()
{
    _simulationTime += _elapsedTimer.elapsed();
    _elapsedTimer.restart();

    while(_simulationTime > _fps)
    {
        _simulationTime -= _fps;
        updateCamera();
    }

    updateGL();
}

void OGLWidget::updateCamera()
{
    QPoint p = mapFromGlobal(QCursor::pos());

    _camera.computeMatrices(p.x(), p.y(), _fps, _keyPressed);

    glm::mat4 ViewMatrix = _camera.getViewMatrix();
    glm::mat4 ProjectionMatrix = _camera.getProjectionMatrix();
    glm::mat4 ModelMatrix = glm::mat4(1.0);

    _terrain.setMvp(ProjectionMatrix * ViewMatrix * ModelMatrix);

    QPoint center = mapToGlobal(QPoint(this->width() / 2, this->height() / 2));
    QCursor::setPos(center);
}

void OGLWidget::initializeGL()
{
    glewExperimental = GL_TRUE;

    if (glewInit() != GLEW_OK) 
    {
        return;
    }

    glViewport(0, 0, this->width(), this->height());

   _terrain.init();
}

void OGLWidget::paintGL()
{
    _terrain.draw();
}

void OGLWidget::resizeGL()
{
    glViewport(0, 0, this->width(), this->height());
}

void OGLWidget::keyPressEvent(QKeyEvent *e)
{
    switch(e->key())
    {
    case Qt::Key::Key_Escape:
        exit(0);
        break;

    case Qt::Key::Key_W:
        _keyPressed = Key::KEY_PRESSED_UP;
        break;

    case Qt::Key::Key_S:
        _keyPressed = Key::KEY_PRESSED_DOWN;
        break;

    case Qt::Key::Key_A:
        _keyPressed = Key::KEY_PRESSED_LEFT;
        break;

    case Qt::Key::Key_D:
        _keyPressed = Key::KEY_PRESSED_RIGHT;
        break;
    }
}

void OGLWidget::keyReleaseEvent(QKeyEvent *e)
{
    if(e->key() == Qt::Key::Key_W ||
       e->key() == Qt::Key::Key_S ||
       e->key() == Qt::Key::Key_A ||
       e->key() == Qt::Key::Key_D)
       _keyPressed = KEY_RELEASED;
}

I'm absolutely sure that Terrain and Camera classes are working correct, because I haven't changed code since my SFML project(Except of using QImage instead of sf::Image, but it's working correct too)

*Camera main algorithm: *

void Camera::computeMatrices(int mouseXpos, int mouseYpos, float deltaTime, int keyPressed)
{
    _horizontalAngle += _mouseSpeed * deltaTime * float(_screenWidth / 2 - mouseXpos);
    _verticalAngle += _mouseSpeed * deltaTime * float(_screenHeight / 2 - mouseYpos);

    _direction = glm::vec3
    (
        cos(_verticalAngle) * sin(_horizontalAngle),
        sin(_verticalAngle),
        cos(_verticalAngle) * cos(_horizontalAngle)
    );

    glm::vec3 right = glm::vec3
    (
        sin(_horizontalAngle - 3.14f/2.0f), 
        0,
        cos(_horizontalAngle - 3.14f/2.0f)
    );

    glm::vec3 up = glm::cross( right, _direction );

    switch(keyPressed)
    {
    case Key::KEY_PRESSED_UP:
        _position += _direction * deltaTime * _speed;
        break;

    case Key::KEY_PRESSED_DOWN:
        _position -= _direction * deltaTime * _speed;
        break;

    case Key::KEY_PRESSED_LEFT:
        _position -= right * deltaTime * _speed;
        break;

    case Key::KEY_PRESSED_RIGHT:
        _position += right * deltaTime * _speed;
        break;

    case Key::KEY_RELEASED:
        break;
    }

    _projectionMatrix = glm::perspective(_initialFoV, 4.0f / 3.0f, 0.1f, 1000.0f);

    _viewMatrix = glm::lookAt
    (
        _position,            // Camera is here
        _position+_direction, // and looks here : at the same position, plus "direction"
        up                    // Head is up (set to 0,-1,0 to look upside-down)
    );
}

Help me fix this issue.

È stato utile?

Soluzione

Ok, I figured out the problem with spinning camera. The cause of it was that I hardcoded an aspect ratio in Camera::computeMatrices, and used a resolution of my widget which doesn't match to it:

_projectionMatrix = glm::perspective
(
_initialFoV, 
4.0f / 3.0f, //here it is
0.1f, 
1000.0f
);

I changed 4.0f / 3.0f on (float)_screenWidth / (float)_screenHeight but it didn't help too.

So then I just changed a resolution of my widget to 800 x 600 and it helped.

The new problem is that it works only on 4/3 dimensions(e.g 800x600, 1024x768).

Altri suggerimenti

The best way to correct

_direction = glm::vec3
(
    cos(_verticalAngle) * sin(_horizontalAngle),
    sin(_verticalAngle),
    cos(_verticalAngle) * cos(_horizontalAngle)
);

_direction.normalize();

...
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top