OpenGL Skybox无法正确渲染边

时间:2011-09-11 23:43:30

标签: c++ opengl

我是OpenGL的新手,并且在尝试渲染天空盒时遇到了问题。 这张图很好地说明了这个问题。

天空盒的两侧没有出现,当我查看边缘时(如图中所示), 它只是看起来像是彼此相邻,而不是像 立方体的不同侧面。

void display ( void )   // Create The Display Function
{
glPushMatrix();
glDepthMask(0);
/* replace this code with your height field implementation */
/* you may also want to precede it with your rotation/translation/scaling */
/* Clear buffers */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt( 0.0, 0.0, 0.0,  // eye position
           0.1, 0.0, 0.1,  // camera direction
           0.0, 1.0, 0.0); // up direction

glPushAttrib(GL_ENABLE_BIT);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_BLEND);

glColor4f(1.0, 1.0, 1.0, 1.0);

/*!!!!!!!!!!!!!! FRONT FACE !!!!!!!!!!!!!!!*/

glBindTexture(GL_TEXTURE_2D,frontTextureId); // select which texture to use
glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3f(  0.5f,  0.5f, -0.5f );
    glTexCoord2f(1, 0); glVertex3f( -0.5f,  0.5f, -0.5f );
    glTexCoord2f(1, 1); glVertex3f( -0.5f, -0.5f, -0.5f );
    glTexCoord2f(0, 1); glVertex3f(  0.5f, -0.5f, -0.5f );
glEnd();

/*!!!!!!!!!!!!!! LEFT FACE !!!!!!!!!!!!!!!*/
glBindTexture(GL_TEXTURE_2D, leftTextureId); // select which texture to use
glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3f(  0.5f,  0.5f,  0.5f );
    glTexCoord2f(1, 0); glVertex3f(  0.5f,  0.5f, -0.5f );
    glTexCoord2f(1, 1); glVertex3f(  0.5f, -0.5f, -0.5f );
    glTexCoord2f(0, 1); glVertex3f(  0.5f, -0.5f,  0.5f );
glEnd();

/*!!!!!!!!!!!!!! RIGHT FACE !!!!!!!!!!!!!!!*/
glBindTexture(GL_TEXTURE_2D, rightTextureId); // select which texture to use
glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3f( -0.5f,  0.5f, -0.5f );
    glTexCoord2f(1, 0); glVertex3f( -0.5f,  0.5f,  0.5f );
    glTexCoord2f(1, 1); glVertex3f( -0.5f, -0.5f,  0.5f );
    glTexCoord2f(0, 1); glVertex3f( -0.5f, -0.5f, -0.5f );
glEnd();

/*!!!!!!!!!!!!!! BACK FACE !!!!!!!!!!!!!!!*/
glBindTexture(GL_TEXTURE_2D, backTextureId); // select which texture to use
glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3f( -0.5f,  0.5f,  0.5f );
    glTexCoord2f(1, 0); glVertex3f(  0.5f,  0.5f,  0.5f );
    glTexCoord2f(1, 1); glVertex3f(  0.5f, -0.5f,  0.5f );
    glTexCoord2f(0, 1); glVertex3f( -0.5f, -0.5f,  0.5f );

glEnd();

/*!!!!!!!!!!!!!! TOP FACE !!!!!!!!!!!!!!!*/
glBindTexture(GL_TEXTURE_2D, upTextureId); // select which texture to use
glBegin(GL_QUADS);
    glTexCoord2f(0, 1); glVertex3f( -0.5f,  0.5f, -0.5f );
    glTexCoord2f(0, 0); glVertex3f( -0.5f,  0.5f,  0.5f );
    glTexCoord2f(1, 0); glVertex3f(  0.5f,  0.5f,  0.5f );
    glTexCoord2f(1, 1); glVertex3f(  0.5f,  0.5f, -0.5f );
glEnd();

/*!!!!!!!!!!!!!! BOTTOM FACE !!!!!!!!!!!!!!!*/
glBindTexture(GL_TEXTURE_2D, downTextureId); // select which texture to use
glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3f( -0.5f, -0.5f, -0.5f );
    glTexCoord2f(0, 1); glVertex3f( -0.5f, -0.5f,  0.5f );
    glTexCoord2f(1, 1); glVertex3f(  0.5f, -0.5f,  0.5f );
    glTexCoord2f(1, 0); glVertex3f(  0.5f, -0.5f, -0.5f );
glEnd();

/*!!!!!!!!!!!!!! end of drawing of a textured quad !!!!!!!!!!!!!!!*/
glPopAttrib();
glPopMatrix();

/* Swap buffers, so one we just drew is displayed */
glutSwapBuffers();
}

我觉得我在这里缺少一些基本的东西,但我不确定是什么。代码是 或多或少地从教程网站(http://sidvind.com/wiki/Skybox_tutorial)逐字逐句。 代码是在Ubuntu Linux上使用g ++ 4.5.2编译的,但同样的问题出现在 视觉工作室也是如此。

我还应该声明,上面代码中使用的数字是值 制作了相关的图片。

2 个答案:

答案 0 :(得分:3)

您的纹理需要在立方体边缘之间形成无缝连接。在您提供的屏幕截图中,这些图像不会形成无缝连接(看天空)。

同时检查投影矩阵 - 您是否设置了透视变换?

设置透视投影(3D应用最常见):

glMatrixMode(GL_PROJECTION);  // switch to projection matrix
glLoadIdentity();  // reset projection
gluPerspective(90.0,4.0/3.0,0.01,10.0);   // 90deg FOV, 4:3 aspect ratio, 0.01 near clip plane, 10.0 far clip plane
glMatrixMode(GL_MODELVIEW);  // back to model matrix

答案 1 :(得分:1)

在我看来,右侧的纹理应该位于左侧图像的左侧。所以我猜你会在你的立方体的错误方面进行纹理化。