CubeMap.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 "CubeMap.h"
00012 #include <Debug.h>
00013 
00014 #include <SDL_image.h>
00015 #include <GL/gl.h>
00016 #include <GL/glu.h>
00017 #include <cmath>
00018 
00019 namespace Graphics
00020 {
00021 
00022 CubeMap::CubeMap(std::string filename_base)
00023     :   Texture()
00024 {
00027     m_target = GL_TEXTURE_CUBE_MAP;
00028     glBindTexture(m_target, m_texture_name);
00029     for (unsigned int face = 0; face < 6; face++)
00030     {
00031         m_filename = filename_base + char('0' + face);
00032         switch (face)
00033         {
00034             case 0:
00035                 m_target = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
00036                 break;
00037             case 1:
00038                 m_target = GL_TEXTURE_CUBE_MAP_POSITIVE_Y;
00039                 break;
00040             case 2:
00041                 m_target = GL_TEXTURE_CUBE_MAP_NEGATIVE_X;
00042                 break;
00043             case 3:
00044                 m_target = GL_TEXTURE_CUBE_MAP_NEGATIVE_Y;
00045                 break;
00046             case 4:
00047                 m_target = GL_TEXTURE_CUBE_MAP_POSITIVE_Z;
00048                 break;
00049             case 5:
00050                 m_target = GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
00051                 break;
00052         }
00053         load_from_file();
00054     }
00055     m_target = GL_TEXTURE_CUBE_MAP;
00056     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00057     DEBUG_MESSAGE("Loaded cubemap " << filename_base);
00058 }
00059 
00060 CubeMap::~CubeMap()
00061 {
00062     DEBUG_MESSAGE("Deleting cubemap.");
00063 }
00064 
00065 void CubeMap::draw_sky_box()
00066 {
00067     // draw the skybox.
00068     bind();
00069     glEnable(GL_TEXTURE_CUBE_MAP);
00070     const float l = std::sqrt(1.0 / 3.0);
00071     
00072     glBegin(GL_QUADS);
00073         // [face][vertex][dimension]
00074         const float cmz[6][4][3] = {{{-l, -l, -l}, {l, -l, -l}, {l, l, -l}, {-l, l, -l}}, // -z
00075                               {{-l, -l, l}, {l, -l, l}, {l, l, l}, {-l, l, l}}, // +z
00076                               {{-l, -l, -l}, {-l, l, -l}, {-l, l, l}, {-l, -l, l}}, // -x
00077                               {{l, -l, -l}, {l, l, -l}, {l, l, l}, {l, -l, l}}, // +x
00078                               {{-l, -l, -l}, {l, -l, -l}, {l, -l, l}, {-l, -l, l}}, // -y
00079                               {{-l, l, -l}, {l, l, -l}, {l, l, l}, {-l, l, l}}}; // +y
00080         
00081         for (unsigned int f = 0; f < 6; f++)
00082         {                  
00083             for (unsigned int v = 0; v < 4; v++)
00084             {
00085                 glNormal3f(cmz[f][v][0], cmz[f][v][1], cmz[f][v][2]);
00086                 // Specify a texture coordinate implies crash.
00087                 glTexCoord3f(cmz[f][v][0], cmz[f][v][1], cmz[f][v][2]);
00088                 glVertex3f(cmz[f][v][0], cmz[f][v][1], cmz[f][v][2]);
00089             } 
00090         }
00091     glEnd();
00092     glDisable(GL_TEXTURE_CUBE_MAP);
00093 }
00094 
00095 }

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.