Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
glfCubeMap.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include "glfCubeMap.hh"
26 #include "glfException.hh"
27 #include <Base/Image/ImageIO.hh>
28 #include "glfFormatDB.hh"
29 
30 using namespace BIAS;
31 
32 // border is currently not used.
33 const int BORDER_WIDTH = 0;
34 
35 glfCubeMap::glfCubeMap() : glfTexture(GL_TEXTURE_CUBE_MAP)
36 {
37 }
38 
39 void glfCubeMap::UploadImage(GLenum target, const ImageBase& image,
40  GLenum internalFormat, int mipmap)
41 {
42  if (enlargeToPot_) {
43  int potWidth = NextPowerOfTwo(image.GetWidth());
44  int potHeight = NextPowerOfTwo(image.GetHeight());
45  if ((int)image.GetWidth() != potWidth || (int)image.GetHeight() != potHeight) {
46  ImageBase paddedImage;
47  image.Pad(paddedImage, potWidth, potHeight);
48  UploadImage(target, paddedImage, internalFormat, mipmap);
49  return;
50  }
51  }
52 
53  // choose internal format if none is given
54  if (internalFormat == 0) {
55  internalFormat = glfFormatDB::ProposeInternalFormat(image.GetStorageType(), image.GetColorModel());
56  }
57 
58  // get pixel format and pixel type of given image
59  GLenum format = glfFormatDB::GetFormatForColorModel(image.GetColorModel());
60  GLenum pixelType = glfFormatDB::GetPixelTypeForStorageType(image.GetStorageType());
61 
62  // upload data.
63  Bind();
64  glTexImage2D(target, mipmap, internalFormat,
65  image.GetWidth(), image.GetHeight(), BORDER_WIDTH,
66  format, pixelType, image.GetImageData());
67 
68  GLF_THROW_ON_OPENGL_ERROR;
69 }
70 
71 void glfCubeMap::UploadImageFromFile(GLenum target, const std::string& fileName,
72  GLenum internalFormat, int mipmap)
73 {
74  ImageBase image;
75  if (ImageIO::Load(fileName, image) != 0) {
76  GLF_THROW_EXCEPTION("Could not load image: " << fileName);
77  }
78  UploadImage(target, image, internalFormat, mipmap);
79 
80  GLF_THROW_ON_OPENGL_ERROR;
81 }
82 
83 void glfCubeMap::Allocate(int width, int height,
84  GLenum internalFormat, int mipmap)
85 {
86  if (enlargeToPot_) {
87  width = NextPowerOfTwo(width);
88  height = NextPowerOfTwo(height);
89  }
90 
91  GLenum format;
92  int numChannels;
93  glfFormatDB::GetFormatForInternalFormat(internalFormat, format, numChannels);
94 
95  Bind();
96  for (int i = 0; i < 6; i++) {
97  glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, mipmap,
98  internalFormat, width, height, BORDER_WIDTH,
99  format, GL_UNSIGNED_BYTE, NULL);
100  }
101 
102  GLF_THROW_ON_OPENGL_ERROR;
103 }
104 
105 void glfCubeMap::Allocate(int width, int height,
106  ImageBase::EStorageType storageType,
107  ImageBase::EColorModel colorModel,
108  int mipmap)
109 {
110  GLenum internalFormat = glfFormatDB::ProposeInternalFormat(storageType, colorModel);
111  Allocate(width, height, internalFormat, mipmap);
112 }
113 
114 void glfCubeMap::CopyToImage(GLenum target, ImageBase& image, int mipmap)
115 {
116  Bind();
117 
118  // get internal format of texture.
119  GLenum internalFormat;
120  glGetTexLevelParameteriv(target, mipmap,
121  GL_TEXTURE_INTERNAL_FORMAT,
122  (GLint*)&internalFormat);
123 
124  // get size
125  int width = GetWidth(mipmap);
126  int height = GetHeight(mipmap);
127 
128  // get format and number of channels
129  GLenum format;
130  int numChannels;
131  glfFormatDB::GetFormatForInternalFormat(internalFormat, format, numChannels);
132 
133  // init image with size of texture and appropriate number of channels
134  ImageBase::EStorageType storageType = image.GetStorageType();
135  if (!image.IsEmpty()) {
136  image.Release(); // ImageBase doesn't like re-Init ...
137  }
138  image.Init(width, height, numChannels, storageType);
139 
140  // get OpenGL pixel type
142 
143  // copy texture to image
144  glGetTexImage(target, mipmap, format, type, image.GetImageData());
145 
146  GLF_THROW_ON_OPENGL_ERROR;
147 }
148 
150  GLenum format, int mipmap)
151 {
152  Bind();
153 
154  // get size
155  int width = GetWidth(mipmap);
156  int height = GetHeight(mipmap);
157 
158  // get number of channels
159  int numChannels = 0;
160  switch (format) {
161  case GL_RED:
162  case GL_GREEN:
163  case GL_BLUE:
164  case GL_ALPHA:
165  case GL_LUMINANCE:
166  numChannels = 1;
167  break;
168  case GL_LUMINANCE_ALPHA:
169  numChannels = 2;
170  break;
171  case GL_RGB:
172  case GL_BGR:
173  numChannels = 3;
174  break;
175  case GL_RGBA:
176  case GL_BGRA:
177  numChannels = 4;
178  break;
179  default:
180  GLF_THROW_EXCEPTION("Invalid format for CopyToImage");
181  break;
182  }
183 
184  // init image with size of texture and appropriate number of channels
185  ImageBase::EStorageType storageType = image.GetStorageType();
186  if (!image.IsEmpty()) {
187  image.Release(); // ImageBase doesn't like re-Init ...
188  }
189  image.Init(width, height, numChannels, storageType);
190 
191  // get OpenGL pixel type
193 
194  // copy texture to image
195  glGetTexImage(target, mipmap, format, type, image.GetImageData());
196 
197  GLF_THROW_ON_OPENGL_ERROR;
198 }
199 
200 int glfCubeMap::GetWidth(int mipmap) const
201 {
202  GLint width;
203  Bind();
204  glGetTexLevelParameteriv(target_, mipmap, GL_TEXTURE_WIDTH, &width);
205  GLF_THROW_ON_OPENGL_ERROR;
206  return width;
207 }
208 
209 int glfCubeMap::GetHeight(int mipmap) const
210 {
211  GLint height;
212  Bind();
213  glGetTexLevelParameteriv(target_, mipmap, GL_TEXTURE_HEIGHT, &height);
214  GLF_THROW_ON_OPENGL_ERROR;
215  return height;
216 }
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
static int NextPowerOfTwo(int i)
Returns the smallest power of two that is greater than i.
Definition: glfTexture.cpp:269
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
void Bind() const
Binds the texture.
Definition: glfTexture.cpp:242
static void GetFormatForInternalFormat(GLenum internalFormat, GLenum &format, int &numChannels)
Computes a pixel format that can be used with glGetTexImage and glReadPixels for an internal format o...
Definition: glfFormatDB.cpp:32
unsigned int GetWidth() const
Definition: ImageBase.hh:312
void UploadImageFromFile(GLenum target, const std::string &fileName, GLenum internalFormat=0, int mipmap=0)
Uploads an image loaded from a file to a mipmap of one side of the cube map.
Definition: glfCubeMap.cpp:71
const void * GetImageData() const
Definition: ImageBase.hh:280
void Allocate(int width, int height, GLenum internalFormat, int mipmap=0)
Creates a cube map with undefined content.
Definition: glfCubeMap.cpp:83
int Pad(BIAS::ImageBase &dest, const unsigned int &newwidth, const unsigned int &newheight, const int &padVal=0) const
Definition: ImageBase.cpp:1207
void CopyChannelsToImage(GLenum target, ImageBase &image, GLenum format, int mipmap=0)
Copies the pixels of a mipmap of a side of the cube map to a BIAS::ImageBase and interprets the textu...
Definition: glfCubeMap.cpp:149
unsigned int GetHeight() const
Definition: ImageBase.hh:319
static GLenum ProposeInternalFormat(ImageBase::EStorageType storageType, ImageBase::EColorModel colorModel)
Returns an OpenGL internal pixel format for a BIAS storage type and color model.
int GetHeight(int mipmap=0) const
Returns the height of a mipmap of each side of the cube map.
Definition: glfCubeMap.cpp:209
void Release(const bool reset_storage_type=false)
Free the allocated data structures Hands off: Do !!NOT!! change the default of reset_storage_type: Im...
Definition: ImageBase.cpp:350
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
static GLenum GetFormatForColorModel(ImageBase::EColorModel colorModel)
Returns the OpenGL pixel format (GL_RGB etc.) for a BIAS color model.
void Init(unsigned int width, unsigned int height, unsigned int nChannels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
Initialize image size and channels.
Definition: ImageBase.cpp:229
static GLenum GetPixelTypeForStorageType(ImageBase::EStorageType storageType)
Returns the OpenGL pixel type (GL_UNSIGNED_BYTE etc.) for a BIAS storage type.
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141
Base class for textures.
Definition: glfTexture.hh:42
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
int GetWidth(int mipmap=0) const
Returns the width of a mipmap of each side of the cube map.
Definition: glfCubeMap.cpp:200
void UploadImage(GLenum target, const BIAS::ImageBase &image, GLenum internalFormat=0, int mipmap=0)
Uploads a BIAS::Image to a mipmap of one side of the cube map.
Definition: glfCubeMap.cpp:39
void CopyToImage(GLenum target, ImageBase &image, int mipmap=0)
Copies the pixels of a mipmap of a side of the cube map to a BIAS::ImageBase.
Definition: glfCubeMap.cpp:114
This is the base class for images in BIAS.
Definition: ImageBase.hh:102