1 #include "SceneBGImage.hh"
3 #include <bias_config.h>
4 #include <Gui/biasgl.h>
5 #include <Base/Image/ImageConvert.hh>
6 #include <Base/Image/ImageIO.hh>
8 #include <Filter/Rescale.hh>
9 #include <Base/Common/FileHandling.hh>
17 IsInitialized_ =
false;
19 BackgroundImageTex_ = 0;
20 ImgWidth_ = ImgHeight_ = 0;
22 MinFilter_ = GL_NEAREST;
23 MagFilter_ = GL_NEAREST;
33 unsigned char current=0,next=1;
37 if(ImageConvert::ToRGB(img, tmp[current])!= 0) {
38 BIASERR(
"Unsupported color model");
49 if (MaxTextureSize_ == 0)
50 glGetIntegerv(GL_MAX_TEXTURE_SIZE,&MaxTextureSize_);
53 if (tmp[current].GetWidth()>(
unsigned)MaxTextureSize_
54 || tmp[current].GetHeight()>(
unsigned)MaxTextureSize_){
60 BIASERR(
"error downsampling");
63 unsigned char ttt=next;
66 }
while (tmp[current].GetWidth()>(
unsigned)MaxTextureSize_ ||
67 tmp[current].GetHeight()>(
unsigned)MaxTextureSize_);
76 if(tmp[current].GetWidth() != ImgWidth_
77 || tmp[current].GetHeight()!= ImgHeight_){
78 return InitializeTexture_(tmp[current]);
82 return InitializeTexture_(tmp[current]);
85 glBindTexture(GL_TEXTURE_2D, BackgroundImageTex_);
87 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
88 glPixelStorei(GL_PACK_ALIGNMENT, 1);
96 {GLenum err = glGetError();
if (err != GL_NO_ERROR) { BIASERR(
"OpenGL Error before TexSubImage: " << gluErrorString(err));}}
98 if (tmp[current].GetChannelCount() ==3){
99 switch(tmp[current].GetColorModel()){
100 case ImageBase::CM_RGB:
101 glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0,tmp[current].GetWidth(),
102 tmp[current].GetHeight(), GL_RGB, GL_UNSIGNED_BYTE,
103 tmp[current].GetImageData());
105 case ImageBase::CM_BGR:
106 glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0,tmp[current].GetWidth(),
107 tmp[current].GetHeight(), GL_BGR, GL_UNSIGNED_BYTE,
108 tmp[current].GetImageData());
111 cerr <<
"invalid color model\n";
116 else if (tmp[current].GetChannelCount() ==1)
117 glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0,tmp[current].GetWidth(),
118 tmp[current].GetHeight(), GL_LUMINANCE, GL_UNSIGNED_BYTE,
119 tmp[current].GetImageData());
121 BIASERR(
"Can not handle texture with "<<tmp[current].GetChannelCount()
122 <<
" channels and Colormodel:"<<tmp[current].GetColorModel());
125 {GLenum err = glGetError();
if (err != GL_NO_ERROR) { BIASERR(
"OpenGL Error after TexSubImage: " << gluErrorString(err));}}
144 if(ImageConvert::ToRGB(img, imgtmp)!= 0) {
145 BIASERR(
"Unsupported color model");
154 glDeleteTextures(1, &BackgroundImageTex_);
155 glGenTextures(1, &BackgroundImageTex_);
156 float maxtexcoordX, maxtexcoordY;
161 if(ImgWidth_ == 0 || ImgHeight_ == 0) {
162 BIASERR(
"Image has zero dimension! Moving to uninitialized state.");
163 IsInitialized_ =
false;
170 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
171 glPixelStorei(GL_PACK_ALIGNMENT, 1);
173 glBindTexture(GL_TEXTURE_2D, BackgroundImageTex_);
177 case ImageBase::CM_RGB:
181 case ImageBase::CM_BGR:
186 cerr <<
"unsupported color model\n";
199 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
200 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
201 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
203 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, MagFilter_);
204 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, MinFilter_);
206 maxtexcoordX = float(ImgWidth_)/float(img.
GetWidth());
207 maxtexcoordY = float(ImgHeight_)/float(img.
GetHeight());
211 TexCoord_[0][0] = 0.0;
212 TexCoord_[0][1] = 0.0;
214 TexCoord_[1][0] = maxtexcoordX;
215 TexCoord_[1][1] = 0.0;
217 TexCoord_[2][0] = maxtexcoordX;
218 TexCoord_[2][1] = maxtexcoordY ;
220 TexCoord_[3][0] = 0.0;
221 TexCoord_[3][1] = maxtexcoordY;
226 IsInitialized_ =
true;
238 if (!IsInitialized_) {
239 BIASERR(
"BGScene not initialized");
243 glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT);
245 glMatrixMode(GL_MODELVIEW);
249 glMatrixMode(GL_PROJECTION);
253 glDepthFunc(GL_LEQUAL);
270 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
272 glDepthMask(GL_FALSE);
274 glEnable(GL_TEXTURE_2D);
276 glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
278 glBindTexture(GL_TEXTURE_2D,BackgroundImageTex_);
280 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
281 glDisable(GL_TEXTURE_GEN_S);
282 glDisable(GL_TEXTURE_GEN_T);
283 glMatrixMode(GL_TEXTURE);
287 glTexCoord2fv(TexCoord_[0].GetData());
288 glVertex3f(-1.0, 1.0, -1.0);
289 glTexCoord2fv(TexCoord_[3].GetData());
290 glVertex3f(-1.0, -1.0, -1.0);
291 glTexCoord2fv(TexCoord_[2].GetData());
292 glVertex3f(1.0, -1.0, -1.0);
293 glTexCoord2fv(TexCoord_[1].GetData());
294 glVertex3f(1.0, 1.0, -1.0);
297 glMatrixMode(GL_PROJECTION);
299 glMatrixMode(GL_MODELVIEW);
307 SetMinMagFilter(
int MinFilter,
int MagFilter)
309 MagFilter_ = MagFilter;
310 MinFilter_ = MinFilter;
311 if (IsInitialized_) {
312 glBindTexture(GL_TEXTURE_2D,BackgroundImageTex_);
313 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, MagFilter_);
314 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, MinFilter_);
unsigned int GetWidth() const
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
unsigned int GetHeight() const
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
enum EColorModel GetColorModel() const
int DownsampleBy2(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
Takes the source image that has to have a defined ROI.
int PadToPowerOfTwo(BIAS::ImageBase &dest, const int &padVal=0) const
increase the size of this image to next power of two (e.g.