]> git.mxchange.org Git - flightgear.git/blob - src/Viewer/renderingpipeline.cxx
Add simple conditions (no opengl related tests) to buffers, stages and attachments
[flightgear.git] / src / Viewer / renderingpipeline.cxx
1 // renderingpipeline.cxx -- description of the cameras needed by the Rembrandt renderer
2 //
3 // Written by Curtis Olson, started May 1997.
4 // This file contains parts of main.cxx prior to october 2004
5 //
6 // Copyright (C) 1997 - 2012  Curtis L. Olson  - http://www.flightgear.org/~curt
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #ifdef HAVE_WINDOWS_H
27 #  include <windows.h>
28 #endif
29
30 #include <boost/lexical_cast.hpp>
31
32 #include <osg/GL>
33 #include <osg/FrameBufferObject> // For texture formats
34
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
37 #include <simgear/scene/model/modellib.hxx>
38 #include <simgear/scene/material/EffectBuilder.hxx>
39 #include <simgear/scene/material/EffectCullVisitor.hxx>
40 #include <simgear/props/props_io.hxx>
41 #include <simgear/structure/exception.hxx>
42 #include <simgear/math/SGMath.hxx>
43
44 #include "renderingpipeline.hxx"
45 #include "CameraGroup.hxx"
46 #include <Main/globals.hxx>
47 #include "renderer.hxx"
48
49 namespace flightgear {
50
51 FGRenderingPipeline* makeRenderingPipeline(const std::string& name,
52                    const simgear::SGReaderWriterOptions* options)
53 {
54     std::string fileName = "Effects/" + name;
55     fileName += ".xml";
56     std::string absFileName
57         = simgear::SGModelLib::findDataFile(fileName, options);
58     if (absFileName.empty()) {
59         SG_LOG(SG_INPUT, SG_ALERT, "can't find \"" << fileName << "\"");
60         return 0;
61     }
62     SGPropertyNode_ptr effectProps = new SGPropertyNode();
63     try {
64         readProperties(absFileName, effectProps.ptr(), 0, true);
65     }
66     catch (sg_io_exception& e) {
67         SG_LOG(SG_INPUT, SG_ALERT, "error reading \"" << absFileName << "\": "
68                << e.getFormattedMessage());
69         return 0;
70     }
71
72     osg::ref_ptr<FGRenderingPipeline> pipeline = new FGRenderingPipeline;
73     std::vector<SGPropertyNode_ptr> buffers = effectProps->getChildren("buffer");
74     for (int i = 0; i < (int)buffers.size(); ++i) {
75         pipeline->buffers.push_back(new FGRenderingPipeline::Buffer(buffers[i]));
76     }
77
78     std::vector<SGPropertyNode_ptr> stages = effectProps->getChildren("stage");
79     for (int i = 0; i < (int)stages.size(); ++i) {
80         pipeline->stages.push_back(new FGRenderingPipeline::Stage(stages[i]));
81     }
82
83     return pipeline.release();
84 }
85
86 }
87
88 void
89 FGRenderingPipeline::Conditionable::parseCondition(SGPropertyNode* prop)
90 {
91     const SGPropertyNode* predProp = prop->getChild("condition");
92     if (!predProp) {
93         setAlwaysValid(true);
94     } else {
95         try {
96             flightgear::PipelinePredParser parser;
97             SGExpressionb* validExp = dynamic_cast<SGExpressionb*>(parser.read(predProp->getChild(0)));
98             if (validExp)
99                 setValidExpression(validExp);
100             else
101                 throw simgear::expression::ParseError("pipeline condition is not a boolean expression");
102         }
103         catch (simgear::expression::ParseError& except)
104         {
105             SG_LOG(SG_INPUT, SG_ALERT,
106                    "parsing pipeline condition " << except.getMessage());
107             setAlwaysValid(false);
108         }
109     }
110 }
111
112 void FGRenderingPipeline::Conditionable::setValidExpression(SGExpressionb* exp)
113 {
114     _validExpression = exp;
115 }
116
117 bool FGRenderingPipeline::Conditionable::valid()
118 {
119     return _alwaysValid || _validExpression->getValue();
120 }
121
122 template<typename T>
123 void findAttrOrHex(const simgear::effect::EffectPropertyMap<T>& pMap,
124               const SGPropertyNode* prop,
125               T& result)
126 {
127     try {
128         simgear::findAttr(pMap, prop, result);
129     } catch (simgear::effect::BuilderException&) {
130         std::string val = prop->getStringValue();
131         try {
132             result = boost::lexical_cast<T>(val);
133         } catch (boost::bad_lexical_cast &) {
134             throw simgear::effect::BuilderException(string("findAttrOrHex: could not find attribute ")
135                                    + string(val));
136         }
137     }
138 }
139
140 const SGPropertyNode* getPropertyNode(const SGPropertyNode* prop)
141 {
142     if (!prop)
143         return 0;
144     if (prop->nChildren() > 0) {
145         const SGPropertyNode* propertyProp = prop->getChild("property");
146         if (!propertyProp)
147             return prop;
148         return globals->get_props()->getNode(propertyProp->getStringValue());
149     }
150     return prop;
151 }
152
153 const SGPropertyNode* getPropertyChild(const SGPropertyNode* prop,
154                                              const char* name)
155 {
156     const SGPropertyNode* child = prop->getChild(name);
157     if (!child)
158         return 0;
159     else
160         return getPropertyNode(child);
161 }
162
163 simgear::effect::EffectNameValue<GLint> internalFormatInit[] =
164 {
165     { "rgb8", GL_RGB8 },
166     { "rgba8", GL_RGBA8 },
167     { "rgb16", GL_RGB16 },
168     { "rgba16", GL_RGBA16 },
169     { "rg16", 0x822C },
170     { "depth-component24", GL_DEPTH_COMPONENT24 },
171     { "depth-component32", GL_DEPTH_COMPONENT32 }
172 };
173 simgear::effect::EffectPropertyMap<GLint> internalFormats(internalFormatInit);
174
175 simgear::effect::EffectNameValue<GLenum> sourceFormatInit[] =
176 {
177     { "rg", 0x8227 },
178     { "rgb", GL_RGB },
179     { "rgba", GL_RGBA },
180     { "depth-component", GL_DEPTH_COMPONENT }
181 };
182 simgear::effect::EffectPropertyMap<GLenum> sourceFormats(sourceFormatInit);
183
184 simgear::effect::EffectNameValue<GLenum> sourceTypeInit[] =
185 {
186     { "unsigned-byte", GL_UNSIGNED_BYTE },
187     { "unsigned-short", GL_UNSIGNED_SHORT },
188     { "unsigned-int", GL_UNSIGNED_INT },
189     { "float", GL_FLOAT }
190 };
191 simgear::effect::EffectPropertyMap<GLenum> sourceTypes(sourceTypeInit);
192
193 simgear::effect::EffectNameValue<GLenum> wrapModesInit[] =
194 {
195     {"clamp", GL_CLAMP},
196     {"clamp-to-border", GL_CLAMP_TO_BORDER_ARB},
197     {"clamp-to-edge", GL_CLAMP_TO_EDGE},
198     {"mirror", GL_MIRRORED_REPEAT_IBM},
199     {"repeat", GL_REPEAT}
200 };
201 simgear::effect::EffectPropertyMap<GLenum> wrapModes(wrapModesInit);
202
203 FGRenderingPipeline::Buffer::Buffer(SGPropertyNode* prop)
204 {
205     SGPropertyNode_ptr nameProp = prop->getChild("name");
206     if (!nameProp.valid()) {
207         throw sg_exception("Buffer name is mandatory");
208     }
209     internalFormat = GL_RGBA8;
210     sourceFormat = GL_RGBA;
211     sourceType = GL_UNSIGNED_BYTE;
212     wrapMode = GL_CLAMP_TO_BORDER_ARB;
213     name = nameProp->getStringValue();
214     SGPropertyNode* internalFormatProp = prop->getChild("internal-format");
215     if (internalFormatProp)
216         findAttrOrHex(internalFormats, internalFormatProp, internalFormat);
217     SGPropertyNode* sourceFormatProp = prop->getChild("source-format");
218     if (sourceFormatProp)
219         findAttrOrHex(sourceFormats, sourceFormatProp, sourceFormat);
220     SGPropertyNode* sourceTypeProp = prop->getChild("source-type");
221     if (sourceTypeProp)
222         findAttrOrHex(sourceTypes, sourceTypeProp, sourceType);
223     SGPropertyNode* wrapModeProp = prop->getChild("wrap-mode");
224     if (wrapModeProp)
225         findAttrOrHex(wrapModes, wrapModeProp, wrapMode);
226     SGConstPropertyNode_ptr widthProp = getPropertyChild(prop, "width");
227     if (!widthProp.valid())
228         width = -1;
229     else if (widthProp->getStringValue() == std::string("screen"))
230         width = -1;
231     else {
232         width = widthProp->getIntValue();
233     }
234     SGConstPropertyNode_ptr heightProp = getPropertyChild(prop, "height");
235     if (!heightProp.valid())
236         height = -1;
237     else if (heightProp->getStringValue() == std::string("screen"))
238         height = -1;
239     else
240         height = heightProp->getIntValue();
241
242     scaleFactor = prop->getFloatValue("scale-factor", 1.f);
243     shadowComparison = prop->getBoolValue("shadow-comparison", false);
244
245     parseCondition(prop);
246 }
247
248 simgear::effect::EffectNameValue<osg::Camera::BufferComponent> componentsInit[] =
249 {
250     {"depth",   osg::Camera::DEPTH_BUFFER},
251     {"stencil", osg::Camera::STENCIL_BUFFER},
252     {"packed-depth-stencil",   osg::Camera::PACKED_DEPTH_STENCIL_BUFFER},
253     {"color0",  osg::Camera::COLOR_BUFFER0},
254     {"color1",  osg::Camera::COLOR_BUFFER1},
255     {"color2",  osg::Camera::COLOR_BUFFER2},
256     {"color3",  osg::Camera::COLOR_BUFFER3}
257 };
258 simgear::effect::EffectPropertyMap<osg::Camera::BufferComponent> components(componentsInit);
259
260 FGRenderingPipeline::Stage::Stage(SGPropertyNode* prop)
261 {
262     SGPropertyNode_ptr nameProp = prop->getChild("name");
263     if (!nameProp.valid()) {
264         throw sg_exception("Stage name is mandatory");
265     }
266     name = nameProp->getStringValue();
267     SGPropertyNode_ptr typeProp = prop->getChild("type");
268     if (!typeProp.valid()) {
269         type = nameProp->getStringValue();
270     } else {
271         type = typeProp->getStringValue();
272     }
273
274     orderNum = prop->getIntValue("order-num", -1);
275     effect = prop->getStringValue("effect", "");
276     needsDuDv = prop->getBoolValue("needs-du-dv", false);
277     scaleFactor = prop->getFloatValue("scale-factor", 1.f);
278
279     std::vector<SGPropertyNode_ptr> attachments = prop->getChildren("attachment");
280     for (int i = 0; i < (int)attachments.size(); ++i) {
281         this->attachments.push_back(new FGRenderingPipeline::Attachment(attachments[i]));
282     }
283
284     parseCondition(prop);
285 }
286
287 FGRenderingPipeline::Attachment::Attachment(SGPropertyNode* prop)
288 {
289     simgear::findAttr(components, prop->getChild("component"), component);
290     SGPropertyNode_ptr bufferProp = prop->getChild("buffer");
291     if (!bufferProp.valid()) {
292         throw sg_exception("Attachment buffer is mandatory");
293     }
294     buffer = bufferProp->getStringValue();
295
296     parseCondition(prop);
297 }
298
299 FGRenderingPipeline::FGRenderingPipeline()
300 {
301 }
302
303 class FGStageCameraCullCallback : public osg::NodeCallback {
304 public:
305     FGStageCameraCullCallback(FGRenderingPipeline::Stage* s, flightgear::CameraInfo* i) : stage(s), info(i) {}
306     virtual void operator()( osg::Node *n, osg::NodeVisitor *nv) {
307         simgear::EffectCullVisitor* cv = dynamic_cast<simgear::EffectCullVisitor*>(nv);
308         osg::Camera* camera = static_cast<osg::Camera*>(n);
309
310         cv->clearBufferList();
311         for (flightgear::RenderBufferMap::iterator ii = info->buffers.begin(); ii != info->buffers.end(); ++ii) {
312             cv->addBuffer(ii->first, ii->second.texture );
313         }
314
315                 if ( !info->getRenderStageInfo(stage->name).fullscreen )
316                         info->setMatrices( camera );
317
318         cv->traverse( *camera );
319     }
320
321 private:
322     osg::ref_ptr<FGRenderingPipeline::Stage> stage;
323     flightgear::CameraInfo* info;
324 };