Search code examples
c++openglvisual-c++glutfreeglut

Zooming Issues with glutMouseWheelFunc


I tired implementing a simple zoom in/out using the glutMouseWheelFunc in VC++. I am able to achieve the zooming in/out but while doing so, the axes(GLlines) tend to disappear after zooming more than a certain level.

Am using the glutMouseWheelFunc to increase/decrease z- axis value in glTranslatef.

I have defined 'z' of glTranslatef for as camera distance:

float cDist= 30.0f; //  camera distance 

Then used it in glTranslatef as,

glTranslatef(0.0f, 0.0f, -cDist); 

in display function below.

void display(void) {
    glClearColor(0.0, 0.0, 0.0, 1.0); //clear the screen to black
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//clear the color buffer and the depth buffer
    enable();
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -cDist);
    glRotatef(xrot, 1.0, 0.0, 0.0);
    ---------------
    glBegin(GL_LINES);

    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(-500, 0, 0);
    glVertex3f(500, 0, 0);

    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(0, -500, 0);
    glVertex3f(0, 500, 0);

    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(0, 0, -500);
    glVertex3f(0, 0, 500);

    glTranslated(-xpos, 0.0f, -zpos); //translate the screento the position of our camera
    glColor3f(1.0f, 1.0f, 1.0f);
    glutSwapBuffers(); 
    }

Afterwards, I defined wheelfunc as,

void mouseWheel(int button, int dir, int x, int y)
{
    if (dir > 0)
    {
        cDist = cDist++;
    }
    else
    {
        cDist= cDist--;
    }

    return;
}

and called it in main function with glutMouseWheelFunc(mouseWheel);

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Window");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutReshapeFunc(reshape);
    glutMotionFunc(mouseMovement); 
    glutMouseWheelFunc(mouseWheel);---- here
    -----
    ---
    glutMainLoop();
    return 0;
}

Is this approach of zooming in proper? if not, what could be the alternative ? Also how can I define axes(lines I drew) to full extent of the screen?


Solution

  • thanks for the help. Seems like I missed up setting up proper depth value for gluPerspective. Once I increased the depth value, zoom out/in was working fine