我正在尝试为正在使用的简单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);

}

最佳答案

渲染天空盒时,您需要消除模型视图矩阵的平移组件。您可以通过翻译当前翻译的反义词来做到这一点:

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

 ... your code ...

 glPopMatrix();

关于c++ - OpenGL Skybox不在相机上绘制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23024019/

10-15 04:25