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 void findAttrOrHex(const simgear::effect::EffectPropertyMap<T>& pMap,
90 const SGPropertyNode* prop,
94 simgear::findAttr(pMap, prop, result);
95 } catch (simgear::effect::BuilderException&) {
96 std::string val = prop->getStringValue();
98 result = boost::lexical_cast<T>(val);
99 } catch (boost::bad_lexical_cast &) {
100 throw simgear::effect::BuilderException(string("findAttrOrHex: could not find attribute ")
106 const SGPropertyNode* getPropertyNode(const SGPropertyNode* prop)
110 if (prop->nChildren() > 0) {
111 const SGPropertyNode* propertyProp = prop->getChild("property");
114 return globals->get_props()->getNode(propertyProp->getStringValue());
119 const SGPropertyNode* getPropertyChild(const SGPropertyNode* prop,
122 const SGPropertyNode* child = prop->getChild(name);
126 return getPropertyNode(child);
129 simgear::effect::EffectNameValue<GLint> internalFormatInit[] =
132 { "rgba8", GL_RGBA8 },
133 { "rgb16", GL_RGB16 },
134 { "rgba16", GL_RGBA16 },
136 { "depth-component24", GL_DEPTH_COMPONENT24 },
137 { "depth-component32", GL_DEPTH_COMPONENT32 }
139 simgear::effect::EffectPropertyMap<GLint> internalFormats(internalFormatInit);
141 simgear::effect::EffectNameValue<GLenum> sourceFormatInit[] =
146 { "depth-component", GL_DEPTH_COMPONENT }
148 simgear::effect::EffectPropertyMap<GLenum> sourceFormats(sourceFormatInit);
150 simgear::effect::EffectNameValue<GLenum> sourceTypeInit[] =
152 { "unsigned-byte", GL_UNSIGNED_BYTE },
153 { "unsigned-short", GL_UNSIGNED_SHORT },
154 { "unsigned-int", GL_UNSIGNED_INT },
155 { "float", GL_FLOAT }
157 simgear::effect::EffectPropertyMap<GLenum> sourceTypes(sourceTypeInit);
159 simgear::effect::EffectNameValue<GLenum> wrapModesInit[] =
162 {"clamp-to-border", GL_CLAMP_TO_BORDER_ARB},
163 {"clamp-to-edge", GL_CLAMP_TO_EDGE},
164 {"mirror", GL_MIRRORED_REPEAT_IBM},
165 {"repeat", GL_REPEAT}
167 simgear::effect::EffectPropertyMap<GLenum> wrapModes(wrapModesInit);
169 FGRenderingPipeline::Buffer::Buffer(SGPropertyNode* prop)
171 SGPropertyNode_ptr nameProp = prop->getChild("name");
172 if (!nameProp.valid()) {
173 throw sg_exception("Buffer name is mandatory");
175 name = nameProp->getStringValue();
176 findAttrOrHex(internalFormats, prop->getChild("internal-format"), internalFormat);
177 findAttrOrHex(sourceFormats, prop->getChild("source-format"), sourceFormat);
178 findAttrOrHex(sourceTypes, prop->getChild("source-type"), sourceType);
179 findAttrOrHex(wrapModes, prop->getChild("wrap-mode"), wrapMode);
180 SGConstPropertyNode_ptr widthProp = getPropertyChild(prop, "width");
181 if (!widthProp.valid())
183 else if (widthProp->getStringValue() == std::string("screen"))
186 width = widthProp->getIntValue();
188 SGConstPropertyNode_ptr heightProp = getPropertyChild(prop, "height");
189 if (!heightProp.valid())
191 else if (heightProp->getStringValue() == std::string("screen"))
194 height = heightProp->getIntValue();
196 scaleFactor = prop->getFloatValue("scale-factor", 1.f);
197 shadowComparison = prop->getBoolValue("shadow-comparison", false);
200 simgear::effect::EffectNameValue<osg::Camera::BufferComponent> componentsInit[] =
202 {"depth", osg::Camera::DEPTH_BUFFER},
203 {"stencil", osg::Camera::STENCIL_BUFFER},
204 {"packed-depth-stencil", osg::Camera::PACKED_DEPTH_STENCIL_BUFFER},
205 {"color0", osg::Camera::COLOR_BUFFER0},
206 {"color1", osg::Camera::COLOR_BUFFER1},
207 {"color2", osg::Camera::COLOR_BUFFER2},
208 {"color3", osg::Camera::COLOR_BUFFER3}
210 simgear::effect::EffectPropertyMap<osg::Camera::BufferComponent> components(componentsInit);
212 FGRenderingPipeline::Stage::Stage(SGPropertyNode* prop)
214 SGPropertyNode_ptr nameProp = prop->getChild("name");
215 if (!nameProp.valid()) {
216 throw sg_exception("Stage name is mandatory");
218 name = nameProp->getStringValue();
219 SGPropertyNode_ptr typeProp = prop->getChild("type");
220 if (!typeProp.valid()) {
221 type = nameProp->getStringValue();
223 type = typeProp->getStringValue();
226 orderNum = prop->getIntValue("order-num", -1);
228 effect = prop->getStringValue("effect", "");
230 std::vector<SGPropertyNode_ptr> attachments = prop->getChildren("attachment");
231 for (int i = 0; i < (int)attachments.size(); ++i) {
232 this->attachments.push_back(new FGRenderingPipeline::Attachment(attachments[i]));
236 FGRenderingPipeline::Attachment::Attachment(SGPropertyNode* prop)
238 simgear::findAttr(components, prop->getChild("component"), component);
239 SGPropertyNode_ptr bufferProp = prop->getChild("buffer");
240 if (!bufferProp.valid()) {
241 throw sg_exception("Attachment buffer is mandatory");
243 buffer = bufferProp->getStringValue();
246 FGRenderingPipeline::FGRenderingPipeline()
250 class FGStageCameraCullCallback : public osg::NodeCallback {
252 FGStageCameraCullCallback(FGRenderingPipeline::Stage* s, flightgear::CameraInfo* i) : stage(s), info(i) {}
253 virtual void operator()( osg::Node *n, osg::NodeVisitor *nv) {
254 simgear::EffectCullVisitor* cv = dynamic_cast<simgear::EffectCullVisitor*>(nv);
255 osg::Camera* camera = static_cast<osg::Camera*>(n);
257 cv->clearBufferList();
258 for (flightgear::RenderBufferMap::iterator ii = info->buffers.begin(); ii != info->buffers.end(); ++ii) {
259 cv->addBuffer(ii->first, ii->second.texture );
262 if ( !info->getRenderStageInfo(stage->name).fullscreen )
263 info->setMatrices( camera );
265 cv->traverse( *camera );
269 osg::ref_ptr<FGRenderingPipeline::Stage> stage;
270 flightgear::CameraInfo* info;