• 12
• 10
• 10
• 13
• 10

Why my viewpoint does not change?

This topic is 2809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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?

Share on other sites
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.

Share on other sites
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.

Share on other sites
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.

Share on other sites
The if(key=='w') statement is good, anyway; tested with exit(0) :D