Developer Documentation

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
52 #include <OpenFlipper/common/GlobalOptions.hh>
53 #include <OpenFlipper/BasePlugin/PluginFunctions.hh>
54 #include <ACG/GL/ShaderCache.hh>
55
56
57
58 // =================================================
59
60 class NormalFragmentModifier : public ACG::ShaderModifier
61 {
62 public:
63
64 void modifyVertexIO( ACG::ShaderGenerator* _shader )
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
Viewer::ViewerProperties
Definition: ViewerProperties.hh:119
ACG::IRenderer::renderObject
virtual void renderObject(ACG::RenderObject *_obj, GLSL::Program *_prog=0, bool _constRenderStates=false, const std::vector< unsigned int > *_shaderModifiers=0)
Render one renderobject.
Definition: IRenderer.cc:986
ACG::ShaderGenerator::addDefine
void addDefine(const QString &_define)
Add one define.
Definition: ShaderGenerator.cc:478
NormalRenderer
Definition: NormalRenderer.hh:59
ACG::IRenderer::sortedObjects_
std::vector< ACG::RenderObject * > sortedObjects_
sorted list of renderobjects without overlay objects (sorted in rendering order)
Definition: IRenderer.hh:463
ACG::ShaderProgGenerator::registerModifier
static unsigned int registerModifier(ShaderModifier *_modifier)
Shader modifiers have to be registered before they can be used. They also must remain allocated for t...
Definition: ShaderGenerator.cc:2412
Viewer::ViewerProperties::drawMode
void drawMode(ACG::SceneGraph::DrawModes::DrawMode _mode)
set draw mode (No test if this mode is available!)
Definition: ViewerProperties.cc:162
ACG::openGLVersion
bool openGLVersion(const int _major, const int _minor)
Definition: gl.cc:95
PluginFunctions::getSceneGraphRootNode
ACG::SceneGraph::BaseNode * getSceneGraphRootNode()
get scenegraph root node
Definition: PluginFunctions.cc:961
ACG::GLState
Definition: GLState.hh:220
NormalFragmentModifier
Definition: NormalRenderer.cc:60
ACG::checkExtensionSupported
bool checkExtensionSupported(const std::string &_extension)
Definition: gl.cc:73
ACG::IRenderer::dumpCurrentRenderObjectsToString
virtual QString dumpCurrentRenderObjectsToString(ACG::RenderObject **_list=0, bool _outputShaders=false, std::vector< ACG::ShaderModifier * > *_modifiers=0)
Outputs the current render objects to the string.
Definition: IRenderer.cc:1135
ACG::ShaderCache::getProgram
GLSL::Program * getProgram(const ShaderGenDesc *_desc, const std::vector< unsigned int > &_mods)
Query a dynamically generated program from cache.
Definition: ShaderCache.cc:108
ACG::ShaderModifier
Definition: ShaderGenerator.hh:932
NormalFragmentModifier::modifyVertexIO
void modifyVertexIO(ACG::ShaderGenerator *_shader)
Add your own inputs/outputs to the vertex shader.
Definition: NormalRenderer.cc:64
ACG::ShaderCache::getInstance
static ShaderCache * getInstance()
Return instance of the ShaderCache singleton.
Definition: ShaderCache.cc:90
GlobalOptions.hh
GLSL::Program
GLSL program class.
Definition: GLSLShader.hh:217
ACG::ShaderProgGenerator::setShaderDir
static void setShaderDir(QString _dir)
Definition: ShaderGenerator.cc:2401
ACG::IRenderer::finishRenderingPipeline
virtual void finishRenderingPipeline(bool _drawOverlay=true)
Draw overlay objects and reset OpenGL state.
Definition: IRenderer.cc:589
NormalFragmentModifier::modifyFragmentEndCode
void modifyFragmentEndCode(QStringList *_code)
Append code the the fragment shader.
Definition: NormalRenderer.cc:70
ACG::IRenderer::prepareRenderingPipeline
virtual void prepareRenderingPipeline(ACG::GLState *_glState, ACG::SceneGraph::DrawModes::DrawMode _drawMode, ACG::SceneGraph::BaseNode *_scenegraphRoot)
Prepares renderer and OpenGL for any draw-related calls including.
Definition: IRenderer.cc:486
PluginFunctions.hh
ACG::ShaderGenerator
Definition: ShaderGenerator.hh:518
ACG::IRenderer::getNumRenderObjects
int getNumRenderObjects() const
Get the number of collected render objects (not including overlay objects or gl4.2 line objects) ...
Definition: IRenderer.cc:1045
NormalRenderer::renderObjectsInfo
QString renderObjectsInfo(bool _outputShaderInfo)
Return a qstring of the current render objects.
Definition: NormalRenderer.cc:101