Why my viewpoint does not change?

Started by
4 comments, last by AmidaBucu 13 years, 9 months ago
void keyboardFunc(unsigned char key, int x, int y){    if (key=='w')    {        ypos-=50;    }    if (key==27)    {        exit(0);    }}void reshapeFunc(int x, int y){    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    gluPerspective(45, width/height, 1.0, 1000.0);    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();    gluLookAt (xpos, ypos, zpos,                  0, 0, 0,                  0, 1, 0);}void displayFunc(){    glClearColor(0,0,1,1);    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    gluLookAt (xpos, ypos, zpos,                  0, 0, 0,                  0, 1, 0);    glBegin(GL_TRIANGLES);     glColor3f(1,1,1);    for(unsigned int i = 0; i < myMesh.nVertices*3;i+=3)    {        glVertex3f(myMesh.vertices[i+0],                   myMesh.vertices[i+1],                   myMesh.vertices[i+2]);    }    glEnd();    glutSwapBuffers();}void load(){    //load a mesh}int main(int argc, char *argv[]){    glutInit(&argc, argv);    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);    glutInitWindowSize(width, height);    glutInitWindowPosition(0,0);    glutCreateWindow("openGL");    glutKeyboardFunc(keyboardFunc);    glutReshapeFunc(reshapeFunc);    glutDisplayFunc(displayFunc);    load();   glutMainLoop();}


If I manually change the ypos, then the viewpoint will change,
but if I press 'w', then nothing happens. Where I am wrong?
Advertisement
Looks like 'w' is not what gets sent to the keyboardFunc. Set a breakpoint (or output some debug message) in that function and look at the value for 'key' when you press W. Use that value in your if(key==..) statement.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

An important thing: gluLookAt calculates a matrix then multiplies the CURRENT modelview matrix with it. So the way you use it now won't work as you expect.
Call glLoadIdentity before gluLookAt in displayFunc too.

And try with capital W instead of w.
The display callback isn't called until it has to, which is either when the window needs to be refreshed or when you explicitly ask for it. Call glutPostRedisplay whenever you want to redraw the window, like after pressing the key.
Thanks for the answers :)
The if(key=='w') statement is good, anyway; tested with exit(0) :D
Thanks all, i have successfully managed to change the camera due to you. THX :D

This topic is closed to new replies.

Advertisement