Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SamplerBinding.cpp
1 #include "SamplerBinding.hh"
2 #include <OpenGLFramework/Base/glfException.hh>
3 
4 using namespace std;
5 using namespace BIAS;
6 
7 SamplerBinding::SamplerBinding() :
8  cached_(false), program_(NULL)
9 {
10  mappedTextures_.clear();
11 }
12 
13 void
15 {
16  cached_ = false;
17  program_=NULL;
18  mappedTextures_.clear();
19 }
20 
21 void
23 {
24  BIASASSERT(program_!=NULL);
25  map<std::string, textureCache>::iterator it = mappedTextures_.begin();
26  for(;it!=mappedTextures_.end(); it++) {
27  GLint loc = program_->GetUniformLocation(it->first);
28  if( loc == -1) {
29  GLF_THROW_EXCEPTION("no active uniform named "<<it->first
30  <<"! Check: 1. Is Program Linked?, 2. Typo?, 3. Correct shader?");
31  }
32  glGetUniformiv(program_->GetProgramId(), loc, &it->second.tu);
33  }
34  cached_ = true;
35 }
36 
37 /** Sets all mapped samplers to appropriate texture units**/
38 void
40 {
41  // BIASASSERT(program_!=NULL);
42 
43  if(cached_) {
44  map<std::string, textureCache>::iterator it = mappedTextures_.begin();
45  for(;it!=mappedTextures_.end(); it++) {
46  // cout<<"binding "<<it->first<<" to TU "<<it->second.tu<<endl;
47  glActiveTexture(GL_TEXTURE0 + it->second.tu);
48  it->second.tex->Bind();
49  }
50  } else {
51 
52  map<std::string, textureCache>::iterator it = mappedTextures_.begin();
53  for(;it!=mappedTextures_.end(); it++) {
54  GLint loc = program_->GetUniformLocation(it->first);
55  if( loc == -1) {
56  GLF_THROW_EXCEPTION("no active uniform named "<<it->first
57  <<"! Check: 1. Is Program Linked?, 2. Typo?, 3. Correct shader?");
58  }
59  GLint tu;
60  glGetUniformiv(program_->GetProgramId(), loc, &tu);
61  glActiveTexture(GL_TEXTURE0 + tu);
62  it->second.tex->Bind();
63  }
64  }
65 
66 }
67 
68 void
70 {
71  program_ = program;
72  cached_ = false;
73 }
74 
75 void
76 SamplerBinding::MapSampler(glfTexture* texture, const std::string& UniformName)
77 {
78  mappedTextures_[UniformName].tex = texture;
79  cached_ = false;
80 }
81 
void SetProgram(glfShaderProgram *program)
GLint GetUniformLocation(const std::string &varName)
A shader program composed of several shaders.
Base class for textures.
Definition: glfTexture.hh:42
void Cache()
Call this to build a cache from the current object states.
void Bind()
Sets all mapped samplers to appropriate texture units.
void MapSampler(glfTexture *texture, const std::string &UniformName)