简体   繁体   中英

Can't move the camera using keyboard commands in qt opengl

I am trying to implement a game using opengl in qt4. So far I have created the football pitch and I am now trying to implement a camera with which the user can move in the world freely using the arrow keys. My friend used a piece of code he found on NeHe's tutorials and simply copy pasted it to his code and the camera worked for him. When I tried the same only the escape button works and it just closes the opengl widget. f1 key is supposed to switch to fullscreen but it just makes the mouse cursor invisible without switching to fullscreen mode. The arrow keys don't work at all. As I'm new to opengl I could not figure out what is wrong with the implementation.

I'm adding the code where I draw the pitch and also the keyboard event handlers.

void metinalifeyyaz::paintGL(){

movePlayer();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

GLfloat xtrans = -xpos;
GLfloat ytrans = -walkbias - 0.50f;
GLfloat ztrans = -zpos;
GLfloat sceneroty = 360.0f - yrot;

glLoadIdentity();
glRotatef(lookupdown, 1.0f, 0.0f, 0.0f);
glRotatef(sceneroty, 0.0f, 1.0f, 0.0f);
glTranslatef(xtrans, ytrans+50, ztrans-130);

glLoadIdentity();
glTranslatef(1.0f,0.0f,-18.0f);
glRotatef(45,1,0,0);
drawScene();

int delay = time.msecsTo(QTime::currentTime());
if (delay == 0)
    delay = 1;
time = QTime::currentTime();
timer->start(qMax(0,10 - delay));

}


void metinalifeyyaz::movePlayer() {

if (keyUp) {
    xpos -= sin(yrot * PI_OVER_180) * 0.5f;
    zpos -= cos(yrot * PI_OVER_180) * 0.5f;
    if (walkbiasangle >= 360.0f)
        walkbiasangle = 0.0f;
    else
        walkbiasangle += 7.0f;
    walkbias = sin(walkbiasangle * PI_OVER_180) / 10.0f;
} else if (keyDown) {
    xpos += sin(yrot * PI_OVER_180)*0.5f;
    zpos += cos(yrot * PI_OVER_180)*0.5f ;
    if (walkbiasangle <= 7.0f)
        walkbiasangle = 360.0f;
    else
        walkbiasangle -= 7.0f;
    walkbias = sin(walkbiasangle * PI_OVER_180) / 10.0f;
}

if (keyLeft)
    yrot += 0.5f;
else if (keyRight)
    yrot -= 0.5f;

if (keyPageUp)
    lookupdown -= 0.5;
else if (keyPageDown)
    lookupdown += 0.5;
}


void metinalifeyyaz::keyPressEvent(QKeyEvent *event) {

switch (event->key()) {
case Qt::Key_Escape:
    close();
    break;
case Qt::Key_F1:
    setWindowState(windowState() ^ Qt::WindowFullScreen);
    break;
default:
    QGLWidget::keyPressEvent(event);
case Qt::Key_PageUp:
    keyPageUp = true;
    break;
case Qt::Key_PageDown:
    keyPageDown = true;
    break;
case Qt::Key_Left:
    keyLeft = true;
    break;
case Qt::Key_Right:
    keyRight = true;
    break;
case Qt::Key_Up:
    keyUp = true;
    break;
case Qt::Key_Down:
    keyDown = true;
    break;

}
}

void metinalifeyyaz::changeEvent(QEvent *event) {

switch (event->type()) {
case QEvent::WindowStateChange:

    if (windowState() == Qt::WindowFullScreen)
        setCursor(Qt::BlankCursor);
    else
        unsetCursor();
    break;
default:
    break;
}
}
void metinalifeyyaz::keyReleaseEvent(QKeyEvent *event) {

switch (event->key()) {
case Qt::Key_PageUp:
    keyPageUp = false;
    break;
case Qt::Key_PageDown:
    keyPageDown = false;
    break;
case Qt::Key_Left:
    keyLeft = false;
    break;
case Qt::Key_Right:
    keyRight = false;
    break;
case Qt::Key_Up:
    keyUp = false;
    break;
case Qt::Key_Down:
    keyDown = false;
    break;
default:
    QGLWidget::keyReleaseEvent(event);
}
}

I know that copy paste is not an efficient method but my friend's project is not different than mine and it works for him. If you know anything that might cause the same code to work on one project and not the other please point it out. Of course any other comment about the code are much appreciated.

Look:

glLoadIdentity();
glRotatef(lookupdown, 1.0f, 0.0f, 0.0f);
glRotatef(sceneroty, 0.0f, 1.0f, 0.0f);
glTranslatef(xtrans, ytrans+50, ztrans-130);

glLoadIdentity(); // get rid of this!
glTranslatef(1.0f,0.0f,-18.0f);
glRotatef(45,1,0,0);
drawScene();

"glLoadIdentity" resets the current matrix. In your code, you rotate and translate the matrix, but afterward, you reset the matrix by calling "glLoadIdentity", so the previous matrix transformations do nothing.

Basically this is your code:

  • OpenGL, reset the matrix.
  • OpenGL, move the player and camera.
  • OpenGL, reset the matrix, and forget anything I just did.
  • OpenGL, do... whatever that is.
  • OpenGL, draw my scene!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM