GLTextureCubeMap.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D 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 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <common/Defines.h>
00022 #include <GLEXT/GLState.h>
00023 #include <GLEXT/GLTextureCubeMap.h>
00024 #include <string.h>
00025 
00026 GLTextureCubeMap::GLTextureCubeMap()
00027 {
00028         memset(cubeTexNum_, 0, sizeof(GLuint) * 6);
00029 }
00030 
00031 GLTextureCubeMap::~GLTextureCubeMap()
00032 {
00033         if (textureValid())
00034         {
00035                 glDeleteTextures(6, cubeTexNum_);
00036                 memset(cubeTexNum_, 0, sizeof(GLuint) * 6);
00037         }
00038 }
00039 
00040 bool GLTextureCubeMap::textureValid()
00041 {
00042         return (glIsTexture(cubeTexNum_[0]) == GL_TRUE);
00043 }
00044 
00045 void GLTextureCubeMap::draw(bool force)
00046 {
00047         if ((this != lastBind_) || force)
00048         {
00049                 lastBind_ = this;
00050         }
00051 }
00052 
00053 bool GLTextureCubeMap::create(Image &bitmap, 
00054                         bool mipMap)
00055 {
00056         GLenum format = 
00057                 (bitmap.getComponents()==1)?GL_LUMINANCE:
00058                 ((bitmap.getComponents() == 3)?GL_RGB:GL_RGBA);
00059 
00060         bool success = create(bitmap.getBits(),
00061                         bitmap.getWidth(), 
00062                         bitmap.getHeight(), 
00063                         bitmap.getComponents(), 
00064                         bitmap.getAlignment(),
00065                         format,
00066                         mipMap);
00067 
00068         return success;
00069 }
00070 
00071 bool GLTextureCubeMap::create(const void *data, 
00072                                            GLint width, 
00073                                            GLint height, 
00074                                            GLint components, 
00075                                            GLint alignment,
00076                                            GLenum format, 
00077                                            bool mipMap)
00078 {
00079         bool success = false;
00080         if (data)
00081         {
00082                 if (!textureValid())
00083                 {
00084                         glGenTextures(6, cubeTexNum_);
00085                 }
00086 
00087                 // Generate a texture GL_TEXTURE_CUBE_MAP_EXT and bind it
00088                 glBindTexture(GL_TEXTURE_CUBE_MAP_EXT, cubeTexNum_[0]);
00089 
00090                 success = createTexture(data, width, height, components, alignment, format, mipMap);
00091         }
00092 
00093         return success;
00094 }
00095 
00096 bool GLTextureCubeMap::createTexture(const void * data, 
00097                                                 GLint width, 
00098                                                 GLint height, 
00099                                                 GLint components, 
00100                                                 GLint alignment,
00101                                                 GLenum format, 
00102                                                 bool mipMap)
00103 {
00104         glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
00105         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
00106         glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
00107         glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
00108         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00109 
00110         GLenum texDefines[] = { 
00111                 GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT,
00112                 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT,
00113                 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT,
00114                 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT,
00115                 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT,
00116                 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT};
00117 
00118         // Load all six cube maps
00119         for (int i=0; i<6; i++)
00120         {
00121                 // Build the texture
00122                 if (mipMap)
00123                 {
00124                         // Texture parameters
00125                         glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_S, GL_REPEAT);
00126                         glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_T, GL_REPEAT);
00127                         glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00128                         glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00129 
00130                         gluBuild2DMipmaps(texDefines[i], components, width, 
00131                                         height, format, GL_UNSIGNED_BYTE, data);
00132                 }
00133                 else
00134                 {
00135                         // Texture parameters
00136                         glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_S, GL_REPEAT);
00137                         glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_T, GL_REPEAT);
00138                         glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00139                         glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00140 
00141                         glTexImage2D(texDefines[i], 0, components, width, 
00142                                 height, 0, format, GL_UNSIGNED_BYTE, data);
00143                 }
00144         }
00145 
00146         return true;
00147 }

Generated on Mon Feb 16 15:14:45 2009 for Scorched3D by  doxygen 1.5.3