Developer Documentation
Loading...
Searching...
No Matches
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//
45// CLASS ScreenQuad - IMPLEMENTATION
46//
47//=============================================================================
48
49
50//== INCLUDES =================================================================
51
52#include <ACG/GL/acg_glew.hh>
53#include "ScreenQuad.hh"
54#include <ACG/ShaderUtils/GLSLShader.hh>
55#include <ACG/GL/GLError.hh>
56#include <ACG/GL/globjects.hh>
57
58//== NAMESPACES ===============================================================
59
60
61namespace ACG {
62
63
64//== IMPLEMENTATION ==========================================================
65
67 vbo_(0),
68 decl_(0),
69 texDrawProg_(0)
70{
71}
72
73//----------------------------------------------------------------------------
74
76{
77 if (vbo_)
78 glDeleteBuffers(1, &vbo_);
79
80 delete decl_;
81 delete texDrawProg_;
82}
83
84//----------------------------------------------------------------------------
85
87{
88 static ScreenQuad singleton;
89 return singleton;
90}
91
92//----------------------------------------------------------------------------
93
95{
96 if (!decl_)
97 {
100 }
101
102 if (!vbo_)
103 {
104 float quad[] =
105 {
106 -1.0f, 1.0f, -1.0f,
107 -1.0f, -1.0f, -1.0f,
108 1.0f, 1.0f, -1.0f,
109 1.0f, -1.0f, -1.0f
110 };
111
112 glGenBuffers(1, &vbo_);
113
114 glBindBuffer(GL_ARRAY_BUFFER, vbo_);
115 glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW);
116
117 glBindBuffer(GL_ARRAY_BUFFER, 0);
118
120 }
121
122
123 if (!texDrawProg_)
124 {
125 // save active program
126 GLint curProg = 0;
127 glGetIntegerv(GL_CURRENT_PROGRAM, &curProg);
128
129 texDrawProg_ = GLSL::loadProgram("ScreenQuad/screenquad.glsl", "ScreenQuad/tex2D.glsl");
130
131 // restore active program
132 glUseProgram(curProg);
133 }
134}
135
136//----------------------------------------------------------------------------
137
139{
140 if (_prog)
141 _prog->use();
142
143 ScreenQuad& quad = instance();
144
145 quad.intDraw(_prog);
146}
147
148//----------------------------------------------------------------------------
149
150void ScreenQuad::drawInstanced( int _count, GLSL::Program* _prog /*= 0*/ )
151{
152 if (_prog)
153 _prog->use();
154
155 ScreenQuad& quad = instance();
156
157 quad.intDraw(_prog, _count);
158}
159
160//----------------------------------------------------------------------------
161
162void ScreenQuad::intDraw (GLSL::Program* _prog, int _numInstances)
163{
164 if (!vbo_)
165 {
166 init();
167 }
168
169
170 glBindBuffer(GL_ARRAY_BUFFER, vbo_);
171
172 if (_prog)
174 else
176
177 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
178
179 if (_numInstances < 1)
180 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
181 else
182 {
183#ifdef GL_VERSION_3_1
184 glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, _numInstances);
185#else
186 std::cerr << "error: instanced ScreenQuad draw - outdated glew version" << std::endl;
187 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
188#endif
189 }
190
191 if (_prog)
193 else
195}
196
197void ScreenQuad::drawTexture2D( GLuint _texture, const Vec2f& _offset /*= Vec2f(0.0f, 0.0f)*/, const Vec2f& _size /*= Vec2f(1.0f, 1.0f)*/ )
198{
199 ScreenQuad& quad = instance();
200
201 if (!quad.texDrawProg_)
202 quad.init();
203
204 if (quad.texDrawProg_)
205 {
206 glActiveTexture(GL_TEXTURE0);
207 glEnable(GL_TEXTURE_2D);
208 glBindTexture(GL_TEXTURE_2D, _texture);
209
210
211 quad.texDrawProg_->use();
212
213 quad.texDrawProg_->setUniform("Tex", 0); // texslot 0
214
215 quad.texDrawProg_->setUniform("offset", _offset);
216 quad.texDrawProg_->setUniform("size", _size);
217
218
219 quad.intDraw(quad.texDrawProg_);
220
221
222 quad.texDrawProg_->disable();
223 }
224}
225
226//----------------------------------------------------------------------------
227
228//=============================================================================
229} // namespace ACG
230//=============================================================================
231
void intDraw(GLSL::Program *_prog, int _numInstances=0)
Internal draw function.
static ScreenQuad & instance()
Get singleton instance.
Definition ScreenQuad.cc:86
static void drawInstanced(int _count, GLSL::Program *_prog=0)
Draw the screen quad with instancing.
static void draw(GLSL::Program *_prog=0)
Draw the screen quad.
void init()
Initialize vbo and format.
Definition ScreenQuad.cc:94
VertexDeclaration * decl_
vertex format of screen quad (float2 pos)
GLSL::Program * texDrawProg_
Simple texture drawing shader.
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.
ScreenQuad()
Default constructor.
Definition ScreenQuad.cc:66
GLuint vbo_
vbo containing the quad in projected coordinates
~ScreenQuad()
Destructor.
Definition ScreenQuad.cc:75
Class to define the vertex input layout.
void addElement(const VertexElement *_pElement)
void deactivateShaderPipeline(GLSL::Program *_prog) const
void activateShaderPipeline(GLSL::Program *_prog) const
GLSL program class.
void disable()
Resets to standard rendering pipeline.
void use()
Enables the program object for using.
void setUniform(const char *_name, GLint _value)
Set int uniform to specified value.
Namespace providing different geometric functions concerning angles.
@ VERTEX_USAGE_POSITION
"inPosition"
void glCheckErrors()
Definition GLError.hh:96
GLSL::PtrProgram loadProgram(const char *vertexShaderFile, const char *tessControlShaderFile, const char *tessEvaluationShaderFile, const char *geometryShaderFile, const char *fragmentShaderFile, const GLSL::StringList *macros, bool verbose)