简体   繁体   中英

Strange perspective issue in OpenGL

So i'm trying to zoom in and out of a sphere drawn at (0,0,0) with radius of 0.6, except once i reach Z of about -1.6, it and the axis i draw for orientation have vanished and i'm not sure why.

Here's my code for translation :

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glRotatef(rotX,1.0f,0.0f,0.0f);
    glRotatef(-rotY,0.0f,1.0f,0.0f);
    help2.setText("xShift : " +xShift+ " yShift : " +yShift+ " zShift : " + zShift);
    glTranslatef(xShift,yShift,zShift);`

Here it is for drawing :

static Sphere temp = new Sphere();
temp.draw(0.6f, 100, 100);

And here is my pre-gl setup stuff :

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(50f, 1.0f, 0.01f, 1000f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LINE_SMOOTH);
    glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glRotatef(rotX,1.0f,0.0f,0.0f);
...

Why are you using the projection matrix here? The projection matrix shouldn't be changed in this way. This code wipes out the work you did with gluPerspective() and replaces it with the identity transform.

You should almost certainly be using the GL_MODELVIEW matrix here instead. Once you've set the perspective you have no reason to ever set the matrix mode to GL_PROJECTION again unless you want to modify the aspect (if you have a resize window event), the FOV, or the near or far planes.

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

By leaving this at the identity transform you're setting the camera at the origin looking down the negative Z axis. Presumably your sphere is drawing at the origin as well so you're seeing the inside of it. Try setting the initial camera position to something different, perhaps using gluLookAt()

gluLookat(0, 0, 3, 0, 0, 0, 0, 1, 0);

This puts you 3 units from the sphere on the Z axis looking at the origin. If your sphere is drawing with a 0.6 radius then it should be visible.

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