OpenGL旋转相机

OpenGL rotation of the camera

本文关键字:相机 旋转 OpenGL      更新时间:2023-10-16

我有一个立方体,当然有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);

如果我需要解释更多,请询问。如果你知道一些关于这些事情的好文档,请这样说。

这可能不是你所期望的答案,但通常OpenGL硬件非常擅长不显示不面向相机的面,这被称为背面剔除。现代桌面硬件每秒可以丢弃大约40亿个三角形。所以这个优化可能不是一个,也可能不值得你花时间在它上

更重要的是,假设您使用的是透视投影,关于立方体相对于"相机"的位置,您关于立方体面的可见性与其旋转相关的假设可能不正确。

我认为行中的代码中有一个愚蠢的错误

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

您应该使用||来代替|