Developer Documentation
NormalRenderer.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 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #include "NormalRenderer.hh"
51 
54 #include <ACG/GL/ShaderCache.hh>
55 
56 
57 
58 // =================================================
59 
61 {
62 public:
63 
65  {
66  // request object space normals
67  _shader->addDefine("SG_REQUEST_NORMALOS");
68  }
69 
70  void modifyFragmentEndCode(QStringList* _code)
71  {
72  _code->push_back("#ifdef SG_INPUT_NORMALOS");
73  _code->push_back("outFragment = vec4(normalize(SG_INPUT_NORMALOS) * 0.5 + vec3(0.5, 0.5, 0.5),1.0);");
74  _code->push_back("#endif ");
75  }
76 
77  static NormalFragmentModifier instance;
78 };
79 
80 
81 NormalFragmentModifier NormalFragmentModifier::instance;
82 
83 // =================================================
84 
85 NormalRenderer::NormalRenderer()
86 {
87  ACG::ShaderProgGenerator::registerModifier(&NormalFragmentModifier::instance);
88 }
89 
90 
91 NormalRenderer::~NormalRenderer()
92 {
93 }
94 
95 
96 void NormalRenderer::initializePlugin()
97 {
98  ACG::ShaderProgGenerator::setShaderDir(OpenFlipper::Options::shaderDirStr());
99 }
100 
101 QString NormalRenderer::renderObjectsInfo(bool _outputShaderInfo) {
102  std::vector<ACG::ShaderModifier*> modifiers;
103  modifiers.push_back(&NormalFragmentModifier::instance);
104  return dumpCurrentRenderObjectsToString(&sortedObjects_[0], _outputShaderInfo, &modifiers);
105 }
106 
107 void NormalRenderer::render(ACG::GLState* _glState, Viewer::ViewerProperties& _properties)
108 {
109  // collect renderobjects + prepare OpenGL state
110  prepareRenderingPipeline(_glState, _properties.drawMode(), PluginFunctions::getSceneGraphRootNode());
111 
112  // render every object
113  for (int i = 0; i < getNumRenderObjects(); ++i) {
114 
115  // Take original shader and modify the output to take only the normal as the color
116  GLSL::Program* prog = ACG::ShaderCache::getInstance()->getProgram(&sortedObjects_[i]->shaderDesc, NormalFragmentModifier::instance);
117  renderObject(sortedObjects_[i],prog);
118  }
119 
120  // restore common opengl state
121  // log window remains hidden otherwise
122  finishRenderingPipeline();
123 }
124 
125 QString NormalRenderer::checkOpenGL()
126 {
127  if (!ACG::openGLVersion(3,2))
128  return QString("Insufficient OpenGL Version! OpenGL 3.2 or higher required");
129 
130  QString missing("");
131  if ( !ACG::checkExtensionSupported("GL_ARB_vertex_buffer_object") )
132  missing += "GL_ARB_vertex_buffer_object extension missing\n";
133 
134 #ifndef __APPLE__
135  if ( !ACG::checkExtensionSupported("GL_ARB_vertex_program") )
136  missing += "GL_ARB_vertex_program extension missing\n";
137 #endif
138 
139  return missing;
140 }
141 
142 #if QT_VERSION < 0x050000
143  Q_EXPORT_PLUGIN2( normalrenderer , NormalRenderer );
144 #endif
145 
146 
static void setShaderDir(QString _dir)
void modifyVertexIO(ACG::ShaderGenerator *_shader)
Add your own inputs/outputs to the vertex shader.
static ShaderCache * getInstance()
Return instance of the ShaderCache singleton.
Definition: ShaderCache.cc:90
bool openGLVersion(const int _major, const int _minor)
Definition: gl.cc:95
void modifyFragmentEndCode(QStringList *_code)
Append code the the fragment shader.
QString renderObjectsInfo(bool _outputShaderInfo)
Return a qstring of the current render objects.
GLSL::Program * getProgram(const ShaderGenDesc *_desc, const std::vector< unsigned int > &_mods)
Query a dynamically generated program from cache.
Definition: ShaderCache.cc:108
void drawMode(ACG::SceneGraph::DrawModes::DrawMode _mode)
set draw mode (No test if this mode is available!)
void addDefine(const QString &_define)
Add one define.
bool checkExtensionSupported(const std::string &_extension)
Definition: gl.cc:73
ACG::SceneGraph::BaseNode * getSceneGraphRootNode()
get scenegraph root node
static unsigned int registerModifier(ShaderModifier *_modifier)
Shader modifiers have to be registered before they can be used. They also must remain allocated for t...
GLSL program class.
Definition: GLSLShader.hh:217