1 // renderingpipeline.cxx -- description of the cameras needed by the Rembrandt renderer
3 // Written by Curtis Olson, started May 1997.
4 // This file contains parts of main.cxx prior to october 2004
6 // Copyright (C) 1997 - 2012 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
30 #include <boost/lexical_cast.hpp>
33 #include <osg/FrameBufferObject> // For texture formats
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>
44 #include "renderingpipeline.hxx"
45 #include "CameraGroup.hxx"
46 #include <Main/globals.hxx>
47 #include "renderer.hxx"
49 namespace flightgear {
51 FGRenderingPipeline* makeRenderingPipeline(const std::string& name,
52 const simgear::SGReaderWriterOptions* options)
54 std::string fileName = "Effects/" + name;
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 << "\"");
62 SGPropertyNode_ptr effectProps = new SGPropertyNode();
64 readProperties(absFileName, effectProps.ptr(), 0, true);
66 catch (sg_io_exception& e) {
67 SG_LOG(SG_INPUT, SG_ALERT, "error reading \"" << absFileName << "\": "
68 << e.getFormattedMessage());
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]));
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]));
83 return pipeline.release();
89 FGRenderingPipeline::Conditionable::parseCondition(SGPropertyNode* prop)
91 const SGPropertyNode* predProp = prop->getChild("condition");
96 flightgear::PipelinePredParser parser;
97 SGExpressionb* validExp = dynamic_cast<SGExpressionb*>(parser.read(predProp->getChild(0)));
99 setValidExpression(validExp);
101 throw simgear::expression::ParseError("pipeline condition is not a boolean expression");
103 catch (simgear::expression::ParseError& except)
105 SG_LOG(SG_INPUT, SG_ALERT,
106 "parsing pipeline condition " << except.getMessage());
107 setAlwaysValid(false);
112 void FGRenderingPipeline::Conditionable::setValidExpression(SGExpressionb* exp)
114 _validExpression = exp;
117 bool FGRenderingPipeline::Conditionable::valid()
119 return _alwaysValid || _validExpression->getValue();
123 void findAttrOrHex(const simgear::effect::EffectPropertyMap<T>& pMap,
124 const SGPropertyNode* prop,
128 simgear::findAttr(pMap, prop, result);
129 } catch (simgear::effect::BuilderException&) {
130 std::string val = prop->getStringValue();
132 result = boost::lexical_cast<T>(val);
133 } catch (boost::bad_lexical_cast &) {
134 throw simgear::effect::BuilderException(string("findAttrOrHex: could not find attribute ")
140 const SGPropertyNode* getPropertyNode(const SGPropertyNode* prop)
144 if (prop->nChildren() > 0) {
145 const SGPropertyNode* propertyProp = prop->getChild("property");
148 return globals->get_props()->getNode(propertyProp->getStringValue());
153 const SGPropertyNode* getPropertyChild(const SGPropertyNode* prop,
156 const SGPropertyNode* child = prop->getChild(name);
160 return getPropertyNode(child);
163 simgear::effect::EffectNameValue<GLint> internalFormatInit[] =
166 { "rgba8", GL_RGBA8 },
167 { "rgb16", GL_RGB16 },
168 { "rgba16", GL_RGBA16 },
170 { "depth-component24", GL_DEPTH_COMPONENT24 },
171 { "depth-component32", GL_DEPTH_COMPONENT32 }
173 simgear::effect::EffectPropertyMap<GLint> internalFormats(internalFormatInit);
175 simgear::effect::EffectNameValue<GLenum> sourceFormatInit[] =
180 { "depth-component", GL_DEPTH_COMPONENT }
182 simgear::effect::EffectPropertyMap<GLenum> sourceFormats(sourceFormatInit);
184 simgear::effect::EffectNameValue<GLenum> sourceTypeInit[] =
186 { "unsigned-byte", GL_UNSIGNED_BYTE },
187 { "unsigned-short", GL_UNSIGNED_SHORT },
188 { "unsigned-int", GL_UNSIGNED_INT },
189 { "float", GL_FLOAT }
191 simgear::effect::EffectPropertyMap<GLenum> sourceTypes(sourceTypeInit);
193 simgear::effect::EffectNameValue<GLenum> wrapModesInit[] =
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}
201 simgear::effect::EffectPropertyMap<GLenum> wrapModes(wrapModesInit);
203 FGRenderingPipeline::Buffer::Buffer(SGPropertyNode* prop)
205 SGPropertyNode_ptr nameProp = prop->getChild("name");
206 if (!nameProp.valid()) {
207 throw sg_exception("Buffer name is mandatory");
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");
222 findAttrOrHex(sourceTypes, sourceTypeProp, sourceType);
223 SGPropertyNode* wrapModeProp = prop->getChild("wrap-mode");
225 findAttrOrHex(wrapModes, wrapModeProp, wrapMode);
226 SGConstPropertyNode_ptr widthProp = getPropertyChild(prop, "width");
227 if (!widthProp.valid())
229 else if (widthProp->getStringValue() == std::string("screen"))
232 width = widthProp->getIntValue();
234 SGConstPropertyNode_ptr heightProp = getPropertyChild(prop, "height");
235 if (!heightProp.valid())
237 else if (heightProp->getStringValue() == std::string("screen"))
240 height = heightProp->getIntValue();
242 scaleFactor = prop->getFloatValue("scale-factor", 1.f);
243 shadowComparison = prop->getBoolValue("shadow-comparison", false);
245 parseCondition(prop);
248 simgear::effect::EffectNameValue<osg::Camera::BufferComponent> componentsInit[] =
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}
258 simgear::effect::EffectPropertyMap<osg::Camera::BufferComponent> components(componentsInit);
260 FGRenderingPipeline::Stage::Stage(SGPropertyNode* prop)
262 SGPropertyNode_ptr nameProp = prop->getChild("name");
263 if (!nameProp.valid()) {
264 throw sg_exception("Stage name is mandatory");
266 name = nameProp->getStringValue();
267 SGPropertyNode_ptr typeProp = prop->getChild("type");
268 if (!typeProp.valid()) {
269 type = nameProp->getStringValue();
271 type = typeProp->getStringValue();
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);
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]));
284 parseCondition(prop);
287 FGRenderingPipeline::Attachment::Attachment(SGPropertyNode* prop)
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");
294 buffer = bufferProp->getStringValue();
296 parseCondition(prop);
299 FGRenderingPipeline::FGRenderingPipeline()
303 class FGStageCameraCullCallback : public osg::NodeCallback {
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);
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 );
315 if ( !info->getRenderStageInfo(stage->name).fullscreen )
316 info->setMatrices( camera );
318 cv->traverse( *camera );
322 osg::ref_ptr<FGRenderingPipeline::Stage> stage;
323 flightgear::CameraInfo* info;