Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PlainPerPixelProcessing.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 <OpenGLFramework/SpecializedBatches/PlainPerPixelProcessing.hh>
26 #include <OpenGLFramework/Utils/Primitives.hh>
27 #include <OpenGLFramework/Base/glfUniformNotFoundException.hh>
28 
29 using namespace BIAS;
30 using namespace std;
31 
34 basicInit_(false), initialized_(false), numInputTextures_(0),
35 flipTexCoord_(flip), //usePingPongTextures_(false), isPing_(true),
36 externalUniformsChanged_(false)
37 {}
38 
42 }
43 
46 {
47  batch_.SetTexture(inputTexture, 0);
48  if( numInputTextures_ != 1 ) {
49  initialized_ = false;
51  }
52 }
53 
54 void
55 PlainPerPixelProcessing::SetSamplerNames(const std::vector<std::string>& samplerNames)
56 {
57  samplerNames_ = samplerNames;
58  initialized_ = false;
59 }
60 
62 SetTextures(const std::vector<BIAS::glfTexture2D*>& inputTextures)
63 {
64  for(unsigned int i=0; i<inputTextures.size(); i++)
65  batch_.SetTexture(inputTextures[i], i);
66  if( numInputTextures_ != inputTextures.size() ) {
67  initialized_ = false;
68  numInputTextures_ = inputTextures.size();
69  }
70 }
71 
73 SetTextures(std::list<BIAS::glfTexture2D*>& inputTextures)
74 {
75  std::list<BIAS::glfTexture2D*>::iterator it = inputTextures.begin();
76  std::list<BIAS::glfTexture2D*>::iterator itEnd = inputTextures.end();
77  unsigned int i=0;
78  for( ;it!=itEnd; it++ ) {
79  batch_.SetTexture((*it), i++);
80  }
81  if( numInputTextures_ != inputTextures.size() ) {
82  initialized_ = false;
83  numInputTextures_ = inputTextures.size();
84  }
85 }
86 
87 
89 SetOutputSize(unsigned int width, unsigned int height)
90 {
91  viewport_.SetSize(static_cast<int>(width),
92  static_cast<int>(height));
93  initialized_ = false;
94 }
95 
98 {
100 }
101 
103 SetFragmentShaderFromFile(const std::string& fileName)
104 {
105  fragmentShader_.CreateFromFile(GL_FRAGMENT_SHADER, fileName);
106  initialized_ = false;
107 }
108 
109 void
111 {
112  fragmentShader_.Create(GL_FRAGMENT_SHADER, source);
113  initialized_ = false;
114 }
115 
118 {
119  if(!initialized_) {
120  Init_();
121  }
123  for(std::map<std::string, float>::iterator it = uniformsFloat_.begin();
124  it!=uniformsFloat_.end(); it++) {
125  shaderProgram_.SetUniform(it->first, it->second);
126  }
127  for(std::map<std::string, int>::iterator it = uniformsInt_.begin();
128  it!=uniformsInt_.end(); it++) {
129  shaderProgram_.SetUniform(it->first, it->second);
130  }
131  externalUniformsChanged_ = false;
132  }
133  batch_.Draw();
134 }
135 
136 void
138 {
139  flipTexCoord_ = flip;
140  basicInit_ = false;
141 }
142 
145 {
146  if(!basicInit_) {
147  InitBatch_();
150  // InitPerFragmentOperations_();
151  InitShaders_();
152  basicInit_=true;
153  }
154 
155 
158 
159  Vector<float> pixelOffset(2);
160  pixelOffset[0] = 1.0f/static_cast<float>(viewport_.GetWidth());
161  pixelOffset[1] = 1.0f/static_cast<float>(viewport_.GetHeight());
162  try {
163  shaderProgram_.SetUniform("MIP_pixelOffset", pixelOffset);
164  } catch(glfUniformNotFoundException& e) {
165  if(!e.NotFoundUniform("MIP_pixelOffset")) {
166  throw e;
167  }
168  }
169 
170  for(int i=0; i<static_cast<int>(numInputTextures_); i++) {
171 
172  if(i>=static_cast<int>(samplerNames_.size())) {
173  stringstream sstr;
174  sstr<<"img"<<i;
175  shaderProgram_.SetUniform(sstr.str(), i);
176  } else {
178  }
179  }
180 }
181 
182 // void
183 // PlainPerPixelProcessing::SetPingPongTextures(const std::vector<BIAS::glfTexture2D*>& ping,
184 // const std::vector<BIAS::glfTexture2D*>& pong,
185 // const PlainPerPixelProcessing::PingPongID& activeID)
186 // {
187 
188 // PingPongTex_[PlainPerPixelProcessing::ping] = ping;
189 // PingPongTex_[PlainPerPixelProcessing::pong] = pong;
190 
191 // usePingPongTextures_ = true;
192 // SetTextures(PingPongTex_[activeID]);
193 // isPing_ = (activeID==PlainPerPixelProcessing::ping);
194 // }
195 
196 // void
197 // PlainPerPixelProcessing::SetPingPongTextures(const std::vector<BIAS::glfTexture2D*>& ping,
198 // const std::vector<BIAS::glfTexture2D*>& pong,
199 // const std::vector<BIAS::glfTexture2D*>& unSwapped,
200 // const PlainPerPixelProcessing::PingPongID& activeID)
201 // {
202 
203 // PingPongTex_[PlainPerPixelProcessing::ping] = ping;
204 // PingPongTex_[PlainPerPixelProcessing::ping].insert(PingPongTex_[PlainPerPixelProcessing::ping].end(),
205 // unSwapped.begin(), unSwapped.end());
206 // PingPongTex_[PlainPerPixelProcessing::pong] = pong;
207 // PingPongTex_[PlainPerPixelProcessing::pong].insert(PingPongTex_[PlainPerPixelProcessing::pong].end(),
208 // unSwapped.begin(), unSwapped.end());
209 
210 
211 // usePingPongTextures_ = true;
212 // SetTextures(PingPongTex_[activeID]);
213 // isPing_ = (activeID==PlainPerPixelProcessing::ping);
214 // }
215 
216 // void
217 // PlainPerPixelProcessing::SetPingPongTextures(const std::vector<BIAS::glfTexture2D*>& ping,
218 // const std::vector<BIAS::glfTexture2D*>& pong,
219 // BIAS::glfTexture2D* unSwapped,
220 // const PlainPerPixelProcessing::PingPongID& activeID)
221 // {
222 
223 // PingPongTex_[PlainPerPixelProcessing::ping] = ping;
224 // PingPongTex_[PlainPerPixelProcessing::ping].insert(PingPongTex_[PlainPerPixelProcessing::ping].end(),
225 // unSwapped);
226 // PingPongTex_[PlainPerPixelProcessing::pong] = pong;
227 // PingPongTex_[PlainPerPixelProcessing::pong].insert(PingPongTex_[PlainPerPixelProcessing::pong].end(),
228 // unSwapped);
229 
230 
231 // usePingPongTextures_ = true;
232 // SetTextures(PingPongTex_[activeID]);
233 // isPing_ = (activeID==PlainPerPixelProcessing::ping);
234 // }
235 
236 // PlainPerPixelProcessing::PingPongID
237 // PlainPerPixelProcessing::SwapPingPongTextures()
238 // {
239 // BIASASSERT(usePingPongTextures_);
240 // isPing_ = !isPing_;
241 // if(isPing_) {
242 // SetTextures(PingPongTex_[PlainPerPixelProcessing::ping]);
243 // return PlainPerPixelProcessing::ping;
244 // } else {
245 // SetTextures(PingPongTex_[PlainPerPixelProcessing::pong]);
246 // return PlainPerPixelProcessing::pong;
247 // }
248 // }
249 
250 // PlainPerPixelProcessing::PingPongID
251 // PlainPerPixelProcessing::GetCurrentPingPongID() const
252 // {
253 // if(isPing_) {
254 // return PlainPerPixelProcessing::ping;
255 // } else {
256 // return PlainPerPixelProcessing::pong;
257 // }
258 // }
259 
262 {
270 }
271 
274 {
277 }
278 
281 {
283 }
284 
285 // void PlainPerPixelProcessing::
286 // InitPerFragmentOperations_()
287 // {
288 // // depthTest_.SetDepthTest(false);
289 // }
290 
293 {
296 }
297 
298 //void
299 //PlainPerPixelProcessing::Link()
300 //{
301 // Init_();
302 //}
303 //
304 //glfShaderProgram*
305 //PlainPerPixelProcessing::GetProgram()
306 //{
307 // return &shaderProgram_;
308 //}
309 
310 void
311 PlainPerPixelProcessing::SetUniform(const std::string& name, float val)
312 {
313  uniformsFloat_[name] = val;
315 }
316 
317 void
318 PlainPerPixelProcessing::SetUniform(const std::string& name, int val)
319 {
320  uniformsInt_[name] = val;
322 }
323 
324 
327 {
328  return depthTest_;
329 }
void SetFragmentShaderFromFile(const std::string &fileName)
std::map< std::string, int > uniformsInt_
void SetVertexBuffer(const glfVertexBuffer *vertexBuffer)
Sets the vertex buffer.
Definition: glfBatch.cpp:129
void SetModelViewMatrix(const glfMatrix *matrix)
Sets the modelview matrix.
Definition: glfBatch.cpp:150
Defines the usage of the depth buffer.
void Create()
Creates the shader program.
void SetUniform(const std::string &varName, float value)
int GetWidth() const
Definition: glfViewport.hh:55
void MakeIdentity()
Definition: glfMatrix.cpp:72
A 2D texture.
Definition: glfTexture2D.hh:40
void Link()
Links the attached shaders.
void AttachShader(const glfShader &shader)
Attaches a shader to the program.
void CreateFromFile(GLenum type, const std::string &fileName)
Creates the shader from GLSL source code loaded from a file.
Definition: glfShader.cpp:94
int GetHeight() const
Definition: glfViewport.hh:56
void SetDepthBufferMode(const glfDepthBufferMode *depthBufferMode)
Sets the depth buffer mode.
Definition: glfBatch.cpp:106
void Create(GLenum type)
Creates the shader without the GLSL source code, use other Create() methods to upload source afterwar...
Definition: glfShader.cpp:55
void Draw()
Draws the batch.
Definition: glfBatch.cpp:218
void SetTexture(BIAS::glfTexture2D *inputTexture)
bool NotFoundUniform(const std::string &name) const
Returns whether the not found uniform has the specified name.
void SetOutputSize(unsigned int width, unsigned int height)
Returns the currently set texture set;.
void SetViewport(const glfViewport *viewport)
Sets the viewport.
Definition: glfBatch.cpp:65
void SetFragmentShaderFromString(const std::string &source)
void SetUniform(const std::string &name, float val)
void SetTexture(const glfTexture *texture, int textureUnit)
Sets the texture of a texture unit.
Definition: glfBatch.cpp:71
void SetRenderTarget(const glfRenderTarget *renderTarget)
Sets the render target.
Definition: glfBatch.cpp:59
static void PlainQuad2DWithTexture2D(BIAS::glfVertexBuffer &vb, BIAS::glfElementBuffer &eb, bool flip=false)
Vertex Order: 3–2 | | 0–1 .
Definition: Primitives.cpp:263
Class is thrown when a uniform is set but was not set active by the gl compiler.
void SetShaderProgram(const glfShaderProgram *shaderProgram)
Sets the shader program.
Definition: glfBatch.cpp:124
void SetSize(int width, int height)
Sets the size of the viewport in pixel coordinates.
Definition: glfViewport.cpp:51
void SetProjectionMatrix(const glfMatrix *matrix)
Sets the projection matrix.
Definition: glfBatch.cpp:156
void SetRenderTarget(BIAS::glfRenderTarget *rt)
void ReleaseTextures(unsigned int fromIndex)
Releases texture including the matrix associated with the argument index.
Definition: glfBatch.cpp:90
void SetElementBuffer(const glfElementBuffer *elementBuffer)
Sets the element buffer.
Definition: glfBatch.cpp:134
void SetTextures(const std::vector< BIAS::glfTexture2D * > &inputTextures)
Adds textures in their respective index order and assigns them the default names img&lt;index&gt; in the sh...
std::map< std::string, float > uniformsFloat_
void SetSamplerNames(const std::vector< std::string > &samplerNames)
Interface for render targets.
std::vector< std::string > samplerNames_