00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
00119 for (int i=0; i<6; i++)
00120 {
00121
00122 if (mipMap)
00123 {
00124
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
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 }