Developer Documentation
Renderer.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: 21016 $ *
45 * $LastChangedBy: schultz $ *
46 * $Date: 2015-07-16 16:48:42 +0200 (Do, 16 Jul 2015) $ *
47 * *
48 \*===========================================================================*/
49 
50 #include <cstdio>
51 #include <iostream>
52 #include <cstdlib>
53 
54 #include <ACG/GL/acg_glew.hh>
55 #include <ACG/GL/gl.hh>
56 
57 #include "Renderer.hh"
58 
59 #include <OpenFlipper/common/ViewObjectMarker.hh>
60 
61 #include <QGLFormat>
62 
63 using namespace ACG;
64 
65 // =================================================
66 
67 Renderer::Renderer()
68 {
69 }
70 
71 
72 Renderer::~Renderer()
73 {
74 }
75 
76 
77 void Renderer::initializePlugin()
78 {
79 }
80 
81 
82 void Renderer::render(ACG::GLState* _glState, Viewer::ViewerProperties& _properties)
83 {
85 
86  if (root) {
87  ViewObjectMarker *oM = _properties.objectMarker();
88  GLuint refBits = 0;
89  QSet<GLuint> references;
90 
91  if (oM)
92  {
93  glClear (GL_STENCIL_BUFFER_BIT);
94  ACG::GLState::enable (GL_STENCIL_TEST);
95  glStencilOp (GL_KEEP, GL_KEEP, GL_ZERO);
96  glStencilFunc (GL_ALWAYS, 0, ~0);
97 
99  o_it != PluginFunctions::objectsEnd(); ++o_it)
100  {
101  bool ok;
102  GLuint ref;
103 
104  ok = oM->stencilRefForObject(*o_it, ref);
105 
106  if (ok)
107  {
108  o_it->stencilRefNode ()->setReference (ref);
109  o_it->stencilRefNode ()->show ();
110  refBits |= ref;
111  references << ref;
112  }
113  else
114  o_it->stencilRefNode ()->hide ();
115  }
116  }
117 
118  ACG::SceneGraph::DrawAction action( _properties.drawMode(), *_glState , false);
119  ACG::SceneGraph::traverse_multipass(root, action, *_glState, _properties.drawMode() );
120 
121  // Second pass for blending
122  ACG::SceneGraph::DrawAction action1(_properties.drawMode(), *_glState, true);
123  ACG::SceneGraph::traverse_multipass(root, action1, *_glState, _properties.drawMode());
124 
125  if (oM)
126  {
127  if (oM->type() == ViewObjectMarker::PerBit)
128  {
129  references.clear ();
130  for (unsigned int i = 0; i < sizeof (GLuint) * 8; i++)
131  if (refBits & (1 << i))
132  references << (1 << i);
133  }
134 
135  glPushAttrib(GL_ALL_ATTRIB_BITS);
136 
137  ACG::GLState::enable(GL_BLEND);
138  ACG::GLState::disable(GL_DEPTH_TEST);
139  ACG::GLState::disable(GL_LIGHTING);
140  ACG::GLState::disable(GL_DITHER);
141 
142  int vp_l, vp_b, vp_w, vp_h;
143  _glState->get_viewport (vp_l, vp_b, vp_w, vp_h);
144 
145  glMatrixMode(GL_PROJECTION);
146  glPushMatrix ();
147  glLoadIdentity();
148  glOrtho(0, vp_w, vp_h, 0, 0, 1.0);
149  glMatrixMode(GL_MODELVIEW);
150  glPushMatrix ();
151  glLoadIdentity();
152 
153  glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
154 
155  foreach (unsigned int ref, references)
156  {
157  bool ok;
158  GLenum sfactor;
159  GLenum dfactor;
160  ACG::Vec4f color;
161  unsigned int mask = ~0;
162 
163  if (oM->type() == ViewObjectMarker::PerBit)
164  {
165  ok = oM->blendForStencilRefBit (ref, sfactor, dfactor, color);
166  mask = ref;
167  }
168  else
169  ok = oM->blendForStencilRefNumber (ref, sfactor, dfactor, color);
170 
171  if (!ok)
172  continue;
173 
174  glStencilFunc (GL_EQUAL, ref, mask);
175 
176  ACG::GLState::blendFunc (sfactor, dfactor);
177  glColor4f (color[0], color [1], color [2], color[3]);
178 
179  glBegin (GL_QUADS);
180  glVertex2i(0, 0);
181  glVertex2i(0, vp_h);
182  glVertex2i(vp_w, vp_h);
183  glVertex2i(vp_w, 0);
184  glEnd ();
185 
186  }
187 
188  glMatrixMode(GL_PROJECTION);
189  glPopMatrix ();
190  glMatrixMode(GL_MODELVIEW);
191  glPopMatrix ();
192 
193  glPopAttrib ();
194  ACG::GLState::disable (GL_STENCIL_TEST);
195  }
196 
197 
198  }
199 }
200 
201 QString Renderer::checkOpenGL()
202 {
203  // Get version and check
204  if ( QGLContext::currentContext()->format().profile() != QGLFormat::CompatibilityProfile )
205  return QString("This plugin requires an OpenGL compatibility context to work.");
206 
207  // This renderer plugin should run on almost any old style hardware
208  return QString("");
209 
210 }
211 
212 
213 #if QT_VERSION < 0x050000
214  Q_EXPORT_PLUGIN2( classicalrenderer , Renderer );
215 #endif
216 
217 
static void enable(GLenum _cap)
replaces glEnable, but supports locking
Definition: GLState.cc:1490
virtual bool blendForStencilRefBit(GLuint _refbit, GLenum &_src, GLenum &_dst, ACG::Vec4f &_color)
virtual bool stencilRefForObject(BaseObjectData *_obj, GLuint &_reference)=0
void traverse_multipass(BaseNode *_node, Action &_action, const unsigned int &_pass)
Definition: SceneGraph.hh:260
void objectMarker(ViewObjectMarker *_marker)
set object marker for viewer
const QStringList ALL_OBJECTS
Iterable object range.
const DataType DATA_ALL(UINT_MAX)
Identifier for all available objects.
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
static void disable(GLenum _cap)
replaces glDisable, but supports locking
Definition: GLState.cc:1504
void drawMode(ACG::SceneGraph::DrawModes::DrawMode _mode)
set draw mode (No test if this mode is available!)
Mark per returned reference bits.
ACG::SceneGraph::BaseNode * getSceneGraphRootNode()
get scenegraph root node
void get_viewport(int &_left, int &_bottom, int &_width, int &_height) const
get viewport
Definition: GLState.hh:819
virtual bool blendForStencilRefNumber(GLuint _reference, GLenum &_src, GLenum &_dst, ACG::Vec4f &_color)
DLLEXPORT ObjectIterator objectsEnd()
Return Iterator to Object End.
static void blendFunc(GLenum _sfactor, GLenum _dfactor)
replaces glBlendFunc, supports locking
Definition: GLState.hh:314