简体   繁体   English

iPhone OpenGL:将视图从对象移回

[英]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. 我有一个在屏幕上旋转的3d立方体,但是我不得不将视图从glOrthof更改为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. 所以我以为我可以使用glTranslate(甚至缩放)将其移回原处,因此我可以正确地将其移回原处,但是对象继续从其旧位置重绘并对其进行了添加,因此通过移动,旋转变为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. 我以前从未使用过glGetFloatv和glMultMatrixf,但是我猜他们采用了旧的旋转方式,并对其进行了添加以保持对象移动。 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 我通过-5的1次移动来固定它,然后重置矩阵

/* 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM