SkyBox.cpp

Go to the documentation of this file.
00001 
00005 /* Copyright © 2009 James Legg.
00006     This program is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 */
00011 #include "SkyBox.h"
00012 
00013 #include <GL/gl.h>
00014 
00015 
00016 namespace Graphics
00017 {
00018 
00019 SkyBox::SkyBox(std::string filename_base)
00020 {
00021     const std::string extensions[6] = {".+z", ".-z", ".-x", ".+x", ".-y", ".+y"};
00022     textures.reserve(6);
00023     for (unsigned int face = 0; face < 6; face++)
00024     {
00025         std::string filename = filename_base + extensions[face];
00026         textures.push_back(boost::shared_ptr<Track::Texture>(new Track::Texture(filename)));
00027         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00028         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00029     }
00030     make_list();
00031 }
00032 
00033 void SkyBox::make_list()
00034 {
00035     display_list = glGenLists(1);
00036     glNewList(display_list, GL_COMPILE);
00037         // l is the scale for the skybox vertex coordinates. 
00038         const float l = 577.35026919;
00039         const float cmz[6][4][3] = {{{-l, l, -l}, {l, l, -l}, {l, -l, -l}, {-l, -l, -l}}, // -z
00040                                   {{-l, -l, l}, {l, -l, l}, {l, l, l}, {-l, l, l}}, // +z
00041                                   {{-l, -l, l}, {-l, l, l}, {-l, l, -l}, {-l, -l, -l}}, // -x
00042                                   {{l, l, l}, {l, -l, l}, {l, -l, -l}, {l, l, -l}}, // +x
00043                                   {{l, -l, l}, {-l, -l, l}, {-l, -l, -l}, {l, -l, -l}}, // -y
00044                                   {{-l, l, l}, {l, l, l}, {l, l, -l}, {-l, l, -l}}}; // +y
00045         const float coord[4][2] = {{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}, {0.0, 1.0}};                       
00046         for (unsigned int face = 0; face < 6; face++)
00047         {
00048             textures[face]->bind();
00049             glBegin(GL_QUADS);
00050                 for (int vertex = 3; vertex >= 0; vertex--)
00051                 {
00052                     glTexCoord2f(coord[vertex][0], coord[vertex][1]);
00053                     glVertex3f(cmz[face][vertex][0], cmz[face][vertex][1], cmz[face][vertex][2]);
00054                 } 
00055             glEnd();
00056         }
00057     glEndList();
00058 }
00059 
00060 SkyBox::SkyBox(const Track::Skybox & source)
00061     :   Track::Skybox(source)
00062 {
00063     for (unsigned int face = 0; face < 6; face++)
00064     {
00065         textures[face]->bind();
00066         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00067         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00068     }
00069     make_list();
00070 }
00071 
00072 SkyBox::~SkyBox()
00073 {
00074     glDeleteLists(display_list, 1);
00075 }
00076 
00077 void SkyBox::draw()
00078 {
00079     glCallList(display_list);
00080 }
00081 
00082 }

Get Racer at SourceForge.net. Fast, secure and Free Open Source software downloads

Generated at Mon Sep 6 00:41:12 2010 by Doxygen version 1.4.7 for Racer version svn335.