OpenGL旋转相机

时间:2012-02-17 16:50:45

标签: c++ opengl

我有一个有6个方面的立方体。现在,当其中一些方面不可见时,我不想渲染所有方面。所以我将相机的旋转传递给下面的绘图功能:

void Voxel::draw(QGLWidget *parent, GLfloat rotationX, GLfloat rotationY, GLfloat rotationZ) {

    glBegin(GL_QUADS);
    parent->qglColor(color);

    if ((rotationY > -90 && rotationY < 90) | rotationY < -270) {
        // Side top
        glVertex3f(-1.0f,  1.0f,  1.0f);  // Top Left Of The Texture and Quad
        glVertex3f( 1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
        glVertex3f( 1.0f,  1.0f,  -1.0f);  // Bottom Right Of The Texture and Quad
        glVertex3f(-1.0f,  1.0f,  -1.0f);  // Bottom Left Of The Texture and Quad
    }

    // Side bottom
    glVertex3f(-1.0f,  -1.0f,  -1.0f);  // Bottom Left Of The Texture and Quad
    glVertex3f( 1.0f,  -1.0f,  -1.0f);  // Bottom Right Of The Texture and Quad
    glVertex3f( 1.0f,  -1.0f,  1.0f);  // Top Right Of The Texture and Quad
    glVertex3f(-1.0f,  -1.0f,  1.0f);  // Top Left Of The Texture and Quad

    // Side 1
    glVertex3f(-1.0f, -1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
    glVertex3f( 1.0f, -1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
    glVertex3f( 1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
    glVertex3f(-1.0f,  1.0f,  1.0f);  // Top Left Of The Texture and Quad

    // Side 2
    glVertex3f(-1.0f,  1.0f,  -1.0f);  // Top Left Of The Texture and Quad
    glVertex3f( 1.0f,  1.0f,  -1.0f);  // Top Right Of The Texture and Quad
    glVertex3f( 1.0f, -1.0f,  -1.0f);  // Bottom Right Of The Texture and Quad
    glVertex3f(-1.0f, -1.0f,  -1.0f);  // Bottom Left Of The Texture and Quad

    // Side 3
    glVertex3f(1.0f, -1.0f,  -1.0f);  // Bottom Right Of The Texture and Quad
    glVertex3f(1.0f,  1.0f,  -1.0f);  // Top Left Of The Texture and Quad
    glVertex3f(1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
    glVertex3f(1.0f, -1.0f,   1.0f);  // Bottom Left Of The Texture and Quad

    // Side 4
    glVertex3f(-1.0f, -1.0f,   1.0f);  // Bottom Left Of The Texture and Quad
    glVertex3f(-1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
    glVertex3f(-1.0f,  1.0f,  -1.0f);  // Top Left Of The Texture and Quad
    glVertex3f(-1.0f, -1.0f,  -1.0f);  // Bottom Right Of The Texture and Quad

    glEnd();
}

但出于某种原因,这并不完全有效。特别是在绕X轴旋转时。在某些旋转点,它不会显示四边形,而应显示它。

我正在设置这样的旋转:

glRotatef(rotationX, 1.0, 0.0, 0.0);
glRotatef(rotationY, 0.0, 1.0, 0.0);

如果我需要解释更多,请询问。如果有人知道关于这些事情的一些好的文件,请说出来。

2 个答案:

答案 0 :(得分:1)

我认为你的代码中存在一个愚蠢的错误

if ((rotationY > -90 && rotationY < 90) | rotationY < -270) {

你应该使用||代替|

答案 1 :(得分:1)

这可能不是您期望的答案,但是,通常OpenGL硬件非常擅长不显示未面向相机的面部,这称为背面剔除。现代桌面硬件可以丢弃大约40亿个三角形/秒。因此,这种优化可能不是一次,也可能不值得花时间。

此外,假设您正在使用透视投影,关于立方体相对于“相机”的位置,关于立方体面的可见性与其旋转相关的假设可能不正确。

相关问题