git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / client / GLEXT / GLTextureCubeMap.cpp
blob0748242ea4145fce8e098d0e65a99fdb3a9e383f
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <common/Defines.h>
22 #include <GLEXT/GLState.h>
23 #include <GLEXT/GLTextureCubeMap.h>
24 #include <string.h>
26 GLTextureCubeMap::GLTextureCubeMap()
28 memset(cubeTexNum_, 0, sizeof(GLuint) * 6);
31 GLTextureCubeMap::~GLTextureCubeMap()
33 if (textureValid())
35 glDeleteTextures(6, cubeTexNum_);
36 memset(cubeTexNum_, 0, sizeof(GLuint) * 6);
40 bool GLTextureCubeMap::textureValid()
42 return (glIsTexture(cubeTexNum_[0]) == GL_TRUE);
45 void GLTextureCubeMap::draw(bool force)
47 if ((this != lastBind_) || force)
49 lastBind_ = this;
53 bool GLTextureCubeMap::create(Image &bitmap,
54 bool mipMap)
56 GLenum format =
57 (bitmap.getComponents()==1)?GL_LUMINANCE:
58 ((bitmap.getComponents() == 3)?GL_RGB:GL_RGBA);
60 bool success = create(bitmap.getBits(),
61 bitmap.getWidth(),
62 bitmap.getHeight(),
63 bitmap.getComponents(),
64 bitmap.getAlignment(),
65 format,
66 mipMap);
68 return success;
71 bool GLTextureCubeMap::create(const void *data,
72 GLint width,
73 GLint height,
74 GLint components,
75 GLint alignment,
76 GLenum format,
77 bool mipMap)
79 bool success = false;
80 if (data)
82 if (!textureValid())
84 glGenTextures(6, cubeTexNum_);
87 // Generate a texture GL_TEXTURE_CUBE_MAP_EXT and bind it
88 glBindTexture(GL_TEXTURE_CUBE_MAP_EXT, cubeTexNum_[0]);
90 success = createTexture(data, width, height, components, alignment, format, mipMap);
93 return success;
96 bool GLTextureCubeMap::createTexture(const void * data,
97 GLint width,
98 GLint height,
99 GLint components,
100 GLint alignment,
101 GLenum format,
102 bool mipMap)
104 glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
105 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
106 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
107 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
108 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
110 GLenum texDefines[] = {
111 GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT,
112 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT,
113 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT,
114 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT,
115 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT,
116 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT};
118 // Load all six cube maps
119 for (int i=0; i<6; i++)
121 // Build the texture
122 if (mipMap)
124 // Texture parameters
125 glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_S, GL_REPEAT);
126 glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_T, GL_REPEAT);
127 glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
128 glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
130 gluBuild2DMipmaps(texDefines[i], components, width,
131 height, format, GL_UNSIGNED_BYTE, data);
133 else
135 // Texture parameters
136 glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_S, GL_REPEAT);
137 glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_T, GL_REPEAT);
138 glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
139 glTexParameterf(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
141 glTexImage2D(texDefines[i], 0, components, width,
142 height, 0, format, GL_UNSIGNED_BYTE, data);
146 return true;