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(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 simgear::effect::EffectNameValue<GLint> internalFormatInit[] =
109 { "rgba8", GL_RGBA8 },
110 { "rgb16", GL_RGB16 },
111 { "rgba16", GL_RGBA16 },
113 { "depth-component24", GL_DEPTH_COMPONENT24 },
114 { "depth-component32", GL_DEPTH_COMPONENT32 }
116 simgear::effect::EffectPropertyMap<GLint> internalFormats(internalFormatInit);
118 simgear::effect::EffectNameValue<GLenum> sourceFormatInit[] =
123 { "depth-component", GL_DEPTH_COMPONENT }
125 simgear::effect::EffectPropertyMap<GLenum> sourceFormats(sourceFormatInit);
127 simgear::effect::EffectNameValue<GLenum> sourceTypeInit[] =
129 { "unsigned-byte", GL_UNSIGNED_BYTE },
130 { "unsigned-short", GL_UNSIGNED_SHORT },
131 { "unsigned-int", GL_UNSIGNED_INT },
132 { "float", GL_FLOAT }
134 simgear::effect::EffectPropertyMap<GLenum> sourceTypes(sourceTypeInit);
136 simgear::effect::EffectNameValue<GLenum> wrapModesInit[] =
139 {"clamp-to-border", GL_CLAMP_TO_BORDER_ARB},
140 {"clamp-to-edge", GL_CLAMP_TO_EDGE},
141 {"mirror", GL_MIRRORED_REPEAT_IBM},
142 {"repeat", GL_REPEAT}
144 simgear::effect::EffectPropertyMap<GLenum> wrapModes(wrapModesInit);
146 FGRenderingPipeline::Buffer::Buffer(SGPropertyNode* prop)
148 SGPropertyNode_ptr nameProp = prop->getChild("name");
149 if (!nameProp.valid()) {
150 throw sg_exception("Buffer name is mandatory");
152 name = nameProp->getStringValue();
153 findAttrOrHex(internalFormats, prop->getChild("internal-format"), internalFormat);
154 findAttrOrHex(sourceFormats, prop->getChild("source-format"), sourceFormat);
155 findAttrOrHex(sourceTypes, prop->getChild("source-type"), sourceType);
156 findAttrOrHex(sourceTypes, prop->getChild("wrap-mode"), wrapMode);
157 SGPropertyNode_ptr widthProp = prop->getChild("width");
158 if (!widthProp.valid())
160 else if (widthProp->getStringValue() == std::string("screen"))
163 width = widthProp->getIntValue();
164 SGPropertyNode_ptr heightProp = prop->getChild("height");
165 if (!heightProp.valid())
167 else if (heightProp->getStringValue() == std::string("screen"))
170 height = heightProp->getIntValue();
172 scaleFactor = prop->getFloatValue("scale-factor", 1.f);
173 shadowComparison = prop->getBoolValue("shadow-comparison", false);
176 simgear::effect::EffectNameValue<osg::Camera::BufferComponent> componentsInit[] =
178 {"depth", osg::Camera::DEPTH_BUFFER},
179 {"stencil", osg::Camera::STENCIL_BUFFER},
180 {"packed-depth-stencil", osg::Camera::PACKED_DEPTH_STENCIL_BUFFER},
181 {"color0", osg::Camera::COLOR_BUFFER0},
182 {"color1", osg::Camera::COLOR_BUFFER1},
183 {"color2", osg::Camera::COLOR_BUFFER2},
184 {"color3", osg::Camera::COLOR_BUFFER3}
186 simgear::effect::EffectPropertyMap<osg::Camera::BufferComponent> components(componentsInit);
188 FGRenderingPipeline::Stage::Stage(SGPropertyNode* prop)
190 SGPropertyNode_ptr nameProp = prop->getChild("name");
191 if (!nameProp.valid()) {
192 throw sg_exception("Stage name is mandatory");
194 name = nameProp->getStringValue();
195 SGPropertyNode_ptr typeProp = prop->getChild("type");
196 if (!typeProp.valid()) {
197 throw sg_exception("Stage type is mandatory");
199 type = typeProp->getStringValue();
201 std::vector<SGPropertyNode_ptr> attachments = prop->getChildren("attachment");
202 for (int i = 0; i < (int)attachments.size(); ++i) {
203 this->attachments.push_back(new FGRenderingPipeline::Attachment(attachments[i]));
207 FGRenderingPipeline::Attachment::Attachment(SGPropertyNode* prop)
209 simgear::findAttr(components, prop->getChild("component"), component);
210 SGPropertyNode_ptr bufferProp = prop->getChild("buffer");
211 if (!bufferProp.valid()) {
212 throw sg_exception("Attachment buffer is mandatory");
214 buffer = bufferProp->getStringValue();
217 FGRenderingPipeline::FGRenderingPipeline()
221 class FGStageCameraCullCallback : public osg::NodeCallback {
223 FGStageCameraCullCallback(FGRenderingPipeline::Stage* s, flightgear::CameraInfo* i) : stage(s), info(i) {}
224 virtual void operator()( osg::Node *n, osg::NodeVisitor *nv) {
225 simgear::EffectCullVisitor* cv = dynamic_cast<simgear::EffectCullVisitor*>(nv);
226 osg::Camera* camera = static_cast<osg::Camera*>(n);
228 cv->clearBufferList();
229 for (flightgear::RenderBufferMap::iterator ii = info->buffers.begin(); ii != info->buffers.end(); ++ii) {
230 cv->addBuffer(ii->first, ii->second.texture );
233 if ( !info->getRenderStageInfo(stage->name).fullscreen )
234 info->setMatrices( camera );
236 cv->traverse( *camera );
240 osg::ref_ptr<FGRenderingPipeline::Stage> stage;
241 flightgear::CameraInfo* info;