OpenGL Skybox Not Rendering Sides Correctly
I am pretty new to OpenGL and have run into a problem trying to render a skybox. This picture illustrates the issue fairly well.
The sides of the skybox do not show up, and when I view an edge (like in the pic), it just looks like the images are rendered right next to each other, instead of as different sides of a cube.
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();
}
I feel like I am missing something basic here, but I am not sure what. The code is more or less verbatim from the tutorial site (http://sidvind.com/wiki/Skybox_tutorial). The code was compiled with g++ 4.5.2 on Ubuntu Linux, but the same problem arises under visual studio as well.
I should state, as well, that the numbers used in the code above are the values that produced the linked picture.
Your textures need to form a seamless join across cube edges. In the screenshot you've supplied, these images don't form a seamless join (look at the sky).
Also check your projection matrix - are you setting a perspective transform?
To set up a perspective projection (the most common for 3D apps):
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
It looks to me like the texture on the right ought to be on the left of the left image. So I'd guess you are texturing the wrong sides of your cube.
精彩评论