/*! -------------------------------------------------------------------- \file skybox.cpp \brief Encapsulate a skybox \author James Kuffner \date 2002-09-18 // -------------------------------------------------------------------*/ #include "skybox.h" //--------------------------------------------------------------------- // Skybox // Method: Init // //! Initialize the skybox // //--------------------------------------------------------------------- bool Skybox::Init(const char* skyboxPath) { const char* skyFiles[NUM_SKY_TEXTURES] = { "front.tif", "back.tif", "right.tif", "left.tif", "up.tif", "down.tif" }; // load each texture for (int i = 0; i < NUM_SKY_TEXTURES; i++) { // set up texture load char imagepath[1024]; sprintf(imagepath, "%s/%s", skyboxPath, skyFiles[i]); if (_skyBoxTexture[i].LoadTIFF(imagepath)) { // set up params and bind name _skyBoxTexture[i].InitTextureParams(false); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); } } return true; } //--------------------------------------------------------------------- // Skybox // Method: Render // //! Render the skybox // //--------------------------------------------------------------------- bool Skybox::Render() const { // constant skybox geometry static const GLfloat SKY_SIZE = 100.0; static const int NUM_VERTS = 8; static const GLfloat SKY_VERTS[NUM_VERTS][3] = { { -SKY_SIZE, -SKY_SIZE, -SKY_SIZE }, { SKY_SIZE, -SKY_SIZE, -SKY_SIZE }, { SKY_SIZE, SKY_SIZE, -SKY_SIZE }, { -SKY_SIZE, SKY_SIZE, -SKY_SIZE }, { SKY_SIZE, -SKY_SIZE, SKY_SIZE }, { -SKY_SIZE, -SKY_SIZE, SKY_SIZE }, { -SKY_SIZE, SKY_SIZE, SKY_SIZE }, { SKY_SIZE, SKY_SIZE, SKY_SIZE } }; static const int NUM_FACES = 6; static const unsigned short SKY_FACES[NUM_FACES][4] = { { 0, 3, 2, 1 }, // front { 4, 7, 6, 5 }, // back { 5, 6, 3, 0 }, // right { 1, 2, 7, 4 }, // left { 6, 7, 2, 3 }, // up { 4, 5, 0, 1 } // down }; // set up rendering attributes glDisable(GL_LIGHTING); glDisable(GL_DEPTH_WRITEMASK); glDisable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); // load camera transformation matrix GLfloat modelview[16]; glPushMatrix(); glGetFloatv(GL_MODELVIEW_MATRIX, modelview); // zero the translation part modelview[12] = 0.0; modelview[13] = 0.0; modelview[14] = 0.0; glLoadMatrixf(modelview); // render all skybox faces glColor3f(1.0, 1.0, 1.0); for (int i = 0; i < NUM_FACES; i++) { // make the texture we want active glBindTexture(GL_TEXTURE_2D, _skyBoxTexture[i].GetName()); // draw the quad glBegin(GL_QUADS); glTexCoord2i(0, 0); glVertex3fv(SKY_VERTS[SKY_FACES[i][0]]); glTexCoord2i(0, 1); glVertex3fv(SKY_VERTS[SKY_FACES[i][1]]); glTexCoord2i(1, 1); glVertex3fv(SKY_VERTS[SKY_FACES[i][2]]); glTexCoord2i(1, 0); glVertex3fv(SKY_VERTS[SKY_FACES[i][3]]); glEnd(); } // restore rendering state glPopMatrix(); glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_WRITEMASK); return true; }