简体   繁体   中英

iPhone OpenGL : Moving the view back from an object

A bit of a weird one here. I have a 3d cube that rotates on the screen but I had to change the view from glOrthof to glFrustumf. Now the cube is place on me and not a in front of me.

So I thought I use a glTranslate (or even scale) to move it back so I can properly so it but the object continues redraws from its old position and adds a bit to it so by moving, the rotation goes Pete Tong. I never used used glGetFloatv and glMultMatrixf before but I guessing they take the old rotation and add a bit to it to keep the object moving. Just wish I could step back from it

Cube code

 GLfloat matrix[16]; 
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);

    glLoadIdentity(); 
    glRotatef(self.angle, self.dy,self.dx,0);

    /* reapply other rotations so far */
    glMultMatrixf(matrix); 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor4f(1.0, 1.0, 1.0, 1.0);

    glVertexPointer(3, GL_FLOAT, 0, texturedVertices);
    glTexCoordPointer(2, GL_FLOAT, 0, texturedCubeCoord);

    glBindTexture(GL_TEXTURE_2D, 1);
    glDrawElements(GL_TRIANGLES,6 , GL_UNSIGNED_BYTE, &texturedCube[0]);

    glBindTexture(GL_TEXTURE_2D, 2);
    glDrawElements(GL_TRIANGLES,6 , GL_UNSIGNED_BYTE, &texturedCube[6]);

    glBindTexture(GL_TEXTURE_2D, 1);
    glDrawElements(GL_TRIANGLES,6 , GL_UNSIGNED_BYTE, &texturedCube[12]);

    glBindTexture(GL_TEXTURE_2D, 2);
    glDrawElements(GL_TRIANGLES,6 , GL_UNSIGNED_BYTE, &texturedCube[18]);

    glBindTexture(GL_TEXTURE_2D, 1);
    glDrawElements(GL_TRIANGLES,6 , GL_UNSIGNED_BYTE, &texturedCube[24]);

    glBindTexture(GL_TEXTURE_2D, 2);
    glDrawElements(GL_TRIANGLES,6 , GL_UNSIGNED_BYTE, &texturedCube[30]);

I fixed it by a 1 time only move of -5 then reset the matrix

/* save current rotation state */
    GLfloat matrix[16]; 
    glGetFloatv(GL_MODELVIEW_MATRIX, matrix);

    /* re-center cube, apply new rotation */
    glLoadIdentity(); 

    if (firstRunOnly == NO) {
        firstRunOnly = YES;
        glTranslatef(0, 0, -5);
    } else {
        glTranslatef(0, 0, 0);
    }

    /* reapply other rotations so far */
    glMultMatrixf(matrix); 

    glRotatef(self.angle, self.dy,self.dx,0);

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