OpenGL Skybox没有通过相机绘制

时间:2014-04-11 23:08:53

标签: c++ opengl

我正在尝试为我正在处理的简单OpenGL应用程序创建一个天空盒,我正在为天空盒绘制的立方体确实存在于3d空间中,但它并未在相机周围绘制当我移动相机时,将一个单元移动到相机移动的方向,当我停止移动相机时停止这样做。我能够让它几乎按照我想要的方式工作的唯一方法就是让立方体变得非常大,并且我希望能够将它作为单位立方体工作。

感谢。

void Scene3D::Skybox()
{

        //glTranslatef(0.0f, 0.0f, 0.0f); //Translate to camera

        float skyboxSize = 1.0f; //Half width and height for skybox cube, used to add or subtract from camera position to create cube.

        glDisable(GL_DEPTH_TEST);
        glDisable(GL_LIGHTING);

        //-Top-\\

        glBegin(GL_QUADS);

        //Far left
        glTexCoord2f(0.25f, 1.0f);
        glVertex3f(cameraPosX - skyboxSize, cameraPosY + skyboxSize, cameraPosZ - skyboxSize);

        //Far right
        glTexCoord2f(0.5f, 1.0f);
        glVertex3f(cameraPosX + skyboxSize, cameraPosY + skyboxSize, cameraPosZ - skyboxSize);

        //Close right
        glTexCoord2f(0.5f, 0.75f);
        glVertex3f(cameraPosX + skyboxSize, cameraPosY + skyboxSize, cameraPosZ + skyboxSize);

        //Close left
        glTexCoord2f(0.25f, 0.75f);
        glVertex3f(cameraPosX - skyboxSize, cameraPosY + skyboxSize, cameraPosZ + skyboxSize);

        glEnd();

        //-Bottom-\\

        glBegin(GL_QUADS);

        //Far left;
        glTexCoord2f(0.25f, 0.25f);
        glVertex3f(cameraPosX - skyboxSize, cameraPosY - skyboxSize, cameraPosZ - skyboxSize);

        //Far right
        glTexCoord2f(0.5f, 0.25f);
        glVertex3f(cameraPosX + skyboxSize, cameraPosY - skyboxSize, cameraPosZ - skyboxSize);

        //Close right
        glTexCoord2f(0.5f, 0.5f);
        glVertex3f(cameraPosX + skyboxSize, cameraPosY - skyboxSize, cameraPosZ + skyboxSize);

        //Close left
        glTexCoord2f(0.25f, 0.5f);
        glVertex3f(cameraPosX - skyboxSize, cameraPosY - skyboxSize, cameraPosZ + skyboxSize);

        glEnd();

        //-Left-\\

        glBegin(GL_QUADS);

        //Top left
        glTexCoord2f(0.0f, 0.75f);
        glVertex3f(cameraPosX - skyboxSize, cameraPosY + skyboxSize, cameraPosZ - skyboxSize);

        //Top right
        glTexCoord2f(0.25f, 0.75f);
        glVertex3f(cameraPosX - skyboxSize, cameraPosY + skyboxSize, cameraPosZ + skyboxSize);

        //Bottom right
        glTexCoord2f(0.25f, 0.5f);
        glVertex3f(cameraPosX - skyboxSize, cameraPosY - skyboxSize, cameraPosZ + skyboxSize);

        //Bottom left
        glTexCoord2f(0.0f, 0.5f);
        glVertex3f(cameraPosX - skyboxSize, cameraPosY - skyboxSize, cameraPosZ - skyboxSize);

        glEnd();

        //-Front-\\

        glBegin(GL_QUADS);

        //Top left
        glTexCoord2f(0.25f, 0.75f);
        glVertex3f(cameraPosX - skyboxSize, cameraPosY + skyboxSize, cameraPosZ + skyboxSize);

        //Top right);
        glTexCoord2f(0.5f, 0.75f);
        glVertex3f(cameraPosX + skyboxSize, cameraPosY + skyboxSize, cameraPosZ + skyboxSize);

        //Bottom right
        glTexCoord2f(0.5f, 0.5f);
        glVertex3f(cameraPosX + skyboxSize, cameraPosY - skyboxSize, cameraPosZ + skyboxSize);

        //Bottom left
        glTexCoord2f(0.25f, 0.5f);
        glVertex3f(cameraPosX - skyboxSize, cameraPosY - skyboxSize, cameraPosZ + skyboxSize);

        glEnd();

        //-Right-\\

        glBegin(GL_QUADS);

        //Top left
        glTexCoord2f(0.5f, 0.75f);
        glVertex3f(cameraPosX + skyboxSize, cameraPosY + skyboxSize, cameraPosZ - skyboxSize);

        //Top right
        glTexCoord2f(0.75f, 0.75f);
        glVertex3f(cameraPosX + skyboxSize, cameraPosY + skyboxSize, cameraPosZ + skyboxSize);

        //Bottom right
        glTexCoord2f(0.75f, 0.5f);
        glVertex3f(cameraPosX + skyboxSize, cameraPosY - skyboxSize, cameraPosZ + skyboxSize);

        //Bottom left
        glTexCoord2f(0.5f, 0.5f);
        glVertex3f(cameraPosX + skyboxSize, cameraPosY - skyboxSize, cameraPosZ - skyboxSize);

        glEnd();

        //-Back-\\

        glBegin(GL_QUADS);

        //Top left
        glTexCoord2f(0.75f, 0.75f);
        glVertex3f(cameraPosX - skyboxSize, cameraPosY + skyboxSize, cameraPosZ - skyboxSize);

        //Top right
        glTexCoord2f(1.0f, 0.75f);
        glVertex3f(cameraPosX + skyboxSize, cameraPosY + skyboxSize, cameraPosZ - skyboxSize);

        //Bottom right
        glTexCoord2f(1.0f, 0.5f);
        glVertex3f(cameraPosX + skyboxSize, cameraPosY - skyboxSize, cameraPosZ - skyboxSize);

        //Bottom left
        glTexCoord2f(0.75f, 0.5f);
        glVertex3f(cameraPosX - skyboxSize, cameraPosY - skyboxSize, cameraPosZ - skyboxSize);

        glEnd();

        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHTING);

}

1 个答案:

答案 0 :(得分:2)

渲染天空盒时,需要消除模型视图矩阵的转换组件。您可以通过翻译当前翻译的倒数来完成此操作:

 glPushMatrix();
 GLfloat matrix[16];  
 glGetFloatv (GL_MODELVIEW_MATRIX, matrix);
 glTranslatef(-matrix[12], -matrix[13], -matrix[14]);

 ... your code ...

 glPopMatrix();