Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ScreenQuad.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision: $ *
45  * $Author: $ *
46  * $Date: $ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 //=============================================================================
53 //
54 // CLASS ScreenQuad - IMPLEMENTATION
55 //
56 //=============================================================================
57 
58 
59 //== INCLUDES =================================================================
60 
61 #include <ACG/GL/acg_glew.hh>
62 #include "ScreenQuad.hh"
63 #include <ACG/ShaderUtils/GLSLShader.hh>
64 #include <ACG/GL/GLError.hh>
65 #include <ACG/GL/globjects.hh>
66 
67 //== NAMESPACES ===============================================================
68 
69 
70 namespace ACG {
71 
72 
73 //== IMPLEMENTATION ==========================================================
74 
76  vbo_(0),
77  decl_(0),
78  texDrawProg_(0)
79 {
80 }
81 
82 //----------------------------------------------------------------------------
83 
85 {
86  if (vbo_)
87  glDeleteBuffers(1, &vbo_);
88 
89  delete decl_;
90  delete texDrawProg_;
91 }
92 
93 //----------------------------------------------------------------------------
94 
96 {
97  static ScreenQuad singleton;
98  return singleton;
99 }
100 
101 //----------------------------------------------------------------------------
102 
104 {
105  if (!decl_)
106  {
107  decl_ = new VertexDeclaration();
109  }
110 
111  if (!vbo_)
112  {
113  float quad[] =
114  {
115  -1.0f, 1.0f, -1.0f,
116  -1.0f, -1.0f, -1.0f,
117  1.0f, 1.0f, -1.0f,
118  1.0f, -1.0f, -1.0f
119  };
120 
121  glGenBuffers(1, &vbo_);
122 
123  glBindBuffer(GL_ARRAY_BUFFER, vbo_);
124  glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW);
125 
126  glBindBuffer(GL_ARRAY_BUFFER, 0);
127 
129  }
130 
131 
132  if (!texDrawProg_)
133  {
134  // save active program
135  GLint curProg = 0;
136  glGetIntegerv(GL_CURRENT_PROGRAM, &curProg);
137 
138  texDrawProg_ = GLSL::loadProgram("ScreenQuad/screenquad.glsl", "ScreenQuad/tex2D.glsl");
139 
140  // restore active program
141  glUseProgram(curProg);
142  }
143 }
144 
145 //----------------------------------------------------------------------------
146 
148 {
149  if (_prog)
150  _prog->use();
151 
152  ScreenQuad& quad = instance();
153 
154  quad.intDraw(_prog);
155 }
156 
157 //----------------------------------------------------------------------------
158 
159 void ScreenQuad::drawInstanced( int _count, GLSL::Program* _prog /*= 0*/ )
160 {
161  if (_prog)
162  _prog->use();
163 
164  ScreenQuad& quad = instance();
165 
166  quad.intDraw(_prog, _count);
167 }
168 
169 //----------------------------------------------------------------------------
170 
171 void ScreenQuad::intDraw (GLSL::Program* _prog, int _numInstances)
172 {
173  if (!vbo_)
174  {
175  init();
176  }
177 
178 
179  glBindBuffer(GL_ARRAY_BUFFER, vbo_);
180 
181  if (_prog)
183  else
185 
186  glPolygonMode(GL_FRONT, GL_FILL);
187 
188  if (_numInstances < 1)
189  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
190  else
191  {
192 #ifdef GL_VERSION_3_1
193  glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, _numInstances);
194 #else
195  std::cerr << "error: instanced ScreenQuad draw - outdated glew version" << std::endl;
196  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
197 #endif
198  }
199 
200  if (_prog)
202  else
204 }
205 
206 void ScreenQuad::drawTexture2D( GLuint _texture, const Vec2f& _offset /*= Vec2f(0.0f, 0.0f)*/, const Vec2f& _size /*= Vec2f(1.0f, 1.0f)*/ )
207 {
208  ScreenQuad& quad = instance();
209 
210  if (!quad.texDrawProg_)
211  quad.init();
212 
213  if (quad.texDrawProg_)
214  {
215  glActiveTexture(GL_TEXTURE0);
216  glEnable(GL_TEXTURE_2D);
217  glBindTexture(GL_TEXTURE_2D, _texture);
218 
219 
220  quad.texDrawProg_->use();
221 
222  quad.texDrawProg_->setUniform("Tex", 0); // texslot 0
223 
224  quad.texDrawProg_->setUniform("offset", _offset);
225  quad.texDrawProg_->setUniform("size", _size);
226 
227 
228  quad.intDraw(quad.texDrawProg_);
229 
230 
231  quad.texDrawProg_->disable();
232  }
233 }
234 
235 //----------------------------------------------------------------------------
236 
237 //=============================================================================
238 } // namespace ACG
239 //=============================================================================
240 
void deactivateShaderPipeline(GLSL::Program *_prog) const
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
ScreenQuad()
Default constructor.
Definition: ScreenQuad.cc:75
static void draw(GLSL::Program *_prog=0)
Draw the screen quad.
Definition: ScreenQuad.cc:147
static void drawTexture2D(GLuint _texture, const Vec2f &_offset=Vec2f(0.0f, 0.0f), const Vec2f &_size=Vec2f(1.0f, 1.0f))
Draw a 2D texture to screen.
Definition: ScreenQuad.cc:206
GLSL::PtrProgram loadProgram(const char *vertexShaderFile, const char *tessControlShaderFile, const char *tessEvaluationShaderFile, const char *geometryShaderFile, const char *fragmentShaderFile, const GLSL::StringList *macros, bool verbose)
Definition: GLSLShader.cc:1082
void deactivateFixedFunction() const
VertexDeclaration * decl_
vertex format of screen quad (float2 pos)
Definition: ScreenQuad.hh:137
GLSL::Program * texDrawProg_
Simple texture drawing shader.
Definition: ScreenQuad.hh:141
static ScreenQuad & instance()
Get singleton instance.
Definition: ScreenQuad.cc:95
void activateShaderPipeline(GLSL::Program *_prog) const
void use()
Enables the program object for using.
Definition: GLSLShader.cc:351
~ScreenQuad()
Destructor.
Definition: ScreenQuad.cc:84
GLuint vbo_
vbo containing the quad in projected coordinates
Definition: ScreenQuad.hh:134
void glCheckErrors()
Definition: GLError.hh:105
void activateFixedFunction() const
void init()
Initialize vbo and format.
Definition: ScreenQuad.cc:103
Class to define the vertex input layout.
void setUniform(const char *_name, GLint _value)
Set int uniform to specified value.
Definition: GLSLShader.cc:391
GLSL program class.
Definition: GLSLShader.hh:217
void disable()
Resets to standard rendering pipeline.
Definition: GLSLShader.cc:361
static void drawInstanced(int _count, GLSL::Program *_prog=0)
Draw the screen quad with instancing.
Definition: ScreenQuad.cc:159
void intDraw(GLSL::Program *_prog, int _numInstances=0)
Internal draw function.
Definition: ScreenQuad.cc:171
void addElement(const VertexElement *_pElement)