Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Texture2DPool.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2008, 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 <OpenGLFramework/Utils/Texture2DPool.hh>
26 
27 using namespace BIAS;
28 using namespace std;
29 
31 {
32  ClearAll();
33 }
34 
36 Texture2DPool::operator[](const std::string& name)
37 {
38  map<string, glfTexture2D*>::iterator it = texturePool_.find(name);
39  if (it != texturePool_.end())
40  {
41  return it->second;
42  }
43  else
44  {
45  BIASWARN("pool does not contain texture named: " << name);
46  return NULL;
47 
48  }
49 }
50 
51 /** Allocates an empty texture **/
53 Texture2DPool::Create(const std::string& name, int width, int height,
54  GLenum internalFormat, GLenum minFilter, GLenum magFilter, GLenum wrapS,
55  GLenum wrapT, int mipmap)
56 {
57  map<string, glfTexture2D*>::iterator it = texturePool_.find(name);
58  if (it != texturePool_.end())
59  {
60  BIASERR("already used pool name : " << name
61  << ". use clear before reusing the name!");
62  ClearAll();
63  BIASABORT;
64  }
65  glfTexture2D* newTexture(NULL);
66  texturePool_[name] = newTexture = new glfTexture2D;
67  newTexture->Create();
68  newTexture->Set(minFilter, magFilter, wrapS, wrapT);
69  newTexture->Allocate(width, height, internalFormat, mipmap);
70  return newTexture;
71 }
72 
73 /** Uploads BIAS image to texture space **/
75 Texture2DPool::Create(const std::string& name, const BIAS::ImageBase& img,
76  GLenum minFilter, GLenum magFilter, GLenum wrapS, GLenum wrapT,
77  GLenum internalFormat, int mipmap)
78 {
79  map<string, glfTexture2D*>::iterator it = texturePool_.find(name);
80  if (it != texturePool_.end())
81  {
82  BIASERR("already used pool name : " << name
83  << ". use clear before reusing the name!");
84  ClearAll();
85  BIASABORT;
86  }
87  glfTexture2D* newTexture(NULL);
88  texturePool_[name] = newTexture = new glfTexture2D;
89  newTexture->Create();
90  newTexture->Set(minFilter, magFilter, wrapS, wrapT);
91  newTexture->UploadImage(img, internalFormat, mipmap);
92  return newTexture;
93 
94 }
95 
96 /** Allocates an empty texture **/
98 Texture2DPool::Create(const string& name, int width, int height,
99  ImageBase::EStorageType storageType, ImageBase::EColorModel colorModel,
100  GLenum minFilter, GLenum magFilter, GLenum wrapS, GLenum wrapT, int mipmap)
101 {
102  map<string, glfTexture2D*>::iterator it = texturePool_.find(name);
103  if (it != texturePool_.end())
104  {
105  BIASERR("already used pool name : " << name
106  << ". use clear before reusing the name!");
107  ClearAll();
108  BIASABORT;
109  }
110  glfTexture2D* newTexture(NULL);
111  texturePool_[name] = newTexture = new glfTexture2D;
112  newTexture->Create();
113  newTexture->Set(minFilter, magFilter, wrapS, wrapT);
114  newTexture->Allocate(width, height, storageType, colorModel, mipmap);
115  return newTexture;
116 }
117 
119 Texture2DPool::UploadImage(const string& name, const BIAS::ImageBase& img,
120  GLenum minFilter, GLenum magFilter, GLenum wrapS, GLenum wrapT,
121  GLenum internalFormat, int mipmap)
122 {
123  map<string, glfTexture2D*>::iterator it = texturePool_.find(name);
124  if (it != texturePool_.end())
125  {
126  glfTexture2D* textureP = it->second;
127  textureP->UploadImage(img, internalFormat, mipmap);
128  return textureP;
129  }
130  else
131  {
132  glfTexture2D* textureP = new glfTexture2D();
133  textureP->Create();
134  textureP->Set(minFilter, magFilter, wrapS, wrapT, GL_TEXTURE0);
135  textureP->UploadImage(img, internalFormat, mipmap);
136  texturePool_[name] = textureP;
137  return textureP;
138  }
139 }
140 
141 void
143 {
144  map<string, glfTexture2D*>::iterator it = texturePool_.begin();
145  for (; it != texturePool_.end(); it++)
146  {
147  delete it->second;
148  it->second = NULL;
149  }
150  texturePool_.clear();
151 }
152 
153 void
154 Texture2DPool::Clear(const std::string& name)
155 {
156  map<string, glfTexture2D*>::iterator it = texturePool_.find(name);
157  if (it != texturePool_.end())
158  {
159  delete it->second;
160  texturePool_.erase(it);
161  }
162 }
163 
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
void Allocate(int width, int height, GLenum internalFormat, int mipmap=0)
Creates a texture with undefined content.
A 2D texture.
Definition: glfTexture2D.hh:40
void Clear(const std::string &name)
void Create()
Creates the texture but does not upload any data yet.
Definition: glfTexture.cpp:80
BIAS::glfTexture2D * operator[](const std::string &name)
Returns NULL if name is not found.
void UploadImage(const BIAS::ImageBase &image, GLenum internalFormat=0, int mipmap=0)
Uploads a BIAS::Image to a mipmap of the texture.
BIAS::glfTexture2D * UploadImage(const std::string &name, const BIAS::ImageBase &img, GLenum minFilter=GL_NEAREST, GLenum magFilter=GL_NEAREST, GLenum wrapS=GL_CLAMP, GLenum wrapT=GL_CLAMP, GLenum internalFormat=0, int mipmap=0)
~Texture2DPool()
Terminates the lifecycle of all textures created in the pool!
void Set(GLenum minFilter=GL_NEAREST, GLenum magFilter=GL_NEAREST, GLenum wrapS=GL_CLAMP, GLenum wrapT=GL_CLAMP, GLint textureNr=GL_TEXTURE0)
Convenience wrapper.
BIAS::glfTexture2D * Create(const std::string &name, int width, int height, GLenum internalFormat, GLenum minFilter=GL_NEAREST, GLenum magFilter=GL_NEAREST, GLenum wrapS=GL_CLAMP, GLenum wrapT=GL_CLAMP, int mipmap=0)
Allocates an empty texture.
This is the base class for images in BIAS.
Definition: ImageBase.hh:102